|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import { IRecurringCollector } from "@graphprotocol/interfaces/contracts/horizon/IRecurringCollector.sol"; |
| 5 | +import { IGraphPayments } from "@graphprotocol/interfaces/contracts/horizon/IGraphPayments.sol"; |
| 6 | + |
| 7 | +import { RecurringCollectorSharedTest } from "./shared.t.sol"; |
| 8 | +import { MockAgreementOwner } from "./MockAgreementOwner.t.sol"; |
| 9 | +import { BareAgreementOwner } from "./BareAgreementOwner.t.sol"; |
| 10 | + |
| 11 | +/// @notice Tests for the IProviderEligibility gate in RecurringCollector._collect() |
| 12 | +contract RecurringCollectorEligibilityTest is RecurringCollectorSharedTest { |
| 13 | + function _newApprover() internal returns (MockAgreementOwner) { |
| 14 | + return new MockAgreementOwner(); |
| 15 | + } |
| 16 | + |
| 17 | + function _acceptUnsignedAgreement( |
| 18 | + MockAgreementOwner approver |
| 19 | + ) internal returns (IRecurringCollector.RecurringCollectionAgreement memory rca, bytes16 agreementId) { |
| 20 | + rca = _recurringCollectorHelper.sensibleRCA( |
| 21 | + IRecurringCollector.RecurringCollectionAgreement({ |
| 22 | + deadline: uint64(block.timestamp + 1 hours), |
| 23 | + endsAt: uint64(block.timestamp + 365 days), |
| 24 | + payer: address(approver), |
| 25 | + dataService: makeAddr("ds"), |
| 26 | + serviceProvider: makeAddr("sp"), |
| 27 | + maxInitialTokens: 100 ether, |
| 28 | + maxOngoingTokensPerSecond: 1 ether, |
| 29 | + minSecondsPerCollection: 600, |
| 30 | + maxSecondsPerCollection: 3600, |
| 31 | + nonce: 1, |
| 32 | + metadata: "" |
| 33 | + }) |
| 34 | + ); |
| 35 | + |
| 36 | + bytes32 agreementHash = _recurringCollector.hashRCA(rca); |
| 37 | + approver.authorize(agreementHash); |
| 38 | + _setupValidProvision(rca.serviceProvider, rca.dataService); |
| 39 | + |
| 40 | + vm.prank(rca.dataService); |
| 41 | + agreementId = _recurringCollector.accept(rca, ""); |
| 42 | + } |
| 43 | + |
| 44 | + /* solhint-disable graph/func-name-mixedcase */ |
| 45 | + |
| 46 | + function test_Collect_OK_WhenEligible() public { |
| 47 | + MockAgreementOwner approver = _newApprover(); |
| 48 | + (IRecurringCollector.RecurringCollectionAgreement memory rca, bytes16 agreementId) = _acceptUnsignedAgreement( |
| 49 | + approver |
| 50 | + ); |
| 51 | + |
| 52 | + // Enable eligibility check and mark provider as eligible |
| 53 | + approver.setEligibilityEnabled(true); |
| 54 | + approver.setProviderEligible(rca.serviceProvider, true); |
| 55 | + |
| 56 | + skip(rca.minSecondsPerCollection); |
| 57 | + uint256 tokens = 1 ether; |
| 58 | + bytes memory data = _generateCollectData(_generateCollectParams(rca, agreementId, bytes32("col1"), tokens, 0)); |
| 59 | + |
| 60 | + vm.prank(rca.dataService); |
| 61 | + uint256 collected = _recurringCollector.collect(IGraphPayments.PaymentTypes.IndexingFee, data); |
| 62 | + assertEq(collected, tokens); |
| 63 | + } |
| 64 | + |
| 65 | + function test_Collect_Revert_WhenNotEligible() public { |
| 66 | + MockAgreementOwner approver = _newApprover(); |
| 67 | + (IRecurringCollector.RecurringCollectionAgreement memory rca, bytes16 agreementId) = _acceptUnsignedAgreement( |
| 68 | + approver |
| 69 | + ); |
| 70 | + |
| 71 | + // Enable eligibility check but provider is NOT eligible |
| 72 | + approver.setEligibilityEnabled(true); |
| 73 | + // defaultEligible is false, and provider not explicitly set |
| 74 | + |
| 75 | + skip(rca.minSecondsPerCollection); |
| 76 | + uint256 tokens = 1 ether; |
| 77 | + bytes memory data = _generateCollectData(_generateCollectParams(rca, agreementId, bytes32("col1"), tokens, 0)); |
| 78 | + |
| 79 | + vm.expectRevert( |
| 80 | + abi.encodeWithSelector( |
| 81 | + IRecurringCollector.RecurringCollectorCollectionNotEligible.selector, |
| 82 | + agreementId, |
| 83 | + rca.serviceProvider |
| 84 | + ) |
| 85 | + ); |
| 86 | + vm.prank(rca.dataService); |
| 87 | + _recurringCollector.collect(IGraphPayments.PaymentTypes.IndexingFee, data); |
| 88 | + } |
| 89 | + |
| 90 | + function test_Collect_OK_WhenPayerDoesNotSupportInterface() public { |
| 91 | + MockAgreementOwner approver = _newApprover(); |
| 92 | + (IRecurringCollector.RecurringCollectionAgreement memory rca, bytes16 agreementId) = _acceptUnsignedAgreement( |
| 93 | + approver |
| 94 | + ); |
| 95 | + |
| 96 | + // eligibilityEnabled is false by default — supportsInterface returns false for IProviderEligibility |
| 97 | + // Collection should proceed normally (backward compatible) |
| 98 | + |
| 99 | + skip(rca.minSecondsPerCollection); |
| 100 | + uint256 tokens = 1 ether; |
| 101 | + bytes memory data = _generateCollectData(_generateCollectParams(rca, agreementId, bytes32("col1"), tokens, 0)); |
| 102 | + |
| 103 | + vm.prank(rca.dataService); |
| 104 | + uint256 collected = _recurringCollector.collect(IGraphPayments.PaymentTypes.IndexingFee, data); |
| 105 | + assertEq(collected, tokens); |
| 106 | + } |
| 107 | + |
| 108 | + function test_Collect_OK_WhenEOAPayer(FuzzyTestCollect calldata fuzzy) public { |
| 109 | + // Use standard ECDSA-signed path (EOA payer) |
| 110 | + ( |
| 111 | + IRecurringCollector.RecurringCollectionAgreement memory acceptedRca, |
| 112 | + , |
| 113 | + , |
| 114 | + bytes16 agreementId |
| 115 | + ) = _sensibleAuthorizeAndAccept(fuzzy.fuzzyTestAccept); |
| 116 | + |
| 117 | + (bytes memory data, uint256 collectionSeconds, uint256 tokens) = _generateValidCollection( |
| 118 | + acceptedRca, |
| 119 | + fuzzy.collectParams, |
| 120 | + fuzzy.collectParams.tokens, |
| 121 | + fuzzy.collectParams.tokens |
| 122 | + ); |
| 123 | + |
| 124 | + skip(collectionSeconds); |
| 125 | + // EOA payer has no code — eligibility check is skipped entirely |
| 126 | + vm.prank(acceptedRca.dataService); |
| 127 | + uint256 collected = _recurringCollector.collect(_paymentType(fuzzy.unboundedPaymentType), data); |
| 128 | + assertEq(collected, tokens); |
| 129 | + } |
| 130 | + |
| 131 | + function test_Collect_OK_WhenPayerHasNoERC165() public { |
| 132 | + // BareAgreementOwner implements IAgreementOwner but NOT IERC165. |
| 133 | + // The supportsInterface call will revert, hitting the catch {} branch. |
| 134 | + BareAgreementOwner bare = new BareAgreementOwner(); |
| 135 | + |
| 136 | + IRecurringCollector.RecurringCollectionAgreement memory rca = _recurringCollectorHelper.sensibleRCA( |
| 137 | + IRecurringCollector.RecurringCollectionAgreement({ |
| 138 | + deadline: uint64(block.timestamp + 1 hours), |
| 139 | + endsAt: uint64(block.timestamp + 365 days), |
| 140 | + payer: address(bare), |
| 141 | + dataService: makeAddr("ds"), |
| 142 | + serviceProvider: makeAddr("sp"), |
| 143 | + maxInitialTokens: 100 ether, |
| 144 | + maxOngoingTokensPerSecond: 1 ether, |
| 145 | + minSecondsPerCollection: 600, |
| 146 | + maxSecondsPerCollection: 3600, |
| 147 | + nonce: 1, |
| 148 | + metadata: "" |
| 149 | + }) |
| 150 | + ); |
| 151 | + |
| 152 | + bytes32 agreementHash = _recurringCollector.hashRCA(rca); |
| 153 | + bare.authorize(agreementHash); |
| 154 | + _setupValidProvision(rca.serviceProvider, rca.dataService); |
| 155 | + |
| 156 | + vm.prank(rca.dataService); |
| 157 | + bytes16 agreementId = _recurringCollector.accept(rca, ""); |
| 158 | + |
| 159 | + skip(rca.minSecondsPerCollection); |
| 160 | + uint256 tokens = 1 ether; |
| 161 | + bytes memory data = _generateCollectData(_generateCollectParams(rca, agreementId, bytes32("col1"), tokens, 0)); |
| 162 | + |
| 163 | + // Collection succeeds — the catch {} swallows the revert from supportsInterface |
| 164 | + vm.prank(rca.dataService); |
| 165 | + uint256 collected = _recurringCollector.collect(IGraphPayments.PaymentTypes.IndexingFee, data); |
| 166 | + assertEq(collected, tokens); |
| 167 | + } |
| 168 | + |
| 169 | + function test_Collect_OK_ZeroTokensSkipsEligibilityCheck() public { |
| 170 | + MockAgreementOwner approver = _newApprover(); |
| 171 | + (IRecurringCollector.RecurringCollectionAgreement memory rca, bytes16 agreementId) = _acceptUnsignedAgreement( |
| 172 | + approver |
| 173 | + ); |
| 174 | + |
| 175 | + // Enable eligibility check, provider is NOT eligible |
| 176 | + approver.setEligibilityEnabled(true); |
| 177 | + // defaultEligible = false |
| 178 | + |
| 179 | + // Zero-token collection should NOT trigger the eligibility gate |
| 180 | + // (the guard is inside `if (0 < tokensToCollect && ...)`) |
| 181 | + skip(rca.minSecondsPerCollection); |
| 182 | + bytes memory data = _generateCollectData(_generateCollectParams(rca, agreementId, bytes32("col1"), 0, 0)); |
| 183 | + |
| 184 | + vm.prank(rca.dataService); |
| 185 | + uint256 collected = _recurringCollector.collect(IGraphPayments.PaymentTypes.IndexingFee, data); |
| 186 | + assertEq(collected, 0); |
| 187 | + } |
| 188 | + |
| 189 | + /* solhint-enable graph/func-name-mixedcase */ |
| 190 | +} |
0 commit comments