diff --git a/packages/arb-token-bridge-ui/package.json b/packages/arb-token-bridge-ui/package.json index abe147b101..63b2533928 100644 --- a/packages/arb-token-bridge-ui/package.json +++ b/packages/arb-token-bridge-ui/package.json @@ -25,6 +25,7 @@ "cheerio": "^1.0.0-rc.12", "dayjs": "^1.11.8", "ethers": "^5.6.0", + "exponential-backoff": "^3.1.1", "graphql": "^16.8.1", "lodash-es": "^4.17.21", "next": "^14.2.12", diff --git a/packages/arb-token-bridge-ui/src/util/ExponentialBackoffUtils.ts b/packages/arb-token-bridge-ui/src/util/ExponentialBackoffUtils.ts index b715e35da9..c46210ce24 100644 --- a/packages/arb-token-bridge-ui/src/util/ExponentialBackoffUtils.ts +++ b/packages/arb-token-bridge-ui/src/util/ExponentialBackoffUtils.ts @@ -1,3 +1,14 @@ +import { backOff as _backOff, BackoffOptions } from 'exponential-backoff' + +const backoffOptions: BackoffOptions = { + startingDelay: 1_000, + timeMultiple: 1.5 +} + +export function backOff(request: () => Promise): Promise { + return _backOff(request, backoffOptions) +} + export function wait(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } diff --git a/packages/arb-token-bridge-ui/src/util/withdrawals/fetchETHWithdrawalsFromEventLogs.ts b/packages/arb-token-bridge-ui/src/util/withdrawals/fetchETHWithdrawalsFromEventLogs.ts index 3d38e5318f..6550370a86 100644 --- a/packages/arb-token-bridge-ui/src/util/withdrawals/fetchETHWithdrawalsFromEventLogs.ts +++ b/packages/arb-token-bridge-ui/src/util/withdrawals/fetchETHWithdrawalsFromEventLogs.ts @@ -22,7 +22,7 @@ export function fetchETHWithdrawalsFromEventLogs({ l2Provider: Provider }) { if (typeof receiver === 'undefined') { - return [] + return Promise.resolve([]) } // funds received by this address diff --git a/packages/arb-token-bridge-ui/src/util/withdrawals/fetchTokenWithdrawalsFromEventLogsSequentially.ts b/packages/arb-token-bridge-ui/src/util/withdrawals/fetchTokenWithdrawalsFromEventLogsSequentially.ts index 6815e32389..5087788d3c 100644 --- a/packages/arb-token-bridge-ui/src/util/withdrawals/fetchTokenWithdrawalsFromEventLogsSequentially.ts +++ b/packages/arb-token-bridge-ui/src/util/withdrawals/fetchTokenWithdrawalsFromEventLogsSequentially.ts @@ -8,7 +8,7 @@ import { } from './fetchTokenWithdrawalsFromEventLogs' import { getNonce } from '../AddressUtils' import { fetchL2Gateways } from '../fetchL2Gateways' -import { wait } from '../ExponentialBackoffUtils' +import { backOff, wait } from '../ExponentialBackoffUtils' async function getGateways(provider: Provider): Promise<{ standardGateway: string @@ -101,7 +101,7 @@ export async function fetchTokenWithdrawalsFromEventLogsSequentially({ } const gateways = await getGateways(provider) - const senderNonce = await getNonce(sender, { provider }) + const senderNonce = await backOff(() => getNonce(sender, { provider })) // sender queries; only add if nonce > 0 if (senderNonce > 0) { @@ -130,7 +130,7 @@ export async function fetchTokenWithdrawalsFromEventLogsSequentially({ const currentPriorityResults = await Promise.all( currentPriorityQueries.map(query => - fetchTokenWithdrawalsFromEventLogs(query.params) + backOff(() => fetchTokenWithdrawalsFromEventLogs(query.params)) ) ) diff --git a/packages/arb-token-bridge-ui/src/util/withdrawals/fetchWithdrawals.ts b/packages/arb-token-bridge-ui/src/util/withdrawals/fetchWithdrawals.ts index fba6ff77e9..9b8e1b6274 100644 --- a/packages/arb-token-bridge-ui/src/util/withdrawals/fetchWithdrawals.ts +++ b/packages/arb-token-bridge-ui/src/util/withdrawals/fetchWithdrawals.ts @@ -12,7 +12,7 @@ import { Withdrawal } from '../../hooks/useTransactionHistory' import { attachTimestampToTokenWithdrawal } from './helpers' import { WithdrawalInitiated } from '../../hooks/arbTokenBridge.types' import { fetchTokenWithdrawalsFromEventLogsSequentially } from './fetchTokenWithdrawalsFromEventLogsSequentially' -import { wait } from '../ExponentialBackoffUtils' +import { backOff, wait } from '../ExponentialBackoffUtils' export type FetchWithdrawalsParams = { sender?: string @@ -84,12 +84,16 @@ export async function fetchWithdrawals({ console.log('Error fetching withdrawals from subgraph', error) } - const ethWithdrawalsFromEventLogs = await fetchETHWithdrawalsFromEventLogs({ - receiver, - fromBlock: toBlock + 1, - toBlock: 'latest', - l2Provider: l2Provider - }) + const ethWithdrawalsFromEventLogs = await backOff(() => + fetchETHWithdrawalsFromEventLogs({ + receiver, + // not sure why eslint is treating "toBlock" as "number | undefined" here + // even though typescript recognizes it as "number" + fromBlock: toBlock ?? 0 + 1, + toBlock: 'latest', + l2Provider: l2Provider + }) + ) await wait(2_000) diff --git a/yarn.lock b/yarn.lock index 5e7210b539..6bc5effd61 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7545,6 +7545,11 @@ expect@^29.0.0, expect@^29.5.0: jest-message-util "^29.5.0" jest-util "^29.5.0" +exponential-backoff@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/exponential-backoff/-/exponential-backoff-3.1.1.tgz#64ac7526fe341ab18a39016cd22c787d01e00bf6" + integrity sha512-dX7e/LHVJ6W3DE1MHWi9S1EYzDESENfLrYohG2G++ovZrYOkm4Knwa0mc1cn84xJOR4KEU0WSchhLbd0UklbHw== + express@^4.17.3: version "4.21.0" resolved "https://registry.yarnpkg.com/express/-/express-4.21.0.tgz#d57cb706d49623d4ac27833f1cbc466b668eb915"