Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(staking): redelegate on behalf #208

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions contracts/src/interfaces/IIPTokenStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ interface IIPTokenStaking {
/// @param validatorUncmpSrcPubkey Source validator's 65 bytes uncompressed secp256k1 public key.
/// @param validatorUncmpDstPubkey Destination validator's 65 bytes uncompressed secp256k1 public key.
/// @param delegationId if delegation has staking period, 0 if flexible
/// @param operatorAddress The caller's address
/// @param amount Token redelegated.
event Redelegate(
bytes delegatorUncmpPubkey,
bytes validatorUncmpSrcPubkey,
bytes validatorUncmpDstPubkey,
uint256 delegationId,
address operatorAddress,
uint256 amount
);

Expand Down Expand Up @@ -280,6 +282,22 @@ interface IIPTokenStaking {
uint256 amount
) external payable;

/// @notice Entry point for redelegating the stake to another validator on behalf of the delegator.
/// @dev For non flexible staking, your staking period will continue as is.
/// @dev For locked tokens, this will fail in CL if the validator doesn't support unlocked staking.
/// @param delegatorUncmpPubkey Delegator's 65 bytes uncompressed secp256k1 public key.
/// @param validatorUncmpSrcPubkey Validator's 65 bytes uncompressed secp256k1 public key.
/// @param validatorUncmpDstPubkey Validator's 65 bytes uncompressed secp256k1 public key.
/// @param delegationId The delegation ID, 0 for flexible staking.
/// @param amount The amount of stake to redelegate.
function redelegateOnBehalf(
bytes calldata delegatorUncmpPubkey,
bytes calldata validatorUncmpSrcPubkey,
bytes calldata validatorUncmpDstPubkey,
uint256 delegationId,
uint256 amount
) external payable;

/// @notice Entry point for unstaking the previously staked token.
/// @dev Unstake (withdrawal) will trigger native minting, so token in this contract is considered as burned.
/// @param delegatorUncmpPubkey Delegator's 65 bytes uncompressed secp256k1 public key.
Expand Down
43 changes: 42 additions & 1 deletion contracts/src/protocol/IPTokenStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,40 @@ contract IPTokenStaking is IIPTokenStaking, Ownable2StepUpgradeable, ReentrancyG
verifyUncmpPubkey(validatorUncmpSrcPubkey)
verifyUncmpPubkey(validatorUncmpDstPubkey)
{
_redelegate(delegatorUncmpPubkey, validatorUncmpSrcPubkey, validatorUncmpDstPubkey, delegationId, amount);
}

/// @notice Entry point for redelegating the stake to another validator on behalf of the delegator.
/// @dev For non flexible staking, your staking period will continue as is.
/// @dev For locked tokens, this will fail in CL if the validator doesn't support unlocked staking.
/// @param delegatorUncmpPubkey Delegator's 65 bytes uncompressed secp256k1 public key.
/// @param validatorUncmpSrcPubkey Validator's 65 bytes uncompressed secp256k1 public key.
/// @param validatorUncmpDstPubkey Validator's 65 bytes uncompressed secp256k1 public key.
/// @param delegationId The delegation ID, 0 for flexible staking.
/// @param amount The amount of stake to redelegate.
function redelegateOnBehalf(
bytes calldata delegatorUncmpPubkey,
bytes calldata validatorUncmpSrcPubkey,
bytes calldata validatorUncmpDstPubkey,
uint256 delegationId,
uint256 amount
)
external
payable
verifyUncmpPubkey(delegatorUncmpPubkey)
verifyUncmpPubkey(validatorUncmpSrcPubkey)
verifyUncmpPubkey(validatorUncmpDstPubkey)
{
_redelegate(delegatorUncmpPubkey, validatorUncmpSrcPubkey, validatorUncmpDstPubkey, delegationId, amount);
}

function _redelegate(
bytes calldata delegatorUncmpPubkey,
bytes calldata validatorUncmpSrcPubkey,
bytes calldata validatorUncmpDstPubkey,
uint256 delegationId,
uint256 amount
) private {
if (keccak256(validatorUncmpSrcPubkey) == keccak256(validatorUncmpDstPubkey)) {
revert Errors.IPTokenStaking__RedelegatingToSameValidator();
}
Expand All @@ -503,7 +537,14 @@ contract IPTokenStaking is IIPTokenStaking, Ownable2StepUpgradeable, ReentrancyG
if (delegationId > _delegationIdCounter) {
revert Errors.IPTokenStaking__InvalidDelegationId();
}
emit Redelegate(delegatorUncmpPubkey, validatorUncmpSrcPubkey, validatorUncmpDstPubkey, delegationId, amount);
emit Redelegate(
delegatorUncmpPubkey,
validatorUncmpSrcPubkey,
validatorUncmpDstPubkey,
delegationId,
msg.sender,
amount
);
}

/// @notice Returns the rounded stake amount and the remainder.
Expand Down
94 changes: 94 additions & 0 deletions contracts/test/stake/IPTokenStaking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ contract IPTokenStakingTest is Test {
validatorUncmpSrcPubkey,
validatorUncmpDstPubkey,
delegationId,
delegatorAddr,
stakeAmount
);
vm.deal(delegatorAddr, stakeAmount);
Expand Down Expand Up @@ -496,6 +497,99 @@ contract IPTokenStakingTest is Test {
);
}

