From cae728181db2027bb16e3a8b6fecf8b5be267d96 Mon Sep 17 00:00:00 2001 From: MoonBoi9001 Date: Mon, 16 Nov 2026 14:40:53 +0100 Subject: [PATCH 1/4] feat(subgraph): add a gateway cut on query fees, sent to a set address The Subgraph Service could only take a cut of query fees for curators, so gateways paid nothing to the protocol. The owner can now set a gateway cut and a destination, and every query fee collection sends that share there, taken alongside the curation cut after the protocol tax. --- .../subgraph-service/ISubgraphService.sol | 73 +++++++++++ .../contracts/SubgraphService.sol | 104 +++++++++++---- .../contracts/SubgraphServiceStorage.sol | 13 ++ .../subgraphService/SubgraphService.t.sol | 55 ++++++-- .../subgraphService/collect/query/query.t.sol | 77 +++++++++++ .../governance/gatewayCut.t.sol | 124 ++++++++++++++++++ 6 files changed, 414 insertions(+), 32 deletions(-) create mode 100644 packages/subgraph-service/test/unit/subgraphService/governance/gatewayCut.t.sol diff --git a/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol b/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol index 5bd235404..f9a696762 100644 --- a/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol +++ b/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol @@ -79,6 +79,33 @@ interface ISubgraphService is IDataServiceAgreements, IDataServiceFees { event IndexingFeesCutSet(uint256 indexingFeesCut); // solhint-disable-previous-line gas-indexed-events + /** + * @notice Emitted when the gateway cut is set + * @param gatewayCut The gateway cut + */ + event GatewayCutSet(uint256 gatewayCut); + // solhint-disable-previous-line gas-indexed-events + + /** + * @notice Emitted when the gateway cut destination is set + * @param gatewayCutDestination The address the gateway cut is sent to + */ + event GatewayCutDestinationSet(address indexed gatewayCutDestination); + + /** + * @notice Emitted when the gateway cut is taken from a query fee payment + * @param serviceProvider The address of the service provider + * @param payer The address paying for the query fees + * @param allocationId The id of the allocation + * @param tokens The amount of tokens sent to the gateway cut destination + */ + event GatewayCutCollected( + address indexed serviceProvider, + address indexed payer, + address indexed allocationId, + uint256 tokens + ); + /** * @notice Emitted when the block closing allocation with active agreement setting is toggled * @param enabled Whether the setting is enabled @@ -98,6 +125,18 @@ interface ISubgraphService is IDataServiceAgreements, IDataServiceFees { */ error SubgraphServiceInvalidIndexingFeesCut(uint256 indexingFeesCut); + /** + * @notice Thrown when trying to set a gateway cut that, added to the curation cut, is not a valid PPM value + * @param gatewayCut The gateway cut value + */ + error SubgraphServiceInvalidGatewayCut(uint256 gatewayCut); + + /** + * @notice Thrown when the gateway cut destination is invalid, or is missing while a gateway cut is set + * @param gatewayCutDestination The gateway cut destination + */ + error SubgraphServiceInvalidGatewayCutDestination(address gatewayCutDestination); + /** * @notice Thrown when an indexer tries to register with an empty URL */ @@ -277,10 +316,32 @@ interface ISubgraphService is IDataServiceAgreements, IDataServiceFees { /** * @notice Sets the curators payment cut for query fees * @dev Emits a {CuratorCutSet} event + * Requirements: + * - The curation cut plus the gateway cut must be a valid PPM value * @param curationCut The curation cut for the payment type */ function setCurationCut(uint256 curationCut) external; + /** + * @notice Sets the cut the protocol takes from every query fee payment, whoever the payer (gateway) is. + * The cut is sent to the gateway cut destination. + * @dev Emits a {GatewayCutSet} event + * Requirements: + * - The gateway cut plus the curation cut must be a valid PPM value + * - A gateway cut destination must be set before the gateway cut can be above zero + * @param gatewayCut The gateway cut in PPM + */ + function setGatewayCut(uint256 gatewayCut) external; + + /** + * @notice Sets the address the gateway cut is sent to + * @dev Emits a {GatewayCutDestinationSet} event + * Requirements: + * - The destination cannot be the zero address or the Subgraph Service itself + * @param gatewayCutDestination The address the gateway cut is sent to + */ + function setGatewayCutDestination(address gatewayCutDestination) external; + /** * @notice Sets the data service payment cut for indexing fees * @dev Emits a {IndexingFeesCutSet} event @@ -424,6 +485,18 @@ interface ISubgraphService is IDataServiceAgreements, IDataServiceFees { */ function curationFeesCut() external view returns (uint256); + /** + * @notice Gets the gateway cut + * @return The gateway cut in PPM + */ + function gatewayCut() external view returns (uint256); + + /** + * @notice Gets the gateway cut destination + * @return The address the gateway cut is sent to + */ + function gatewayCutDestination() external view returns (address); + /** * @notice Gets the payments destination * @param indexer The address of the indexer diff --git a/packages/subgraph-service/contracts/SubgraphService.sol b/packages/subgraph-service/contracts/SubgraphService.sol index 114af4dab..eee12c655 100644 --- a/packages/subgraph-service/contracts/SubgraphService.sol +++ b/packages/subgraph-service/contracts/SubgraphService.sol @@ -21,7 +21,7 @@ import { DataService } from "@graphprotocol/horizon/contracts/data-service/DataS import { DataServiceFees } from "@graphprotocol/horizon/contracts/data-service/extensions/DataServiceFees.sol"; import { Directory } from "./utilities/Directory.sol"; import { AllocationManager } from "./utilities/AllocationManager.sol"; -import { SubgraphServiceV2Storage } from "./SubgraphServiceStorage.sol"; +import { SubgraphServiceV3Storage } from "./SubgraphServiceStorage.sol"; import { TokenUtils } from "@graphprotocol/contracts/contracts/utils/TokenUtils.sol"; import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol"; @@ -47,7 +47,7 @@ contract SubgraphService is AllocationManager, IRewardsIssuer, ISubgraphService, - SubgraphServiceV2Storage + SubgraphServiceV3Storage { using PPMMath for uint256; using Allocation for mapping(address => IAllocation.State); @@ -359,11 +359,38 @@ contract SubgraphService is /// @inheritdoc ISubgraphService function setCurationCut(uint256 curationCut) external override onlyOwner { - require(PPMMath.isValidPPM(curationCut), SubgraphServiceInvalidCurationCut(curationCut)); + require( + PPMMath.isValidPPM(curationCut) && PPMMath.isValidPPM(curationCut + gatewayCut), + SubgraphServiceInvalidCurationCut(curationCut) + ); curationFeesCut = curationCut; emit CurationCutSet(curationCut); } + /// @inheritdoc ISubgraphService + function setGatewayCut(uint256 gatewayCut_) external override onlyOwner { + require( + PPMMath.isValidPPM(gatewayCut_) && PPMMath.isValidPPM(gatewayCut_ + curationFeesCut), + SubgraphServiceInvalidGatewayCut(gatewayCut_) + ); + require( + gatewayCut_ == 0 || gatewayCutDestination != address(0), + SubgraphServiceInvalidGatewayCutDestination(address(0)) + ); + gatewayCut = gatewayCut_; + emit GatewayCutSet(gatewayCut_); + } + + /// @inheritdoc ISubgraphService + function setGatewayCutDestination(address gatewayCutDestination_) external override onlyOwner { + require( + gatewayCutDestination_ != address(0) && gatewayCutDestination_ != address(this), + SubgraphServiceInvalidGatewayCutDestination(gatewayCutDestination_) + ); + gatewayCutDestination = gatewayCutDestination_; + emit GatewayCutDestinationSet(gatewayCutDestination_); + } + /// @inheritdoc ISubgraphService function setIndexingFeesCut(uint256 indexingFeesCut_) external override onlyOwner { require(PPMMath.isValidPPM(indexingFeesCut_), SubgraphServiceInvalidIndexingFeesCut(indexingFeesCut_)); @@ -639,6 +666,7 @@ contract SubgraphService is * Emits a {StakeClaimsReleased} event, and a {StakeClaimReleased} event for each claim released. * Emits a {StakeClaimLocked} event. * Emits a {QueryFeesCollected} event. + * Emits a {GatewayCutCollected} event if a gateway cut is taken. * * @param _indexer The address of the indexer * @param _data Encoded data: @@ -674,23 +702,12 @@ contract SubgraphService is // release expired stake claims _releaseStake(_indexer, 0); - // Collect from GraphPayments - only curators cut is sent back to the subgraph service - uint256 tokensCollected; - uint256 tokensCurators; - { - uint256 balanceBefore = _graphToken().balanceOf(address(this)); - - tokensCollected = _graphTallyCollector().collect( - IGraphPayments.PaymentTypes.QueryFee, - _encodeGraphTallyData(signedRav, _curation().isCurated(subgraphDeploymentId) ? curationFeesCut : 0), - tokensToCollect - ); - - uint256 balanceAfter = _graphToken().balanceOf(address(this)); - // solhint-disable-next-line gas-strict-inequalities - require(balanceAfter >= balanceBefore, SubgraphServiceInconsistentCollection(balanceBefore, balanceAfter)); - tokensCurators = balanceAfter - balanceBefore; - } + // Collect from GraphPayments - the curation and gateway cuts are sent back to the subgraph service + (uint256 tokensCollected, uint256 tokensCurators, uint256 tokensGateway) = _collectQueryFeePayment( + signedRav, + subgraphDeploymentId, + tokensToCollect + ); if (tokensCollected > 0) { // lock stake as economic security for fees @@ -708,6 +725,11 @@ contract SubgraphService is _graphToken().pushTokens(address(_curation()), tokensCurators); _curation().collect(subgraphDeploymentId, tokensCurators); } + + if (tokensGateway > 0) { + _graphToken().pushTokens(gatewayCutDestination, tokensGateway); + emit GatewayCutCollected(_indexer, signedRav.rav.payer, allocationId, tokensGateway); + } } emit QueryFeesCollected( @@ -721,6 +743,42 @@ contract SubgraphService is return tokensCollected; } + /** + * @notice Collects a query fee payment through the GraphTallyCollector and splits the tokens sent back to + * this contract between curators and the gateway cut destination. + * @dev The curation cut and the gateway cut are passed to GraphPayments as a single data service cut, so the + * tokens received are split in proportion to the 2 cuts. Curators receive any rounding remainder. + * @param _signedRav The signed RAV + * @param _subgraphDeploymentId The id of the subgraph deployment the RAV pays for + * @param _tokensToCollect The amount of tokens to collect, 0 to collect the entire RAV + * @return tokensCollected The amount of tokens collected + * @return tokensCurators The amount of tokens curators receive + * @return tokensGateway The amount of tokens the gateway cut destination receives + */ + function _collectQueryFeePayment( + IGraphTallyCollector.SignedRAV memory _signedRav, + bytes32 _subgraphDeploymentId, + uint256 _tokensToCollect + ) private returns (uint256 tokensCollected, uint256 tokensCurators, uint256 tokensGateway) { + uint256 curationCut = _curation().isCurated(_subgraphDeploymentId) ? curationFeesCut : 0; + uint256 gatewayCut_ = gatewayCut; + uint256 balanceBefore = _graphToken().balanceOf(address(this)); + + tokensCollected = _graphTallyCollector().collect( + IGraphPayments.PaymentTypes.QueryFee, + _encodeGraphTallyData(_signedRav, curationCut + gatewayCut_), + _tokensToCollect + ); + + uint256 balanceAfter = _graphToken().balanceOf(address(this)); + // solhint-disable-next-line gas-strict-inequalities + require(balanceAfter >= balanceBefore, SubgraphServiceInconsistentCollection(balanceBefore, balanceAfter)); + uint256 tokensDataService = balanceAfter - balanceBefore; + + if (gatewayCut_ > 0) tokensGateway = (tokensDataService * gatewayCut_) / (curationCut + gatewayCut_); + tokensCurators = tokensDataService - tokensGateway; + } + /** * @notice Collect indexing rewards * @param _indexer The address of the indexer @@ -816,13 +874,13 @@ contract SubgraphService is * @notice Encodes the data for the GraphTallyCollector * @dev The purpose of this function is just to avoid stack too deep errors * @param _signedRav The signed RAV - * @param _curationCut The curation cut + * @param _dataServiceCut The data service cut, the curation cut plus the gateway cut * @return The encoded data */ function _encodeGraphTallyData( IGraphTallyCollector.SignedRAV memory _signedRav, - uint256 _curationCut + uint256 _dataServiceCut ) private view returns (bytes memory) { - return abi.encode(_signedRav, _curationCut, paymentsDestination[_signedRav.rav.serviceProvider]); + return abi.encode(_signedRav, _dataServiceCut, paymentsDestination[_signedRav.rav.serviceProvider]); } } diff --git a/packages/subgraph-service/contracts/SubgraphServiceStorage.sol b/packages/subgraph-service/contracts/SubgraphServiceStorage.sol index 1296bd9ed..f73f63309 100644 --- a/packages/subgraph-service/contracts/SubgraphServiceStorage.sol +++ b/packages/subgraph-service/contracts/SubgraphServiceStorage.sol @@ -39,3 +39,16 @@ abstract contract SubgraphServiceV2Storage is SubgraphServiceV1Storage { /// @notice When true, closing an allocation that has an active indexing agreement will revert. bool internal blockClosingAllocationWithActiveAgreement; } + +/** + * @title SubgraphServiceV3Storage + * @author Edge & Node + * @notice Adds the gateway cut taken from query fee payments. + */ +abstract contract SubgraphServiceV3Storage is SubgraphServiceV2Storage { + /// @notice The cut the protocol takes from every query fee payment, whoever the payer is. In PPM. + uint256 public override gatewayCut; + + /// @notice The address the gateway cut is sent to + address public override gatewayCutDestination; +} diff --git a/packages/subgraph-service/test/unit/subgraphService/SubgraphService.t.sol b/packages/subgraph-service/test/unit/subgraphService/SubgraphService.t.sol index 02c5f3fc0..8fdbf1371 100644 --- a/packages/subgraph-service/test/unit/subgraphService/SubgraphService.t.sol +++ b/packages/subgraph-service/test/unit/subgraphService/SubgraphService.t.sol @@ -186,15 +186,24 @@ contract SubgraphServiceTest is SubgraphServiceSharedTest { struct QueryFeeData { uint256 curationCut; + uint256 gatewayCut; uint256 protocolPaymentCut; } + struct QueryFeeSplit { + uint256 tokensProtocol; + uint256 tokensCurators; + uint256 tokensGateway; + uint256 tokensIndexer; + } + struct CollectPaymentData { uint256 rewardsDestinationBalance; uint256 indexerProvisionBalance; uint256 delegationPoolBalance; uint256 indexerBalance; uint256 curationBalance; + uint256 gatewayCutDestinationBalance; uint256 lockedTokens; uint256 indexerStake; } @@ -254,6 +263,10 @@ contract SubgraphServiceTest is SubgraphServiceSharedTest { ); collectPaymentData.indexerBalance = token.balanceOf(_indexer); collectPaymentData.curationBalance = token.balanceOf(address(curation)); + address gatewayCutDestination = subgraphService.gatewayCutDestination(); + collectPaymentData.gatewayCutDestinationBalance = gatewayCutDestination == address(0) + ? 0 + : token.balanceOf(gatewayCutDestination); collectPaymentData.lockedTokens = subgraphService.feesProvisionTracker(_indexer); collectPaymentData.indexerStake = staking.getStake(_indexer); return collectPaymentData; @@ -283,9 +296,12 @@ contract SubgraphServiceTest is SubgraphServiceSharedTest { ); paymentCollected = tokensToCollect == 0 ? signedRav.rav.valueAggregate - tokensCollected : tokensToCollect; - QueryFeeData memory queryFeeData = _queryFeeData(allocation.subgraphDeploymentId); - uint256 tokensProtocol = paymentCollected.mulPPMRoundUp(queryFeeData.protocolPaymentCut); - uint256 tokensCurators = (paymentCollected - tokensProtocol).mulPPMRoundUp(queryFeeData.curationCut); + QueryFeeSplit memory split = _queryFeeSplit(paymentCollected, _queryFeeData(subgraphDeploymentId)); + + if (split.tokensGateway > 0) { + vm.expectEmit(address(subgraphService)); + emit ISubgraphService.GatewayCutCollected(_indexer, payer, allocationId, split.tokensGateway); + } vm.expectEmit(address(subgraphService)); emit ISubgraphService.QueryFeesCollected( @@ -294,7 +310,7 @@ contract SubgraphServiceTest is SubgraphServiceSharedTest { allocationId, subgraphDeploymentId, paymentCollected, - tokensCurators + split.tokensCurators ); return paymentCollected; @@ -305,9 +321,25 @@ contract SubgraphServiceTest is SubgraphServiceSharedTest { queryFeeData.protocolPaymentCut = graphPayments.PROTOCOL_PAYMENT_CUT(); uint256 curationFeesCut = subgraphService.curationFeesCut(); queryFeeData.curationCut = curation.isCurated(_subgraphDeploymentId) ? curationFeesCut : 0; + queryFeeData.gatewayCut = subgraphService.gatewayCut(); return queryFeeData; } + /// @dev Mirrors how GraphPayments and the Subgraph Service split a query fee payment + function _queryFeeSplit( + uint256 _paymentCollected, + QueryFeeData memory _queryFeeData + ) private pure returns (QueryFeeSplit memory split) { + split.tokensProtocol = _paymentCollected.mulPPMRoundUp(_queryFeeData.protocolPaymentCut); + uint256 dataServiceCut = _queryFeeData.curationCut + _queryFeeData.gatewayCut; + uint256 tokensDataService = (_paymentCollected - split.tokensProtocol).mulPPMRoundUp(dataServiceCut); + split.tokensGateway = _queryFeeData.gatewayCut == 0 + ? 0 + : (tokensDataService * _queryFeeData.gatewayCut) / dataServiceCut; + split.tokensCurators = tokensDataService - split.tokensGateway; + split.tokensIndexer = _paymentCollected - split.tokensProtocol - tokensDataService; + } + function _handleIndexingRewardsCollection( bytes memory _data ) private returns (uint256 paymentCollected, address allocationId, IndexingRewardsData memory indexingRewardsData) { @@ -373,10 +405,8 @@ contract SubgraphServiceTest is SubgraphServiceSharedTest { IAllocation.State memory allocation = subgraphService.getAllocation( address(uint160(uint256(signedRav.rav.collectionId))) ); - QueryFeeData memory queryFeeData = _queryFeeData(allocation.subgraphDeploymentId); - uint256 tokensProtocol = _paymentCollected.mulPPMRoundUp(queryFeeData.protocolPaymentCut); - uint256 curationTokens = (_paymentCollected - tokensProtocol).mulPPMRoundUp(queryFeeData.curationCut); - uint256 expectedIndexerTokensPayment = _paymentCollected - tokensProtocol - curationTokens; + QueryFeeSplit memory split = _queryFeeSplit(_paymentCollected, _queryFeeData(allocation.subgraphDeploymentId)); + uint256 expectedIndexerTokensPayment = split.tokensIndexer; assertEq( collectPaymentDataAfter.indexerBalance - collectPaymentDataBefore.indexerBalance, @@ -398,7 +428,14 @@ contract SubgraphServiceTest is SubgraphServiceSharedTest { ? expectedIndexerTokensPayment : 0 ); - assertEq(collectPaymentDataAfter.curationBalance, collectPaymentDataBefore.curationBalance + curationTokens); + assertEq( + collectPaymentDataAfter.curationBalance, + collectPaymentDataBefore.curationBalance + split.tokensCurators + ); + assertEq( + collectPaymentDataAfter.gatewayCutDestinationBalance, + collectPaymentDataBefore.gatewayCutDestinationBalance + split.tokensGateway + ); // Check locked tokens uint256 tokensToLock = _paymentCollected * subgraphService.stakeToFeesRatio(); diff --git a/packages/subgraph-service/test/unit/subgraphService/collect/query/query.t.sol b/packages/subgraph-service/test/unit/subgraphService/collect/query/query.t.sol index 4915ac17f..21a3f862d 100644 --- a/packages/subgraph-service/test/unit/subgraphService/collect/query/query.t.sol +++ b/packages/subgraph-service/test/unit/subgraphService/collect/query/query.t.sol @@ -76,6 +76,14 @@ contract SubgraphServiceRegisterTest is SubgraphServiceTest { escrow.deposit(address(graphTallyCollector), users.indexer, tokens); } + function _setGatewayCut(uint256 gatewayCut) private { + (, address msgSender, ) = vm.readCallers(); + resetPrank(users.governor); + subgraphService.setGatewayCutDestination(makeAddr("gatewayCutDestination")); + subgraphService.setGatewayCut(gatewayCut); + resetPrank(msgSender); + } + function _authorizeSigner() private { uint256 proofDeadline = block.timestamp + 1; bytes memory proof = _getSignerProof(proofDeadline, signerPrivateKey); @@ -137,6 +145,75 @@ contract SubgraphServiceRegisterTest is SubgraphServiceTest { _collect(users.indexer, IGraphPayments.PaymentTypes.QueryFee, data); } + function testCollect_QueryFees_WithGatewayCut( + uint256 tokensAllocated, + uint256 tokensPayment, + uint256 gatewayCut + ) public useIndexer useAllocation(tokensAllocated) { + vm.assume(tokensAllocated > MINIMUM_PROVISION_TOKENS * STAKE_TO_FEES_RATIO); + uint256 maxTokensPayment = tokensAllocated / STAKE_TO_FEES_RATIO > type(uint128).max + ? type(uint128).max + : tokensAllocated / STAKE_TO_FEES_RATIO; + tokensPayment = bound(tokensPayment, MINIMUM_PROVISION_TOKENS, maxTokensPayment); + gatewayCut = bound(gatewayCut, 1, MAX_PPM - subgraphService.curationFeesCut()); + _setGatewayCut(gatewayCut); + + resetPrank(users.gateway); + _deposit(tokensPayment); + _authorizeSigner(); + + resetPrank(users.indexer); + // forge-lint: disable-next-line(unsafe-typecast) + bytes memory data = _getQueryFeeEncodedData(users.indexer, uint128(tokensPayment), 0); + _collect(users.indexer, IGraphPayments.PaymentTypes.QueryFee, data); + } + + function testCollect_QueryFees_WithGatewayCut_ExactSplit() public useIndexer useAllocation(10_000 ether) { + // 1% protocol cut, then 1% curation cut and 5% gateway cut taken together from the remaining 990 GRT + assertEq(PROTOCOL_PAYMENT_CUT, 10_000); + assertEq(subgraphService.curationFeesCut(), 10_000); + _setGatewayCut(50_000); + address gatewayCutDestination = subgraphService.gatewayCutDestination(); + uint256 curationBalanceBefore = token.balanceOf(address(curation)); + + resetPrank(users.gateway); + _deposit(1_000 ether); + _authorizeSigner(); + + resetPrank(users.indexer); + bytes memory data = _getQueryFeeEncodedData(users.indexer, 1_000 ether, 0); + _collect(users.indexer, IGraphPayments.PaymentTypes.QueryFee, data); + + // 990 GRT * 6% = 59.4 GRT for the data service, split 5:1 between the gateway cut and curators + assertEq(token.balanceOf(gatewayCutDestination), 49.5 ether); + assertEq(token.balanceOf(address(curation)) - curationBalanceBefore, 9.9 ether); + } + + function testCollect_QueryFees_WithGatewayCut_NotCurated( + uint256 tokensAllocated, + uint256 tokensPayment, + uint256 gatewayCut + ) public useIndexer useAllocation(tokensAllocated) { + vm.assume(tokensAllocated > MINIMUM_PROVISION_TOKENS * STAKE_TO_FEES_RATIO); + uint256 maxTokensPayment = tokensAllocated / STAKE_TO_FEES_RATIO > type(uint128).max + ? type(uint128).max + : tokensAllocated / STAKE_TO_FEES_RATIO; + tokensPayment = bound(tokensPayment, MINIMUM_PROVISION_TOKENS, maxTokensPayment); + gatewayCut = bound(gatewayCut, 1, MAX_PPM - subgraphService.curationFeesCut()); + _setGatewayCut(gatewayCut); + // Without curation signal, the whole data service cut goes to the gateway cut destination + vm.mockCall(address(curation), abi.encodeWithSelector(curation.isCurated.selector), abi.encode(false)); + + resetPrank(users.gateway); + _deposit(tokensPayment); + _authorizeSigner(); + + resetPrank(users.indexer); + // forge-lint: disable-next-line(unsafe-typecast) + bytes memory data = _getQueryFeeEncodedData(users.indexer, uint128(tokensPayment), 0); + _collect(users.indexer, IGraphPayments.PaymentTypes.QueryFee, data); + } + function testCollect_MultipleQueryFees( uint256 tokensAllocated, uint8 numPayments diff --git a/packages/subgraph-service/test/unit/subgraphService/governance/gatewayCut.t.sol b/packages/subgraph-service/test/unit/subgraphService/governance/gatewayCut.t.sol new file mode 100644 index 000000000..84867f18d --- /dev/null +++ b/packages/subgraph-service/test/unit/subgraphService/governance/gatewayCut.t.sol @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.27; + +import { ISubgraphService } from "@graphprotocol/interfaces/contracts/subgraph-service/ISubgraphService.sol"; +import { SubgraphServiceTest } from "../SubgraphService.t.sol"; +import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; + +contract SubgraphServiceGovernanceGatewayCutTest is SubgraphServiceTest { + address internal gatewayCutDestination = makeAddr("gatewayCutDestination"); + + /* + * TESTS: setGatewayCutDestination + */ + + function test_Governance_SetGatewayCutDestination(address destination) public useGovernor { + vm.assume(destination != address(0) && destination != address(subgraphService)); + + vm.expectEmit(address(subgraphService)); + emit ISubgraphService.GatewayCutDestinationSet(destination); + subgraphService.setGatewayCutDestination(destination); + + assertEq(subgraphService.gatewayCutDestination(), destination); + } + + function test_Governance_SetGatewayCutDestination_RevertWhen_ZeroAddress() public useGovernor { + vm.expectRevert( + abi.encodeWithSelector(ISubgraphService.SubgraphServiceInvalidGatewayCutDestination.selector, address(0)) + ); + subgraphService.setGatewayCutDestination(address(0)); + } + + function test_Governance_SetGatewayCutDestination_RevertWhen_SubgraphService() public useGovernor { + vm.expectRevert( + abi.encodeWithSelector( + ISubgraphService.SubgraphServiceInvalidGatewayCutDestination.selector, + address(subgraphService) + ) + ); + subgraphService.setGatewayCutDestination(address(subgraphService)); + } + + function test_Governance_SetGatewayCutDestination_RevertWhen_NotGovernor() public useIndexer { + vm.expectRevert(abi.encodeWithSelector(OwnableUpgradeable.OwnableUnauthorizedAccount.selector, users.indexer)); + subgraphService.setGatewayCutDestination(gatewayCutDestination); + } + + /* + * TESTS: setGatewayCut + */ + + function test_Governance_SetGatewayCut(uint256 gatewayCut) public useGovernor { + gatewayCut = bound(gatewayCut, 0, MAX_PPM - subgraphService.curationFeesCut()); + subgraphService.setGatewayCutDestination(gatewayCutDestination); + + vm.expectEmit(address(subgraphService)); + emit ISubgraphService.GatewayCutSet(gatewayCut); + subgraphService.setGatewayCut(gatewayCut); + + assertEq(subgraphService.gatewayCut(), gatewayCut); + } + + function test_Governance_SetGatewayCut_ToZeroWithoutDestination() public useGovernor { + vm.expectEmit(address(subgraphService)); + emit ISubgraphService.GatewayCutSet(0); + subgraphService.setGatewayCut(0); + + assertEq(subgraphService.gatewayCut(), 0); + } + + function test_Governance_SetGatewayCut_RevertWhen_AboveCurationCutHeadroom(uint256 gatewayCut) public useGovernor { + vm.assume(gatewayCut > MAX_PPM - subgraphService.curationFeesCut()); + subgraphService.setGatewayCutDestination(gatewayCutDestination); + + vm.expectRevert(abi.encodeWithSelector(ISubgraphService.SubgraphServiceInvalidGatewayCut.selector, gatewayCut)); + subgraphService.setGatewayCut(gatewayCut); + } + + function test_Governance_SetGatewayCut_RevertWhen_NoDestination(uint256 gatewayCut) public useGovernor { + gatewayCut = bound(gatewayCut, 1, MAX_PPM - subgraphService.curationFeesCut()); + + vm.expectRevert( + abi.encodeWithSelector(ISubgraphService.SubgraphServiceInvalidGatewayCutDestination.selector, address(0)) + ); + subgraphService.setGatewayCut(gatewayCut); + } + + function test_Governance_SetGatewayCut_RevertWhen_NotGovernor() public useIndexer { + vm.expectRevert(abi.encodeWithSelector(OwnableUpgradeable.OwnableUnauthorizedAccount.selector, users.indexer)); + subgraphService.setGatewayCut(100_000); + } + + /* + * TESTS: setCurationCut with a gateway cut set + */ + + function test_Governance_SetCurationCut_RevertWhen_AboveGatewayCutHeadroom( + uint256 gatewayCut, + uint256 curationCut + ) public useGovernor { + gatewayCut = bound(gatewayCut, 1, MAX_PPM - subgraphService.curationFeesCut()); + subgraphService.setGatewayCutDestination(gatewayCutDestination); + subgraphService.setGatewayCut(gatewayCut); + curationCut = bound(curationCut, MAX_PPM - gatewayCut + 1, MAX_PPM); + + vm.expectRevert( + abi.encodeWithSelector(ISubgraphService.SubgraphServiceInvalidCurationCut.selector, curationCut) + ); + subgraphService.setCurationCut(curationCut); + } + + function test_Governance_SetCurationCut_UpToGatewayCutHeadroom( + uint256 gatewayCut, + uint256 curationCut + ) public useGovernor { + gatewayCut = bound(gatewayCut, 1, MAX_PPM - subgraphService.curationFeesCut()); + subgraphService.setGatewayCutDestination(gatewayCutDestination); + subgraphService.setGatewayCut(gatewayCut); + curationCut = bound(curationCut, 0, MAX_PPM - gatewayCut); + + subgraphService.setCurationCut(curationCut); + + assertEq(subgraphService.curationFeesCut(), curationCut); + } +} From 5228874ff84788102f5dbbbb79ad2428ae072f67 Mon Sep 17 00:00:00 2001 From: MoonBoi9001 Date: Mon, 16 Nov 2026 14:45:05 +0100 Subject: [PATCH 2/4] perf(subgraph): read the gateway cut settings with 1 storage read Every query fee collection reads the gateway cut and its destination. Storing the cut as a 32-bit number puts both in 1 storage slot, saving a slot read (about 2,100 gas) per collection. --- .../contracts/subgraph-service/ISubgraphService.sol | 2 +- packages/subgraph-service/contracts/SubgraphService.sol | 3 ++- .../subgraph-service/contracts/SubgraphServiceStorage.sol | 7 +++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol b/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol index f9a696762..3de8411ec 100644 --- a/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol +++ b/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol @@ -489,7 +489,7 @@ interface ISubgraphService is IDataServiceAgreements, IDataServiceFees { * @notice Gets the gateway cut * @return The gateway cut in PPM */ - function gatewayCut() external view returns (uint256); + function gatewayCut() external view returns (uint32); /** * @notice Gets the gateway cut destination diff --git a/packages/subgraph-service/contracts/SubgraphService.sol b/packages/subgraph-service/contracts/SubgraphService.sol index eee12c655..76861e292 100644 --- a/packages/subgraph-service/contracts/SubgraphService.sol +++ b/packages/subgraph-service/contracts/SubgraphService.sol @@ -377,7 +377,8 @@ contract SubgraphService is gatewayCut_ == 0 || gatewayCutDestination != address(0), SubgraphServiceInvalidGatewayCutDestination(address(0)) ); - gatewayCut = gatewayCut_; + // forge-lint: disable-next-line(unsafe-typecast) + gatewayCut = uint32(gatewayCut_); // safe: checked above to be at most MAX_PPM emit GatewayCutSet(gatewayCut_); } diff --git a/packages/subgraph-service/contracts/SubgraphServiceStorage.sol b/packages/subgraph-service/contracts/SubgraphServiceStorage.sol index f73f63309..1db79b44f 100644 --- a/packages/subgraph-service/contracts/SubgraphServiceStorage.sol +++ b/packages/subgraph-service/contracts/SubgraphServiceStorage.sol @@ -46,9 +46,12 @@ abstract contract SubgraphServiceV2Storage is SubgraphServiceV1Storage { * @notice Adds the gateway cut taken from query fee payments. */ abstract contract SubgraphServiceV3Storage is SubgraphServiceV2Storage { - /// @notice The cut the protocol takes from every query fee payment, whoever the payer is. In PPM. - uint256 public override gatewayCut; + // Both variables share a storage slot with `blockClosingAllocationWithActiveAgreement`, + // so collecting query fees reads them with a single storage read. /// @notice The address the gateway cut is sent to address public override gatewayCutDestination; + + /// @notice The cut the protocol takes from every query fee payment, whoever the payer is. In PPM. + uint32 public override gatewayCut; } From 58ead41971fa3e616764c43aa28e0d0daa9f4df5 Mon Sep 17 00:00:00 2001 From: MoonBoi9001 Date: Mon, 16 Nov 2026 14:45:09 +0100 Subject: [PATCH 3/4] test(subgraph): rename a helper parameter that shadowed a function The query fee split helper named its parameter after an existing function, which made the compiler warn on every build. --- .../test/unit/subgraphService/SubgraphService.t.sol | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/subgraph-service/test/unit/subgraphService/SubgraphService.t.sol b/packages/subgraph-service/test/unit/subgraphService/SubgraphService.t.sol index 8fdbf1371..730963ad8 100644 --- a/packages/subgraph-service/test/unit/subgraphService/SubgraphService.t.sol +++ b/packages/subgraph-service/test/unit/subgraphService/SubgraphService.t.sol @@ -328,14 +328,12 @@ contract SubgraphServiceTest is SubgraphServiceSharedTest { /// @dev Mirrors how GraphPayments and the Subgraph Service split a query fee payment function _queryFeeSplit( uint256 _paymentCollected, - QueryFeeData memory _queryFeeData + QueryFeeData memory _fees ) private pure returns (QueryFeeSplit memory split) { - split.tokensProtocol = _paymentCollected.mulPPMRoundUp(_queryFeeData.protocolPaymentCut); - uint256 dataServiceCut = _queryFeeData.curationCut + _queryFeeData.gatewayCut; + split.tokensProtocol = _paymentCollected.mulPPMRoundUp(_fees.protocolPaymentCut); + uint256 dataServiceCut = _fees.curationCut + _fees.gatewayCut; uint256 tokensDataService = (_paymentCollected - split.tokensProtocol).mulPPMRoundUp(dataServiceCut); - split.tokensGateway = _queryFeeData.gatewayCut == 0 - ? 0 - : (tokensDataService * _queryFeeData.gatewayCut) / dataServiceCut; + split.tokensGateway = _fees.gatewayCut == 0 ? 0 : (tokensDataService * _fees.gatewayCut) / dataServiceCut; split.tokensCurators = tokensDataService - split.tokensGateway; split.tokensIndexer = _paymentCollected - split.tokensProtocol - tokensDataService; } From fd8782c93f52f17b5b0805cccc062854ec144263 Mon Sep 17 00:00:00 2001 From: MoonBoi9001 Date: Mon, 16 Nov 2026 14:45:37 +0100 Subject: [PATCH 4/4] docs(subgraph): say the gateway cut comes out of what indexers receive The payer of a query fee pays the same amount whatever the gateway cut is, so the docs now say indexers and delegators bear it, and that the curation cut setter can reject a cut over 100%. --- .../contracts/subgraph-service/ISubgraphService.sol | 8 +++++--- .../subgraph-service/contracts/SubgraphServiceStorage.sol | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol b/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol index 3de8411ec..d1465e625 100644 --- a/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol +++ b/packages/interfaces/contracts/subgraph-service/ISubgraphService.sol @@ -114,7 +114,8 @@ interface ISubgraphService is IDataServiceAgreements, IDataServiceFees { // solhint-disable-previous-line gas-indexed-events /** - * @notice Thrown when trying to set a curation cut that is not a valid PPM value + * @notice Thrown when trying to set a curation cut that is not a valid PPM value, or that adds up to more + * than 100% with the gateway cut * @param curationCut The curation cut value */ error SubgraphServiceInvalidCurationCut(uint256 curationCut); @@ -324,11 +325,12 @@ interface ISubgraphService is IDataServiceAgreements, IDataServiceFees { /** * @notice Sets the cut the protocol takes from every query fee payment, whoever the payer (gateway) is. - * The cut is sent to the gateway cut destination. + * The cut is sent to the gateway cut destination. The payer pays the same amount either way: the cut + * comes out of what the indexer and its delegators receive. * @dev Emits a {GatewayCutSet} event * Requirements: * - The gateway cut plus the curation cut must be a valid PPM value - * - A gateway cut destination must be set before the gateway cut can be above zero + * - A gateway cut destination must be set before the gateway cut can be above 0 * @param gatewayCut The gateway cut in PPM */ function setGatewayCut(uint256 gatewayCut) external; diff --git a/packages/subgraph-service/contracts/SubgraphServiceStorage.sol b/packages/subgraph-service/contracts/SubgraphServiceStorage.sol index 1db79b44f..128cf0a87 100644 --- a/packages/subgraph-service/contracts/SubgraphServiceStorage.sol +++ b/packages/subgraph-service/contracts/SubgraphServiceStorage.sol @@ -52,6 +52,7 @@ abstract contract SubgraphServiceV3Storage is SubgraphServiceV2Storage { /// @notice The address the gateway cut is sent to address public override gatewayCutDestination; - /// @notice The cut the protocol takes from every query fee payment, whoever the payer is. In PPM. + /// @notice The cut the protocol takes from every query fee payment, whoever the payer is, out of what the + /// indexer and its delegators receive. In PPM. uint32 public override gatewayCut; }