Skip to content

Commit d9c6190

Browse files
committed
fix: move legacyWithdrawDelegated to withdrawDelegated (TRST-H07)
1 parent 6e5a295 commit d9c6190

File tree

7 files changed

+51
-87
lines changed

7 files changed

+51
-87
lines changed

packages/horizon/contracts/interfaces/internal/IHorizonStakingExtension.sol

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ interface IHorizonStakingExtension is IRewardsIssuer {
8383
*/
8484
event StakeSlashed(address indexed indexer, uint256 tokens, uint256 reward, address beneficiary);
8585

86-
/**
87-
* @dev Emitted when `delegator` withdrew delegated `tokens` from `indexer` using `legacyWithdrawDelegated`.
88-
*/
89-
event StakeDelegatedWithdrawn(address indexed indexer, address indexed delegator, uint256 tokens);
90-
9186
/**
9287
* @notice Close an allocation and free the staked tokens.
9388
* To be eligible for rewards a proof of indexing must be presented.
@@ -169,13 +164,4 @@ interface IHorizonStakingExtension is IRewardsIssuer {
169164
* @param beneficiary Address of a beneficiary to receive a reward for the slashing
170165
*/
171166
function legacySlash(address indexer, uint256 tokens, uint256 reward, address beneficiary) external;
172-
173-
/**
174-
* @notice Withdraw undelegated tokens once the unbonding period has passed.
175-
* @param _indexer Withdraw available tokens delegated to indexer
176-
*/
177-
function legacyWithdrawDelegated(
178-
address _indexer,
179-
address /* _newIndexer, deprecated */
180-
) external returns (uint256);
181167
}

packages/horizon/contracts/interfaces/internal/IHorizonStakingMain.sol

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ interface IHorizonStakingMain {
205205
uint256 tokens
206206
);
207207

208+
/**
209+
* @notice Emitted when `delegator` withdrew delegated `tokens` from `indexer` using `withdrawDelegated`.
210+
* @dev This event is for the legacy `withdrawDelegated` function.
211+
* @param indexer The address of the indexer
212+
* @param delegator The address of the delegator
213+
* @param tokens The amount of tokens withdrawn
214+
*/
215+
event StakeDelegatedWithdrawn(address indexed indexer, address indexed delegator, uint256 tokens);
216+
208217
/**
209218
* @notice Emitted when tokens are added to a delegation pool's reserve.
210219
* @param serviceProvider The address of the service provider
@@ -861,13 +870,14 @@ interface IHorizonStakingMain {
861870
/**
862871
* @notice Withdraw undelegated tokens from the subgraph data service provision after thawing.
863872
* This function is for backwards compatibility with the legacy staking contract.
864-
* It only allows withdrawing from the subgraph data service and DOES NOT have slippage protection in
865-
* case the caller opts for re-delegating.
873+
* It only allows withdrawing tokens undelegated before horizon upgrade.
866874
* @dev See {delegate}.
867875
* @param serviceProvider The service provider address
868-
* @param newServiceProvider The address of a new service provider, if the delegator wants to re-delegate
869876
*/
870-
function withdrawDelegated(address serviceProvider, address newServiceProvider) external;
877+
function withdrawDelegated(
878+
address serviceProvider,
879+
address // newServiceProvider, deprecated
880+
) external returns (uint256);
871881