function testIPTokenStaking_RedelegationOnBehalf() public {
uint256 stakeAmount = ipTokenStaking.minStakeAmount();
uint256 delegationId = 1;
// Use VM setStorage to set the counter to delegationId == 1
vm.store(
address(ipTokenStaking),
bytes32(uint256(3)), // _delegationIdCounter
bytes32(uint256(1))
);
bytes
memory validatorUncmpSrcPubkey = hex"04e38d15ae6cc5d41cce27a2307903cb12a406cbf463fe5fef215bdf8aa988ced195e9327ac89cd362eaa0397f8d7f007c02b2a75642f174e455d339e4a1efe222"; // pragma: allowlist-secret
bytes
memory validatorUncmpDstPubkey = hex"04e38d15ae6cc5d41cce27a2307903cb12a406cbf463fe5fef215bdf8aa988ced195e9327ac89cd362eaa0397f8d7f007c02b2a75642f174e455d339e4a1000000"; // pragma: allowlist-secret

address operator = address(0xf398c12A45BC409b6C652e25bb0A3e702492A4AA);

vm.expectEmit(true, true, true, true);
emit IIPTokenStaking.Redelegate(
delegatorUncmpPubkey,
validatorUncmpSrcPubkey,
validatorUncmpDstPubkey,
delegationId,
operator,
stakeAmount
);
vm.deal(operator, stakeAmount);
vm.prank(operator);
ipTokenStaking.redelegateOnBehalf{ value: stakeAmount }(
delegatorUncmpPubkey,
validatorUncmpSrcPubkey,
validatorUncmpDstPubkey,
delegationId,
stakeAmount
);

// Redelegating to same validator
vm.deal(operator, stakeAmount);
vm.prank(operator);
vm.expectRevert(Errors.IPTokenStaking__RedelegatingToSameValidator.selector);
ipTokenStaking.redelegateOnBehalf{ value: stakeAmount }(
delegatorUncmpPubkey,
validatorUncmpSrcPubkey,
validatorUncmpSrcPubkey,
delegationId,
stakeAmount
);
// Malformed Src
vm.deal(operator, stakeAmount);
vm.prank(operator);
vm.expectRevert(Errors.IPTokenStaking__InvalidPubkeyLength.selector);
ipTokenStaking.redelegateOnBehalf{ value: stakeAmount }(
delegatorUncmpPubkey,
hex"04e38d15ae6cc5d41cce27a2307903cb", // pragma: allowlist secret
validatorUncmpDstPubkey,
delegationId,
stakeAmount
);
// Malformed Dst
vm.deal(operator, stakeAmount);
vm.prank(operator);
vm.expectRevert(Errors.IPTokenStaking__InvalidPubkeyLength.selector);
ipTokenStaking.redelegateOnBehalf{ value: stakeAmount }(
delegatorUncmpPubkey,
validatorUncmpSrcPubkey,
hex"04e38d15ae6cc5d41cce27a2307903cb", // pragma: allowlist secret
delegationId,
stakeAmount
);
// Stake < Min
vm.deal(operator, stakeAmount);
vm.prank(operator);
vm.expectRevert(Errors.IPTokenStaking__StakeAmountUnderMin.selector);
ipTokenStaking.redelegateOnBehalf{ value: stakeAmount - 1 }(
delegatorUncmpPubkey,
validatorUncmpSrcPubkey,
validatorUncmpDstPubkey,
delegationId,
stakeAmount + 100
);

// Revert if delegationId is invalid
delegationId++;
vm.prank(operator);
vm.expectRevert(Errors.IPTokenStaking__InvalidDelegationId.selector);
ipTokenStaking.redelegateOnBehalf{ value: stakeAmount }(
delegatorUncmpPubkey,
validatorUncmpSrcPubkey,
validatorUncmpDstPubkey,
delegationId,
stakeAmount
);
}

function testIPTokenStaking_SetWithdrawalAddress() public {
uint256 feeAmount = ipTokenStaking.fee();
// Network shall allow the delegators to set their withdrawal address
Expand Down
Loading