From c052943e1f35cb97ecc6ce6e2cc96ea3fb78140b Mon Sep 17 00:00:00 2001 From: Miguel de Elias Date: Mon, 20 May 2024 14:30:08 -0300 Subject: [PATCH] chore: uint tests refactor --- packages/horizon/test/GraphBase.t.sol | 12 +- .../horizon/test/escrow/GraphEscrow.t.sol | 304 ++---------------- packages/horizon/test/escrow/collect.t.sol | 75 +++++ packages/horizon/test/escrow/collector.t.sol | 87 +++++ packages/horizon/test/escrow/deposit.t.sol | 53 +++ packages/horizon/test/escrow/thaw.t.sol | 34 ++ packages/horizon/test/escrow/withdraw.t.sol | 48 +++ .../horizon/test/payments/GraphPayments.t.sol | 4 +- .../horizon-staking/HorizonStaking.t.sol | 10 +- 9 files changed, 332 insertions(+), 295 deletions(-) create mode 100644 packages/horizon/test/escrow/collect.t.sol create mode 100644 packages/horizon/test/escrow/collector.t.sol create mode 100644 packages/horizon/test/escrow/deposit.t.sol create mode 100644 packages/horizon/test/escrow/thaw.t.sol create mode 100644 packages/horizon/test/escrow/withdraw.t.sol diff --git a/packages/horizon/test/GraphBase.t.sol b/packages/horizon/test/GraphBase.t.sol index 5367dc0c1..b26bc14e1 100644 --- a/packages/horizon/test/GraphBase.t.sol +++ b/packages/horizon/test/GraphBase.t.sol @@ -57,6 +57,14 @@ abstract contract GraphBaseTest is Test, Constants { // Deploy protocol contracts deployProtocolContracts(); unpauseProtocol(); + + // Label contracts + vm.label({ account: address(controller), newLabel: "Controller" }); + vm.label({ account: address(token), newLabel: "GraphToken" }); + vm.label({ account: address(payments), newLabel: "GraphPayments" }); + vm.label({ account: address(escrow), newLabel: "GraphEscrow" }); + vm.label({ account: address(staking), newLabel: "HorizonStaking" }); + vm.label({ account: address(stakingExtension), newLabel: "HorizonStakingExtension" }); } function deployProtocolContracts() private { @@ -158,7 +166,9 @@ abstract contract GraphBaseTest is Test, Constants { function createUser(string memory name) private returns (address) { address user = makeAddr(name); - deal({ token: address(token), to: user, give: 10000 ether }); + vm.deal({ account: user, newBalance: 100 ether }); + deal({ token: address(token), to: user, give: type(uint256).max }); + vm.label({ account: user, newLabel: name }); return user; } diff --git a/packages/horizon/test/escrow/GraphEscrow.t.sol b/packages/horizon/test/escrow/GraphEscrow.t.sol index ced2cbd18..f420c1964 100644 --- a/packages/horizon/test/escrow/GraphEscrow.t.sol +++ b/packages/horizon/test/escrow/GraphEscrow.t.sol @@ -3,306 +3,38 @@ pragma solidity ^0.8.24; import "forge-std/Test.sol"; -import { IGraphPayments } from "../../contracts/interfaces/IGraphPayments.sol"; +import { HorizonStakingSharedTest } from "../shared/horizon-staking/HorizonStaking.t.sol"; -import { HorizonStaking_Shared_Test } from "../shared/horizon-staking/HorizonStaking.t.sol"; +contract GraphEscrowTest is HorizonStakingSharedTest { -contract GraphEscrowTest is HorizonStaking_Shared_Test { - - // Collector approve tests - - function testCollector_Approve() public { - vm.prank(users.gateway); - escrow.approveCollector(users.verifier, 1000 ether); - - (bool authorized,, uint256 thawEndTimestamp) = escrow.authorizedCollectors(users.gateway, users.verifier); - assertEq(authorized, true); - assertEq(thawEndTimestamp, 0); - } - - // Collector thaw tests - - function testCollector_Thaw() public { - vm.startPrank(users.gateway); - escrow.approveCollector(users.verifier, 1000 ether); - escrow.thawCollector(users.verifier); - vm.stopPrank(); - - (bool authorized,, uint256 thawEndTimestamp) = escrow.authorizedCollectors(users.gateway, users.verifier); - assertEq(authorized, true); - assertEq(thawEndTimestamp, block.timestamp + revokeCollectorThawingPeriod); - } - - // Collector cancel thaw tests - - function testCollector_CancelThaw() public { - vm.startPrank(users.gateway); - escrow.approveCollector(users.verifier, 1000 ether); - escrow.thawCollector(users.verifier); - vm.stopPrank(); - - (bool authorized,, uint256 thawEndTimestamp) = escrow.authorizedCollectors(users.gateway, users.verifier); - assertEq(authorized, true); - assertEq(thawEndTimestamp, block.timestamp + revokeCollectorThawingPeriod); - - vm.prank(users.gateway); - escrow.cancelThawCollector(users.verifier); - - (authorized,, thawEndTimestamp) = escrow.authorizedCollectors(users.gateway, users.verifier); - assertEq(authorized, true); - assertEq(thawEndTimestamp, 0); - } - - function testCollector_RevertWhen_CancelThawIsNotThawing() public { - vm.startPrank(users.gateway); - escrow.approveCollector(users.verifier, 1000 ether); - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowNotThawing()"); - vm.expectRevert(expectedError); - escrow.cancelThawCollector(users.verifier); - vm.stopPrank(); - } - - // Collector revoke tests - - function testCollector_Revoke() public { - vm.startPrank(users.gateway); - escrow.approveCollector(users.verifier, 1000 ether); - escrow.thawCollector(users.verifier); - skip(revokeCollectorThawingPeriod + 1); - escrow.revokeCollector(users.verifier); - vm.stopPrank(); - - (bool authorized,,) = escrow.authorizedCollectors(users.gateway, users.verifier); - assertEq(authorized, false); - } - - function testCollector_RevertWhen_RevokeIsNotThawing() public { - vm.startPrank(users.gateway); - escrow.approveCollector(users.verifier, 1000 ether); - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowNotThawing()"); - vm.expectRevert(expectedError); - escrow.revokeCollector(users.verifier); - vm.stopPrank(); - } - - function testCollector_RevertWhen_RevokeIsStillThawing() public { + modifier useGateway() { vm.startPrank(users.gateway); - escrow.approveCollector(users.verifier, 1000 ether); - escrow.thawCollector(users.verifier); - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowStillThawing(uint256,uint256)", block.timestamp, block.timestamp + revokeCollectorThawingPeriod); - vm.expectRevert(expectedError); - escrow.revokeCollector(users.verifier); + _; vm.stopPrank(); } - // Deposit tests - - function testDeposit_Tokens() public { - mint(users.gateway, 10000 ether); - vm.startPrank(users.gateway); - token.approve(address(escrow), 1000 ether); - escrow.deposit(users.indexer, 1000 ether); - vm.stopPrank(); - - (uint256 indexerEscrowBalance,,) = escrow.escrowAccounts(users.gateway, users.indexer); - assertEq(indexerEscrowBalance, 1000 ether); - } - - function testDeposit_ManyDeposits() public { - address otherIndexer = address(0xB3); - address[] memory indexers = new address[](2); - indexers[0] = users.indexer; - indexers[1] = otherIndexer; - - uint256[] memory amounts = new uint256[](2); - amounts[0] = 1000 ether; - amounts[1] = 2000 ether; - - mint(users.gateway, 3000 ether); - vm.startPrank(users.gateway); - token.approve(address(escrow), 3000 ether); - escrow.depositMany(indexers, amounts); - vm.stopPrank(); - - (uint256 indexerEscrowBalance,,) = escrow.escrowAccounts(users.gateway, users.indexer); - assertEq(indexerEscrowBalance, 1000 ether); - - (uint256 otherIndexerEscrowBalance,,) = escrow.escrowAccounts(users.gateway, otherIndexer); - assertEq(otherIndexerEscrowBalance, 2000 ether); + modifier approveEscrow(uint256 amount) { + _approveEscrow(amount); + _; } - function testDeposit_RevertWhen_ManyDepositsInputsLengthMismatch() public { - address otherIndexer = address(0xB3); - address[] memory indexers = new address[](2); - indexers[0] = users.indexer; - indexers[1] = otherIndexer; - - uint256[] memory amounts = new uint256[](1); - amounts[0] = 1000 ether; - - mint(users.gateway, 1000 ether); - token.approve(address(escrow), 1000 ether); - - // revert - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowInputsLengthMismatch()"); - vm.expectRevert(expectedError); - vm.prank(users.gateway); - escrow.depositMany(indexers, amounts); + modifier depositTokens(uint256 amount) { + vm.assume(amount > 0); + vm.assume(amount <= 10000 ether); + _depositTokens(amount); + _; } - // Thaw tests - - function testThaw_Tokens() public { - mint(users.gateway, 1000 ether); - vm.startPrank(users.gateway); - token.approve(address(escrow), 1000 ether); - escrow.deposit(users.indexer, 1000 ether); - escrow.thaw(users.indexer, 100 ether); - vm.stopPrank(); - - (, uint256 amountThawing,uint256 thawEndTimestamp) = escrow.escrowAccounts(users.gateway, users.indexer); - assertEq(amountThawing, 100 ether); - assertEq(thawEndTimestamp, block.timestamp + withdrawEscrowThawingPeriod); + function setUp() public virtual override { + HorizonStakingSharedTest.setUp(); } - function testThaw_RevertWhen_InsufficientThawAmount() public { - mint(users.gateway, 1000 ether); - vm.startPrank(users.gateway); - token.approve(address(escrow), 1000 ether); - escrow.deposit(users.indexer, 1000 ether); - - // revert - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowInsufficientThawAmount()"); - vm.expectRevert(expectedError); - escrow.thaw(users.indexer, 0); - vm.stopPrank(); - } - - function testThaw_RevertWhen_InsufficientAmount() public { - mint(users.gateway, 1000 ether); - vm.startPrank(users.gateway); - token.approve(address(escrow), 1000 ether); - escrow.deposit(users.indexer, 1000 ether); - - // revert - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowInsufficientAmount(uint256,uint256)", 1000 ether, 2000 ether); - vm.expectRevert(expectedError); - escrow.thaw(users.indexer, 2000 ether); - vm.stopPrank(); - } - - // Withdraw tests - - function testWithdraw_Tokens() public { - mint(users.gateway, 1000 ether); - vm.startPrank(users.gateway); - token.approve(address(escrow), 1000 ether); - escrow.deposit(users.indexer, 1000 ether); - escrow.thaw(users.indexer, 100 ether); - - // advance time - skip(withdrawEscrowThawingPeriod + 1); - - escrow.withdraw(users.indexer); - vm.stopPrank(); - - (uint256 indexerEscrowBalance,,) = escrow.escrowAccounts(users.gateway, users.indexer); - assertEq(indexerEscrowBalance, 900 ether); - } - - function testWithdraw_RevertWhen_NotThawing() public { - mint(users.gateway, 1000 ether); - vm.startPrank(users.gateway); - token.approve(address(escrow), 1000 ether); - escrow.deposit(users.indexer, 1000 ether); - - // revert - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowNotThawing()"); - vm.expectRevert(expectedError); - escrow.withdraw(users.indexer); - vm.stopPrank(); - } - - function testWithdraw_RevertWhen_StillThawing() public { - mint(users.gateway, 1000 ether); - vm.startPrank(users.gateway); - token.approve(address(escrow), 1000 ether); - escrow.deposit(users.indexer, 1000 ether); - escrow.thaw(users.indexer, 100 ether); - - // revert - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowStillThawing(uint256,uint256)", block.timestamp, block.timestamp + withdrawEscrowThawingPeriod); - vm.expectRevert(expectedError); - escrow.withdraw(users.indexer); - vm.stopPrank(); - } - - // Collect tests - - function testCollect() public { - uint256 amount = 1000 ether; - createProvision(amount); - setDelegationFeeCut(0, 100000); - - vm.startPrank(users.gateway); - escrow.approveCollector(users.verifier, 1000 ether); - token.approve(address(escrow), 1000 ether); - escrow.deposit(users.indexer, 1000 ether); - vm.stopPrank(); - - uint256 indexerPreviousBalance = token.balanceOf(users.indexer); - vm.prank(users.verifier); - escrow.collect(users.gateway, users.indexer, subgraphDataServiceAddress, 100 ether, IGraphPayments.PaymentType.IndexingFees, 3 ether); - - uint256 indexerBalance = token.balanceOf(users.indexer); - assertEq(indexerBalance - indexerPreviousBalance, 86 ether); - } - - function testCollect_RevertWhen_CollectorNotAuthorized() public { - address indexer = address(0xA3); - uint256 amount = 1000 ether; - - vm.startPrank(users.verifier); - uint256 dataServiceCut = 30000; // 3% - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowCollectorNotAuthorized(address,address)", users.gateway, users.verifier); - vm.expectRevert(expectedError); - escrow.collect(users.gateway, indexer, subgraphDataServiceAddress, amount, IGraphPayments.PaymentType.IndexingFees, dataServiceCut); - vm.stopPrank(); - } - - function testCollect_RevertWhen_CollectorHasInsufficientAmount() public { - vm.prank(users.gateway); - escrow.approveCollector(users.verifier, 100 ether); - - address indexer = address(0xA3); - uint256 amount = 1000 ether; - - mint(users.gateway, amount); - vm.startPrank(users.gateway); + function _depositTokens(uint256 amount) internal { token.approve(address(escrow), amount); - escrow.deposit(indexer, amount); - vm.stopPrank(); - - vm.startPrank(users.verifier); - uint256 dataServiceCut = 30 ether; - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowCollectorInsufficientAmount(uint256,uint256)", 100 ether, 1000 ether); - vm.expectRevert(expectedError); - escrow.collect(users.gateway, indexer, subgraphDataServiceAddress, 1000 ether, IGraphPayments.PaymentType.IndexingFees, dataServiceCut); - vm.stopPrank(); + escrow.deposit(users.indexer, amount); } - function testCollect_RevertWhen_SenderHasInsufficientAmountInEscrow() public { - mint(users.gateway, 1000 ether); - vm.startPrank(users.gateway); - escrow.approveCollector(users.verifier, 1000 ether); - token.approve(address(escrow), 1000 ether); - escrow.deposit(users.indexer, 100 ether); - vm.stopPrank(); - - vm.prank(users.verifier); - bytes memory expectedError = abi.encodeWithSignature("GraphEscrowInsufficientAmount(uint256,uint256)", 100 ether, 200 ether); - vm.expectRevert(expectedError); - escrow.collect(users.gateway, users.indexer, subgraphDataServiceAddress, 200 ether, IGraphPayments.PaymentType.IndexingFees, 3 ether); - vm.stopPrank(); + function _approveEscrow(uint256 amount) internal { + token.approve(address(escrow), amount); } } \ No newline at end of file diff --git a/packages/horizon/test/escrow/collect.t.sol b/packages/horizon/test/escrow/collect.t.sol new file mode 100644 index 000000000..a1d7a3a5f --- /dev/null +++ b/packages/horizon/test/escrow/collect.t.sol @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +import "forge-std/Test.sol"; + +import { GraphEscrowTest } from "./GraphEscrow.t.sol"; +import { IGraphPayments } from "../../contracts/interfaces/IGraphPayments.sol"; + +contract GraphEscrowCollectTest is GraphEscrowTest { + + function testCollect() public { + uint256 amount = 1000 ether; + createProvision(amount); + setDelegationFeeCut(0, 100000); + + vm.startPrank(users.gateway); + escrow.approveCollector(users.verifier, 1000 ether); + token.approve(address(escrow), 1000 ether); + escrow.deposit(users.indexer, 1000 ether); + vm.stopPrank(); + + uint256 indexerPreviousBalance = token.balanceOf(users.indexer); + vm.prank(users.verifier); + escrow.collect(users.gateway, users.indexer, subgraphDataServiceAddress, 100 ether, IGraphPayments.PaymentType.IndexingFees, 3 ether); + + uint256 indexerBalance = token.balanceOf(users.indexer); + assertEq(indexerBalance - indexerPreviousBalance, 86 ether); + } + + function testCollect_RevertWhen_CollectorNotAuthorized() public { + address indexer = address(0xA3); + uint256 amount = 1000 ether; + + vm.startPrank(users.verifier); + uint256 dataServiceCut = 30000; // 3% + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowCollectorNotAuthorized(address,address)", users.gateway, users.verifier); + vm.expectRevert(expectedError); + escrow.collect(users.gateway, indexer, subgraphDataServiceAddress, amount, IGraphPayments.PaymentType.IndexingFees, dataServiceCut); + vm.stopPrank(); + } + + function testCollect_RevertWhen_CollectorHasInsufficientAmount() public { + vm.prank(users.gateway); + escrow.approveCollector(users.verifier, 100 ether); + + address indexer = address(0xA3); + uint256 amount = 1000 ether; + + vm.startPrank(users.gateway); + token.approve(address(escrow), amount); + escrow.deposit(indexer, amount); + vm.stopPrank(); + + vm.startPrank(users.verifier); + uint256 dataServiceCut = 30 ether; + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowCollectorInsufficientAmount(uint256,uint256)", 100 ether, 1000 ether); + vm.expectRevert(expectedError); + escrow.collect(users.gateway, indexer, subgraphDataServiceAddress, 1000 ether, IGraphPayments.PaymentType.IndexingFees, dataServiceCut); + vm.stopPrank(); + } + + function testCollect_RevertWhen_SenderHasInsufficientAmountInEscrow() public { + vm.startPrank(users.gateway); + escrow.approveCollector(users.verifier, 1000 ether); + token.approve(address(escrow), 1000 ether); + escrow.deposit(users.indexer, 100 ether); + vm.stopPrank(); + + vm.prank(users.verifier); + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowInsufficientAmount(uint256,uint256)", 100 ether, 200 ether); + vm.expectRevert(expectedError); + escrow.collect(users.gateway, users.indexer, subgraphDataServiceAddress, 200 ether, IGraphPayments.PaymentType.IndexingFees, 3 ether); + vm.stopPrank(); + } +} \ No newline at end of file diff --git a/packages/horizon/test/escrow/collector.t.sol b/packages/horizon/test/escrow/collector.t.sol new file mode 100644 index 000000000..f4ef2b11c --- /dev/null +++ b/packages/horizon/test/escrow/collector.t.sol @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +import "forge-std/Test.sol"; + +import { GraphEscrowTest } from "./GraphEscrow.t.sol"; + +contract GraphEscrowCollectorTest is GraphEscrowTest { + function setUp() public virtual override { + GraphEscrowTest.setUp(); + vm.prank(users.gateway); + escrow.approveCollector(users.verifier, 1000 ether); + } + + // Collector approve tests + + function testCollector_Approve() public view { + (bool authorized,, uint256 thawEndTimestamp) = escrow.authorizedCollectors(users.gateway, users.verifier); + assertEq(authorized, true); + assertEq(thawEndTimestamp, 0); + } + + // Collector thaw tests + + function testCollector_Thaw() public { + vm.prank(users.gateway); + escrow.thawCollector(users.verifier); + + (bool authorized,, uint256 thawEndTimestamp) = escrow.authorizedCollectors(users.gateway, users.verifier); + assertEq(authorized, true); + assertEq(thawEndTimestamp, block.timestamp + revokeCollectorThawingPeriod); + } + + // Collector cancel thaw tests + + function testCollector_CancelThaw() public { + vm.prank(users.gateway); + escrow.thawCollector(users.verifier); + + (bool authorized,, uint256 thawEndTimestamp) = escrow.authorizedCollectors(users.gateway, users.verifier); + assertEq(authorized, true); + assertEq(thawEndTimestamp, block.timestamp + revokeCollectorThawingPeriod); + + vm.prank(users.gateway); + escrow.cancelThawCollector(users.verifier); + + (authorized,, thawEndTimestamp) = escrow.authorizedCollectors(users.gateway, users.verifier); + assertEq(authorized, true); + assertEq(thawEndTimestamp, 0); + } + + function testCollector_RevertWhen_CancelThawIsNotThawing() public { + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowNotThawing()"); + vm.expectRevert(expectedError); + escrow.cancelThawCollector(users.verifier); + vm.stopPrank(); + } + + // Collector revoke tests + + function testCollector_Revoke() public { + vm.startPrank(users.gateway); + escrow.thawCollector(users.verifier); + skip(revokeCollectorThawingPeriod + 1); + escrow.revokeCollector(users.verifier); + vm.stopPrank(); + + (bool authorized,,) = escrow.authorizedCollectors(users.gateway, users.verifier); + assertEq(authorized, false); + } + + function testCollector_RevertWhen_RevokeIsNotThawing() public { + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowNotThawing()"); + vm.expectRevert(expectedError); + vm.prank(users.gateway); + escrow.revokeCollector(users.verifier); + } + + function testCollector_RevertWhen_RevokeIsStillThawing() public { + vm.startPrank(users.gateway); + escrow.thawCollector(users.verifier); + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowStillThawing(uint256,uint256)", block.timestamp, block.timestamp + revokeCollectorThawingPeriod); + vm.expectRevert(expectedError); + escrow.revokeCollector(users.verifier); + vm.stopPrank(); + } +} \ No newline at end of file diff --git a/packages/horizon/test/escrow/deposit.t.sol b/packages/horizon/test/escrow/deposit.t.sol new file mode 100644 index 000000000..d7898bd37 --- /dev/null +++ b/packages/horizon/test/escrow/deposit.t.sol @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +import "forge-std/Test.sol"; + +import { GraphEscrowTest } from "./GraphEscrow.t.sol"; + +contract GraphEscrowDepositTest is GraphEscrowTest { + + function testDeposit_Tokens(uint256 amount) public useGateway depositTokens(amount) { + (uint256 indexerEscrowBalance,,) = escrow.escrowAccounts(users.gateway, users.indexer); + assertEq(indexerEscrowBalance, amount); + } + + function testDeposit_ManyDeposits(uint256 amount) public useGateway approveEscrow(amount) { + uint256 amountOne = amount / 2; + uint256 amountTwo = amount - amountOne; + + address otherIndexer = address(0xB3); + address[] memory indexers = new address[](2); + indexers[0] = users.indexer; + indexers[1] = otherIndexer; + + uint256[] memory amounts = new uint256[](2); + amounts[0] = amountOne; + amounts[1] = amountTwo; + + escrow.depositMany(indexers, amounts); + + (uint256 indexerEscrowBalance,,) = escrow.escrowAccounts(users.gateway, users.indexer); + assertEq(indexerEscrowBalance, amountOne); + + (uint256 otherIndexerEscrowBalance,,) = escrow.escrowAccounts(users.gateway, otherIndexer); + assertEq(otherIndexerEscrowBalance, amountTwo); + } + + function testDeposit_RevertWhen_ManyDepositsInputsLengthMismatch( + uint256 amount + ) public useGateway approveEscrow(amount) { + address otherIndexer = address(0xB3); + address[] memory indexers = new address[](2); + indexers[0] = users.indexer; + indexers[1] = otherIndexer; + + uint256[] memory amounts = new uint256[](1); + amounts[0] = 1000 ether; + + // revert + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowInputsLengthMismatch()"); + vm.expectRevert(expectedError); + escrow.depositMany(indexers, amounts); + } +} \ No newline at end of file diff --git a/packages/horizon/test/escrow/thaw.t.sol b/packages/horizon/test/escrow/thaw.t.sol new file mode 100644 index 000000000..51400feac --- /dev/null +++ b/packages/horizon/test/escrow/thaw.t.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +import "forge-std/Test.sol"; + +import { GraphEscrowTest } from "./GraphEscrow.t.sol"; + +contract GraphEscrowThawTest is GraphEscrowTest { + + function testThaw_Tokens(uint256 amount) public useGateway depositTokens(amount) { + escrow.thaw(users.indexer, amount); + + (, uint256 amountThawing,uint256 thawEndTimestamp) = escrow.escrowAccounts(users.gateway, users.indexer); + assertEq(amountThawing, amount); + assertEq(thawEndTimestamp, block.timestamp + withdrawEscrowThawingPeriod); + } + + function testThaw_RevertWhen_InsufficientThawAmount( + uint256 amount + ) public useGateway depositTokens(amount) { + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowInsufficientThawAmount()"); + vm.expectRevert(expectedError); + escrow.thaw(users.indexer, 0); + } + + function testThaw_RevertWhen_InsufficientAmount( + uint256 amount + ) public useGateway depositTokens(amount) { + uint256 overAmount = amount + 1; + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowInsufficientAmount(uint256,uint256)", amount, overAmount); + vm.expectRevert(expectedError); + escrow.thaw(users.indexer, overAmount); + } +} \ No newline at end of file diff --git a/packages/horizon/test/escrow/withdraw.t.sol b/packages/horizon/test/escrow/withdraw.t.sol new file mode 100644 index 000000000..075a16a74 --- /dev/null +++ b/packages/horizon/test/escrow/withdraw.t.sol @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +import "forge-std/Test.sol"; + +import { GraphEscrowTest } from "./GraphEscrow.t.sol"; + +contract GraphEscrowWithdrawTest is GraphEscrowTest { + + modifier depositAndThawTokens(uint256 amount, uint256 thawAmount) { + vm.assume(thawAmount > 0); + vm.assume(amount > thawAmount); + _depositTokens(amount); + escrow.thaw(users.indexer, thawAmount); + _; + } + + function testWithdraw_Tokens( + uint256 amount, + uint256 thawAmount + ) public useGateway depositAndThawTokens(amount, thawAmount) { + // advance time + skip(withdrawEscrowThawingPeriod + 1); + + escrow.withdraw(users.indexer); + vm.stopPrank(); + + (uint256 indexerEscrowBalance,,) = escrow.escrowAccounts(users.gateway, users.indexer); + assertEq(indexerEscrowBalance, amount - thawAmount); + } + + function testWithdraw_RevertWhen_NotThawing(uint256 amount) public useGateway depositTokens(amount) { + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowNotThawing()"); + vm.expectRevert(expectedError); + escrow.withdraw(users.indexer); + vm.stopPrank(); + } + + function testWithdraw_RevertWhen_StillThawing( + uint256 amount, + uint256 thawAmount + ) public useGateway depositAndThawTokens(amount, thawAmount) { + bytes memory expectedError = abi.encodeWithSignature("GraphEscrowStillThawing(uint256,uint256)", block.timestamp, block.timestamp + withdrawEscrowThawingPeriod); + vm.expectRevert(expectedError); + escrow.withdraw(users.indexer); + vm.stopPrank(); + } +} \ No newline at end of file diff --git a/packages/horizon/test/payments/GraphPayments.t.sol b/packages/horizon/test/payments/GraphPayments.t.sol index e7889101f..afcecd803 100644 --- a/packages/horizon/test/payments/GraphPayments.t.sol +++ b/packages/horizon/test/payments/GraphPayments.t.sol @@ -5,9 +5,9 @@ import "forge-std/Test.sol"; import { IGraphPayments } from "../../contracts/interfaces/IGraphPayments.sol"; -import { HorizonStaking_Shared_Test } from "../shared/horizon-staking/HorizonStaking.t.sol"; +import { HorizonStakingSharedTest } from "../shared/horizon-staking/HorizonStaking.t.sol"; -contract GraphPaymentsTest is HorizonStaking_Shared_Test { +contract GraphPaymentsTest is HorizonStakingSharedTest { function testCollect() public { // Setup Staking diff --git a/packages/horizon/test/shared/horizon-staking/HorizonStaking.t.sol b/packages/horizon/test/shared/horizon-staking/HorizonStaking.t.sol index 62a8db3ac..c5aec8aa2 100644 --- a/packages/horizon/test/shared/horizon-staking/HorizonStaking.t.sol +++ b/packages/horizon/test/shared/horizon-staking/HorizonStaking.t.sol @@ -5,15 +5,13 @@ import "forge-std/Test.sol"; import { GraphBaseTest } from "../../GraphBase.t.sol"; -abstract contract HorizonStaking_Shared_Test is GraphBaseTest { +abstract contract HorizonStakingSharedTest is GraphBaseTest { /* Set Up */ - // function setUp() public virtual override { - // GraphBaseTest.setUp(); - - - // } + function setUp() public virtual override { + GraphBaseTest.setUp(); + } /* Helpers */