-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
971 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
import { IHorizonStakingTypes } from "../../contracts/interfaces/IHorizonStakingTypes.sol"; | ||
import { HorizonStakingSharedTest } from "../shared/horizon-staking/HorizonStakingShared.t.sol"; | ||
|
||
contract HorizonStakingTest is HorizonStakingSharedTest, IHorizonStakingTypes { | ||
|
||
modifier useOperator() { | ||
vm.startPrank(users.indexer); | ||
staking.setOperator(users.operator, subgraphDataServiceAddress, true); | ||
vm.startPrank(users.operator); | ||
_; | ||
vm.stopPrank(); | ||
} | ||
|
||
modifier useStake(uint256 amount) { | ||
vm.assume(amount > MIN_PROVISION_SIZE); | ||
approve(address(staking), amount); | ||
staking.stake(amount); | ||
_; | ||
} | ||
|
||
modifier useStakeTo(address to, uint256 amount) { | ||
vm.assume(amount > MIN_PROVISION_SIZE); | ||
_stakeTo(to, amount); | ||
_; | ||
} | ||
|
||
modifier useThawRequest(uint256 thawAmount) { | ||
vm.assume(thawAmount > 0); | ||
_createThawRequest(thawAmount); | ||
_; | ||
} | ||
|
||
modifier useThawAndDeprovision(uint256 amount, uint64 thawingPeriod) { | ||
vm.assume(amount > 0); | ||
_createThawRequest(amount); | ||
skip(thawingPeriod + 1); | ||
_deprovision(amount); | ||
_; | ||
} | ||
|
||
modifier useDelegation(uint256 delegationAmount) { | ||
address msgSender; | ||
(, msgSender,) = vm.readCallers(); | ||
vm.assume(delegationAmount > MIN_DELEGATION); | ||
vm.assume(delegationAmount <= 10_000_000_000 ether); | ||
vm.startPrank(users.delegator); | ||
_delegate(delegationAmount); | ||
vm.startPrank(msgSender); | ||
_; | ||
} | ||
|
||
function _stakeTo(address to, uint256 amount) internal { | ||
approve(address(staking), amount); | ||
staking.stakeTo(to, amount); | ||
} | ||
|
||
function _createThawRequest(uint256 thawAmount) internal returns (bytes32) { | ||
return staking.thaw(users.indexer, subgraphDataServiceAddress, thawAmount); | ||
} | ||
|
||
function _deprovision(uint256 amount) internal { | ||
staking.deprovision(users.indexer, subgraphDataServiceAddress, amount); | ||
} | ||
|
||
function _delegate(uint256 amount) internal { | ||
token.approve(address(staking), amount); | ||
staking.delegate(users.indexer, subgraphDataServiceAddress, amount, 0); | ||
} | ||
|
||
function _getDelegation() internal view returns (Delegation memory) { | ||
return staking.getDelegation(users.delegator, users.indexer, subgraphDataServiceAddress); | ||
} | ||
|
||
function _undelegate(uint256 shares) internal { | ||
staking.undelegate(users.indexer, subgraphDataServiceAddress, shares); | ||
} | ||
|
||
function _getDelegationPool() internal view returns (DelegationPool memory) { | ||
return staking.getDelegationPool(users.indexer, subgraphDataServiceAddress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
import { HorizonStakingTest } from "../HorizonStaking.t.sol"; | ||
|
||
contract HorizonStakingDelegateTest is HorizonStakingTest { | ||
|
||
function testDelegate_Tokens( | ||
uint256 amount, | ||
uint256 delegationAmount | ||
) public useIndexer useProvision(amount, 0, 0) useDelegation(delegationAmount) { | ||
uint256 delegatedTokens = staking.getDelegatedTokensAvailable(users.indexer, subgraphDataServiceAddress); | ||
assertEq(delegatedTokens, delegationAmount); | ||
} | ||
|
||
function testDelegate_RevertWhen_ZeroTokens( | ||
uint256 amount | ||
) public useIndexer useProvision(amount, 0, 0) { | ||
vm.startPrank(users.delegator); | ||
bytes memory expectedError = abi.encodeWithSignature("HorizonStakingInvalidZeroTokens()"); | ||
vm.expectRevert(expectedError); | ||
staking.delegate(users.indexer, subgraphDataServiceAddress, 0, 0); | ||
} | ||
|
||
function testDelegate_RevertWhen_BelowMinimum( | ||
uint256 amount, | ||
uint256 delegationAmount | ||
) public useIndexer useProvision(amount, 0, 0) { | ||
vm.startPrank(users.delegator); | ||
delegationAmount = bound(delegationAmount, 1, MIN_DELEGATION - 1); | ||
token.approve(address(staking), delegationAmount); | ||
bytes memory expectedError = abi.encodeWithSignature( | ||
"HorizonStakingInsufficientTokens(uint256,uint256)", | ||
MIN_DELEGATION, | ||
delegationAmount | ||
); | ||
vm.expectRevert(expectedError); | ||
staking.delegate(users.indexer, subgraphDataServiceAddress, delegationAmount, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
import { HorizonStakingTest } from "../HorizonStaking.t.sol"; | ||
|
||
contract HorizonStakingUndelegateTest is HorizonStakingTest { | ||
|
||
function testUndelegate_Tokens( | ||
uint256 amount, | ||
uint256 delegationAmount | ||
) public useIndexer useProvision(amount, 0, 0) useDelegation(delegationAmount) { | ||
// TODO: maybe create a changePrank | ||
vm.stopPrank(); | ||
vm.startPrank(users.delegator); | ||
Delegation memory delegation = _getDelegation(); | ||
_undelegate(delegation.shares); | ||
|
||
Delegation memory thawingDelegation = _getDelegation(); | ||
ThawRequest memory thawRequest = staking.getThawRequest(thawingDelegation.lastThawRequestId); | ||
|
||
assertEq(thawRequest.shares, delegation.shares); | ||
} | ||
|
||
function testUndelegate_RevertWhen_ZeroTokens( | ||
uint256 amount, | ||
uint256 delegationAmount | ||
) public useIndexer useProvision(amount, 0, 0) useDelegation(delegationAmount) { | ||
// TODO: maybe create a changePrank | ||
vm.stopPrank(); | ||
vm.startPrank(users.delegator); | ||
bytes memory expectedError = abi.encodeWithSignature("HorizonStakingInvalidZeroTokens()"); | ||
vm.expectRevert(expectedError); | ||
_undelegate(0); | ||
} | ||
|
||
function testUndelegate_RevertWhen_OverUndelegation( | ||
uint256 amount, | ||
uint256 delegationAmount, | ||
uint256 overDelegationShares | ||
) public useIndexer useProvision(amount, 0, 0) useDelegation(delegationAmount) { | ||
// TODO: maybe create a changePrank | ||
vm.stopPrank(); | ||
vm.startPrank(users.delegator); | ||
Delegation memory delegation = _getDelegation(); | ||
vm.assume(overDelegationShares > delegation.shares); | ||
|
||
bytes memory expectedError = abi.encodeWithSignature( | ||
"HorizonStakingInsufficientTokens(uint256,uint256)", | ||
overDelegationShares, | ||
delegation.shares | ||
); | ||
vm.expectRevert(expectedError); | ||
_undelegate(overDelegationShares); | ||
} | ||
|
||
function testUndelegate_RevertWhen_UndelegateLeavesInsufficientTokens( | ||
uint256 delegationAmount, | ||
uint256 withdrawShares | ||
) | ||
public | ||
useIndexer | ||
useProvision(10_000_000 ether, 0, 0) | ||
useDelegation(delegationAmount) | ||
{ | ||
// TODO: maybe create a changePrank | ||
vm.stopPrank(); | ||
vm.startPrank(users.delegator); | ||
uint256 minShares = delegationAmount - MIN_DELEGATION + 1; | ||
withdrawShares = bound(withdrawShares, minShares, delegationAmount - 1); | ||
|
||
vm.expectRevert("!minimum-delegation"); | ||
_undelegate(withdrawShares); | ||
} | ||
} |
Oops, something went wrong.