Skip to content

Commit

Permalink
Merge pull request #115 from paraswap/chore/use-rpc-to-fetch-gas-used…
Browse files Browse the repository at this point in the history
…-over-covalent

gas-refund: rate limited fetch gas used fn
  • Loading branch information
mwamedacen authored Jun 21, 2024
2 parents 248de74 + 013f227 commit 642ca4d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions scripts/gas-refund-program/transactions-indexing/utils.ts
Original file line number Diff line number Diff line change
@@ -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<number> => {
const provider = Provider.getJsonRpcProvider(chainId);
const tx = await provider.getTransactionReceipt(txHash);
const txGasUsed = tx.gasUsed.toNumber();
return txGasUsed;
};

export const fetchTxGasUsed = limiter.wrap(_fetchTxGasUsed);
10 changes: 0 additions & 10 deletions src/lib/utils/covalent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@ export async function getTransaction({
return tx;
}

export async function getTransactionGasUsed({
chainId,
txHash,
}: TransactionQueryOptions): Promise<number> {
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;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,11 @@ [email protected]:
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"
Expand Down

0 comments on commit 642ca4d

Please sign in to comment.