|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.27; |
| 3 | + |
| 4 | +import { IRecurringCollector } from "@graphprotocol/horizon/contracts/interfaces/IRecurringCollector.sol"; |
| 5 | +import { IGraphPayments } from "@graphprotocol/horizon/contracts/interfaces/IGraphPayments.sol"; |
| 6 | +import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol"; |
| 7 | +import { ISubgraphService } from "../../../contracts/interfaces/ISubgraphService.sol"; |
| 8 | + |
| 9 | +import { SubgraphServiceIndexingAgreementSharedTest } from "./shared.t.sol"; |
| 10 | + |
| 11 | +import { RecurringCollectorHelper } from "@graphprotocol/horizon/test/payments/recurring-collector/RecurringCollectorHelper.t.sol"; |
| 12 | + |
| 13 | +contract SubgraphServiceIndexingAgreementCancelTest is SubgraphServiceIndexingAgreementSharedTest { |
| 14 | + using PPMMath for uint256; |
| 15 | + |
| 16 | + struct TestState { |
| 17 | + uint256 escrowBalance; |
| 18 | + uint256 indexerBalance; |
| 19 | + uint256 indexerTokensLocked; |
| 20 | + } |
| 21 | + |
| 22 | + RecurringCollectorHelper private _recurringCollectorHelper; |
| 23 | + |
| 24 | + function setUp() public override { |
| 25 | + super.setUp(); |
| 26 | + |
| 27 | + _recurringCollectorHelper = new RecurringCollectorHelper(recurringCollector); |
| 28 | + } |
| 29 | + |
| 30 | + /* |
| 31 | + * TESTS |
| 32 | + */ |
| 33 | + |
| 34 | + /* solhint-disable graph/func-name-mixedcase */ |
| 35 | + function test_SubgraphService_CollectIndexingFee_Integration( |
| 36 | + SetupTestIndexerParams calldata fuzzyParams, |
| 37 | + IRecurringCollector.RecurrentCollectionVoucher memory fuzzyRCV, |
| 38 | + uint256 unboundedSignerPrivateKey, |
| 39 | + uint256 fuzzyTokensCollected |
| 40 | + ) public { |
| 41 | + uint256 expectedTotalTokensCollected = bound(fuzzyTokensCollected, 1000, 1_000_000); |
| 42 | + uint256 expectedTokensLocked = stakeToFeesRatio * expectedTotalTokensCollected; |
| 43 | + uint256 expectedProtocolTokensBurnt = expectedTotalTokensCollected.mulPPMRoundUp( |
| 44 | + graphPayments.PROTOCOL_PAYMENT_CUT() |
| 45 | + ); |
| 46 | + uint256 expectedIndexerTokensCollected = expectedTotalTokensCollected - expectedProtocolTokensBurnt; |
| 47 | + |
| 48 | + TestIndexerParams memory params = _setupIndexer(fuzzyParams, expectedTokensLocked); |
| 49 | + |
| 50 | + uint256 signerPrivateKey = boundKey(unboundedSignerPrivateKey); |
| 51 | + _setupPayerWithEscrow(fuzzyRCV.payer, signerPrivateKey, params.indexer, expectedTotalTokensCollected); |
| 52 | + |
| 53 | + uint256 agreementTokensPerSecond = 1; |
| 54 | + // Create the Indexing Agreement |
| 55 | + fuzzyRCV.acceptDeadline = block.timestamp; // accept now |
| 56 | + fuzzyRCV.duration = type(uint256).max; // no expiration |
| 57 | + fuzzyRCV.maxInitialTokens = 0; // no initial payment |
| 58 | + fuzzyRCV.maxOngoingTokensPerSecond = type(uint32).max; // unlimited tokens per second |
| 59 | + fuzzyRCV.minSecondsPerCollection = 1; // 1 second between collections |
| 60 | + fuzzyRCV.maxSecondsPerCollection = type(uint32).max; // no maximum time between collections |
| 61 | + fuzzyRCV.serviceProvider = params.indexer; // service provider is the indexer |
| 62 | + fuzzyRCV.dataService = address(subgraphService); // data service is the subgraph service |
| 63 | + fuzzyRCV.metadata = abi.encode( |
| 64 | + ISubgraphService.RCVMetadata({ |
| 65 | + tokensPerSecond: agreementTokensPerSecond, |
| 66 | + tokensPerEntityPerSecond: 0, // no payment for entities |
| 67 | + subgraphDeploymentId: params.subgraphDeploymentId |
| 68 | + }) |
| 69 | + ); |
| 70 | + |
| 71 | + ISubgraphService.IndexingAgreementKey memory key = ISubgraphService.IndexingAgreementKey({ |
| 72 | + payer: fuzzyRCV.payer, |
| 73 | + indexer: params.indexer, |
| 74 | + agreementId: fuzzyRCV.agreementId |
| 75 | + }); |
| 76 | + |
| 77 | + resetPrank(params.indexer); |
| 78 | + |
| 79 | + // Accept the Indexing Agreement |
| 80 | + subgraphService.acceptIndexingAgreement( |
| 81 | + params.allocationId, |
| 82 | + _recurringCollectorHelper.generateSignedRCV(fuzzyRCV, signerPrivateKey) |
| 83 | + ); |
| 84 | + |
| 85 | + // Skip ahead to collection point |
| 86 | + skip(expectedTotalTokensCollected / agreementTokensPerSecond); |
| 87 | + |
| 88 | + TestState memory beforeCollect = _getState(fuzzyRCV.payer, params.indexer); |
| 89 | + uint256 tokensCollected = subgraphService.collect( |
| 90 | + params.indexer, |
| 91 | + IGraphPayments.PaymentTypes.IndexingFee, |
| 92 | + abi.encode(key, 1, keccak256(abi.encodePacked("poi"))) |
| 93 | + ); |
| 94 | + TestState memory afterCollect = _getState(fuzzyRCV.payer, params.indexer); |
| 95 | + |
| 96 | + uint256 indexerTokensCollected = afterCollect.indexerBalance - beforeCollect.indexerBalance; |
| 97 | + uint256 protocolTokensBurnt = tokensCollected - indexerTokensCollected; |
| 98 | + |
| 99 | + assertEq( |
| 100 | + afterCollect.escrowBalance, |
| 101 | + beforeCollect.escrowBalance - tokensCollected, |
| 102 | + "Escrow balance should be reduced by the amount collected" |
| 103 | + ); |
| 104 | + assertEq(tokensCollected, expectedTotalTokensCollected, "Total tokens collected should match"); |
| 105 | + assertEq(expectedProtocolTokensBurnt, protocolTokensBurnt, "Protocol tokens burnt should match"); |
| 106 | + assertEq(indexerTokensCollected, expectedIndexerTokensCollected, "Indexer tokens collected should match"); |
| 107 | + assertEq( |
| 108 | + afterCollect.indexerTokensLocked, |
| 109 | + beforeCollect.indexerTokensLocked + expectedTokensLocked, |
| 110 | + "Locked tokens should match" |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + /* solhint-enable graph/func-name-mixedcase */ |
| 115 | + |
| 116 | + function _setupIndexer( |
| 117 | + SetupTestIndexerParams calldata _fuzzyParams, |
| 118 | + uint256 _tokensToAddToProvision |
| 119 | + ) private returns (TestIndexerParams memory) { |
| 120 | + TestIndexerParams memory params = _setupTestIndexer(_fuzzyParams); |
| 121 | + deal({ token: address(token), to: params.indexer, give: _tokensToAddToProvision }); |
| 122 | + vm.startPrank(params.indexer); |
| 123 | + _addToProvision(params.indexer, _tokensToAddToProvision); |
| 124 | + vm.stopPrank(); |
| 125 | + |
| 126 | + return params; |
| 127 | + } |
| 128 | + |
| 129 | + function _setupPayerWithEscrow( |
| 130 | + address _payer, |
| 131 | + uint256 _signerPrivateKey, |
| 132 | + address _indexer, |
| 133 | + uint256 _escrowTokens |
| 134 | + ) private { |
| 135 | + _recurringCollectorHelper.authorizeSignerWithChecks(_payer, _signerPrivateKey); |
| 136 | + |
| 137 | + deal({ token: address(token), to: _payer, give: _escrowTokens }); |
| 138 | + vm.startPrank(_payer); |
| 139 | + _escrow(_escrowTokens, _indexer); |
| 140 | + vm.stopPrank(); |
| 141 | + } |
| 142 | + |
| 143 | + function _escrow(uint256 _tokens, address _indexer) private { |
| 144 | + token.approve(address(escrow), _tokens); |
| 145 | + escrow.deposit(address(recurringCollector), _indexer, _tokens); |
| 146 | + } |
| 147 | + |
| 148 | + function _getState(address _payer, address _indexer) private view returns (TestState memory) { |
| 149 | + CollectPaymentData memory collect = _collectPaymentData(_indexer); |
| 150 | + |
| 151 | + return |
| 152 | + TestState({ |
| 153 | + escrowBalance: escrow.getBalance(_payer, address(recurringCollector), _indexer), |
| 154 | + indexerBalance: collect.indexerBalance, |
| 155 | + indexerTokensLocked: collect.lockedTokens |
| 156 | + }); |
| 157 | + } |
| 158 | +} |
0 commit comments