Skip to content

Commit 3538e00

Browse files
authored
Merge pull request #550 from graphprotocol/ariel/multicall-staged
Add multicall to staking contract + keep legacy bulk calls
2 parents 3541624 + fdf526c commit 3538e00

File tree

11 files changed

+84
-47
lines changed

11 files changed

+84
-47
lines changed
Binary file not shown.

cli/commands/contracts/staking.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export const allocate = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<v
3636
const staking = cli.contracts.Staking
3737

3838
logger.info(`Allocating ${cliArgs.amount} tokens on ${subgraphDeploymentID}...`)
39-
await sendTransaction(cli.wallet, staking, 'allocate', [
39+
await sendTransaction(cli.wallet, staking, 'allocateFrom', [
40+
cli.walletAddress,
4041
subgraphDeploymentID,
4142
amount,
4243
allocationID,

contracts/staking/Staking.sol

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pragma abicoder v2;
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;

test/disputes/poi.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ describe('DisputeManager:POI', async () => {
7979
await staking.connect(indexerAccount.signer).stake(indexerTokens)
8080
await staking
8181
.connect(indexerAccount.signer)
82-
.allocate(
82+
.allocateFrom(
83+
indexerAccount.address,
8384
subgraphDeploymentID,
8485
indexerAllocatedTokens,
8586
allocationID,
@@ -147,7 +148,8 @@ describe('DisputeManager:POI', async () => {
147148
await staking.connect(indexer.signer).stake(indexerTokens)
148149
const tx1 = await staking
149150
.connect(indexer.signer)
150-
.allocate(
151+
.allocateFrom(
152+
indexer.address,
151153
subgraphDeploymentID,
152154
indexerAllocatedTokens,
153155
allocationID,

test/disputes/query.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ describe('DisputeManager:Query', async () => {
113113
await staking.connect(indexerAccount.signer).stake(indexerTokens)
114114
await staking
115115
.connect(indexerAccount.signer)
116-
.allocate(
116+
.allocateFrom(
117+
indexerAccount.address,
117118
dispute.receipt.subgraphDeploymentID,
118119
indexerAllocatedTokens,
119120
allocationID,
@@ -194,7 +195,8 @@ describe('DisputeManager:Query', async () => {
194195
await staking.connect(indexer.signer).stake(indexerTokens)
195196
const tx1 = await staking
196197
.connect(indexer.signer)
197-
.allocate(
198+
.allocateFrom(
199+
indexer.address,
198200
dispute.receipt.subgraphDeploymentID,
199201
indexerAllocatedTokens,
200202
indexer1ChannelKey.address,

test/payments/allocationExchange.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ describe('AllocationExchange', () => {
102102
await staking.connect(indexer.signer).stake(stakeTokens)
103103
await staking
104104
.connect(indexer.signer)
105-
.allocate(
105+
.allocateFrom(
106+
indexer.address,
106107
subgraphDeploymentID,
107108
stakeTokens,
108109
allocationID,

test/payments/withdrawHelper.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ describe('WithdrawHelper', () => {
8686
await staking.connect(indexer.signer).stake(stakeTokens)
8787
await staking
8888
.connect(indexer.signer)
89-
.allocate(
89+
.allocateFrom(
90+
indexer.address,
9091
subgraphDeploymentID,
9192
stakeTokens,
9293
allocationID,

test/rewards/rewards.test.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ describe('Rewards', () => {
391391
await staking.connect(indexer1.signer).stake(tokensToAllocate)
392392
await staking
393393
.connect(indexer1.signer)
394-
.allocate(
394+
.allocateFrom(
395+
indexer1.address,
395396
subgraphDeploymentID1,
396397
tokensToAllocate,
397398
allocationID,
@@ -428,7 +429,8 @@ describe('Rewards', () => {
428429
await staking.connect(indexer1.signer).stake(tokensToAllocate)
429430
await staking
430431
.connect(indexer1.signer)
431-
.allocate(
432+
.allocateFrom(
433+
indexer1.address,
432434
subgraphDeploymentID1,
433435
tokensToAllocate,
434436
allocationID,
@@ -471,7 +473,8 @@ describe('Rewards', () => {
471473
await staking.connect(indexer1.signer).stake(tokensToAllocate)
472474
await staking
473475
.connect(indexer1.signer)
474-
.allocate(
476+
.allocateFrom(
477+
indexer1.address,
475478
subgraphDeploymentID1,
476479
tokensToAllocate,
477480
allocationID,
@@ -516,7 +519,8 @@ describe('Rewards', () => {
516519
await staking.connect(indexer1.signer).stake(tokensToAllocate)
517520
await staking
518521
.connect(indexer1.signer)
519-
.allocate(
522+
.allocateFrom(
523+
indexer1.address,
520524
subgraphDeploymentID1,
521525
tokensToAllocate,
522526
allocationID,
@@ -558,7 +562,8 @@ describe('Rewards', () => {
558562
// Allocate
559563
await staking
560564
.connect(indexer1.signer)
561-
.allocate(
565+
.allocateFrom(
566+
indexer1.address,
562567
subgraphDeploymentID1,
563568
tokensToAllocate,
564569
allocationID,
@@ -754,7 +759,8 @@ describe('Rewards', () => {
754759
await staking.connect(indexer1.signer).stake(tokensToAllocate)
755760
await staking
756761
.connect(indexer1.signer)
757-
.allocate(
762+
.allocateFrom(
763+
indexer1.address,
758764
subgraphDeploymentID1,
759765
tokensToAllocate,
760766
allocationID,

test/staking/allocation.test.ts

+52-31
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'
@@ -67,7 +67,8 @@ describe('Staking:Allocation', () => {
6767
const allocate = async (tokens: BigNumber) => {
6868
return staking
6969
.connect(indexer.signer)
70-
.allocate(
70+
.allocateFrom(
71+
indexer.address,
7172
subgraphDeploymentID,
7273
tokens,
7374
allocationID,
@@ -206,7 +207,14 @@ describe('Staking:Allocation', () => {
206207
it('reject allocate with invalid allocationID', async function () {
207208
const tx = staking
208209
.connect(indexer.signer)
209-
.allocate(subgraphDeploymentID, tokensToAllocate, AddressZero, metadata, randomHexBytes(20))
210+
.allocateFrom(
211+
indexer.address,
212+
subgraphDeploymentID,
213+
tokensToAllocate,
214+
AddressZero,
215+
metadata,
216+
randomHexBytes(20),
217+
)
210218
await expect(tx).revertedWith('!alloc')
211219
})
212220

@@ -273,7 +281,8 @@ describe('Staking:Allocation', () => {
273281
const invalidProof = await channelKey.generateProof(randomHexBytes(20))
274282
const tx = staking
275283
.connect(indexer.signer)
276-
.allocate(
284+
.allocateFrom(
285+
indexer.address,
277286
subgraphDeploymentID,
278287
tokensToAllocate,
279288
indexer.address,
@@ -286,7 +295,8 @@ describe('Staking:Allocation', () => {
286295
it('invalid proof signature format', async function () {
287296
const tx = staking
288297
.connect(indexer.signer)
289-
.allocate(
298+
.allocateFrom(
299+
indexer.address,
290300
subgraphDeploymentID,
291301
tokensToAllocate,
292302
indexer.address,
@@ -631,7 +641,8 @@ describe('Staking:Allocation', () => {
631641
const allocationID2 = channelKey2.address
632642
await staking
633643
.connect(indexer.signer)
634-
.allocate(
644+
.allocateFrom(
645+
indexer.address,
635646
subgraphDeploymentID,
636647
tokensToAllocate,
637648
allocationID2,
@@ -644,17 +655,21 @@ describe('Staking:Allocation', () => {
644655
await advanceToNextEpoch(epochManager)
645656

646657
// 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)
658+
const requests = await Promise.all(
659+
[
660+
{
661+
allocationID: allocationID,
662+
poi: poi,
663+
},
664+
{
665+
allocationID: allocationID2,
666+
poi: poi,
667+
},
668+
].map(({ allocationID, poi }) =>
669+
staking.connect(indexer.signer).populateTransaction.closeAllocation(allocationID, poi),
670+
),
671+
).then((e) => e.map((e: PopulatedTransaction) => e.data))
672+
await staking.connect(indexer.signer).multicall(requests)
658673
})
659674
})
660675

@@ -672,19 +687,22 @@ describe('Staking:Allocation', () => {
672687
// Close and allocate
673688
const newChannelKey = deriveChannelKey()
674689
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
690+
691+
// Close multiple allocations in one tx
692+
const requests = await Promise.all([
693+
staking.connect(indexer.signer).populateTransaction.closeAllocation(allocationID, poi),
694+
staking
695+
.connect(indexer.signer)
696+
.populateTransaction.allocateFrom(
697+
indexer.address,
698+
subgraphDeploymentID,
699+
tokensToAllocate,
700+
newAllocationID,
701+
metadata,
702+
await newChannelKey.generateProof(indexer.address),
703+
),
704+
]).then((e) => e.map((e: PopulatedTransaction) => e.data))
705+
await staking.connect(indexer.signer).multicall(requests)
688706
})
689707
})
690708

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

871889
// Claim with restake
872890
expect(await staking.getAllocationState(allocationID)).eq(AllocationState.Finalized)
873-
await staking.connect(indexer.signer).claimMany([allocationID], true)
891+
const tx = await staking
892+
.connect(indexer.signer)
893+
.populateTransaction.claim(allocationID, true)
894+
await staking.connect(indexer.signer).multicall([tx.data])
874895

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

test/staking/delegation.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ describe('Staking::Delegation', () => {
534534
const setupAllocation = async (tokens: BigNumber) => {
535535
return staking
536536
.connect(indexer.signer)
537-
.allocate(
537+
.allocateFrom(
538+
indexer.address,
538539
subgraphDeploymentID,
539540
tokens,
540541
allocationID,

test/staking/staking.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ describe('Staking:Stakes', () => {
5353
const allocate = async (tokens: BigNumber) => {
5454
return staking
5555
.connect(indexer.signer)
56-
.allocate(
56+
.allocateFrom(
57+
indexer.address,
5758
subgraphDeploymentID,
5859
tokens,
5960
allocationID,

0 commit comments

Comments
 (0)