Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Arbitrum network transaction gas estimates #861

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/indexer-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"clean": "rm -rf ./node_modules ./dist ./tsconfig.tsbuildinfo"
},
"dependencies": {
"@arbitrum/sdk": "^3.1.13",
"@graphprotocol/common-ts": "2.0.9",
"@graphprotocol/cost-model": "0.1.16",
"@thi.ng/heaps": "1.2.38",
Expand Down
113 changes: 113 additions & 0 deletions packages/indexer-common/src/__tests__/transactions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { Overrides } from 'ethers'
import {
connectContracts,
createLogger,
createMetrics,
Logger,
mutable,
NetworkContracts,
} from '@graphprotocol/common-ts'
import { connectWallet, Network, TransactionManager } from '@graphprotocol/indexer-common'
import { TransactionMonitoring } from '../network-specification'
import geohash from 'ngeohash'

// Make global Jest variables available
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare const __LOG_LEVEL__: never

let contracts: NetworkContracts
let logger: Logger
let transactionManager: TransactionManager

const setup = async () => {
logger = createLogger({
name: 'transactions.test.ts',
async: false,
level: __LOG_LEVEL__,
})
const metrics = createMetrics()
const provider = await Network.provider(
logger,
metrics,
'arbsepolia',
// 'https://sepolia.publicgoods.network',
'https://sepolia-rollup.arbitrum.io/rpc',
// 'https://wiser-evocative-energy.arbitrum-sepolia.quiknode.pro/fa97af810dfc2e91b4dedecdd381e92a82ef70e3/',
1000,
)
const testPhrase =
'myth like bonus scare over problem client lizard pioneer submit female collect'
const wallet = await connectWallet(provider, 'arbsepolia', testPhrase, logger)
transactionManager = new TransactionManager(
provider,
wallet,
mutable(false),
mutable(true),
TransactionMonitoring.parse({}),
)
contracts = await connectContracts(wallet, 421614, undefined)
}

describe('Transaction Manager tests', () => {
beforeAll(setup)

// Use higher timeout because tests make requests to an open RPC provider
jest.setTimeout(30_000)

test('Identify Arbitrum provider', async () => {
await expect(transactionManager.isArbitrumChain()).resolves.toEqual(true)
})

test('Get gas price', async () => {
const gasP = await transactionManager.ethereum.getFeeData()
console.log('FeeData', gasP)
expect(gasP).toEqual(true)
})

test('Arbitrum gas estimation', async () => {
const contractAddress = contracts.serviceRegistry.address
const txData = await contracts.serviceRegistry.populateTransaction.register(
'http://testindexer.hi',
geohash.encode(100, 100),
)
const estimatedFee = await transactionManager.arbGasEstimation(
logger,
contractAddress,
txData.data!,
)
console.log('ef', estimatedFee)

const gasP = await transactionManager.ethereum.getFeeData()
console.log('FeeData', gasP)
await expect(estimatedFee).toEqual(4)
})

test('Estimate gas usage of contract function', async () => {
const overrides = [
{ maxPriorityFeePerGas: 0 },
{ },
// { maxPriorityFeePerGas: 0, maxFeePerGas: 100000000 },
]
for (const override of overrides) {
const gasEstimate = await contracts.serviceRegistry.estimateGas.register(
'http://testindexer.hi',
geohash.encode(100, 100),
override,
)
console.log('gasEstimate', gasEstimate.toString())
}

await expect(overrides).toEqual({ maxFeePerGas: 0 })
})

test('Calculate transaction overrides', async () => {
const estimationFn = (overrides: Overrides | undefined) =>
contracts.serviceRegistry.estimateGas.register(
'http://testindexer.hi',
geohash.encode(100, 100),
overrides,
)

await expect(transactionManager.txOverrides(estimationFn)).resolves.toHaveProperty('maxPriorityFeePerGas', 0)
})
})
11 changes: 5 additions & 6 deletions packages/indexer-common/src/allocations/query-fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
specification as spec,
} from '..'
import { DHeap } from '@thi.ng/heaps'
import { BigNumber, BigNumberish, Contract } from 'ethers'
import { BigNumber, Contract } from 'ethers'
import { Op } from 'sequelize'
import pReduce from 'p-reduce'

