Skip to content

Commit

Permalink
fix: use multicall for closeAndAllocate
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarranzav committed Jun 26, 2023
1 parent 6870e07 commit c2acb8b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 65 deletions.
2 changes: 1 addition & 1 deletion docs/action-queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
77 changes: 38 additions & 39 deletions packages/indexer-common/src/indexer-management/allocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface ActionStakeUsageSummary {
balance: BigNumber
}

export type PopulateTransactionResult = PopulatedTransaction | ActionFailure
export type PopulateTransactionResult = PopulatedTransaction | PopulatedTransaction[] | ActionFailure

export type TransactionResult =
| ContractReceipt
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -870,7 +871,7 @@ export class AllocationManager {
receipt: ContractReceipt | 'paused' | 'unauthorized',
): Promise<ReallocateAllocationResult> {
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') {
Expand Down Expand Up @@ -985,24 +986,27 @@ export class AllocationManager {
poi: string | undefined,
amount: BigNumber,
force: boolean,
): Promise<PopulatedTransaction> {
): Promise<PopulatedTransaction[]> {
const params = await this.prepareReallocateParams(
logger,
allocationID,
poi,
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(
Expand All @@ -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,
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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') {
Expand Down

0 comments on commit c2acb8b

Please sign in to comment.