Skip to content

Commit

Permalink
feat: Find finalization tx using monitoring indexer. (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
paouvrard authored Jan 4, 2024
1 parent c134739 commit 6d2d883
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 30 deletions.
9 changes: 9 additions & 0 deletions .yarn/versions/17863933.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions packages/aurora-erc20/src/natural-erc20/sendToAurora/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -68,7 +68,7 @@ export interface TransferOptions {
maxFindEthProofInterval?: number
nearClientAccount?: string
auroraEvmAccount?: string
callIndexer?: (query: string) => Promise<Array<{originated_from_transaction_hash: string, included_in_block_timestamp: string}>>
callIndexer?: (query: string) => Promise<ExplorerIndexerResult[] | string>
eventRelayerAccount?: string
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions packages/aurora-ether/src/natural-ether/sendToAurora/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface TransferOptions {
nearProvider?: najProviders.Provider
maxFindEthProofInterval?: number
nearClientAccount?: string
callIndexer?: (query: string) => Promise<Array<{originated_from_transaction_hash: string, included_in_block_timestamp: string}>>
callIndexer?: (query: string) => Promise<ExplorerIndexerResult[] | string>
eventRelayerAccount?: string
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions packages/near-ether/src/bridged-near/sendToNear/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -57,7 +57,7 @@ export interface TransferOptions {
nearAccount?: Account
nearProvider?: najProviders.Provider
nearClientAccount?: string
callIndexer?: (query: string) => Promise<Array<{originated_from_transaction_hash: string, included_in_block_timestamp: string}>>
callIndexer?: (query: string) => Promise<ExplorerIndexerResult[] | string>
eventRelayerAccount?: string
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions packages/near-ether/src/natural-ether/sendToNear/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface TransferOptions {
nearAccount?: Account
nearProvider?: najProviders.Provider
nearClientAccount?: string
callIndexer?: (query: string) => Promise<Array<{originated_from_transaction_hash: string, included_in_block_timestamp: string}>>
callIndexer?: (query: string) => Promise<ExplorerIndexerResult[] | string>
eventRelayerAccount?: string
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions packages/nep141-erc20/src/natural-erc20/sendToNear/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -66,7 +66,7 @@ export interface TransferOptions {
nearAccount?: Account
nearProvider?: najProviders.Provider
nearClientAccount?: string
callIndexer?: (query: string) => Promise<Array<{originated_from_transaction_hash: string, included_in_block_timestamp: string}>>
callIndexer?: (query: string) => Promise<ExplorerIndexerResult[] | string>
eventRelayerAccount?: string
}

Expand Down Expand Up @@ -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
Expand Down
52 changes: 33 additions & 19 deletions packages/utils/src/findFinalizationTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<{originated_from_transaction_hash: string, included_in_block_timestamp: string}>>
proof?: string
connectorAccount?: string
eventRelayerAccount?: string
finalizationMethod?: string
ethTxHash?: string
callIndexer: (query: string) => Promise<ExplorerIndexerResult[] | string>
}): 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 (
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 6d2d883

Please sign in to comment.