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: delegate tokens unlock after 21days #3

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion contracts/validator-manager/PoSValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ abstract contract PoSValidatorManager is
error InvalidStakeMultiplier(uint8 maximumStakeMultiplier);
error MaxWeightExceeded(uint64 newValidatorWeight);
error MinStakeDurationNotPassed(uint64 endTime);
error UnlockDelegateDurationNotPassed(uint64 endTime);
error UnauthorizedOwner(address sender);
error ValidatorNotPoS(bytes32 validationID);
error ValidatorIneligibleForRewards(bytes32 validationID);
Expand Down Expand Up @@ -91,6 +92,7 @@ abstract contract PoSValidatorManager is
minimumStakeAmount: settings.minimumStakeAmount,
maximumStakeAmount: settings.maximumStakeAmount,
minimumStakeDuration: settings.minimumStakeDuration,
unlockDelegateDuration: settings.unlockDelegateDuration,
minimumDelegationFeeBips: settings.minimumDelegationFeeBips,
maximumStakeMultiplier: settings.maximumStakeMultiplier,
weightToValueFactor: settings.weightToValueFactor,
Expand All @@ -104,6 +106,7 @@ abstract contract PoSValidatorManager is
uint256 minimumStakeAmount,
uint256 maximumStakeAmount,
uint64 minimumStakeDuration,
uint64 unlockDelegateDuration,
uint16 minimumDelegationFeeBips,
uint8 maximumStakeMultiplier,
uint256 weightToValueFactor,
Expand Down Expand Up @@ -141,6 +144,7 @@ abstract contract PoSValidatorManager is
$._weightToValueFactor = weightToValueFactor;
$._rewardCalculator = rewardCalculator;
$._uptimeBlockchainID = uptimeBlockchainID;
$._unlockDelegateDuration = unlockDelegateDuration;
}

