|
| 1 | +/* |
| 2 | +This file contains the logic for checking retryables in the given block range for the chain |
| 3 | +*/ |
| 4 | + |
| 5 | +import { providers } from 'ethers' |
| 6 | +import { |
| 7 | + ParentTransactionReceipt, |
| 8 | + ParentToChildMessageStatus, |
| 9 | +} from '@arbitrum/sdk' |
| 10 | +import { ChildNetwork } from '../../utils' |
| 11 | +import { |
| 12 | + getMessageDeliveredEventData, |
| 13 | + getDepositInitiatedLogs, |
| 14 | +} from './depositEventFetcher' |
| 15 | +import { |
| 16 | + getParentChainRetryableReport, |
| 17 | + getChildChainRetryableReport, |
| 18 | +} from './reportGenerator' |
| 19 | +import { getExplorerUrlPrefixes } from '../../utils' |
| 20 | +import { OnFailedRetryableFound } from './types' |
| 21 | +import { getTokenDepositData } from './tokenDataFetcher' |
| 22 | + |
| 23 | +export const checkRetryables = async ( |
| 24 | + parentChainProvider: providers.Provider, |
| 25 | + childChainProvider: providers.Provider, |
| 26 | + childChain: ChildNetwork, |
| 27 | + bridgeAddress: string, |
| 28 | + fromBlock: number, |
| 29 | + toBlock: number, |
| 30 | + enableAlerting: boolean, |
| 31 | + onFailedRetryableFound?: OnFailedRetryableFound |
| 32 | +): Promise<boolean> => { |
| 33 | + let retryablesFound = false |
| 34 | + |
| 35 | + const messageDeliveredLogs = await getMessageDeliveredEventData( |
| 36 | + bridgeAddress, |
| 37 | + { fromBlock, toBlock }, |
| 38 | + parentChainProvider |
| 39 | + ) |
| 40 | + |
| 41 | + // used for finding the token-details associated with a deposit, if any |
| 42 | + const depositsInitiatedLogs = await getDepositInitiatedLogs({ |
| 43 | + fromBlock, |
| 44 | + toBlock, |
| 45 | + parentChainProvider, |
| 46 | + gatewayAddresses: { |
| 47 | + parentErc20Gateway: childChain.tokenBridge!.parentErc20Gateway, |
| 48 | + parentCustomGateway: childChain.tokenBridge!.parentCustomGateway, |
| 49 | + parentWethGateway: childChain.tokenBridge!.parentWethGateway, |
| 50 | + }, |
| 51 | + }) |
| 52 | + |
| 53 | + const uniqueTxHashes = new Set<string>() |
| 54 | + for (let messageDeliveredLog of messageDeliveredLogs) { |
| 55 | + const { transactionHash: parentTxHash } = messageDeliveredLog |
| 56 | + uniqueTxHashes.add(parentTxHash) |
| 57 | + } |
| 58 | + |
| 59 | + const { PARENT_CHAIN_TX_PREFIX, CHILD_CHAIN_TX_PREFIX } = |
| 60 | + getExplorerUrlPrefixes(childChain) |
| 61 | + |
| 62 | + // for each parent-chain-transaction found, extract the Retryables thus created by it |
| 63 | + for (const parentTxHash of uniqueTxHashes) { |
| 64 | + const parentTxReceipt = await parentChainProvider.getTransactionReceipt( |
| 65 | + parentTxHash |
| 66 | + ) |
| 67 | + const arbParentTxReceipt = new ParentTransactionReceipt(parentTxReceipt) |
| 68 | + const retryables = await arbParentTxReceipt.getParentToChildMessages( |
| 69 | + childChainProvider |
| 70 | + ) |
| 71 | + |
| 72 | + if (retryables.length > 0) { |
| 73 | + console.log( |
| 74 | + `${retryables.length} retryable${ |
| 75 | + retryables.length === 1 ? '' : 's' |
| 76 | + } found for ${ |
| 77 | + childChain.name |
| 78 | + } chain. Checking their status:\n\nParentChainTxHash: ${ |
| 79 | + PARENT_CHAIN_TX_PREFIX + parentTxHash |
| 80 | + }` |
| 81 | + ) |
| 82 | + console.log('----------------------------------------------------------') |
| 83 | + |
| 84 | + // for each retryable, extract the detail for it's status / redemption |
| 85 | + for (let msgIndex = 0; msgIndex < retryables.length; msgIndex++) { |
| 86 | + const retryableMessage = retryables[msgIndex] |
| 87 | + const retryableTicketId = retryableMessage.retryableCreationId |
| 88 | + let status = await retryableMessage.status() |
| 89 | + |
| 90 | + // if a Retryable is not in a successful state, extract it's details |
| 91 | + if (status !== ParentToChildMessageStatus.REDEEMED) { |
| 92 | + const childChainTx = await childChainProvider.getTransaction( |
| 93 | + retryableTicketId |
| 94 | + ) |
| 95 | + const childChainTxReceipt = |
| 96 | + await childChainProvider.getTransactionReceipt( |
| 97 | + retryableMessage.retryableCreationId |
| 98 | + ) |
| 99 | + |
| 100 | + if (!childChainTxReceipt) { |
| 101 | + // if child-chain tx is very recent, the tx receipt might not be found yet |
| 102 | + // if not handled, this will result in `undefined` error while trying to extract retryable details |
| 103 | + console.log( |
| 104 | + `${msgIndex + 1}. ${ |
| 105 | + ParentToChildMessageStatus[status] |
| 106 | + }:\nChildChainTxHash: ${ |
| 107 | + CHILD_CHAIN_TX_PREFIX + retryableTicketId |
| 108 | + } (Receipt not found yet)` |
| 109 | + ) |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + const parentChainRetryableReport = getParentChainRetryableReport( |
| 114 | + arbParentTxReceipt, |
| 115 | + retryableMessage |
| 116 | + ) |
| 117 | + const childChainRetryableReport = await getChildChainRetryableReport({ |
| 118 | + retryableMessage, |
| 119 | + childChainTx, |
| 120 | + childChainTxReceipt, |
| 121 | + childChainProvider, |
| 122 | + }) |
| 123 | + const tokenDepositData = await getTokenDepositData({ |
| 124 | + childChainTx, |
| 125 | + retryableMessage, |
| 126 | + arbParentTxReceipt, |
| 127 | + depositsInitiatedLogs, |
| 128 | + parentChainProvider, |
| 129 | + }) |
| 130 | + |
| 131 | + // Call the provided callback if it exists |
| 132 | + if (enableAlerting && onFailedRetryableFound) { |
| 133 | + await onFailedRetryableFound({ |
| 134 | + parentChainRetryableReport, |
| 135 | + childChainRetryableReport, |
| 136 | + tokenDepositData, |
| 137 | + childChain, |
| 138 | + }) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // format the result message |
| 143 | + console.log( |
| 144 | + `${msgIndex + 1}. ${ |
| 145 | + ParentToChildMessageStatus[status] |
| 146 | + }:\nChildChainTxHash: ${CHILD_CHAIN_TX_PREFIX + retryableTicketId}` |
| 147 | + ) |
| 148 | + console.log( |
| 149 | + '----------------------------------------------------------' |
| 150 | + ) |
| 151 | + } |
| 152 | + retryablesFound = true // Set to true if retryables are found |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return retryablesFound |
| 157 | +} |
0 commit comments