From 0260b446307b85980bd4bee2839fdf325b9182b9 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Velez Date: Mon, 26 Jun 2023 12:58:56 -0300 Subject: [PATCH] fix: use multicall for closeAndAllocate --- docs/action-queue.md | 2 +- .../src/indexer-management/allocations.ts | 77 +++++++++---------- .../resolvers/allocations.ts | 45 +++++------ 3 files changed, 59 insertions(+), 65 deletions(-) diff --git a/docs/action-queue.md b/docs/action-queue.md index 35eade859..5a882c4d7 100644 --- a/docs/action-queue.md +++ b/docs/action-queue.md @@ -41,7 +41,7 @@ Local usage from source # Queue allocate action (allocateFrom()) ./bin/graph-indexer indexer actions queue allocate QmeqJ6hsdyk9dVbo1tvRgAxWrVS3rkERiEMsxzPShKLco6 5000 -# Queue reallocate action (closeAndAllocate()) +# Queue reallocate action (close and allocate using multicall()) ./bin/graph-indexer indexer actions queue reallocate QmeqJ6hsdyk9dVbo1tvRgAxWrVS3rkERiEMsxzPShKLco6 0x4a58d33e27d3acbaecc92c15101fbc82f47c2ae5 55000 # Queue unallocate action (closeAllocation()) diff --git a/packages/indexer-common/src/indexer-management/allocations.ts b/packages/indexer-common/src/indexer-management/allocations.ts index 9a34f3b3e..a2a1aa5ee 100644 --- a/packages/indexer-common/src/indexer-management/allocations.ts +++ b/packages/indexer-common/src/indexer-management/allocations.ts @@ -79,7 +79,7 @@ export interface ActionStakeUsageSummary { balance: BigNumber } -export type PopulateTransactionResult = PopulatedTransaction | ActionFailure +export type PopulateTransactionResult = PopulatedTransaction | PopulatedTransaction[] | ActionFailure export type TransactionResult = | ContractReceipt @@ -124,6 +124,7 @@ export class AllocationManager { } const callData = populateTransactionsResults + .flat() .map((tx) => tx as PopulatedTransaction) .filter((tx: PopulatedTransaction) => !!tx.data) .map((tx) => tx.data as string) @@ -840,7 +841,7 @@ export class AllocationManager { allocationIDProof: proof, }) - logger.info(`Prepared closeAndAllocate transaction`, { + logger.info(`Prepared close and allocate multicall transaction`, { indexer: this.indexer, oldAllocationAmount: formatGRT(allocation.allocatedTokens), oldAllocation: allocation.id, @@ -870,7 +871,7 @@ export class AllocationManager { receipt: ContractReceipt | 'paused' | 'unauthorized', ): Promise { const logger = this.logger.child({ action: actionID }) - logger.info(`Confirming 'closeAndAllocate' transaction`, { + logger.info(`Confirming close and allocate 'multicall' transaction`, { allocationID, }) if (receipt === 'paused' || receipt === 'unauthorized') { @@ -985,7 +986,7 @@ export class AllocationManager { poi: string | undefined, amount: BigNumber, force: boolean, - ): Promise { + ): Promise { const params = await this.prepareReallocateParams( logger, allocationID, @@ -993,16 +994,19 @@ export class AllocationManager { amount, force, ) - return await this.contracts.staking.populateTransaction.closeAndAllocate( - params.closingAllocationID, - params.poi, - params.indexer, - params.subgraphDeploymentID, - params.tokens, - params.newAllocationID, - params.metadata, - params.proof, - ) + return [ + await this.contracts.staking.populateTransaction.closeAllocation( + params.closingAllocationID, + params.poi, + ), + await this.contracts.staking.populateTransaction.allocate( + params.subgraphDeploymentID, + params.tokens, + params.newAllocationID, + params.metadata, + params.proof, + ), + ] } async reallocate( @@ -1020,7 +1024,7 @@ export class AllocationManager { force, ) - this.logger.info(`Sending closeAndAllocate transaction`, { + this.logger.info(`Sending close and allocate multicall transaction`, { indexer: params.indexer, oldAllocation: params.closingAllocationID, newAllocation: params.newAllocationID, @@ -1030,31 +1034,26 @@ export class AllocationManager { proof: params.proof, }) + const callData = [ + await this.contracts.staking.populateTransaction.closeAllocation( + params.closingAllocationID, + params.poi, + ), + await this.contracts.staking.populateTransaction.allocate( + params.subgraphDeploymentID, + params.tokens, + params.newAllocationID, + params.metadata, + params.proof, + ), + ].map((tx) => tx.data as string) + const receipt = await this.transactionManager.executeTransaction( - async () => - this.contracts.staking.estimateGas.closeAndAllocate( - params.closingAllocationID, - params.poi, - params.indexer, - params.subgraphDeploymentID, - params.tokens, - params.newAllocationID, - params.metadata, - params.proof, - ), - async (gasLimit) => - this.contracts.staking.closeAndAllocate( - params.closingAllocationID, - params.poi, - params.indexer, - params.subgraphDeploymentID, - params.tokens, - params.newAllocationID, - params.metadata, - params.proof, - { gasLimit }, - ), - this.logger.child({ function: 'staking.closeAndAllocate' }), + async () => this.contracts.staking.estimateGas.multicall(callData), + async (gasLimit) => this.contracts.staking.multicall(callData, { gasLimit }), + this.logger.child({ + function: 'closeAndAllocate', + }), ) return await this.confirmReallocate(0, allocationID, receipt) diff --git a/packages/indexer-common/src/indexer-management/resolvers/allocations.ts b/packages/indexer-common/src/indexer-management/resolvers/allocations.ts index 5fce74084..45be6febc 100644 --- a/packages/indexer-common/src/indexer-management/resolvers/allocations.ts +++ b/packages/indexer-common/src/indexer-management/resolvers/allocations.ts @@ -967,7 +967,7 @@ export default { allocationIDProof: proof, }) - logger.info(`Sending closeAndAllocate transaction`, { + logger.info(`Sending close and allocate multicall transaction`, { indexer: address, amount: formatGRT(allocationAmount), oldAllocation: allocationData.id, @@ -979,31 +979,26 @@ export default { epoch: currentEpoch.toString(), }) + const callData = [ + await contracts.staking.populateTransaction.closeAllocation( + allocationData.id, + allocationPOI, + ), + await contracts.staking.populateTransaction.allocate( + allocationData.subgraphDeployment.id.bytes32, + allocationAmount, + newAllocationId, + utils.hexlify(Array(32).fill(0)), // metadata + proof, + ), + ].map((tx) => tx.data as string) + const receipt = await transactionManager.executeTransaction( - async () => - contracts.staking.estimateGas.closeAndAllocate( - allocationData.id, - allocationPOI, - address, - allocationData.subgraphDeployment.id.bytes32, - allocationAmount, - newAllocationId, - utils.hexlify(Array(32).fill(0)), // metadata - proof, - ), - async (gasLimit) => - contracts.staking.closeAndAllocate( - allocationData.id, - allocationPOI, - address, - allocationData.subgraphDeployment.id.bytes32, - allocationAmount, - newAllocationId, - utils.hexlify(Array(32).fill(0)), // metadata - proof, - { gasLimit }, - ), - logger.child({ action: 'closeAndAllocate' }), + async () => contracts.staking.estimateGas.multicall(callData), + async (gasLimit) => contracts.staking.multicall(callData, { gasLimit }), + logger.child({ + function: 'closeAndAllocate', + }), ) if (receipt === 'paused' || receipt === 'unauthorized') {