Skip to content

Commit

Permalink
Merge pull request #206 from G7DAO/feat/network-toggle
Browse files Browse the repository at this point in the history
getInfo() for both withdraw and deposit
  • Loading branch information
elclandestin0 authored Nov 25, 2024
2 parents 6ef6ba4 + dd9f3e9 commit 6a10317
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions webapps/world-builder-dashboard/src/hooks/useBridgeTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const useBridgeTransfer = () => {
? 1 * 60 * 1000
: false,
refetchOnWindowFocus: false,
enabled: !!txRecord,
enabled: !!txRecord
// retryDelay: (attempt) => Math.min(1000 * 2 ** attempt, 2000)
}
)
Expand Down Expand Up @@ -221,32 +221,30 @@ export const useBridgeTransfer = () => {
const destinationRpc = getNetworks(selectedNetworkType)?.find((n) => n.chainId === destinationChainId)?.rpcs[0]
const originRpc = getNetworks(selectedNetworkType)?.find((n) => n.chainId === originChainId)?.rpcs[0]
const storageKey = `transaction-inputs-${connectedAccount}`

// Retrieve cached transaction inputs from localStorage
const getCachedTransactionInputs = () => {
const cachedData = localStorage.getItem(storageKey)
if (!cachedData) return null

const cachedTransactions = JSON.parse(cachedData)

// Return the specific transaction input based on txHash
return cachedTransactions.find((input: any) => input.txHash === txHash) || null
}

// Save transaction inputs to localStorage as an array
const saveTransactionInputsToCache = (newInput: any) => {
const cachedData = localStorage.getItem(storageKey)
const cachedTransactions = cachedData ? JSON.parse(cachedData) : []

const updatedTransactions = cachedTransactions.some((input: any) => input.txHash === newInput.txHash)
? cachedTransactions.map((input: any) =>
input.txHash === newInput.txHash ? { ...input, ...newInput } : input
)
? cachedTransactions.map((input: any) => (input.txHash === newInput.txHash ? { ...input, ...newInput } : input))
: [...cachedTransactions, newInput]

localStorage.setItem(storageKey, JSON.stringify(updatedTransactions))
}

// Use React Query to fetch or cache transaction inputs
return useQuery(
['transactionInputs', txHash],
Expand All @@ -256,22 +254,23 @@ export const useBridgeTransfer = () => {
if (cachedTransactionInputs) {
return cachedTransactionInputs
}

// Otherwise, fetch transaction inputs from the bridge transfer instance
const _bridgeTransfer = new BridgeTransfer({
txHash: txHash ?? '',
destinationNetworkChainId: destinationChainId ?? 0,
originNetworkChainId: originChainId ?? 0,
destinationSignerOrProviderOrRpc: destinationRpc,
originSignerOrProviderOrRpc: originRpc,
originSignerOrProviderOrRpc: originRpc
})

const transactionInputs = await _bridgeTransfer.getInfo()

saveTransactionInputsToCache({
...transactionInputs,
txHash
})


const transactionInputs = isDeposit ? await _bridgeTransfer.getInfo() : await _bridgeTransfer.getTransactionInputs()

// Save the fetched transaction inputs to cache
saveTransactionInputsToCache({ txHash, ...transactionInputs })

return transactionInputs
},
{
Expand All @@ -280,7 +279,7 @@ export const useBridgeTransfer = () => {
},
staleTime: 2 * 60 * 1000, // Data is considered fresh for 2 minutes
refetchOnWindowFocus: false, // Disable refetching on window focus
enabled: !!txRecord, // Ensure the query only runs when txRecord exists
enabled: !!txRecord // Ensure the query only runs when txRecord exists
}
)
}
Expand Down

0 comments on commit 6a10317

Please sign in to comment.