Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -87,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);
Expand All @@ -98,6 +126,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
*/
Expand Down Expand Up @@ -277,10 +317,33 @@ 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. 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 0
* @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
Expand Down Expand Up @@ -424,6 +487,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 (uint32);

/**
* @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
Expand Down
105 changes: 82 additions & 23 deletions packages/subgraph-service/contracts/SubgraphService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -47,7 +47,7 @@ contract SubgraphService is
AllocationManager,
IRewardsIssuer,
ISubgraphService,
SubgraphServiceV2Storage
SubgraphServiceV3Storage
{
using PPMMath for uint256;
using Allocation for mapping(address => IAllocation.State);
Expand Down Expand Up @@ -359,11 +359,39 @@ 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))
);
// forge-lint: disable-next-line(unsafe-typecast)
gatewayCut = uint32(gatewayCut_); // safe: checked above to be at most MAX_PPM
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_));
Expand Down Expand Up @@ -639,6 +667,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:
Expand Down Expand Up @@ -674,23 +703,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
Expand All @@ -708,6 +726,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(
Expand All @@ -721,6 +744,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
Expand Down Expand Up @@ -816,13 +875,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]);
}
}
17 changes: 17 additions & 0 deletions packages/subgraph-service/contracts/SubgraphServiceStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,20 @@ 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 {
// 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, out of what the
/// indexer and its delegators receive. In PPM.
uint32 public override gatewayCut;
}
Loading
Loading