diff --git a/package.json b/package.json index 920a8af3..6ac6e6e7 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "axios-retry": "^3.2.4", "bignumber.js": "9.0.1", "body-parser": "1.19.0", + "bottleneck": "^2.19.5", "cls-hooked": "^4.2.2", "compression": "1.7.4", "cookie-parser": "1.4.5", diff --git a/scripts/gas-refund-program/transactions-indexing/transaction-resolver.ts b/scripts/gas-refund-program/transactions-indexing/transaction-resolver.ts index ca23cd1e..85ac3000 100644 --- a/scripts/gas-refund-program/transactions-indexing/transaction-resolver.ts +++ b/scripts/gas-refund-program/transactions-indexing/transaction-resolver.ts @@ -12,7 +12,6 @@ */ import { SPSPAddresses } from '../../../src/lib/staking/spsp-helper'; import { covalentGetTXsForContractV3 } from './txs-covalent'; -import { getTransactionGasUsed } from '../../../src/lib/utils/covalent'; import StakesTracker from '../staking/stakes-tracker'; import { getSuccessfulSwaps } from './swaps-subgraph'; import { GasRefundTransaction } from '../types'; @@ -34,7 +33,7 @@ import { getMigrationsTxs } from '../staking/2.0/migrations'; import { MIGRATION_SEPSP2_100_PERCENT_KEY } from '../staking/2.0/utils'; import { grp2ConfigByChain } from '../../../src/lib/gas-refund/config'; import { assert } from 'ts-essentials'; -import { Provider } from '../../../src/lib/provider'; +import { fetchTxGasUsed } from './utils'; type GetAllTXsInput = { startTimestamp: number; @@ -176,9 +175,7 @@ export const getSwapTXs = async ({ const swapsWithGasUsedNormalised: GasRefundTransaction[] = await Promise.all( swapsOfQualifyingStakers.map( async ({ txHash, txOrigin, txGasPrice, timestamp, blockNumber }) => { - const provider = Provider.getJsonRpcProvider(chainId); - const tx = await provider.getTransactionReceipt(txHash); - const txGasUsed = tx.gasUsed.toNumber(); + const txGasUsed = await fetchTxGasUsed(chainId, txHash); assert( txGasUsed, diff --git a/scripts/gas-refund-program/transactions-indexing/utils.ts b/scripts/gas-refund-program/transactions-indexing/utils.ts new file mode 100644 index 00000000..bbd533cd --- /dev/null +++ b/scripts/gas-refund-program/transactions-indexing/utils.ts @@ -0,0 +1,19 @@ +import { Provider } from '../../../src/lib/provider'; + +const Bottleneck = require('bottleneck'); + +const limiter = new Bottleneck({ + minTime: 200, // 200ms interval between calls (5 calls per second) +}); + +const _fetchTxGasUsed = async ( + chainId: number, + txHash: string, +): Promise => { + const provider = Provider.getJsonRpcProvider(chainId); + const tx = await provider.getTransactionReceipt(txHash); + const txGasUsed = tx.gasUsed.toNumber(); + return txGasUsed; +}; + +export const fetchTxGasUsed = limiter.wrap(_fetchTxGasUsed); diff --git a/src/lib/utils/covalent.ts b/src/lib/utils/covalent.ts index 348a0f75..dd4fbff6 100644 --- a/src/lib/utils/covalent.ts +++ b/src/lib/utils/covalent.ts @@ -82,16 +82,6 @@ export async function getTransaction({ return tx; } -export async function getTransactionGasUsed({ - chainId, - txHash, -}: TransactionQueryOptions): Promise { - const tx = await getTransaction({ chainId, txHash }); - const gasUsed = tx.gas_spent; - assert(gasUsed > 0, 'Expected transaction to non zero gas_spent'); - return gasUsed; -} - interface TokensHoldersResponse { data: TokensHoldersData; error: false; diff --git a/yarn.lock b/yarn.lock index ef8de50d..e288faf2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2149,6 +2149,11 @@ body-parser@1.19.0: raw-body "2.4.0" type-is "~1.6.17" +bottleneck@^2.19.5: + version "2.19.5" + resolved "https://registry.yarnpkg.com/bottleneck/-/bottleneck-2.19.5.tgz#5df0b90f59fd47656ebe63c78a98419205cadd91" + integrity sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw== + boxen@^5.0.0: version "5.1.2" resolved "https://registry.yarnpkg.com/boxen/-/boxen-5.1.2.tgz#788cb686fc83c1f486dfa8a40c68fc2b831d2b50"