From f675a5699386c9fe28c92a0e06cc688acd87ac27 Mon Sep 17 00:00:00 2001 From: schmanu Date: Tue, 14 Nov 2023 09:59:22 +0100 Subject: [PATCH] fix: fix and optimise fetching the recovery state --- src/services/recovery/recovery-state.ts | 43 ++++++++++++++++--------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/services/recovery/recovery-state.ts b/src/services/recovery/recovery-state.ts index 6de6791e2a..07108ef81b 100644 --- a/src/services/recovery/recovery-state.ts +++ b/src/services/recovery/recovery-state.ts @@ -7,17 +7,10 @@ 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, - txNonce: BigNumber, -): Array => { - // Only queued transactions with queueNonce >= current txNonce - return transactionsAdded.filter(({ args }) => args.queueNonce.gte(txNonce)) -} - export const _getRecoveryQueueItem = async ( transactionAdded: TransactionAddedEvent, txCooldown: BigNumber, @@ -48,7 +41,7 @@ export const _getSafeCreationReceipt = memoize( safeAddress: string provider: JsonRpcProvider }): Promise => { - const url = `${transactionService}/v1/${safeAddress}/creation/` + const url = `${transactionService}api/v1/safes/${safeAddress}/creation/` const { transactionHash } = await fetch(url).then((res) => { if (res.ok && res.status === 200) { @@ -63,6 +56,28 @@ export const _getSafeCreationReceipt = memoize( ({ transactionService, safeAddress }) => transactionService + safeAddress, ) +const queryAddedTransactions = async ( + delayModifier: Delay, + queueNonce: BigNumber, + txNonce: BigNumber, + startBlockNumber: number, +) => { + if (queueNonce.eq(txNonce)) { + // There are no queued txs + return [] + } + + const transactionAddedFilter = delayModifier.filters.TransactionAdded() + + const diff = queueNonce.sub(txNonce).toNumber() + if (transactionAddedFilter.topics) { + const queueNonceFilter = Array.from({ length: diff }, (_, idx) => hexZeroPad(txNonce.add(idx).toHexString(), 32)) + transactionAddedFilter.topics[1] = queueNonceFilter + } + + return await delayModifier.queryFilter(transactionAddedFilter, startBlockNumber, 'latest') +} + export const getRecoveryState = async ({ delayModifier, ...rest @@ -72,21 +87,17 @@ export const getRecoveryState = async ({ safeAddress: string provider: JsonRpcProvider }): Promise => { - const { blockHash } = await _getSafeCreationReceipt(rest) - - const transactionAddedFilter = delayModifier.filters.TransactionAdded() + const { blockNumber } = await _getSafeCreationReceipt(rest) - 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, blockNumber) const queue = await Promise.all( queuedTransactionsAdded.map((transactionAdded) =>