Skip to content
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

feat: cache deposit completion timestamps #2188

Draft
wants to merge 1 commit into
base: add-executed-withdrawals-to-cache
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 115 additions & 27 deletions packages/arb-token-bridge-ui/src/util/deposits/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import {
isCustomDestinationAddressTx,
normalizeTimestamp
} from '../../state/app/utils'
import { addToLocalStorageObjectSequentially } from '../CommonUtils'

type DepositCompletionTimestampsLocalStorage = { [key: string]: string }

export const updateAdditionalDepositData = async ({
depositTx,
Expand Down Expand Up @@ -252,6 +255,41 @@ const getRetryableFee = async ({
return utils.formatEther(gasUsed)
}

const depositCompletionTimestampsLocalStorageKey =
'arbitrum:bridge:deposit-completion-timestamps'

const getDepositCompletionTimestampFromCache = ({
txID,
childChainId
}: Pick<Transaction, 'txID' | 'childChainId'>): string | undefined => {
const depositCompletionTimestamps = JSON.parse(
localStorage.getItem(depositCompletionTimestampsLocalStorageKey) || '{}'
) as DepositCompletionTimestampsLocalStorage

const localStorageTransactionKey = `${childChainId}-${txID}`

return depositCompletionTimestamps[localStorageTransactionKey]
}

const setDepositCompletionTimestampFromCache = ({
txID,
childChainId,
timestamp
}: Pick<Transaction, 'txID' | 'childChainId'> & {
timestamp: string | undefined
}) => {
if (!timestamp) {
return
}

const localStorageTransactionKey = `${childChainId}-${txID}`

addToLocalStorageObjectSequentially({
localStorageKey: depositCompletionTimestampsLocalStorageKey,
localStorageValue: { [localStorageTransactionKey]: timestamp }
})
}

const updateETHDepositStatusData = async ({
depositTx,
ethDepositMessage,
Expand All @@ -264,23 +302,39 @@ const updateETHDepositStatusData = async ({
childProvider: Provider
}): Promise<Transaction> => {
// from the eth-deposit-message, extract more things like retryableCreationTxID, status, etc

if (!ethDepositMessage) return depositTx

const status = await ethDepositMessage.status()
const timestampResolvedFromCache =
getDepositCompletionTimestampFromCache(depositTx)

const status =
typeof timestampResolvedFromCache !== 'undefined'
? EthDepositMessageStatus.DEPOSITED
: await ethDepositMessage.status()
const isDeposited = status === EthDepositMessageStatus.DEPOSITED

const retryableCreationTxID = ethDepositMessage.childTxHash

const childBlockNum = isDeposited
? (await childProvider.getTransaction(retryableCreationTxID)).blockNumber
: null
let timestampResolved

const timestampResolved = childBlockNum
? normalizeTimestamp(
(await childProvider.getBlock(childBlockNum)).timestamp
)
: null
if (typeof timestampResolvedFromCache !== 'undefined') {
timestampResolved = timestampResolvedFromCache
} else {
const childBlockNum = isDeposited
? (await childProvider.getTransaction(retryableCreationTxID)).blockNumber
: null

timestampResolved = childBlockNum
? normalizeTimestamp(
(await childProvider.getBlock(childBlockNum)).timestamp
)
: null

setDepositCompletionTimestampFromCache({
...depositTx,
timestamp: String(timestampResolved)
})
}

// return the data to populate on UI
const updatedDepositTx: Transaction = {
Expand Down Expand Up @@ -334,6 +388,9 @@ const updateTokenDepositStatusData = async ({

if (!parentToChildMsg) return updatedDepositTx

const timestampResolvedFromCache =
getDepositCompletionTimestampFromCache(depositTx)

// get the status data of `parentToChildMsg`, if it is redeemed - `getSuccessfulRedeem` also returns its l2TxReceipt
const res = await parentToChildMsg.getSuccessfulRedeem()

Expand All @@ -352,16 +409,30 @@ const updateTokenDepositStatusData = async ({
const isDeposited =
parentToChildMsgData.status === ParentToChildMessageStatus.REDEEMED

const childBlockNum = isDeposited
? (await childProvider.getTransaction(parentToChildMsg.retryableCreationId))
.blockNumber
: null

const timestampResolved = childBlockNum
? normalizeTimestamp(
(await childProvider.getBlock(childBlockNum)).timestamp
)
: null
let timestampResolved

if (typeof timestampResolvedFromCache !== 'undefined') {
timestampResolved = timestampResolvedFromCache
} else {
const childBlockNum = isDeposited
? (
await childProvider.getTransaction(
parentToChildMsg.retryableCreationId
)
).blockNumber
: null

timestampResolved = childBlockNum
? normalizeTimestamp(
(await childProvider.getBlock(childBlockNum)).timestamp
)
: null

setDepositCompletionTimestampFromCache({
...depositTx,
timestamp: String(timestampResolved)
})
}

const completeDepositTx: Transaction = {
...updatedDepositTx,
Expand Down Expand Up @@ -394,7 +465,13 @@ const updateClassicDepositStatusData = async ({
timestampCreated
}

const status = await parentToChildMsg.status()
const timestampResolvedFromCache =
getDepositCompletionTimestampFromCache(depositTx)

const status =
typeof timestampResolvedFromCache !== 'undefined'
? ParentToChildMessageStatus.REDEEMED
: await parentToChildMsg.status()

const isCompletedEthDeposit =
!isRetryableDeposit &&
Expand All @@ -419,13 +496,24 @@ const updateClassicDepositStatusData = async ({
retryableCreationTxID: parentToChildMsg.retryableCreationId
}

const l2BlockNum = childTxId
? (await childProvider.getTransaction(childTxId)).blockNumber
: null
let timestampResolved

if (typeof timestampResolvedFromCache !== 'undefined') {
timestampResolved = timestampResolvedFromCache
} else {
const l2BlockNum = childTxId
? (await childProvider.getTransaction(childTxId)).blockNumber
: null

const timestampResolved = l2BlockNum
? normalizeTimestamp((await childProvider.getBlock(l2BlockNum)).timestamp)
: null
timestampResolved = l2BlockNum
? normalizeTimestamp((await childProvider.getBlock(l2BlockNum)).timestamp)
: null

setDepositCompletionTimestampFromCache({
...depositTx,
timestamp: String(timestampResolved)
})
}

const completeDepositTx: Transaction = {
...updatedDepositTx,
Expand Down
Loading