-
Notifications
You must be signed in to change notification settings - Fork 445
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
fix: fix and optimise fetching the recovery state #2807
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,11 @@ import type { JsonRpcProvider } from '@ethersproject/providers' | |
import type { TransactionReceipt } from '@ethersproject/abstract-provider' | ||
|
||
import type { RecoveryQueueItem, RecoveryState } from '@/store/recoverySlice' | ||
import { hexZeroPad } from 'ethers/lib/utils' | ||
|
||
const MAX_PAGE_SIZE = 100 | ||
|
||
export const _getQueuedTransactionsAdded = ( | ||
transactionsAdded: Array<TransactionAddedEvent>, | ||
txNonce: BigNumber, | ||
): Array<TransactionAddedEvent> => { | ||
// Only queued transactions with queueNonce >= current txNonce | ||
return transactionsAdded.filter(({ args }) => args.queueNonce.gte(txNonce)) | ||
} | ||
export const normalizeTxServiceUrl = (url: string): string => (url.endsWith('/') ? url : `${url}/`) | ||
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. Nit: there is a |
||
|
||
export const _getRecoveryQueueItem = async ( | ||
transactionAdded: TransactionAddedEvent, | ||
|
@@ -48,7 +43,7 @@ export const _getSafeCreationReceipt = memoize( | |
safeAddress: string | ||
provider: JsonRpcProvider | ||
}): Promise<TransactionReceipt> => { | ||
const url = `${transactionService}/v1/${safeAddress}/creation/` | ||
const url = `${normalizeTxServiceUrl(transactionService)}api/v1/safes/${safeAddress}/creation/` | ||
|
||
const { transactionHash } = await fetch(url).then((res) => { | ||
if (res.ok && res.status === 200) { | ||
|
@@ -63,30 +58,61 @@ export const _getSafeCreationReceipt = memoize( | |
({ transactionService, safeAddress }) => transactionService + safeAddress, | ||
) | ||
|
||
const queryAddedTransactions = async ( | ||
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. Can we add a unit test for this? 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. I added more tests for it's outside function effectively also testing this logic. |
||
delayModifier: Delay, | ||
queueNonce: BigNumber, | ||
txNonce: BigNumber, | ||
transactionService: string, | ||
provider: JsonRpcProvider, | ||
safeAddress: string, | ||
) => { | ||
if (queueNonce.eq(txNonce)) { | ||
// There are no queued txs | ||
return [] | ||
} | ||
|
||
const transactionAddedFilter = delayModifier.filters.TransactionAdded() | ||
|
||
if (transactionAddedFilter.topics) { | ||
// We filter for the valid nonces while fetching the event logs. | ||
// The nonce has to be one between the current queueNonce and the txNonce. | ||
const diff = queueNonce.sub(txNonce).toNumber() | ||
const queueNonceFilter = Array.from({ length: diff }, (_, idx) => hexZeroPad(txNonce.add(idx).toHexString(), 32)) | ||
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. Can we add a comment explaining this? |
||
transactionAddedFilter.topics[1] = queueNonceFilter | ||
} | ||
|
||
const { blockNumber } = await _getSafeCreationReceipt({ transactionService, provider, safeAddress }) | ||
|
||
return await delayModifier.queryFilter(transactionAddedFilter, blockNumber, 'latest') | ||
} | ||
|
||
export const getRecoveryState = async ({ | ||
delayModifier, | ||
...rest | ||
transactionService, | ||
safeAddress, | ||
provider, | ||
}: { | ||
delayModifier: Delay | ||
transactionService: string | ||
safeAddress: string | ||
provider: JsonRpcProvider | ||
}): Promise<RecoveryState[number]> => { | ||
const { blockHash } = await _getSafeCreationReceipt(rest) | ||
|
||
const transactionAddedFilter = delayModifier.filters.TransactionAdded() | ||
|
||
const [[modules], txExpiration, txCooldown, txNonce, queueNonce, transactionsAdded] = await Promise.all([ | ||
const [[modules], txExpiration, txCooldown, txNonce, queueNonce] = await Promise.all([ | ||
delayModifier.getModulesPaginated(SENTINEL_ADDRESS, MAX_PAGE_SIZE), | ||
delayModifier.txExpiration(), | ||
delayModifier.txCooldown(), | ||
delayModifier.txNonce(), | ||
delayModifier.queueNonce(), | ||
// TODO: Improve log retrieval | ||
delayModifier.queryFilter(transactionAddedFilter, blockHash), | ||
]) | ||
|
||
const queuedTransactionsAdded = _getQueuedTransactionsAdded(transactionsAdded, txNonce) | ||
const queuedTransactionsAdded = await queryAddedTransactions( | ||
delayModifier, | ||
queueNonce, | ||
txNonce, | ||
transactionService, | ||
provider, | ||
safeAddress, | ||
) | ||
|
||
const queue = await Promise.all( | ||
queuedTransactionsAdded.map((transactionAdded) => | ||
|
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.
Let's remove the unit test for this.