Skip to content

Commit 0b80863

Browse files
committed
staking: save gas when collect() is called with zero tokens
1 parent 1d84991 commit 0b80863

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

contracts/staking/Staking.sol

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -929,43 +929,47 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
929929
AllocationState allocState = _getAllocationState(_allocationID);
930930
require(allocState != AllocationState.Null, "!collect");
931931

932-
// Pull tokens to collect from the authorized sender
933-
IGraphToken graphToken = graphToken();
934-
require(graphToken.transferFrom(msg.sender, address(this), _tokens), "!transfer");
935-
936932
// Get allocation
937933
Allocation storage alloc = allocations[_allocationID];
938-
bytes32 subgraphDeploymentID = alloc.subgraphDeploymentID;
939934
uint256 queryFees = _tokens;
935+
uint256 curationFees = 0;
936+
bytes32 subgraphDeploymentID = alloc.subgraphDeploymentID;
940937

941-
// -- Collect protocol tax --
942-
// If the Allocation is not active or closed we are going to charge a 100% protocol tax
943-
uint256 usedProtocolPercentage = (allocState == AllocationState.Active ||
944-
allocState == AllocationState.Closed)
945-
? protocolPercentage
946-
: MAX_PPM;
947-
uint256 protocolFees = _collectTax(graphToken, queryFees, usedProtocolPercentage);
948-
queryFees = queryFees.sub(protocolFees);
949-
950-
// -- Collect curation fees --
951-
// Only if the subgraph deployment is curated
952-
uint256 curationFees = _collectCurationFees(
953-
graphToken,
954-
subgraphDeploymentID,
955-
queryFees,
956-
curationPercentage
957-
);
958-
queryFees = queryFees.sub(curationFees);
959-
960-
// Add funds to the allocation
961-
alloc.collectedFees = alloc.collectedFees.add(queryFees);
962-
963-
// When allocation is closed redirect funds to the rebate pool
964-
// This way we can keep collecting tokens even after the allocation is closed and
965-
// before it gets to the finalized state.
966-
if (allocState == AllocationState.Closed) {
967-
Rebates.Pool storage rebatePool = rebates[alloc.closedAtEpoch];
968-
rebatePool.fees = rebatePool.fees.add(queryFees);
938+
// Process query fees only if non-zero amount
939+
if (queryFees > 0) {
940+
// Pull tokens to collect from the authorized sender
941+
IGraphToken graphToken = graphToken();
942+
require(graphToken.transferFrom(msg.sender, address(this), _tokens), "!transfer");
943+
944+
// -- Collect protocol tax --
945+
// If the Allocation is not active or closed we are going to charge a 100% protocol tax
946+
uint256 usedProtocolPercentage = (allocState == AllocationState.Active ||
947+
allocState == AllocationState.Closed)
948+
? protocolPercentage
949+
: MAX_PPM;
950+
uint256 protocolFees = _collectTax(graphToken, queryFees, usedProtocolPercentage);
951+
queryFees = queryFees.sub(protocolFees);
952+
953+
// -- Collect curation fees --
954+
// Only if the subgraph deployment is curated
955+
curationFees = _collectCurationFees(
956+
graphToken,
957+
subgraphDeploymentID,
958+
queryFees,
959+
curationPercentage
960+
);
961+
queryFees = queryFees.sub(curationFees);
962+
963+
// Add funds to the allocation
964+
alloc.collectedFees = alloc.collectedFees.add(queryFees);
965+
966+
// When allocation is closed redirect funds to the rebate pool
967+
// This way we can keep collecting tokens even after the allocation is closed and
968+
// before it gets to the finalized state.
969+
if (allocState == AllocationState.Closed) {
970+
Rebates.Pool storage rebatePool = rebates[alloc.closedAtEpoch];
971+
rebatePool.fees = rebatePool.fees.add(queryFees);
972+
}
969973
}
970974

971975
emit AllocationCollected(

0 commit comments

Comments
 (0)