Skip to content

Commit 6ae3b28

Browse files
committed
staking: use multicall on the Staking contract
1 parent 45900af commit 6ae3b28

File tree

3 files changed

+37
-100
lines changed

3 files changed

+37
-100
lines changed

contracts/staking/IStaking.sol

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,10 @@ interface IStaking is IStakingData {
113113

114114
function closeAllocation(address _allocationID, bytes32 _poi) external;
115115

116-
function closeAllocationMany(CloseAllocationRequest[] calldata _requests) external;
117-
118-
function closeAndAllocate(
119-
address _oldAllocationID,
120-
bytes32 _poi,
121-
address _indexer,
122-
bytes32 _subgraphDeploymentID,
123-
uint256 _tokens,
124-
address _allocationID,
125-
bytes32 _metadata,
126-
bytes calldata _proof
127-
) external;
128-
129116
function collect(uint256 _tokens, address _allocationID) external;
130117

131118
function claim(address _allocationID, bool _restake) external;
132119

133-
function claimMany(address[] calldata _allocationID, bool _restake) external;
134-
135120
// -- Getters and calculations --
136121

137122
function hasStake(address _indexer) external view returns (bool);

contracts/staking/Staking.sol

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pragma experimental ABIEncoderV2;
55

66
import "@openzeppelin/contracts/cryptography/ECDSA.sol";
77

8+
import "../base/Multicall.sol";
89
import "../upgrades/GraphUpgradeable.sol";
910
import "../utils/TokenUtils.sol";
1011

@@ -20,7 +21,7 @@ import "./libs/Stakes.sol";
2021
* Allocations on a Subgraph. It also allows Delegators to Delegate towards an Indexer. The
2122
* contract also has the slashing functionality.
2223
*/
23-
contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
24+
contract Staking is StakingV2Storage, GraphUpgradeable, IStaking, Multicall {
2425
using SafeMath for uint256;
2526
using Stakes for Stakes.Indexer;
2627
using Rebates for Rebates.Pool;
@@ -908,49 +909,6 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
908909
_closeAllocation(_allocationID, _poi);
909910
}
910911

911-
/**
912-
* @dev Close multiple allocations and free the staked tokens.
913-
* To be eligible for rewards a proof of indexing must be presented.
914-
* Presenting a bad proof is subject to slashable condition.
915-
* To opt out for rewards set _poi to 0x0
916-
* @param _requests An array of CloseAllocationRequest
917-
*/
918-
function closeAllocationMany(CloseAllocationRequest[] calldata _requests)
919-
external
920-
override
921-
notPaused
922-
{
923-
for (uint256 i = 0; i < _requests.length; i++) {
924-
_closeAllocation(_requests[i].allocationID, _requests[i].poi);
925-
}
926-
}
927-
928-
/**
929-
* @dev Close and allocate. This will perform a close and then create a new Allocation
930-
* atomically on the same transaction.
931-
* @param _closingAllocationID The identifier of the allocation to be closed
932-
* @param _poi Proof of indexing submitted for the allocated period
933-
* @param _indexer Indexer address to allocate funds from.
934-
* @param _subgraphDeploymentID ID of the SubgraphDeployment where tokens will be allocated
935-
* @param _tokens Amount of tokens to allocate
936-
* @param _allocationID The allocation identifier
937-
* @param _metadata IPFS hash for additional information about the allocation
938-
* @param _proof A 65-bytes Ethereum signed message of `keccak256(indexerAddress,allocationID)`
939-
*/
940-
function closeAndAllocate(
941-
address _closingAllocationID,
942-
bytes32 _poi,
943-
address _indexer,
944-
bytes32 _subgraphDeploymentID,
945-
uint256 _tokens,
946-
address _allocationID,
947-
bytes32 _metadata,
948-
bytes calldata _proof
949-
) external override notPaused {
950-
_closeAllocation(_closingAllocationID, _poi);
951-
_allocate(_indexer, _subgraphDeploymentID, _tokens, _allocationID, _metadata, _proof);
952-
}
953-
954912
/**
955913
* @dev Collect query fees from state channels and assign them to an allocation.
956914
* Funds received are only accepted from a valid sender.
@@ -1036,21 +994,6 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
1036994
_claim(_allocationID, _restake);
1037995
}
1038996

1039-
/**
1040-
* @dev Claim tokens from the rebate pool for many allocations.
1041-
* @param _allocationID Array of allocations from where we are claiming tokens
1042-
* @param _restake True if restake fees instead of transfer to indexer
1043-
*/
1044-
function claimMany(address[] calldata _allocationID, bool _restake)
1045-
external
1046-
override
1047-
notPaused
1048-
{
1049-
for (uint256 i = 0; i < _allocationID.length; i++) {
1050-
_claim(_allocationID[i], _restake);
1051-
}
1052-
}
1053-
1054997
/**
1055998
* @dev Stake tokens on the indexer.
1056999
* This function does not check minimum indexer stake requirement to allow

test/staking/allocation.test.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'chai'
2-
import { constants, BigNumber } from 'ethers'
2+
import { constants, BigNumber, PopulatedTransaction } from 'ethers'
33

44
import { Curation } from '../../build/types/Curation'
55
import { EpochManager } from '../../build/types/EpochManager'
@@ -644,17 +644,21 @@ describe('Staking:Allocation', () => {
644644
await advanceToNextEpoch(epochManager)
645645

646646
// Close multiple allocations in one tx
647-
const requests = [
648-
{
649-
allocationID: allocationID,
650-
poi: poi,
651-
},
652-
{
653-
allocationID: allocationID2,
654-
poi: poi,
655-
},
656-
]
657-
await staking.connect(indexer.signer).closeAllocationMany(requests)
647+
const requests = await Promise.all(
648+
[
649+
{
650+
allocationID: allocationID,
651+
poi: poi,
652+
},
653+
{
654+
allocationID: allocationID2,
655+
poi: poi,
656+
},
657+
].map(({ allocationID, poi }) =>
658+
staking.connect(indexer.signer).populateTransaction.closeAllocation(allocationID, poi),
659+
),
660+
).then((e) => e.map((e: PopulatedTransaction) => e.data))
661+
await staking.connect(indexer.signer).multicall(requests)
658662
})
659663
})
660664

@@ -672,19 +676,21 @@ describe('Staking:Allocation', () => {
672676
// Close and allocate
673677
const newChannelKey = deriveChannelKey()
674678
const newAllocationID = newChannelKey.address
675-
const tx = staking
676-
.connect(indexer.signer)
677-
.closeAndAllocate(
678-
allocationID,
679-
HashZero,
680-
indexer.address,
681-
subgraphDeploymentID,
682-
tokensToAllocate,
683-
newAllocationID,
684-
metadata,
685-
await newChannelKey.generateProof(indexer.address),
686-
)
687-
await tx
679+
680+
// Close multiple allocations in one tx
681+
const requests = await Promise.all([
682+
staking.connect(indexer.signer).populateTransaction.closeAllocation(allocationID, poi),
683+
staking
684+
.connect(indexer.signer)
685+
.populateTransaction.allocate(
686+
subgraphDeploymentID,
687+
tokensToAllocate,
688+
newAllocationID,
689+
metadata,
690+
await newChannelKey.generateProof(indexer.address),
691+
),
692+
]).then((e) => e.map((e: PopulatedTransaction) => e.data))
693+
await staking.connect(indexer.signer).multicall(requests)
688694
})
689695
})
690696

@@ -870,7 +876,10 @@ describe('Staking:Allocation', () => {
870876

871877
// Claim with restake
872878
expect(await staking.getAllocationState(allocationID)).eq(AllocationState.Finalized)
873-
await staking.connect(indexer.signer).claimMany([allocationID], true)
879+
const tx = await staking
880+
.connect(indexer.signer)
881+
.populateTransaction.claim(allocationID, true)
882+
await staking.connect(indexer.signer).multicall([tx.data])
874883

875884
// Verify that the claimed tokens are restaked
876885
const afterIndexerStake = await staking.getIndexerStakedTokens(indexer.address)

0 commit comments

Comments
 (0)