-
Notifications
You must be signed in to change notification settings - Fork 203
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
perf: fetch withdrawals in parallel when possible #2147
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
312cf06
wip
spsjvc a6dfd9c
export return type
spsjvc 6adc70f
remove unused stuff
spsjvc 189139e
rename stuff and simplify
spsjvc cfc30d6
clean up
spsjvc 492f3cd
unused
spsjvc c232e36
oops
spsjvc b39e73f
Merge branch 'master' into perf-optimize-withdrawals
spsjvc 6d64595
use chain id
spsjvc bcd2617
Merge branch 'master' into perf-optimize-withdrawals
spsjvc 3231ceb
remove async
spsjvc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,61 @@ | ||
import { constants } from 'ethers' | ||
import { Provider, BlockTag } from '@ethersproject/providers' | ||
import { Erc20Bridger, getArbitrumNetwork } from '@arbitrum/sdk' | ||
import { Erc20Bridger } from '@arbitrum/sdk' | ||
|
||
import { | ||
fetchTokenWithdrawalsFromEventLogs, | ||
FetchTokenWithdrawalsFromEventLogsParams | ||
} from './fetchTokenWithdrawalsFromEventLogs' | ||
import { getNonce } from '../AddressUtils' | ||
import { fetchL2Gateways } from '../fetchL2Gateways' | ||
import { backOff, wait } from '../ExponentialBackoffUtils' | ||
|
||
async function getGateways(provider: Provider): Promise<{ | ||
standardGateway: string | ||
wethGateway: string | ||
customGateway: string | ||
otherGateways: string[] | ||
}> { | ||
const network = await getArbitrumNetwork(provider) | ||
|
||
const standardGateway = network.tokenBridge?.childErc20Gateway | ||
const customGateway = network.tokenBridge?.childCustomGateway | ||
const wethGateway = network.tokenBridge?.childWethGateway | ||
const otherGateways = await fetchL2Gateways(provider) | ||
|
||
return { | ||
standardGateway: standardGateway ?? constants.AddressZero, | ||
wethGateway: wethGateway ?? constants.AddressZero, | ||
customGateway: customGateway ?? constants.AddressZero, | ||
otherGateways | ||
} | ||
} | ||
import { backOff, wait } from '../ExponentialBackoffUtils' | ||
|
||
type TokenWithdrawalQuery = { | ||
type FetchTokenWithdrawalsFromEventLogsQuery = { | ||
params: FetchTokenWithdrawalsFromEventLogsParams | ||
priority: number | ||
} | ||
|
||
export type Query = { | ||
sender?: string | ||
receiver?: string | ||
gateways?: string[] | ||
} | ||
|
||
export type FetchTokenWithdrawalsFromEventLogsSequentiallyParams = { | ||
sender?: string | ||
receiver?: string | ||
provider: Provider | ||
fromBlock?: BlockTag | ||
toBlock?: BlockTag | ||
/** | ||
* How long to delay in-between queries of different priority. | ||
* How long to delay in-between queries of different priority. Defaults to 0. | ||
*/ | ||
delayMs?: number | ||
queries: Query[] | ||
} | ||
|
||
export type FetchTokenWithdrawalsFromEventLogsSequentiallyResult = Awaited< | ||
ReturnType<Erc20Bridger['getWithdrawalEvents']> | ||
> | ||
|
||
export async function fetchTokenWithdrawalsFromEventLogsSequentially({ | ||
sender, | ||
receiver, | ||
provider, | ||
fromBlock = 0, | ||
toBlock = 'latest', | ||
delayMs = 2_000 | ||
delayMs = 0, | ||
queries: queriesProp | ||
}: FetchTokenWithdrawalsFromEventLogsSequentiallyParams): Promise<FetchTokenWithdrawalsFromEventLogsSequentiallyResult> { | ||
// keep track of priority; increment as queries are added | ||
let priority = 0 | ||
|
||
// keep track of queries | ||
const queries: TokenWithdrawalQuery[] = [] | ||
const queries: FetchTokenWithdrawalsFromEventLogsQuery[] = [] | ||
|
||
// helper function to reuse common params | ||
function buildQueryParams({ | ||
sender, | ||
receiver, | ||
gateways = [] | ||
}: { | ||
sender?: string | ||
receiver?: string | ||
gateways?: string[] | ||
}): TokenWithdrawalQuery['params'] { | ||
}: Query): FetchTokenWithdrawalsFromEventLogsQuery['params'] { | ||
return { | ||
sender, | ||
receiver, | ||
|
@@ -86,7 +67,7 @@ export async function fetchTokenWithdrawalsFromEventLogsSequentially({ | |
} | ||
|
||
// for sanitizing, adding queries and incrementing priority | ||
function addQuery(params: TokenWithdrawalQuery['params']) { | ||
function addQuery(params: FetchTokenWithdrawalsFromEventLogsQuery['params']) { | ||
const gateways = params.l2GatewayAddresses ?? [] | ||
const gatewaysSanitized = gateways.filter(g => g !== constants.AddressZero) | ||
|
||
|
@@ -100,22 +81,9 @@ export async function fetchTokenWithdrawalsFromEventLogsSequentially({ | |
}) | ||
} | ||
|
||
const gateways = await getGateways(provider) | ||
const senderNonce = await backOff(() => getNonce(sender, { provider })) | ||
|
||
// sender queries; only add if nonce > 0 | ||
if (senderNonce > 0) { | ||
addQuery(buildQueryParams({ sender, gateways: [gateways.standardGateway] })) | ||
addQuery(buildQueryParams({ sender, gateways: [gateways.wethGateway] })) | ||
addQuery(buildQueryParams({ sender, gateways: [gateways.customGateway] })) | ||
addQuery(buildQueryParams({ sender, gateways: gateways.otherGateways })) | ||
} | ||
|
||
// receiver queries | ||
addQuery(buildQueryParams({ receiver, gateways: [gateways.standardGateway] })) | ||
addQuery(buildQueryParams({ receiver, gateways: [gateways.wethGateway] })) | ||
addQuery(buildQueryParams({ receiver, gateways: [gateways.customGateway] })) | ||
addQuery(buildQueryParams({ receiver, gateways: gateways.otherGateways })) | ||
Comment on lines
-103
to
-118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. baiscally, all this was moved to |
||
queriesProp.forEach(query => { | ||
addQuery(buildQueryParams(query)) | ||
}) | ||
|
||
// for iterating through all priorities in the while loop below | ||
let currentPriority = 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused import