Skip to content

Commit 03cab69

Browse files
committed
staking,curation: gas optimizations
Staking.collect(): - Merge _collect() and collect() to avoid multiple STATICCALL/SLOAD graphToken() - Pass graphToken in burnTokens function to avoid multiple STATICCALL/SLOAD - Change Curation.collect() to work as a "transferAndCall" instead of pulling tokens - Avoid reading STATICCALL/SLOAD staking() multiple times in takeRewards() - Remove unused onlyCuration() modifier from Managed - Remove onlyStaking() modifier and use within the respective function to avoid reading staking() multiple times Reduce the gas cost of calling Staking.collect() about 20% _getAllocationState(): - Use storage instead of memory to avoid SLOAD all the attributes that could not be used Claim(): - Remove setting Allocation struct variables to zero as as the struct is not completely cleaned the refund is not happening. By removing the SSTORE we avoid spending about 20000 gas.
1 parent d3b8cc2 commit 03cab69

File tree

9 files changed

+148
-182
lines changed

9 files changed

+148
-182
lines changed

contracts/curation/Curation.sol

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,16 @@ contract Curation is CurationV1Storage, GraphUpgradeable, ICuration {
160160

161161
/**
162162
* @dev Assign Graph Tokens collected as curation fees to the curation pool reserve.
163+
* This function can only be called by the Staking contract and will do the bookeeping of
164+
* transferred tokens into this contract.
163165
* @param _subgraphDeploymentID SubgraphDeployment where funds should be allocated as reserves
164166
* @param _tokens Amount of Graph Tokens to add to reserves
165167
*/
166-
function collect(bytes32 _subgraphDeploymentID, uint256 _tokens) external override onlyStaking {
167-
// Transfer tokens collected from the staking contract to this contract
168-
require(
169-
graphToken().transferFrom(address(staking()), address(this), _tokens),
170-
"Cannot transfer tokens to collect"
171-
);
168+
function collect(bytes32 _subgraphDeploymentID, uint256 _tokens) external override {
169+
// Only Staking contract is authorized as caller
170+
require(msg.sender == address(staking()), "Caller must be the staking contract");
172171

173-
// Collect tokens and assign them to the reserves
172+
// Must be curated to accept tokens
174173
require(
175174
isCurated(_subgraphDeploymentID),
176175
"Subgraph deployment must be curated to collect fees"

contracts/discovery/GNS.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ contract GNS is GNSV1Storage, GraphUpgradeable, IGNS {
163163
/**
164164
* @dev Approve curation contract to pull funds.
165165
*/
166-
function approveAll() external override onlyGovernor {
166+
function approveAll() external override {
167167
graphToken().approve(address(curation()), MAX_UINT256);
168168
}
169169

contracts/governance/Managed.sol

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ contract Managed {
5757
_;
5858
}
5959

60-
modifier onlyStaking() {
61-
require(msg.sender == address(staking()), "Caller must be the staking contract");
62-
_;
63-
}
64-
65-
modifier onlyCuration() {
66-
require(msg.sender == address(curation()), "Caller must be the curation contract");
67-
_;
68-
}
69-
7060
/**
7161
* @dev Initialize the controller
7262
*/

contracts/rewards/RewardsManager.sol

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,17 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
346346

347347
/**
348348
* @dev Pull rewards from the contract for a particular allocation.
349-
* This function will mint the necessary tokens to reward based on the inflation calculation
349+
* This function can only be called by the Staking contract.
350+
* This function will mint the necessary tokens to reward based on the inflation calculation.
350351
* @param _allocationID Allocation
351352
* @return Assigned rewards amount
352353
*/
353-
function takeRewards(address _allocationID) external override onlyStaking returns (uint256) {
354-
IGraphToken graphToken = graphToken();
354+
function takeRewards(address _allocationID) external override returns (uint256) {
355+
// Only Staking contract is authorized as caller
355356
IStaking staking = staking();
357+
require(msg.sender == address(staking), "Caller must be the staking contract");
358+
359+
IGraphToken graphToken = graphToken();
356360
IStaking.Allocation memory alloc = staking.getAllocation(_allocationID);
357361

358362
uint256 accRewardsPerAllocatedToken = onSubgraphAllocationUpdate(

0 commit comments

Comments
 (0)