/**
Expand Down Expand Up @@ -717,7 +721,7 @@ abstract contract PoSValidatorManager is
// the complete step, even if the delivered nonce is greater than the nonce used to
// initialize the removal.
$._delegatorStakes[delegationID].status = DelegatorStatus.PendingRemoved;

$._delegatorStakes[delegationID].endedAt = uint64(block.timestamp);
($._delegatorStakes[delegationID].endingNonce,) =
_setValidatorWeight(validationID, validator.weight - delegator.weight);

Expand Down Expand Up @@ -850,6 +854,9 @@ abstract contract PoSValidatorManager is
revert InvalidNonce(nonce);
}
}
if(block.timestamp < delegator.endedAt + $._unlockDelegateDuration) {
revert UnlockDelegateDurationNotPassed(uint64(block.timestamp));
}

_completeEndDelegation(delegationID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct PoSValidatorManagerSettings {
uint256 minimumStakeAmount;
uint256 maximumStakeAmount;
uint64 minimumStakeDuration;
uint64 unlockDelegateDuration;
uint16 minimumDelegationFeeBips;
uint8 maximumStakeMultiplier;
uint256 weightToValueFactor;
Expand All @@ -53,6 +54,7 @@ struct Delegator {
bytes32 validationID;
uint64 weight;
uint64 startedAt;
uint64 endedAt;
uint64 startingNonce;
uint64 endingNonce;
}
Expand Down Expand Up @@ -82,6 +84,8 @@ struct PoSValidatorManagerStorage {
uint64 _minimumStakeDuration;
/// @notice The minimum delegation fee percentage, in basis points, required to delegate to a validator.
uint16 _minimumDelegationFeeBips;
/// @notice The duration in seconds after a delegator's delegation is ended before the delegator's stake is unlocked.
uint64 _unlockDelegateDuration;
/**
* @notice A multiplier applied to validator's initial stake amount to determine
* the maximum amount of stake a validator can have with delegations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ abstract contract ERC721PoSValidatorManagerTest is ERC721ValidatorManagerTest {
address public constant DEFAULT_VALIDATOR_OWNER_ADDRESS =
address(0x2345234523452345234523452345234523452345);
uint64 public constant DEFAULT_REWARD_RATE = uint64(60);
uint64 public constant DEFAULT_UNLOCK_DELEGATE_DURATION = 21 days;
uint64 public constant DEFAULT_MINIMUM_STAKE_DURATION = 24 hours;
uint16 public constant DEFAULT_MINIMUM_DELEGATION_FEE_BIPS = 100;
uint16 public constant DEFAULT_DELEGATION_FEE_BIPS = 150;
Expand Down Expand Up @@ -667,6 +668,7 @@ abstract contract ERC721PoSValidatorManagerTest is ERC721ValidatorManagerTest {
_calculateValidatorFeesFromDelegator(expectedTotalReward, DEFAULT_DELEGATION_FEE_BIPS);
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand Down Expand Up @@ -771,6 +773,7 @@ abstract contract ERC721PoSValidatorManagerTest is ERC721ValidatorManagerTest {
_calculateValidatorFeesFromDelegator(expectedTotalReward, DEFAULT_DELEGATION_FEE_BIPS);
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand Down Expand Up @@ -818,7 +821,7 @@ abstract contract ERC721PoSValidatorManagerTest is ERC721ValidatorManagerTest {
uint256 expectedValidatorFees =
_calculateValidatorFeesFromDelegator(expectedTotalReward, DEFAULT_DELEGATION_FEE_BIPS);
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand Down Expand Up @@ -977,6 +980,7 @@ abstract contract ERC721PoSValidatorManagerTest is ERC721ValidatorManagerTest {

address delegator = DEFAULT_DELEGATOR_ADDRESS;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand Down Expand Up @@ -1112,6 +1116,7 @@ abstract contract ERC721PoSValidatorManagerTest is ERC721ValidatorManagerTest {
_expectStakeUnlock(DEFAULT_DELEGATOR_ADDRESS, _weightToValue(DEFAULT_DELEGATOR_TOKEN_ID));
_expectRewardIssuance(DEFAULT_DELEGATOR_ADDRESS, expectedDelegatorReward);

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
posValidatorManager.completeEndDelegation(delegationID, 0);
assertEq(
_getStakeAssetBalance(DEFAULT_DELEGATOR_ADDRESS),
Expand Down Expand Up @@ -1245,7 +1250,7 @@ abstract contract ERC721PoSValidatorManagerTest is ERC721ValidatorManagerTest {
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

address delegator = DEFAULT_DELEGATOR_ADDRESS;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
// Complete delegation1 by delivering the weight update from nonce 4 (delegator2's nonce)
_completeEndDelegationWithChecks({
validationID: validationID,
Expand Down Expand Up @@ -2108,6 +2113,7 @@ abstract contract ERC721PoSValidatorManagerTest is ERC721ValidatorManagerTest {
uint256 expectedValidatorFees =
_calculateValidatorFeesFromDelegator(expectedTotalReward, DEFAULT_DELEGATION_FEE_BIPS);
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;
vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand Down Expand Up @@ -2475,6 +2481,7 @@ abstract contract ERC721PoSValidatorManagerTest is ERC721ValidatorManagerTest {
minimumStakeAmount: DEFAULT_MINIMUM_STAKE_AMOUNT,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE_AMOUNT,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
unlockDelegateDuration: DEFAULT_UNLOCK_DELEGATE_DURATION,
minimumDelegationFeeBips: DEFAULT_MINIMUM_DELEGATION_FEE_BIPS,
maximumStakeMultiplier: DEFAULT_MAXIMUM_STAKE_MULTIPLIER,
weightToValueFactor: DEFAULT_WEIGHT_TO_VALUE_FACTOR,
Expand Down
21 changes: 12 additions & 9 deletions contracts/validator-manager/tests/PoSValidatorManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
uint64 public constant DEFAULT_UPTIME = uint64(100);
uint64 public constant DEFAULT_DELEGATOR_WEIGHT = uint64(1e5);
uint64 public constant DEFAULT_UNLOCK_DELEGATE_DURATION = 24 days;
uint64 public constant DEFAULT_DELEGATOR_INIT_REGISTRATION_TIMESTAMP =
DEFAULT_REGISTRATION_TIMESTAMP + DEFAULT_EXPIRY;
uint64 public constant DEFAULT_DELEGATOR_COMPLETE_REGISTRATION_TIMESTAMP =
Expand Down Expand Up @@ -573,9 +574,10 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
bytes32 validationID = _registerDefaultValidator();
bytes32 delegationID = _registerDefaultDelegator(validationID);

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeDefaultDelegator(validationID, delegationID);
}
/*

function testClaimDelegationFeesInvalidValidatorStatus() public {
bytes32 validationID = _registerDefaultValidator();
bytes32 delegationID = _registerDefaultDelegator(validationID);
Expand Down Expand Up @@ -665,7 +667,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
uint256 expectedValidatorFees =
_calculateValidatorFeesFromDelegator(expectedTotalReward, DEFAULT_DELEGATION_FEE_BIPS);
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand Down Expand Up @@ -772,7 +774,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
uint256 expectedValidatorFees =
_calculateValidatorFeesFromDelegator(expectedTotalReward, DEFAULT_DELEGATION_FEE_BIPS);
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand All @@ -786,7 +788,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
rewardRecipient: DEFAULT_DELEGATOR_ADDRESS
});
}
*/

