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: new tx history - cache tx details #1371

Merged
merged 94 commits into from
Dec 21, 2023

Conversation

brtkx
Copy link
Contributor

@brtkx brtkx commented Dec 18, 2023

Previous PR: #1369 | Next PR: #1372

  • Stores deposits from event logs in local storage (we currently don't fetch those)
  • Stores parent chain's tx ID and timestamp when claiming withdrawals

? 'subgraph'
: 'event_logs',
timestampCreated: String(dayjs().valueOf() / 1_000),
nonce: tx.nonce
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Requested by @fionnachan, adding nonce so that we can fix #986 in the future

@@ -109,12 +112,17 @@ export function useClaimWithdrawal(): UseClaimWithdrawalResult {
}

const isSuccess = (res as ContractReceipt).status === 1
const txHash = (res as ContractReceipt).transactionHash
Copy link
Member

Choose a reason for hiding this comment

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

Read timestamp from here

Comment on lines 179 to 183
return (
cachedTx.txID === tx.txID &&
cachedTx.parentChainId === tx.parentChainId &&
cachedTx.childChainId === tx.childChainId
)
Copy link
Member

Choose a reason for hiding this comment

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

Use isSameTransaction after reducing the type to only these 3 params

@@ -443,6 +471,30 @@ export const useArbTokenBridge = (
childChainId: Number(l2NetworkID)
})

addDepositToCache({
sender: walletAddress,
destination: walletAddress,
Copy link
Member

Choose a reason for hiding this comment

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

Can be custom

assetType: AssetType.ERC20,
l1NetworkID,
l2NetworkID,
value: utils.formatUnits(amount, nativeCurrency.decimals),
Copy link
Member

Choose a reason for hiding this comment

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

should be token decimals

})
? 'subgraph'
: 'event_logs',
timestampCreated: String(dayjs().valueOf() / 1_000),
Copy link
Member

Choose a reason for hiding this comment

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

just use block timestamp

})
? 'subgraph'
: 'event_logs',
timestampCreated: String(dayjs().valueOf() / 1_000),
Copy link
Member

Choose a reason for hiding this comment

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

just use block timestamp

childChainId: Number(l2NetworkID),
direction: 'deposit',
type: 'deposit-l1',
source: subgraphExistsForChainPair({
Copy link
Member

Choose a reason for hiding this comment

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

Let's add another value local_storage_cache

Comment on lines 292 to 293
(depositsData || []).flat(),
...getDepositsWithoutStatusesFromCache()
Copy link
Member

Choose a reason for hiding this comment

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

Switch places

numberOfDays: oldestTransactionDaysAgo
},
loading: fetching,
completed: transactions.length === data.length,
completed: dedupedTransactions.length >= data.length,
Copy link
Member

Choose a reason for hiding this comment

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

Let's throw if deduped.length > data.length, and return to the previous implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved deduping to getting txs without statuses, I don't think we need to throw anymore

Comment on lines 322 to 335
const dedupedTransactions = useMemo(
() =>
Array.from(
new Map(
transactions.map(tx => [
`${tx.parentChainId}-${tx.childChainId}-${getTxIdFromTransaction(
tx
)}}`,
tx
])
).values()
).sort(sortByTimestampDescending),
[transactions]
)
Copy link
Member

Choose a reason for hiding this comment

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

use Set instead?

Suggested change
const dedupedTransactions = useMemo(
() =>
Array.from(
new Map(
transactions.map(tx => [
`${tx.parentChainId}-${tx.childChainId}-${getTxIdFromTransaction(
tx
)}}`,
tx
])
).values()
).sort(sortByTimestampDescending),
[transactions]
)
const dedupedTransactions = useMemo(
() => Array.from(new Set(transactions)).sort(sortByTimestampDescending),
[transactions]
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we may want to stick with Map. Set dedupes by reference and we have unrelated objects with the same data so we could end up with duplicates.

Copy link
Member

Choose a reason for hiding this comment

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

as discussed on Slack, we should keep the Map implementation but leave a comment above to ensure it doesn't accidentally get refactored to Set

import { getWagmiChain } from '../../util/wagmi/getWagmiChain'
import { getL1ToL2MessageDataFromL1TxHash } from '../../util/deposits/helpers'
import { AssetType } from '../../hooks/arbTokenBridge.types'
import { getDepositStatus } from '../../state/app/utils'
import { getBlockBeforeConfirmation } from '../../state/cctpState'
import { getAttestationHashAndMessageFromReceipt } from '../../util/cctp/getAttestationHashAndMessageFromReceipt'

const CLAIM_PARENT_CHAIN_DETAILS_LOCAL_STORAGE_KEY =
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Can we name it better? Wasn't clear at first what it meant 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Something like PARENT_CHAIN_TX_DETAILS_OF_CLAIM_TX ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

* @param {MergedTransaction} tx - Transaction that initiated the withdrawal (child chain transaction).
* @param {string} parentChainTxId - Transaction ID of the claim transaction (parent chain transaction ID).
*/
export function setWithdrawalClaimParentChainTxDetails(
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: setParentChainTxDetailsOfWithdrawalClaimTx and similarly in other function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

@@ -188,6 +189,9 @@ export const useArbTokenBridge = (
}

const ethBridger = await EthBridger.fromProvider(l2.provider)
const parentChainBlockTimestamp = Math.floor(
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is Math.floor needed here since it will never be a decimal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm I was getting a decimal value for some reason before but might have been a mistake on my part, removing math floor

Array.from(
new Map(
transactions.map(tx => [
`${tx.parentChainId}-${tx.childChainId}-${getTxIdFromTransaction(
Copy link
Contributor

Choose a reason for hiding this comment

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

Dumb Q: while setting the tx-id key, have we thought about upper-lowercase considerations that might mismatch same keys while checking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fair point, added toLowerCase

@@ -15,7 +15,7 @@ export type FetchWithdrawalsFromSubgraphResult = {
l2TxHash: string
l2BlockNum: string
direction: 'deposit' | 'withdrawal'
source: 'subgraph' | 'event_logs'
source: 'subgraph' | 'event_logs' | 'local_storage_cache'
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: Hmm not sure why we have the 3 different sources when the type is FetchWithdrawalsFromSubgraphResult - cleary a result from subgraph.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you're right, updated to 'subgraph' only

@brtkx brtkx merged commit d5a768b into new-tx-history-old-ui Dec 21, 2023
3 checks passed
@brtkx brtkx deleted the new-tx-history-tx-cache branch December 21, 2023 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants