Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fionnachan committed Dec 18, 2024
1 parent 21761e5 commit 5239c68
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import { BigNumber } from 'ethers'
import { BlockTag } from '@ethersproject/providers'
import { EthDepositMessage, getArbitrumNetworks } from '@arbitrum/sdk'

import { getParentTxReceipt } from '../helpers'
import { getProviderForChainId } from '@/token-bridge-sdk/utils'
import { normalizeTimestamp } from '../../../state/app/utils'

export type FetchDepositTxFromEventLogResult = {
receiver: string
sender: string
timestamp: string
transactionHash: string
type: 'EthDeposit' | 'TokenDeposit'
isClassic: boolean
id: string
ethValue: string
tokenAmount?: string
blockCreatedAt: string
l1Token?: {
symbol: string
decimals: number
id: string
name: string
registeredAtBlock: string
}
}

/**
* Get event logs for ParentToChild transactions.
Expand All @@ -19,97 +42,66 @@ import { getParentTxReceipt } from '../helpers'
* @returns Any classic and nitro events that match the provided filters.
*/
export async function getParentToChildEventsByTxHash(
childChainId: number,
parentChainId: number,
filter: { fromBlock: BlockTag; toBlock: BlockTag },
position?: BigNumber,
destination?: string,
txHash?: string,
indexInBatch?: BigNumber
): Promise<(ParentToChildTransactionEvent & { transactionHash: string })[]> {
): Promise<FetchDepositTxFromEventLogResult[]> {
if (!txHash) {
return []
}
const getParentTxReceiptResult = await getParentTxReceipt(
txHash,
childChainId
)
const parentTxReceipt = await getParentTxReceipt(txHash, parentChainId)

const childChain = await getArbitrumNetwork(childProvider)
const childNitroGenesisBlock = getNitroGenesisBlock(childChain)

const inClassicRange = (blockTag: BlockTag, nitroGenBlock: number) => {
if (typeof blockTag === 'string') {
// taking classic of "earliest", "latest", "earliest" and the nitro gen block
// yields 0, nitro gen, nitro gen since the classic range is always between 0 and nitro gen

switch (blockTag) {
case 'earliest':
return 0
case 'latest':
return nitroGenBlock
case 'pending':
return nitroGenBlock
default:
throw new Error(`Unrecognised block tag. ${blockTag}`)
}
}
return Math.min(blockTag, nitroGenBlock)
const parentProvider = getProviderForChainId(parentChainId)

if (!parentTxReceipt) {
return []
}

const inNitroRange = (blockTag: BlockTag, nitroGenBlock: number) => {
// taking nitro range of "earliest", "latest", "earliest" and the nitro gen block
// yields nitro gen, latest, pending since the nitro range is always between nitro gen and latest/pending

if (typeof blockTag === 'string') {
switch (blockTag) {
case 'earliest':
return nitroGenBlock
case 'latest':
return 'latest'
case 'pending':
return 'pending'
default:
throw new Error(`Unrecognised block tag. ${blockTag}`)
}
}
// to test for child chain, filter chains that has this parent chain id as parent
// and then loop through it.............
const childChains = getArbitrumNetworks()
.filter(childChain => childChain.parentChainId === parentChainId)
.map(network => network.chainId)

return Math.max(blockTag, nitroGenBlock)
}
parentTxReceipt.

// only fetch nitro events after the genesis block
const classicFilter = {
fromBlock: inClassicRange(filter.fromBlock, childNitroGenesisBlock),
toBlock: inClassicRange(filter.toBlock, childNitroGenesisBlock)
}
const logQueries = []
if (classicFilter.fromBlock !== classicFilter.toBlock) {
logQueries.push(
classic.ChildToParentMessageClassic.getChildToParentEvents(
childProvider,
classicFilter,
position,
destination,
hash,
indexInBatch
)
)
}
const parentToChildMessages = isClassic
? await parentTxReceipt.getParentToChildMessagesClassic(childProvider)
: await parentTxReceipt.getParentToChildMessages(childProvider)

const nitroFilter = {
fromBlock: inNitroRange(filter.fromBlock, childNitroGenesisBlock),
toBlock: inNitroRange(filter.toBlock, childNitroGenesisBlock)
}
if (nitroFilter.fromBlock !== nitroFilter.toBlock) {
logQueries.push(
nitro.ChildToParentMessageNitro.getChildToParentEvents(
childProvider,
nitroFilter,
position,
destination,
hash
)
)
const parentToChildMessageReader = parentToChildMessages[0]

if (!parentToChildMessageReader) {
return []
}

return (await Promise.all(logQueries)).flat(1)
const ethDeposits = await parentTxReceipt.getEthDeposits(childProvider)

const timestamp = normalizeTimestamp(
(await parentProvider.getBlock(parentTxReceipt.blockNumber)).timestamp
)

const ethTransactions: FetchDepositTxFromEventLogResult[] = ethDeposits.map(
async depositMessage => {
const childProvider = getProviderForChainId(depositMessage.childChainId)

const isClassic = await parentTxReceipt.isClassic(childProvider)

return {
receiver: depositMessage.to,
sender: depositMessage.from,
timestamp,
transactionHash: depositMessage.
type: 'EthDeposit',
isClassic,
ethValue: depositMessage.value.toString(),
blockCreatedAt: parentTxReceipt.blockNumber.toString()
}
}
)

return transaction
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const getParentToChildMessagesAndDepositMessages = async (
parentChainId: number
): Promise<ParentToChildMessagesAndDepositMessages> => {
try {
const childNetworks = getArbitrumNetworks().map(network => network.chainId)
const childNetworks = getArbitrumNetworks()
.filter(childChain => childChain.parentChainId === parentChainId)
.map(network => network.chainId)
const messagesPromises = childNetworks.map(async childChainId => {
// TODO: error handle
const childNetwork = await getArbitrumNetwork(childChainId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { BlockTag, Provider } from '@ethersproject/providers'
import { getParentToChildMessagesAndDepositMessages } from '../../components/TransactionHistory/TransactionHistoryTxHashSearch/getParentToChildMessagesAndDepositMessages'
import { ReceiptState } from '../../components/TransactionHistory/TransactionHistoryTxHashSearch/helpers'

export async function fetchEthDepositsFromEventLogs({
sender,
Expand Down Expand Up @@ -37,42 +39,7 @@ export async function fetchEthDepositsFromEventLogs({
}

if (typeof getParentTxReceiptResult === 'undefined') {
const res = await getChildToParentMessages(txHash)
const { childTxStatus, childToParentMessages } = res

// TODO: handle terminal states
if (childToParentMessages.length > 0) {
return {
...defaultReturn,
parentTxReceipt: parentTxReceiptAndChainId?.parentTxReceipt,
parentChainId: parentTxReceiptAndChainId?.parentChainId,
txHashState: ReceiptState.MESSAGES_FOUND,
l2ToL1MessagesToShow: childToParentMessages
}
}
if (childTxStatus === ChildTxStatus.SUCCESS) {
return {
...defaultReturn,
parentTxReceipt: parentTxReceiptAndChainId?.parentTxReceipt,
parentChainId: parentTxReceiptAndChainId?.parentChainId,
txHashState: ReceiptState.NO_L2_L1_MESSAGES
}
}
if (childTxStatus === ChildTxStatus.FAILURE) {
return {
...defaultReturn,
parentTxReceipt: parentTxReceiptAndChainId?.parentTxReceipt,
parentChainId: parentTxReceiptAndChainId?.parentChainId,
txHashState: ReceiptState.L2_FAILED
}
}

return {
...defaultReturn,
parentTxReceipt: parentTxReceiptAndChainId?.parentTxReceipt,
parentChainId: parentTxReceiptAndChainId?.parentChainId,
txHashState: ReceiptState.NOT_FOUND
}
return undefined
}

const { parentTxReceipt: _parentTxReceipt, parentChainId } =
Expand Down

0 comments on commit 5239c68

Please sign in to comment.