mx-chain-go icon indicating copy to clipboard operation
mx-chain-go copied to clipboard

Migrate from sp to sp

Open sasurobert opened this issue 4 months ago • 0 comments

Reasoning behind the pull request

This is a feature request asked by a lot of community members and someone has written a proposal as well.

Proposed changes

The solution I propose is to enable users to move funds from one provider to another once per 30 days.

changeStakingProvider@amount@sourceSCAddress@destinationSCAddress vmInput.Caller address is the delegator, it is the user address, or the SC address vmInput.RecipientAddress is the delegation manager contract, which will forward the call to the delegation contract the user wants to move his funds. sourceSCAddress and destinationSCAddress are verified to be a system delegation contract.

removeDelegationFromSource@delegatorAddress@amount: does basic checks, can be called by delegation manager address only, and removes from the user the specified amount if the user has enough delegation there. The system checks what was the last time the delegator moved funds to this contract, by checking a new storage in which the epoch was saved. If epoch == 0 or epoch + 30 < currentEpoch we can go to the next step of moving. Rewards are calculated for the user and saved into claimable rewards (this code already exists and used when user delegates more), and the new amount of delegation is computed by subtracting the amount value.

moveDelegationToDestination@delegatorAddress@amount: does basic checks, can be called by delegation manager address only, adds the stated amount to the user’s delegation. It creates new storage where the current epoch is saved, the user cannot changeProvider from this place for the next 30 days.He can always unStake and unBond. It will call the internal delegate function, compute rewards for the user if the user already had delegation, increase the delegation amount for the user

New function in ValidatorSC is called which makes unStake and unBond in one call for the staking provider, actually the only thing it needs to do is to change the TotalStakedAmount from the staking provider, without adding it to the unStakedFunds. Unstaking of nodes is happening at the end of the epoch if the SP does not have enough funds (this process is the existing code - which works like this even now).

moveStakeFromValidatorToValidator@stakeAmount@valiadtorA@validatorB - it can be called by system delegationContracts only. The caller is the delegation contract from which the funds are moved out. This function will also increase the TotalStakedAmount for validatorB. Nothing else to do. removeDelegationFromSource, moveDelegationToDestination and moveStakeFromValidatorToValidator are called synchronously. If one of them fails, no changes are made. Process finished.

Testing procedure

Start a few staking providers. Make a few users who delegate to all of them/some of them. Start moving delegation from one SP to another. All first moves should work. Second moves (like fund was moved to address X and you want to move again) will not work for 30 epochs. If a user moves some funds from SP A to SP B and he has some funds on SP B he wants to move, he can't do that.

Pre-requisites

Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:

  • was the PR targeted to the correct branch?
  • if this is a larger feature that probably needs more than one PR, is there a feat branch created?
  • if this is a feat branch merging, do all satellite projects have a proper tag inside go.mod?

sasurobert avatar Oct 01 '24 13:10 sasurobert