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: store withdrawal statuses in local storage #2136

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions packages/arb-token-bridge-ui/src/util/CommonUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
let localStoragePromise = Promise.resolve()

export function addToLocalStorageObjectSequentially({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use it when we call multiple local storage setters in parallel, then it will work like a queue system instead of overwriting each others values

localStorageKey,
localStorageValue
}: {
localStorageKey: string
localStorageValue: Record<string, string | number | boolean>
}) {
localStoragePromise = localStoragePromise.then(() => {
const localStorageItem = localStorage.getItem(localStorageKey)

if (!localStorageItem) {
localStorage.setItem(
localStorageKey,
JSON.stringify({ ...localStorageValue })
)
return
}

localStorage.setItem(
localStorageKey,
JSON.stringify({
...JSON.parse(localStorageItem),
...localStorageValue
})
)
})
}

export function shortenAddress(address: string) {
const addressLength = address.length

Expand Down
16 changes: 14 additions & 2 deletions packages/arb-token-bridge-ui/src/util/withdrawals/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '../../hooks/arbTokenBridge.types'
import { getExecutedMessagesCacheKey } from '../../hooks/useArbTokenBridge'
import { fetchNativeCurrency } from '../../hooks/useNativeCurrency'
import { addToLocalStorageObjectSequentially } from '../CommonUtils'

/**
* `l2TxHash` exists on result from subgraph
Expand Down Expand Up @@ -107,13 +108,15 @@ export async function getOutgoingMessageState(
l2Provider: Provider,
l2ChainID: number
) {
const localStorageKey = 'arbitrum:bridge:executed-messages'

const cacheKey = getExecutedMessagesCacheKey({
event,
l2ChainId: l2ChainID
})

const executedMessagesCache = JSON.parse(
localStorage.getItem('arbitrum:bridge:executed-messages') || '{}'
localStorage.getItem(localStorageKey) || '{}'
)
if (executedMessagesCache[cacheKey]) {
return OutgoingMessageState.EXECUTED
Expand All @@ -122,7 +125,16 @@ export async function getOutgoingMessageState(
const messageReader = new ChildToParentMessageReader(l1Provider, event)

try {
return await messageReader.status(l2Provider)
const status = await messageReader.status(l2Provider)

if (status === OutgoingMessageState.EXECUTED) {
addToLocalStorageObjectSequentially({
localStorageKey,
localStorageValue: { [cacheKey]: true }
})
}

return status
} catch (error) {
return OutgoingMessageState.UNCONFIRMED
}
Expand Down
Loading