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

[WIP] Horizon: fix unit tests and refactor #972

Merged
merged 7 commits into from
May 23, 2024
Merged
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
5 changes: 1 addition & 4 deletions packages/contracts/contracts/upgrades/GraphProxy.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later

pragma solidity ^0.7.6;

import { Address } from "@openzeppelin/contracts/utils/Address.sol";
pragma solidity >=0.6.12 <0.9.0;

import { GraphProxyStorage } from "./GraphProxyStorage.sol";

Expand Down Expand Up @@ -160,7 +158,6 @@ contract GraphProxy is GraphProxyStorage, IGraphProxy {
*/
function _acceptUpgrade() internal {
address _pendingImplementation = _getPendingImplementation();
require(Address.isContract(_pendingImplementation), "Impl must be a contract");
require(_pendingImplementation != address(0), "Impl cannot be zero address");
require(msg.sender == _pendingImplementation, "Only pending implementation");

Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/contracts/upgrades/GraphProxyAdmin.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later

pragma solidity ^0.7.6;
pragma solidity >=0.6.12 <0.9.0;

import { Governed } from "../governance/Governed.sol";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later

pragma solidity ^0.7.6;
pragma solidity >=0.6.12 <0.9.0;

/**
* @title Graph Proxy Storage
Expand Down
20 changes: 13 additions & 7 deletions packages/horizon/contracts/payments/GraphPayments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract GraphPayments is Multicall, GraphDirectory, GraphPaymentsStorageV1Stora
error GraphPaymentsNotThawing();
error GraphPaymentsStillThawing(uint256 currentTimestamp, uint256 thawEndTimestamp);
error GraphPaymentsCollectorNotAuthorized(address sender, address dataService);
error GraphPaymentsCollectorInsufficientAmount(uint256 available, uint256 required);
error GraphPaymentsInsufficientAmount(uint256 available, uint256 required);

// -- Events --

Expand All @@ -54,22 +54,28 @@ contract GraphPayments is Multicall, GraphDirectory, GraphPaymentsStorageV1Stora
) external {
_graphToken().pullTokens(msg.sender, tokens);

// Pay protocol cut
// Calculate cuts
uint256 tokensProtocol = (tokens * PROTOCOL_PAYMENT_CUT) / MAX_PPM;
uint256 delegationFeeCut = _graphStaking().getDelegationFeeCut(receiver, dataService, uint8(paymentType));
uint256 tokensDelegationPool = (tokens * delegationFeeCut) / MAX_PPM;
uint256 totalCut = tokensProtocol + tokensDataService + tokensDelegationPool;
if (totalCut > tokens) {
revert GraphPaymentsInsufficientAmount(tokens, totalCut);
}

// Pay protocol cut
_graphToken().burnTokens(tokensProtocol);

// Pay data service cut
_graphToken().pushTokens(dataService, tokensDataService);

// Get delegation cut
uint256 delegationFeeCut = _graphStaking().getDelegationFeeCut(receiver, dataService, uint8(paymentType));
uint256 tokensDelegationPool = (tokens * delegationFeeCut) / MAX_PPM;
// Pay delegators
if (tokensDelegationPool > 0) {
_graphStaking().addToDelegationPool(receiver, dataService, tokensDelegationPool);
}

// Pay the rest to the receiver
uint256 tokensReceiverRemaining = tokens - tokensProtocol - tokensDataService - tokensDelegationPool;
// Pay receiver
uint256 tokensReceiverRemaining = tokens - totalCut;
_graphToken().pushTokens(receiver, tokensReceiverRemaining);

emit GraphPaymentsCollected(
Expand Down
13 changes: 9 additions & 4 deletions packages/horizon/contracts/payments/PaymentsEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,19 @@ contract PaymentsEscrow is Multicall, GraphDirectory, IPaymentsEscrow {
revert GraphEscrowThawingPeriodTooLong(withdrawEscrowThawingPeriod, MAX_THAWING_PERIOD);
}

revokeCollectorThawingPeriod = revokeCollectorThawingPeriod;
withdrawEscrowThawingPeriod = withdrawEscrowThawingPeriod;
REVOKE_COLLECTOR_THAWING_PERIOD = revokeCollectorThawingPeriod;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL very nice catch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha I was so confused for a moment

WITHDRAW_ESCROW_THAWING_PERIOD = withdrawEscrowThawingPeriod;
}

// approve a data service to collect funds
function approveCollector(address dataService, uint256 amount) external {
authorizedCollectors[msg.sender][dataService].authorized = true;
authorizedCollectors[msg.sender][dataService].amount = amount;
Collector storage collector = authorizedCollectors[msg.sender][dataService];
if (collector.amount > amount) {
revert GraphEscrowCollectorInsufficientAmount(collector.amount, amount);
}

collector.authorized = true;
collector.amount = amount;
emit AuthorizedCollector(msg.sender, dataService);
}

Expand Down
73 changes: 33 additions & 40 deletions packages/horizon/test/GraphBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity ^0.8.24;

import "forge-std/Test.sol";

import { GraphProxyAdmin } from "@graphprotocol/contracts/contracts/upgrades/GraphProxyAdmin.sol";
import { GraphProxy } from "@graphprotocol/contracts/contracts/upgrades/GraphProxy.sol";
import { Controller } from "@graphprotocol/contracts/contracts/governance/Controller.sol";

import { PaymentsEscrow } from "contracts/payments/PaymentsEscrow.sol";
Expand All @@ -18,6 +20,7 @@ abstract contract GraphBaseTest is Test, Constants {

/* Contracts */

GraphProxyAdmin public proxyAdmin;
Controller public controller;
MockGRTToken public token;
GraphPayments public payments;
Expand All @@ -41,6 +44,7 @@ abstract contract GraphBaseTest is Test, Constants {

function setUp() public virtual {
// Deploy ERC20 token
vm.prank(users.deployer);
token = new MockGRTToken();

// Setup Users
Expand All @@ -67,10 +71,16 @@ abstract contract GraphBaseTest is Test, Constants {
}

function deployProtocolContracts() private {
vm.prank(users.governor);
vm.startPrank(users.governor);
proxyAdmin = new GraphProxyAdmin();
controller = new Controller();
vm.stopPrank();

// GraphPayments preddict address
// Staking Proxy
vm.prank(users.deployer);
GraphProxy stakingProxy = new GraphProxy(address(0), address(proxyAdmin));

// GraphPayments predict address
bytes32 saltPayments = keccak256("GraphPaymentsSalt");
bytes32 paymentsHash = keccak256(bytes.concat(
vm.getCode("GraphPayments.sol:GraphPayments"),
Expand All @@ -82,7 +92,7 @@ abstract contract GraphBaseTest is Test, Constants {
users.deployer
);

// GraphEscrow preddict address
// GraphEscrow predict address
bytes32 saltEscrow = keccak256("GraphEscrowSalt");
bytes32 escrowHash = keccak256(bytes.concat(
vm.getCode("GraphEscrow.sol:GraphEscrow"),
Expand All @@ -98,40 +108,18 @@ abstract contract GraphBaseTest is Test, Constants {
users.deployer
);

// HorizonStakingExtension preddict address
bytes32 saltHorizonStakingExtension = keccak256("HorizonStakingExtensionSalt");
bytes32 horizonStakingExtensionHash = keccak256(bytes.concat(
vm.getCode("HorizonStakingExtension.sol:HorizonStakingExtension"),
abi.encode(address(controller), subgraphDataServiceAddress)
));
address predictedAddressHorizonStakingExtension = vm.computeCreate2Address(
saltHorizonStakingExtension,
horizonStakingExtensionHash,
users.deployer
);

// HorizonStaking preddict address
bytes32 saltHorizonStaking = keccak256("saltHorizonStaking");
bytes32 horizonStakingHash = keccak256(bytes.concat(
vm.getCode("HorizonStaking.sol:HorizonStaking"),
abi.encode(
address(controller),
predictedAddressHorizonStakingExtension,
subgraphDataServiceAddress
)
));
// address predictedAddressHorizonStaking = vm.computeCreate2Address(
// saltHorizonStaking,
// horizonStakingHash,
// users.deployer
// );

// Setup controller
vm.startPrank(users.governor);
controller.setContractProxy(keccak256("GraphToken"), address(token));
controller.setContractProxy(keccak256("GraphEscrow"), predictedAddressEscrow);
controller.setContractProxy(keccak256("PaymentsEscrow"), predictedAddressEscrow);
controller.setContractProxy(keccak256("GraphPayments"), predictedPaymentsAddress);
// controller.setContractProxy(keccak256("Staking"), predictedAddressHorizonStaking);
controller.setContractProxy(keccak256("Staking"), address(stakingProxy));
controller.setContractProxy(keccak256("EpochManager"), makeAddr("EpochManager"));
controller.setContractProxy(keccak256("RewardsManager"), makeAddr("RewardsManager"));
controller.setContractProxy(keccak256("Curation"), makeAddr("Curation"));
controller.setContractProxy(keccak256("GraphTokenGateway"), makeAddr("GraphTokenGateway"));
controller.setContractProxy(keccak256("BridgeEscrow"), makeAddr("BridgeEscrow"));
controller.setContractProxy(keccak256("GraphProxyAdmin"), makeAddr("GraphProxyAdmin"));
vm.stopPrank();

vm.startPrank(users.deployer);
Expand All @@ -144,16 +132,21 @@ abstract contract GraphBaseTest is Test, Constants {
revokeCollectorThawingPeriod,
withdrawEscrowThawingPeriod
);
stakingBase = new HorizonStaking{salt: saltHorizonStaking}(
stakingExtension = new HorizonStakingExtension(
address(controller),
predictedAddressHorizonStakingExtension,
subgraphDataServiceAddress
);
staking = IHorizonStaking(address(stakingBase));
// stakingExtension = new HorizonStakingExtension{salt: saltHorizonStakingExtension}(
// address(controller),
// subgraphDataServiceAddress
// );
stakingBase = new HorizonStaking(
address(controller),
address(stakingExtension),
subgraphDataServiceAddress
);
vm.stopPrank();

vm.startPrank(users.governor);
proxyAdmin.upgrade(stakingProxy, address(stakingBase));
proxyAdmin.acceptProxy(stakingBase, stakingProxy);
staking = IHorizonStaking(address(stakingProxy));
vm.stopPrank();
}

Expand Down
33 changes: 0 additions & 33 deletions packages/horizon/test/HorizonStaking.t.sol

This file was deleted.

38 changes: 0 additions & 38 deletions packages/horizon/test/HorizonStaking.ts

This file was deleted.

30 changes: 19 additions & 11 deletions packages/horizon/test/escrow/GraphEscrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,36 @@ contract GraphEscrowTest is HorizonStakingSharedTest {
vm.stopPrank();
}

modifier approveEscrow(uint256 amount) {
_approveEscrow(amount);
modifier approveEscrow(uint256 tokens) {
changePrank(users.gateway);
_approveEscrow(tokens);
_;
}

modifier depositTokens(uint256 amount) {
vm.assume(amount > 0);
vm.assume(amount <= 10000 ether);
_depositTokens(amount);
modifier useDeposit(uint256 tokens) {
changePrank(users.gateway);
vm.assume(tokens > 0);
vm.assume(tokens <= 10_000_000_000 ether);
_depositTokens(tokens);
_;
}

modifier useCollector(uint256 tokens) {
changePrank(users.gateway);
escrow.approveCollector(users.verifier, tokens);
_;
}

function setUp() public virtual override {
HorizonStakingSharedTest.setUp();
}

function _depositTokens(uint256 amount) internal {
token.approve(address(escrow), amount);
escrow.deposit(users.indexer, amount);
function _depositTokens(uint256 tokens) internal {
token.approve(address(escrow), tokens);
escrow.deposit(users.indexer, tokens);
}

function _approveEscrow(uint256 amount) internal {
token.approve(address(escrow), amount);
function _approveEscrow(uint256 tokens) internal {
token.approve(address(escrow), tokens);
}
}
Loading
Loading