From 64c38844ed15d320a93c09c3dc2a739d93040eaa Mon Sep 17 00:00:00 2001 From: Pierre-Alain Ouvrard Date: Wed, 3 Jan 2024 14:23:56 +0900 Subject: [PATCH] feat: Find finalization tx using monitoring indexer. --- .yarn/versions/17863933.yml | 9 ++++ .../src/natural-erc20/sendToAurora/index.ts | 5 +- .../src/natural-ether/sendToAurora/index.ts | 5 +- .../src/bridged-near/sendToNear/index.ts | 5 +- .../src/natural-ether/sendToNear/index.ts | 5 +- .../src/natural-erc20/sendToNear/index.ts | 5 +- packages/utils/src/findFinalizationTx.ts | 52 ++++++++++++------- packages/utils/src/index.ts | 2 +- 8 files changed, 58 insertions(+), 30 deletions(-) create mode 100644 .yarn/versions/17863933.yml diff --git a/.yarn/versions/17863933.yml b/.yarn/versions/17863933.yml new file mode 100644 index 00000000..16faf26e --- /dev/null +++ b/.yarn/versions/17863933.yml @@ -0,0 +1,9 @@ +releases: + "@near-eth/aurora-erc20": patch + "@near-eth/aurora-ether": patch + "@near-eth/aurora-nep141": patch + "@near-eth/near-ether": patch + "@near-eth/nep141-erc20": patch + "@near-eth/rainbow": patch + "@near-eth/utils": patch + rainbow-bridge-client-monorepo: patch diff --git a/packages/aurora-erc20/src/natural-erc20/sendToAurora/index.ts b/packages/aurora-erc20/src/natural-erc20/sendToAurora/index.ts index 8f3d9e02..31aaa99a 100644 --- a/packages/aurora-erc20/src/natural-erc20/sendToAurora/index.ts +++ b/packages/aurora-erc20/src/natural-erc20/sendToAurora/index.ts @@ -7,7 +7,7 @@ import { CodeResult } from 'near-api-js/lib/providers/provider' import { getEthProvider, getSignerProvider, getNearProvider, formatLargeNum, getBridgeParams } from '@near-eth/client/dist/utils' import { TransferStatus, TransactionInfo } from '@near-eth/client/dist/types' import { findReplacementTx, TxValidationError } from 'find-replacement-tx' -import { ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear } from '@near-eth/utils' +import { ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear, ExplorerIndexerResult } from '@near-eth/utils' import { getDecimals, getSymbol } from '../getMetadata' export const SOURCE_NETWORK = 'ethereum' @@ -68,7 +68,7 @@ export interface TransferOptions { maxFindEthProofInterval?: number nearClientAccount?: string auroraEvmAccount?: string - callIndexer?: (query: string) => Promise> + callIndexer?: (query: string) => Promise eventRelayerAccount?: string } @@ -696,6 +696,7 @@ export async function checkSync ( connectorAccount: options.nep141Factory ?? bridgeParams.nep141Factory, eventRelayerAccount: options.eventRelayerAccount ?? bridgeParams.eventRelayerAccount, finalizationMethod: 'deposit', + ethTxHash: lockReceipt.transactionHash, callIndexer: options.callIndexer }) let finishTime: string | undefined diff --git a/packages/aurora-ether/src/natural-ether/sendToAurora/index.ts b/packages/aurora-ether/src/natural-ether/sendToAurora/index.ts index 4e5304a2..8c518270 100644 --- a/packages/aurora-ether/src/natural-ether/sendToAurora/index.ts +++ b/packages/aurora-ether/src/natural-ether/sendToAurora/index.ts @@ -7,7 +7,7 @@ import { CodeResult } from 'near-api-js/lib/providers/provider' import { TransferStatus, TransactionInfo } from '@near-eth/client/dist/types' import { getEthProvider, getSignerProvider, getNearProvider, formatLargeNum, getBridgeParams } from '@near-eth/client/dist/utils' import { findReplacementTx, TxValidationError } from 'find-replacement-tx' -import { ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear } from '@near-eth/utils' +import { ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear, ExplorerIndexerResult } from '@near-eth/utils' export const SOURCE_NETWORK = 'ethereum' export const DESTINATION_NETWORK = 'aurora' @@ -58,7 +58,7 @@ export interface TransferOptions { nearProvider?: najProviders.Provider maxFindEthProofInterval?: number nearClientAccount?: string - callIndexer?: (query: string) => Promise> + callIndexer?: (query: string) => Promise eventRelayerAccount?: string } @@ -524,6 +524,7 @@ export async function checkSync ( connectorAccount: options.auroraEvmAccount ?? bridgeParams.auroraEvmAccount, eventRelayerAccount: options.eventRelayerAccount ?? bridgeParams.eventRelayerAccount, finalizationMethod: 'deposit', + ethTxHash: lockReceipt.transactionHash, callIndexer: options.callIndexer }) let finishTime: string | undefined diff --git a/packages/near-ether/src/bridged-near/sendToNear/index.ts b/packages/near-ether/src/bridged-near/sendToNear/index.ts index 0ff87e2f..ad9c8e1e 100644 --- a/packages/near-ether/src/bridged-near/sendToNear/index.ts +++ b/packages/near-ether/src/bridged-near/sendToNear/index.ts @@ -7,7 +7,7 @@ import { stepsFor } from '@near-eth/client/dist/i18nHelpers' import * as status from '@near-eth/client/dist/statuses' import { getEthProvider, getNearWallet, getNearProvider, formatLargeNum, getSignerProvider, getBridgeParams } from '@near-eth/client/dist/utils' import { TransferStatus, TransactionInfo } from '@near-eth/client/dist/types' -import { urlParams, ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear } from '@near-eth/utils' +import { urlParams, ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear, ExplorerIndexerResult } from '@near-eth/utils' import { findReplacementTx, TxValidationError } from 'find-replacement-tx' export const SOURCE_NETWORK = 'ethereum' @@ -57,7 +57,7 @@ export interface TransferOptions { nearAccount?: Account nearProvider?: najProviders.Provider nearClientAccount?: string - callIndexer?: (query: string) => Promise> + callIndexer?: (query: string) => Promise eventRelayerAccount?: string } @@ -542,6 +542,7 @@ export async function checkSync ( connectorAccount: options.nativeNEARLockerAddress ?? bridgeParams.nativeNEARLockerAddress, eventRelayerAccount: options.eventRelayerAccount ?? bridgeParams.eventRelayerAccount, finalizationMethod: 'finalise_eth_to_near_transfer', + ethTxHash: burnReceipt.transactionHash, callIndexer: options.callIndexer }) let finishTime: string | undefined diff --git a/packages/near-ether/src/natural-ether/sendToNear/index.ts b/packages/near-ether/src/natural-ether/sendToNear/index.ts index b6bc0298..03ee8b4a 100644 --- a/packages/near-ether/src/natural-ether/sendToNear/index.ts +++ b/packages/near-ether/src/natural-ether/sendToNear/index.ts @@ -8,7 +8,7 @@ import * as status from '@near-eth/client/dist/statuses' import { getEthProvider, getSignerProvider, getNearWallet, getNearProvider, formatLargeNum, getBridgeParams } from '@near-eth/client/dist/utils' import { TransferStatus, TransactionInfo } from '@near-eth/client/dist/types' import { findReplacementTx, TxValidationError } from 'find-replacement-tx' -import { urlParams, ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear } from '@near-eth/utils' +import { urlParams, ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear, ExplorerIndexerResult } from '@near-eth/utils' export const SOURCE_NETWORK = 'ethereum' export const DESTINATION_NETWORK = 'near' @@ -58,7 +58,7 @@ export interface TransferOptions { nearAccount?: Account nearProvider?: najProviders.Provider nearClientAccount?: string - callIndexer?: (query: string) => Promise> + callIndexer?: (query: string) => Promise eventRelayerAccount?: string } @@ -546,6 +546,7 @@ export async function checkSync ( connectorAccount: options.auroraEvmAccount ?? bridgeParams.auroraEvmAccount, eventRelayerAccount: options.eventRelayerAccount ?? bridgeParams.eventRelayerAccount, finalizationMethod: 'deposit', + ethTxHash: lockReceipt.transactionHash, callIndexer: options.callIndexer }) let finishTime: string | undefined diff --git a/packages/nep141-erc20/src/natural-erc20/sendToNear/index.ts b/packages/nep141-erc20/src/natural-erc20/sendToNear/index.ts index 0fe80d75..0128ab21 100644 --- a/packages/nep141-erc20/src/natural-erc20/sendToNear/index.ts +++ b/packages/nep141-erc20/src/natural-erc20/sendToNear/index.ts @@ -7,7 +7,7 @@ import { stepsFor } from '@near-eth/client/dist/i18nHelpers' import * as status from '@near-eth/client/dist/statuses' import { getEthProvider, getNearWallet, getNearProvider, formatLargeNum, getSignerProvider, getBridgeParams } from '@near-eth/client/dist/utils' import { TransferStatus, TransactionInfo } from '@near-eth/client/dist/types' -import { urlParams, ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear } from '@near-eth/utils' +import { urlParams, ethOnNearSyncHeight, findEthProof, findFinalizationTxOnNear, ExplorerIndexerResult } from '@near-eth/utils' import { findReplacementTx, TxValidationError } from 'find-replacement-tx' import { getDecimals, getSymbol } from '../getMetadata' @@ -66,7 +66,7 @@ export interface TransferOptions { nearAccount?: Account nearProvider?: najProviders.Provider nearClientAccount?: string - callIndexer?: (query: string) => Promise> + callIndexer?: (query: string) => Promise eventRelayerAccount?: string } @@ -723,6 +723,7 @@ export async function checkSync ( connectorAccount: options.nep141Factory ?? bridgeParams.nep141Factory, eventRelayerAccount: options.eventRelayerAccount ?? bridgeParams.eventRelayerAccount, finalizationMethod: 'deposit', + ethTxHash: lockReceipt.transactionHash, callIndexer: options.callIndexer }) let finishTime: string | undefined diff --git a/packages/utils/src/findFinalizationTx.ts b/packages/utils/src/findFinalizationTx.ts index 300109e4..40285964 100644 --- a/packages/utils/src/findFinalizationTx.ts +++ b/packages/utils/src/findFinalizationTx.ts @@ -3,33 +3,47 @@ import bs58 from 'bs58' export class SearchError extends Error {} +export interface ExplorerIndexerResult { + originated_from_transaction_hash: string + included_in_block_timestamp: string +} + export async function findFinalizationTxOnNear ({ proof, connectorAccount, eventRelayerAccount, finalizationMethod, + ethTxHash, callIndexer }: { - proof: string - connectorAccount: string - eventRelayerAccount: string - finalizationMethod: string - callIndexer: (query: string) => Promise> + proof?: string + connectorAccount?: string + eventRelayerAccount?: string + finalizationMethod?: string + ethTxHash?: string + callIndexer: (query: string) => Promise }): Promise<{ transactions: string[], timestamps: number[] }> { - const query = `SELECT public.receipts.originated_from_transaction_hash, public.receipts.included_in_block_timestamp - FROM public.receipts - JOIN public.action_receipt_actions - ON public.action_receipt_actions.receipt_id = public.receipts.receipt_id - WHERE (receipt_predecessor_account_id = '${eventRelayerAccount}' - AND receipt_receiver_account_id = '${connectorAccount}' - AND args ->> 'method_name' = '${finalizationMethod}' - AND args ->> 'args_base64' = '${proof}' - )` - // NOTE: Generally only 1 result is returned, but allow multiple in case finalization attempts are made. - const results = await callIndexer(query) - const transactions = results.map(tx => tx.originated_from_transaction_hash) - const timestamps = results.map(tx => Number(tx.included_in_block_timestamp)) - return { transactions, timestamps } + if (ethTxHash) { + const nearTxHash = await callIndexer(ethTxHash) as string + return { transactions: [nearTxHash], timestamps: [] } + } else if (proof && connectorAccount && eventRelayerAccount && finalizationMethod) { + const query = `SELECT public.receipts.originated_from_transaction_hash, public.receipts.included_in_block_timestamp + FROM public.receipts + JOIN public.action_receipt_actions + ON public.action_receipt_actions.receipt_id = public.receipts.receipt_id + WHERE (receipt_predecessor_account_id = '${eventRelayerAccount}' + AND receipt_receiver_account_id = '${connectorAccount}' + AND args ->> 'method_name' = '${finalizationMethod}' + AND args ->> 'args_base64' = '${proof}' + )` + // NOTE: Generally only 1 result is returned, but allow multiple in case finalization attempts are made. + const results = await callIndexer(query) as ExplorerIndexerResult[] + const transactions = results.map(tx => tx.originated_from_transaction_hash) + const timestamps = results.map(tx => Number(tx.included_in_block_timestamp)) + return { transactions, timestamps } + } else { + throw new Error('Expected ethTxHash or sql query params') + } } async function proofAlreadyUsed ( diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 275d123a..6efd3dbc 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -2,7 +2,7 @@ export * as urlParams from './url-params' export * as erc20 from './erc20' export * as nep141 from './nep141' export * as aurora from './aurora' -export { findFinalizationTxOnEthereum, findFinalizationTxOnNear } from './findFinalizationTx' +export { findFinalizationTxOnEthereum, findFinalizationTxOnNear, ExplorerIndexerResult } from './findFinalizationTx' export { borshifyOutcomeProof } from './borshify-proof' export { ethOnNearSyncHeight } from './ethOnNearClient' export { nearOnEthSyncHeight } from './nearOnEthClient'