|
| 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 { ISubgraphService } from "../../../contracts/interfaces/ISubgraphService.sol"; |
| 7 | + |
| 8 | +import { SubgraphServiceIndexingAgreementSharedTest } from "./shared.t.sol"; |
| 9 | + |
| 10 | +import { RecurringCollectorHelper } from "@graphprotocol/horizon/test/payments/recurring-collector/RecurringCollectorHelper.t.sol"; |
| 11 | + |
| 12 | +contract SubgraphServiceIndexingAgreementCancelTest is SubgraphServiceIndexingAgreementSharedTest { |
| 13 | + RecurringCollectorHelper private _recurringCollectorHelper; |
| 14 | + |
| 15 | + function setUp() public override { |
| 16 | + super.setUp(); |
| 17 | + |
| 18 | + _recurringCollectorHelper = new RecurringCollectorHelper(recurringCollector); |
| 19 | + } |
| 20 | + |
| 21 | + /* |
| 22 | + * TESTS |
| 23 | + */ |
| 24 | + |
| 25 | + /* solhint-disable graph/func-name-mixedcase */ |
| 26 | + function test_SubgraphService_CollectIndexingFee_Integration( |
| 27 | + SetupTestIndexerParams calldata fuzzyParams, |
| 28 | + IRecurringCollector.RecurrentCollectionVoucher memory fuzzyRCV, |
| 29 | + uint256 escrowTokens |
| 30 | + ) public { |
| 31 | + vm.assume(escrowTokens > 0); |
| 32 | + |
| 33 | + // Authorize Signer to issue Vouchers on behalf of the gateway |
| 34 | + (, uint256 signerPrivateKey) = makeAddrAndKey("signer"); |
| 35 | + _recurringCollectorHelper.authorizeSignerWithChecks(users.gateway, signerPrivateKey); |
| 36 | + |
| 37 | + // Setup Indexer |
| 38 | + TestIndexerParams memory params = _setupTestIndexer(fuzzyParams); |
| 39 | + deal({ token: address(token), to: params.indexer, give: 1 ether }); |
| 40 | + vm.startPrank(params.indexer); |
| 41 | + _addToProvision(params.indexer, 1 ether); |
| 42 | + subgraphService.setRewardsDestination(params.indexer); |
| 43 | + vm.stopPrank(); |
| 44 | + |
| 45 | + // Gateway deposits tokens to the Escrow |
| 46 | + deal({ token: address(token), to: users.gateway, give: 1 ether }); |
| 47 | + vm.startPrank(users.gateway); |
| 48 | + // FIX-ME: sometimes this fails with ERC20InsufficientBalance?!?!? |
| 49 | + _escrow(1 ether, params.indexer); |
| 50 | + vm.stopPrank(); |
| 51 | + |
| 52 | + // Create the Indexing Agreement |
| 53 | + fuzzyRCV.acceptDeadline = block.timestamp; // accept now |
| 54 | + fuzzyRCV.duration = type(uint256).max; // no expiration |
| 55 | + fuzzyRCV.maxInitialTokens = 0; // no initial payment |
| 56 | + fuzzyRCV.maxOngoingTokensPerSecond = 1; // 1 token per second |
| 57 | + fuzzyRCV.minSecondsPerCollection = 0; // no minimum time between collections |
| 58 | + fuzzyRCV.maxSecondsPerCollection = type(uint32).max; // no maximum time between collections |
| 59 | + fuzzyRCV.payer = users.gateway; // payer is the gateway |
| 60 | + fuzzyRCV.serviceProvider = params.indexer; // service provider is the indexer |
| 61 | + fuzzyRCV.dataService = address(subgraphService); // data service is the subgraph service |
| 62 | + fuzzyRCV.metadata = abi.encode( |
| 63 | + ISubgraphService.RCVMetadata({ |
| 64 | + tokensPerSecond: 1, |
| 65 | + tokensPerEntityPerSecond: 0, |
| 66 | + subgraphDeploymentId: params.subgraphDeploymentId |
| 67 | + }) |
| 68 | + ); |
| 69 | + |
| 70 | + resetPrank(params.indexer); |
| 71 | + subgraphService.acceptIndexingAgreement( |
| 72 | + params.allocationId, |
| 73 | + _recurringCollectorHelper.generateSignedRCV(fuzzyRCV, signerPrivateKey) |
| 74 | + ); |
| 75 | + |
| 76 | + // check that indexer stake is enough to cover the fee |
| 77 | + // vm.assume(tokensAllocated > minimumProvisionTokens * stakeToFeesRatio); |
| 78 | + |
| 79 | + // uint256 maxTokensPayment = params.tokens / stakeToFeesRatio > type(uint128).max |
| 80 | + // ? type(uint128).max |
| 81 | + // : tokensAllocated / stakeToFeesRatio; |
| 82 | + // tokensPayment = bound(tokensPayment, minimumProvisionTokens, maxTokensPayment); |
| 83 | + |
| 84 | + skip(fuzzyRCV.minSecondsPerCollection + 1000); |
| 85 | + ISubgraphService.IndexingAgreementKey memory key = ISubgraphService.IndexingAgreementKey({ |
| 86 | + payer: users.gateway, |
| 87 | + indexer: params.indexer, |
| 88 | + agreementId: fuzzyRCV.agreementId |
| 89 | + }); |
| 90 | + uint256 expectedTotalTokensCollected = 1000; |
| 91 | + uint256 expectedProtocolTokensBurnt = 10; |
| 92 | + uint256 expectedIndexerTokensCollected = expectedTotalTokensCollected - expectedProtocolTokensBurnt; |
| 93 | + uint256 entities = 1; |
| 94 | + bytes32 poi = keccak256(abi.encodePacked("poi")); |
| 95 | + vm.expectEmit(); |
| 96 | + emit ISubgraphService.IndexingFeesCollected( |
| 97 | + key.indexer, |
| 98 | + key.payer, |
| 99 | + key.agreementId, |
| 100 | + params.allocationId, |
| 101 | + params.subgraphDeploymentId, |
| 102 | + epochManager.currentEpoch(), |
| 103 | + expectedTotalTokensCollected, |
| 104 | + entities, |
| 105 | + poi |
| 106 | + ); |
| 107 | + CollectPaymentData memory collectPaymentDataBefore = _collectPaymentData(params.indexer); |
| 108 | + uint256 tokensCollected = subgraphService.collect( |
| 109 | + params.indexer, |
| 110 | + IGraphPayments.PaymentTypes.IndexingFee, |
| 111 | + abi.encode(key, entities, poi) |
| 112 | + ); |
| 113 | + CollectPaymentData memory collectPaymentDataAfter = _collectPaymentData(params.indexer); |
| 114 | + |
| 115 | + assertEq(tokensCollected, expectedTotalTokensCollected); |
| 116 | + assertEq( |
| 117 | + collectPaymentDataAfter.indexerBalance, |
| 118 | + collectPaymentDataBefore.indexerBalance + expectedIndexerTokensCollected |
| 119 | + ); |
| 120 | + assertEq(collectPaymentDataAfter.curationBalance, collectPaymentDataBefore.curationBalance + 0); |
| 121 | + assertEq(collectPaymentDataAfter.lockedTokens, collectPaymentDataBefore.lockedTokens + 2000); |
| 122 | + } |
| 123 | + |
| 124 | + /* solhint-enable graph/func-name-mixedcase */ |
| 125 | + |
| 126 | + function _escrow(uint256 tokens, address _indexer) private { |
| 127 | + token.approve(address(escrow), tokens); |
| 128 | + escrow.deposit(address(recurringCollector), _indexer, tokens); |
| 129 | + } |
| 130 | +} |
0 commit comments