Expand Down Expand Up @@ -567,11 +567,10 @@ export class AllocationReceiptCollector implements ReceiptCollector {
try {
// Submit the voucher on chain
const txReceipt = await this.transactionManager.executeTransaction(
() => this.allocationExchange.estimateGas.redeemMany(onchainVouchers),
async (gasLimit: BigNumberish) =>
this.allocationExchange.redeemMany(onchainVouchers, {
gasLimit,
}),
(overrides) =>
this.allocationExchange.estimateGas.redeemMany(onchainVouchers, overrides),
async (overrides) =>
this.allocationExchange.redeemMany(onchainVouchers, overrides),
logger.child({ action: 'redeemMany' }),
)

Expand Down
6 changes: 3 additions & 3 deletions packages/indexer-common/src/indexer-management/allocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ export class AllocationManager {
logger.trace('Prepared transaction calldata', { callData })

return await this.network.transactionManager.executeTransaction(
async () => this.network.contracts.staking.estimateGas.multicall(callData),
async (gasLimit) =>
this.network.contracts.staking.multicall(callData, { gasLimit }),
async (overrides) =>
this.network.contracts.staking.estimateGas.multicall(callData, overrides),
async (overrides) => this.network.contracts.staking.multicall(callData, overrides),
this.logger.child({
actions: `${JSON.stringify(validatedActions.map((action) => action.id))}`,
function: 'staking.multicall',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,24 +544,25 @@ export default {
})

const receipt = await transactionManager.executeTransaction(
async () =>
async (overrides) =>
contracts.staking.estimateGas.allocateFrom(
address,
subgraphDeployment.bytes32,
allocationAmount,
allocationId,
utils.hexlify(Array(32).fill(0)),
proof,
overrides,
),
async (gasLimit) =>
async (overrides) =>
contracts.staking.allocateFrom(
address,
subgraphDeployment.bytes32,
allocationAmount,
allocationId,
utils.hexlify(Array(32).fill(0)),
proof,
{ gasLimit },
overrides,
),
logger.child({ action: 'allocate' }),
)
Expand Down Expand Up @@ -700,12 +701,15 @@ export default {
logger.debug('Sending closeAllocation transaction')
const receipt = await transactionManager.executeTransaction(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
() => contracts.staking.estimateGas.closeAllocation(allocationData.id, poi!),
(gasLimit) =>
(overrides) =>
contracts.staking.estimateGas.closeAllocation(
allocationData.id,
poi!,
overrides,
),
(overrides) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
contracts.staking.closeAllocation(allocationData.id, poi!, {
gasLimit,
}),
contracts.staking.closeAllocation(allocationData.id, poi!, overrides),
logger,
)

Expand Down Expand Up @@ -1009,8 +1013,8 @@ export default {
].map((tx) => tx.data as string)

const receipt = await transactionManager.executeTransaction(
async () => contracts.staking.estimateGas.multicall(callData),
async (gasLimit) => contracts.staking.multicall(callData, { gasLimit }),
async (overrides) => contracts.staking.estimateGas.multicall(callData, overrides),
async (overrides) => contracts.staking.multicall(callData, overrides),
logger.child({
function: 'closeAndAllocate',
}),
Expand Down
11 changes: 5 additions & 6 deletions packages/indexer-common/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,19 @@ export class Network {
}
}
const receipt = await this.transactionManager.executeTransaction(
() =>
(overrides) =>
this.contracts.serviceRegistry.estimateGas.registerFor(
this.specification.indexerOptions.address,
this.specification.indexerOptions.url,
geoHash,
overrides,
),
(gasLimit) =>
(overrides) =>
this.contracts.serviceRegistry.registerFor(
this.specification.indexerOptions.address,
this.specification.indexerOptions.url,
geoHash,
{
gasLimit,
},
overrides,
),
logger.child({ function: 'serviceRegistry.registerFor' }),
)
Expand Down Expand Up @@ -417,7 +416,7 @@ export class Network {
}
}

async function connectWallet(
export async function connectWallet(
networkProvider: providers.Provider,
networkIdentifier: string,
mnemonic: string,
Expand Down
Loading