function testChangeDelegatorRewardRecipient() public {
bytes32 validationID = _registerDefaultValidator();
bytes32 delegationID = _registerDefaultDelegator(validationID);
Expand Down Expand Up @@ -820,7 +822,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
uint256 expectedValidatorFees =
_calculateValidatorFeesFromDelegator(expectedTotalReward, DEFAULT_DELEGATION_FEE_BIPS);
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand Down Expand Up @@ -978,7 +980,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

address delegator = DEFAULT_DELEGATOR_ADDRESS;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand Down Expand Up @@ -1107,7 +1109,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {

_expectStakeUnlock(DEFAULT_DELEGATOR_ADDRESS, _weightToValue(DEFAULT_DELEGATOR_WEIGHT));
_expectRewardIssuance(DEFAULT_DELEGATOR_ADDRESS, expectedDelegatorReward);

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
posValidatorManager.completeEndDelegation(delegationID, 0);

assertEq(
Expand Down Expand Up @@ -1238,7 +1240,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

address delegator = DEFAULT_DELEGATOR_ADDRESS;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
// Complete delegation1 by delivering the weight update from nonce 4 (delegator2's nonce)
_completeEndDelegationWithChecks({
validationID: validationID,
Expand Down Expand Up @@ -2102,7 +2104,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
uint256 expectedValidatorFees =
_calculateValidatorFeesFromDelegator(expectedTotalReward, DEFAULT_DELEGATION_FEE_BIPS);
uint256 expectedDelegatorReward = expectedTotalReward - expectedValidatorFees;

vm.warp(DEFAULT_DELEGATOR_END_DELEGATION_TIMESTAMP + DEFAULT_UNLOCK_DELEGATE_DURATION + 1);
_completeEndDelegationWithChecks({
validationID: validationID,
delegationID: delegationID,
Expand Down Expand Up @@ -2465,6 +2467,7 @@ abstract contract PoSValidatorManagerTest is ValidatorManagerTest {
minimumDelegationFeeBips: DEFAULT_MINIMUM_DELEGATION_FEE_BIPS,
maximumStakeMultiplier: DEFAULT_MAXIMUM_STAKE_MULTIPLIER,
weightToValueFactor: DEFAULT_WEIGHT_TO_VALUE_FACTOR,
unlockDelegateDuration: DEFAULT_UNLOCK_DELEGATE_DURATION,
rewardCalculator: IRewardCalculator(address(0)),
uptimeBlockchainID: DEFAULT_SOURCE_BLOCKCHAIN_ID
});
Expand Down