Skip to content

Commit

Permalink
dev: review comms
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanshparashar committed Jan 21, 2025
1 parent ae5c717 commit d7e0dfc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
import { BigNumber } from 'ethers'
import { ChainId } from '../types/ChainId'
import { ConnectionInfo } from 'ethers/lib/utils.js'

type CachedReceipts = Record<string, TransactionReceipt>
import { isNetwork } from '../util/networks'

interface Storage {
getItem(key: string): string | null
Expand Down Expand Up @@ -69,7 +68,7 @@ const txReceiptFromString = (stringified: string): TransactionReceipt => {
}

/**
* Checks if a transaction receipt can be cached based on its status and confirmations.
* Checks if a transaction receipt can be cached based on its chain and confirmations.
* @param chainId - The ID of the chain.
* @param txReceipt - The transaction receipt to check.
* @returns True if the receipt can be cached, false otherwise.
Expand All @@ -80,8 +79,8 @@ const shouldCacheTxReceipt = (
): boolean => {
if (!enableCaching) return false

// Don't cache failed transactions
if (txReceipt.status === 0) {
// for now, only enable caching for testnets,
if (!isNetwork(chainId).isTestnet) {
return false
}

Expand All @@ -104,41 +103,26 @@ function getTxReceiptFromCache(
) {
if (!enableCaching) return undefined

const cachedReceipts = storage.getItem(localStorageKey)
if (!cachedReceipts) return undefined

const allReceipts: CachedReceipts = {}
const parsedReceipts = JSON.parse(cachedReceipts)
const cachedReceipts = JSON.parse(storage.getItem(localStorageKey) || '{}')
const receipt = cachedReceipts[getCacheKey(chainId, txHash)]

Object.entries(parsedReceipts).forEach(([key, receipt]) => {
allReceipts[key] = txReceiptFromString(JSON.stringify(receipt))
})
if (!receipt) return undefined

return allReceipts[getCacheKey(chainId, txHash)]
return txReceiptFromString(JSON.stringify(receipt))
}

function addTxReceiptToCache(
storage: Storage,
chainId: number,
txReceipt: TransactionReceipt
) {
const cachedReceipts = storage.getItem(localStorageKey)
const allReceipts: CachedReceipts = {}

if (cachedReceipts) {
const parsedReceipts = JSON.parse(cachedReceipts)
Object.entries(parsedReceipts).forEach(([key, receipt]) => {
allReceipts[key] = JSON.parse(
txReceiptToString(receipt as TransactionReceipt)
)
})
}
const cachedReceipts = JSON.parse(storage.getItem(localStorageKey) || '{}')

const key = getCacheKey(chainId, txReceipt.transactionHash)
storage.setItem(
localStorageKey,
JSON.stringify({
...allReceipts,
...cachedReceipts,
[key]: JSON.parse(txReceiptToString(txReceipt))
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,5 @@ describe('EnhancedProvider', () => {
const cache = storage.getItem('arbitrum:bridge:tx-receipts-cache')
expect(cache).toBeTruthy()
})

it('should not cache failed transactions', async () => {
const mockReceipt = {
...testTxReceipt,
status: 0 // Failed transaction
}

// Mock the parent class's getTransactionReceipt
jest
.spyOn(StaticJsonRpcProvider.prototype, 'getTransactionReceipt')
.mockResolvedValue(mockReceipt)

const receipt = await provider.getTransactionReceipt(
testTxReceipt.transactionHash
)
expect(receipt).toBeTruthy()
expect(receipt.status).toBe(0)

const cache = storage.getItem('arbitrum:bridge:tx-receipts-cache')
expect(cache).toBeFalsy()
})
})
})

0 comments on commit d7e0dfc

Please sign in to comment.