872882
/**
873883
* @notice Slash a service provider. This can only be called by a verifier to which

packages/horizon/contracts/staking/HorizonStaking.sol

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -412,16 +412,37 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain {
412412
/**
413413
* @notice See {IHorizonStakingMain-withdrawDelegated}.
414414
*/
415-
function withdrawDelegated(address serviceProvider, address newServiceProvider) external override notPaused {
416-
_withdrawDelegated(
417-
ThawRequestType.Delegation,
418-
serviceProvider,
419-
SUBGRAPH_DATA_SERVICE_ADDRESS,
420-
newServiceProvider,
421-
SUBGRAPH_DATA_SERVICE_ADDRESS,
422-
0,
423-
0
424-
);
415+
function withdrawDelegated(
416+
address serviceProvider,
417+
address // newServiceProvider, deprecated
418+
) external override notPaused returns (uint256) {
419+
// Get the delegation pool of the indexer
420+
address delegator = msg.sender;
421+
DelegationPoolInternal storage pool = _legacyDelegationPools[serviceProvider];
422+
DelegationInternal storage delegation = pool.delegators[delegator];
423+
424+
// Validation
425+
uint256 tokensToWithdraw = 0;
426+
uint256 currentEpoch = _graphEpochManager().currentEpoch();
427+
if (
428+
delegation.__DEPRECATED_tokensLockedUntil > 0 && currentEpoch >= delegation.__DEPRECATED_tokensLockedUntil
429+
) {
430+
tokensToWithdraw = delegation.__DEPRECATED_tokensLocked;
431+
}
432+
require(tokensToWithdraw > 0, "!tokens");
433+
434+
// Reset lock
435+
delegation.__DEPRECATED_tokensLocked = 0;
436+
delegation.__DEPRECATED_tokensLockedUntil = 0;
437+
438+
emit StakeDelegatedWithdrawn(serviceProvider, delegator, tokensToWithdraw);
439+
440+
// -- Interactions --
441+
442+
// Return tokens to the delegator
443+
_graphToken().pushTokens(delegator, tokensToWithdraw);
444+
445+
return tokensToWithdraw;
425446
}
426447

427448
/*

packages/horizon/contracts/staking/HorizonStakingExtension.sol

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -312,43 +312,6 @@ contract HorizonStakingExtension is HorizonStakingBase, IHorizonStakingExtension
312312
emit StakeSlashed(indexer, tokens, reward, beneficiary);
313313
}
314314

315-
/**
316-
* @notice Withdraw undelegated tokens once the unbonding period has passed.
317-
* @param indexer Withdraw available tokens delegated to indexer
318-
*/
319-
function legacyWithdrawDelegated(
320-
address indexer,
321-
address // newIndexer, deprecated
322-
) external override notPaused returns (uint256) {
323-
// Get the delegation pool of the indexer
324-
address delegator = msg.sender;
325-
DelegationPoolInternal storage pool = _legacyDelegationPools[indexer];
326-
DelegationInternal storage delegation = pool.delegators[delegator];
327-
328-
// Validation
329-
uint256 tokensToWithdraw = 0;
330-
uint256 currentEpoch = _graphEpochManager().currentEpoch();
331-
if (
332-
delegation.__DEPRECATED_tokensLockedUntil > 0 && currentEpoch >= delegation.__DEPRECATED_tokensLockedUntil
333-
) {
334-
tokensToWithdraw = delegation.__DEPRECATED_tokensLocked;
335-
}
336-
require(tokensToWithdraw > 0, "!tokens");
337-
338-
// Reset lock
339-
delegation.__DEPRECATED_tokensLocked = 0;
340-
delegation.__DEPRECATED_tokensLockedUntil = 0;
341-
342-
emit StakeDelegatedWithdrawn(indexer, delegator, tokensToWithdraw);
343-
344-
// -- Interactions --
345-
346-
// Return tokens to the delegator
347-
_graphToken().pushTokens(delegator, tokensToWithdraw);
348-
349-
return tokensToWithdraw;
350-
}
351-
352315
/**
353316
* @notice (Legacy) Return true if operator is allowed for the service provider on the subgraph data service.
354317
* @dev TODO: Delete after the transition period

packages/horizon/test/shared/horizon-staking/HorizonStakingShared.t.sol

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest {
10461046
newVerifier: address(0),
10471047
minSharesForNewProvider: 0,
10481048
nThawRequests: nThawRequests,
1049-
legacy: false
1049+
legacy: verifier == subgraphDataServiceLegacyAddress
10501050
});
10511051
__withdrawDelegated(params);
10521052
}
@@ -1090,20 +1090,6 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest {
10901090
__withdrawDelegated(params);
10911091
}
10921092

1093-
function _withdrawDelegated(address serviceProvider, address newServiceProvider) internal {
1094-
Params_WithdrawDelegated memory params = Params_WithdrawDelegated({
1095-
thawRequestType: IHorizonStakingTypes.ThawRequestType.Delegation,
1096-
serviceProvider: serviceProvider,
1097-
verifier: subgraphDataServiceLegacyAddress,
1098-
newServiceProvider: newServiceProvider,
1099-
newVerifier: subgraphDataServiceLegacyAddress,
1100-
minSharesForNewProvider: 0,
1101-
nThawRequests: 0,
1102-
legacy: true
1103-
});
1104-
__withdrawDelegated(params);
1105-
}
1106-
11071093
struct BeforeValues_WithdrawDelegated {
11081094
DelegationPoolInternalTest pool;
11091095
DelegationPoolInternalTest newPool;
@@ -1197,9 +1183,7 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest {
11971183
msgSender,
11981184
calcValues.tokensThawed
11991185
);
1200-
if (params.legacy) {
1201-
staking.withdrawDelegated(params.serviceProvider, params.newServiceProvider);
1202-
} else if (reDelegate) {
1186+
if (reDelegate) {
12031187
staking.redelegate(
12041188
params.serviceProvider,
12051189
params.verifier,

packages/horizon/test/staking/delegation/legacyWithdraw.t.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ contract HorizonStakingLegacyWithdrawDelegationTest is HorizonStakingTest {
5454
uint256 beforeDelegatorBalance = token.balanceOf(users.delegator);
5555

5656
vm.expectEmit(address(staking));
57-
emit IHorizonStakingExtension.StakeDelegatedWithdrawn(_indexer, delegator, pool.tokens);
58-
staking.legacyWithdrawDelegated(users.indexer, address(0));
57+
emit IHorizonStakingMain.StakeDelegatedWithdrawn(_indexer, delegator, pool.tokens);
58+
staking.withdrawDelegated(users.indexer, address(0));
5959

6060
uint256 afterStakingBalance = token.balanceOf(address(staking));
6161
uint256 afterDelegatorBalance = token.balanceOf(users.delegator);
@@ -93,6 +93,6 @@ contract HorizonStakingLegacyWithdrawDelegationTest is HorizonStakingTest {
9393
_setLegacyDelegation(users.indexer, users.delegator, 0, 0, 0);
9494

9595
vm.expectRevert("!tokens");
96-
staking.legacyWithdrawDelegated(users.indexer, address(0));
96+
staking.withdrawDelegated(users.indexer, address(0));
9797
}
9898
}

packages/horizon/test/staking/delegation/withdraw.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ contract HorizonStakingWithdrawDelegationTest is HorizonStakingTest {
8787

8888
skip(thawRequest.thawingUntil + 1);
8989

90-
_withdrawDelegated(users.indexer, address(0));
90+
_withdrawDelegated(users.indexer, subgraphDataServiceLegacyAddress, 0);
9191
}
9292

9393
function testWithdrawDelegation_RevertWhen_InvalidPool(

0 commit comments

Comments
 (0)