-
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: load withdrawal tx history by priority #2128
Merged
Merged
Changes from 12 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4588f8c
don't fetch withdrawals if nonce is zero
spsjvc fb8e92f
fix comments
spsjvc 57d4a29
rename l2Provider to provider
spsjvc b153022
wip
spsjvc 565ad23
reduce diff
spsjvc edcd366
remove duplicate check
spsjvc 5992894
oops
spsjvc fc97519
Merge branch 'master' into perf-sequential-tx-history
spsjvc e9c8f56
dupe check
spsjvc b158c14
add backOff
spsjvc a33a55f
clean up
spsjvc 92e66ec
clean up
spsjvc 924e0a4
use custom custom gateways
spsjvc e15683a
clean up
spsjvc 75d9823
clean up
spsjvc 3941f7b
clean up more
spsjvc 6067f9c
clean up
spsjvc 43a9f82
clean up
spsjvc aeaa9a9
clean up more
spsjvc 33b2240
revert exponential back off for now
spsjvc b45452b
wrap up
spsjvc 57ef6b9
increase delay
spsjvc 7bcda1f
Merge branch 'master' into perf-sequential-tx-history
spsjvc 82c13c1
use awaited
spsjvc 751be46
move file
spsjvc 8ec6b17
fetch eth and token withdrawals sequentially
spsjvc a7deab2
Merge branch 'master' into perf-sequential-tx-history
spsjvc 5cbb81c
Merge branch 'master' into perf-sequential-tx-history
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 |
---|---|---|
|
@@ -2,8 +2,6 @@ import { Provider, BlockTag } from '@ethersproject/providers' | |
import { Erc20Bridger, EventArgs } from '@arbitrum/sdk' | ||
import { WithdrawalInitiatedEvent } from '@arbitrum/sdk/dist/lib/abi/L2ArbitrumGateway' | ||
|
||
import { getNonce } from '../AddressUtils' | ||
|
||
function dedupeEvents( | ||
events: (EventArgs<WithdrawalInitiatedEvent> & { | ||
txHash: string | ||
|
@@ -12,6 +10,15 @@ function dedupeEvents( | |
return [...new Map(events.map(item => [item.txHash, item])).values()] | ||
} | ||
|
||
export type FetchTokenWithdrawalsFromEventLogsParams = { | ||
sender?: string | ||
receiver?: string | ||
fromBlock: BlockTag | ||
toBlock: BlockTag | ||
l2Provider: Provider | ||
l2GatewayAddresses?: string[] | ||
} | ||
|
||
/** | ||
* Fetches initiated token withdrawals from event logs in range of [fromBlock, toBlock]. | ||
* | ||
|
@@ -30,22 +37,13 @@ export async function fetchTokenWithdrawalsFromEventLogs({ | |
toBlock, | ||
l2Provider, | ||
l2GatewayAddresses = [] | ||
}: { | ||
sender?: string | ||
receiver?: string | ||
fromBlock: BlockTag | ||
toBlock: BlockTag | ||
l2Provider: Provider | ||
l2GatewayAddresses?: string[] | ||
}) { | ||
}: FetchTokenWithdrawalsFromEventLogsParams) { | ||
const erc20Bridger = await Erc20Bridger.fromProvider(l2Provider) | ||
const promises: ReturnType<Erc20Bridger['getWithdrawalEvents']>[] = [] | ||
|
||
const senderNonce = await getNonce(sender, { provider: l2Provider }) | ||
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. moved to |
||
|
||
l2GatewayAddresses.forEach(gatewayAddress => { | ||
// funds sent by this address | ||
if (sender && senderNonce > 0) { | ||
if (sender) { | ||
promises.push( | ||
erc20Bridger.getWithdrawalEvents( | ||
l2Provider, | ||
|
157 changes: 157 additions & 0 deletions
157
...rb-token-bridge-ui/src/util/withdrawals/fetchTokenWithdrawalsFromEventLogsSequentially.ts
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 |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { constants } from 'ethers' | ||
import { Provider, BlockTag } from '@ethersproject/providers' | ||
import { Erc20Bridger, getArbitrumNetwork } from '@arbitrum/sdk' | ||
|
||
import { | ||
fetchTokenWithdrawalsFromEventLogs, | ||
FetchTokenWithdrawalsFromEventLogsParams | ||
} from './fetchTokenWithdrawalsFromEventLogs' | ||
import { getNonce } from '../AddressUtils' | ||
|
||
function wait(ms: number) { | ||
return new Promise(resolve => setTimeout(resolve, ms)) | ||
} | ||
|
||
type WithdrawalQuery = { | ||
params: FetchTokenWithdrawalsFromEventLogsParams | ||
priority: number | ||
} | ||
|
||
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T | ||
brtkx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type Result = UnwrapPromise<ReturnType<Erc20Bridger['getWithdrawalEvents']>> | ||
|
||
export type FetchTokenWithdrawalsFromEventLogsSequentiallyParams = { | ||
sender?: string | ||
receiver?: string | ||
provider: Provider | ||
fromBlock?: BlockTag | ||
toBlock?: BlockTag | ||
} | ||
|
||
export async function fetchTokenWithdrawalsFromEventLogsSequentially({ | ||
sender, | ||
receiver, | ||
provider, | ||
fromBlock = 0, | ||
toBlock = 'latest' | ||
}: FetchTokenWithdrawalsFromEventLogsSequentiallyParams): Promise<Result> { | ||
await wait(2000) | ||
|
||
const network = await getArbitrumNetwork(provider) | ||
const senderNonce = await getNonce(sender, { provider }) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain | ||
const standardGateway = network.tokenBridge?.childErc20Gateway! | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain | ||
const customGateway = network.tokenBridge?.childCustomGateway! | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain | ||
const wethGateway = network.tokenBridge?.childWethGateway! | ||
|
||
let prio = 1 | ||
|
||
const queries: WithdrawalQuery[] = [] | ||
|
||
if (senderNonce > 0) { | ||
queries.push({ | ||
params: { | ||
sender, | ||
fromBlock, | ||
toBlock, | ||
l2Provider: provider, | ||
l2GatewayAddresses: [standardGateway] | ||
}, | ||
priority: prio | ||
}) | ||
prio++ | ||
if (wethGateway !== constants.AddressZero) { | ||
queries.push({ | ||
params: { | ||
sender, | ||
fromBlock, | ||
toBlock: 'latest', | ||
l2Provider: provider, | ||
l2GatewayAddresses: [wethGateway] | ||
}, | ||
priority: prio | ||
}) | ||
prio++ | ||
} | ||
queries.push({ | ||
params: { | ||
sender, | ||
fromBlock, | ||
toBlock: 'latest', | ||
l2Provider: provider, | ||
l2GatewayAddresses: [customGateway] | ||
}, | ||
priority: prio | ||
}) | ||
prio++ | ||
} | ||
|
||
queries.push({ | ||
params: { | ||
receiver, | ||
fromBlock, | ||
toBlock: 'latest', | ||
l2Provider: provider, | ||
l2GatewayAddresses: [standardGateway] | ||
}, | ||
priority: prio | ||
}) | ||
prio++ | ||
if (wethGateway !== constants.AddressZero) { | ||
queries.push({ | ||
params: { | ||
receiver, | ||
fromBlock, | ||
toBlock: 'latest', | ||
l2Provider: provider, | ||
l2GatewayAddresses: [wethGateway] | ||
}, | ||
priority: prio | ||
}) | ||
prio++ | ||
} | ||
queries.push({ | ||
params: { | ||
receiver, | ||
fromBlock, | ||
toBlock: 'latest', | ||
l2Provider: provider, | ||
l2GatewayAddresses: [customGateway] | ||
}, | ||
priority: prio | ||
}) | ||
prio++ | ||
|
||
const maxPriority = queries.map(query => query.priority).sort()[ | ||
queries.length - 1 | ||
]! | ||
|
||
const result: Result = [] | ||
let currentPriority = 1 | ||
|
||
while (currentPriority <= maxPriority) { | ||
const filteredQueries = queries.filter( | ||
query => query.priority === currentPriority | ||
) | ||
|
||
const results = await Promise.all( | ||
filteredQueries.map(query => | ||
fetchTokenWithdrawalsFromEventLogs(query.params) | ||
) | ||
) | ||
|
||
results.forEach(r => { | ||
result.push(...r) | ||
}) | ||
|
||
await wait(2000) | ||
|
||
currentPriority += 1 | ||
} | ||
|
||
return result | ||
} |
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.
extracted into type
FetchTokenWithdrawalsFromEventLogsParams