@@ -32,7 +32,7 @@ contract HorizonStakingUndelegateTest is HorizonStakingTest {
3232 uint256 undelegateSteps
3333 ) public useIndexer useProvision (amount, 0 , 0 ) {
3434 undelegateSteps = bound (undelegateSteps, 1 , 10 );
35- delegationAmount = bound (delegationAmount, 10 wei , MAX_STAKING_TOKENS);
35+ delegationAmount = bound (delegationAmount, MIN_DELEGATION * undelegateSteps , MAX_STAKING_TOKENS);
3636
3737 resetPrank (users.delegator);
3838 _delegate (users.indexer, subgraphDataServiceAddress, delegationAmount, 0 );
@@ -44,9 +44,17 @@ contract HorizonStakingUndelegateTest is HorizonStakingTest {
4444 );
4545
4646 uint256 undelegateAmount = delegation.shares / undelegateSteps;
47- for (uint i = 0 ; i < undelegateSteps; i++ ) {
47+ for (uint i = 0 ; i < undelegateSteps - 1 ; i++ ) {
4848 _undelegate (users.indexer, subgraphDataServiceAddress, undelegateAmount);
4949 }
50+
51+ delegation = _getStorage_Delegation (
52+ users.indexer,
53+ subgraphDataServiceAddress,
54+ users.delegator,
55+ false
56+ );
57+ _undelegate (users.indexer, subgraphDataServiceAddress, delegation.shares);
5058 }
5159
5260 function testUndelegate_WithBeneficiary (
@@ -60,6 +68,29 @@ contract HorizonStakingUndelegateTest is HorizonStakingTest {
6068 _undelegateWithBeneficiary (users.indexer, subgraphDataServiceAddress, delegation.shares, beneficiary);
6169 }
6270
71+ function testUndelegate_RevertWhen_InsuficientTokens (
72+ uint256 amount ,
73+ uint256 delegationAmount ,
74+ uint256 undelegateAmount
75+ ) public useIndexer useProvision (amount, 0 , 0 ) useDelegation (delegationAmount) {
76+ undelegateAmount = bound (undelegateAmount, 1 , delegationAmount);
77+ resetPrank (users.delegator);
78+ DelegationInternal memory delegation = _getStorage_Delegation (
79+ users.indexer,
80+ subgraphDataServiceAddress,
81+ users.delegator,
82+ false
83+ );
84+ undelegateAmount = bound (undelegateAmount, delegation.shares - MIN_DELEGATION + 1 , delegation.shares - 1 );
85+ bytes memory expectedError = abi.encodeWithSelector (
86+ IHorizonStakingMain.HorizonStakingInsufficientTokens.selector ,
87+ delegation.shares - undelegateAmount,
88+ MIN_DELEGATION
89+ );
90+ vm.expectRevert (expectedError);
91+ staking.undelegate (users.indexer, subgraphDataServiceAddress, undelegateAmount);
92+ }
93+
6394 function testUndelegate_RevertWhen_TooManyUndelegations ()
6495 public
6596 useIndexer
@@ -112,7 +143,7 @@ contract HorizonStakingUndelegateTest is HorizonStakingTest {
112143
113144 function testUndelegate_LegacySubgraphService (uint256 amount , uint256 delegationAmount ) public useIndexer {
114145 amount = bound (amount, 1 , MAX_STAKING_TOKENS);
115- delegationAmount = bound (delegationAmount, 1 , MAX_STAKING_TOKENS);
146+ delegationAmount = bound (delegationAmount, MIN_DELEGATION , MAX_STAKING_TOKENS);
116147 _createProvision (users.indexer, subgraphDataServiceLegacyAddress, amount, 0 , 0 );
117148
118149 resetPrank (users.delegator);
@@ -131,7 +162,7 @@ contract HorizonStakingUndelegateTest is HorizonStakingTest {
131162 uint256 tokens ,
132163 uint256 delegationTokens
133164 ) public useIndexer useProvision (tokens, 0 , 0 ) useDelegationSlashing () {
134- delegationTokens = bound (delegationTokens, 1 , MAX_STAKING_TOKENS);
165+ delegationTokens = bound (delegationTokens, MIN_DELEGATION , MAX_STAKING_TOKENS);
135166
136167 resetPrank (users.delegator);
137168 _delegate (users.indexer, subgraphDataServiceAddress, delegationTokens, 0 );
@@ -162,7 +193,7 @@ contract HorizonStakingUndelegateTest is HorizonStakingTest {
162193 uint256 tokens ,
163194 uint256 delegationTokens
164195 ) public useIndexer useProvision (tokens, 0 , 0 ) useDelegationSlashing {
165- delegationTokens = bound (delegationTokens, 1 , MAX_STAKING_TOKENS);
196+ delegationTokens = bound (delegationTokens, MIN_DELEGATION , MAX_STAKING_TOKENS);
166197
167198 // delegate
168199 resetPrank (users.delegator);
0 commit comments