From f7aade7c0499ea36161c913679faa4a892333a14 Mon Sep 17 00:00:00 2001 From: jenya Date: Mon, 21 Nov 2022 14:02:18 +0400 Subject: [PATCH] internal transactions fallback to rpc when older than 34000000 --- src/api/client.ts | 31 ++++++++++++++++++++++++++++- src/api/rpc.ts | 18 +++++++++++++++++ src/pages/TransactionPage/index.tsx | 7 +++++-- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/api/client.ts b/src/api/client.ts index 41a891a..b1ef37d 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -16,6 +16,7 @@ import { } from "./client.interface"; import { ApiCache } from "./ApiCache"; import { get4byteSignatureByHex } from "./3rdPartyApi"; +import { eth_traceTransaction } from './rpc' // import { ClientCache } from "./clientCache"; // const clientCache = new ClientCache({ @@ -66,7 +67,35 @@ export function getStakingTransactionByField(params: [number, "hash", string]) { ) as Promise; } -export function getInternalTransactionsByField(params: any[]) { +export function getInternalTransactionsByField(params: any[], blockNumber?: string) { + // fallback to rpc as we don't keep old records older than 34000000 any more in postgres + if (!blockNumber || +blockNumber < 34000000) { + const queryType = params[1] + if (params[1] !== queryType) { + console.error('use only transaction hash to get internal transactions') + return [] as InternalTransaction[] + } + + const txHash = params[2] + // todo note check error field may not work properly + return eth_traceTransaction(txHash).then(txs => { + const mapTxs = txs.map((tx: any, i: number) => ({ + type: tx.action.type || tx.type, + value: tx.value, + input: tx.action.input, + output: tx.result.output, + transactionHash: tx.transactionHash, + gasUsed: tx.result.gasUsed, + index: i, + to: tx.action.to, + from: tx.action.from, + error: tx.result.error, + gas: tx.action.gas + })) + return mapTxs as InternalTransaction[] + }) + } + return transport("getInternalTransactionsByField", params) as Promise< InternalTransaction[] >; diff --git a/src/api/rpc.ts b/src/api/rpc.ts index 446fe02..4db998f 100644 --- a/src/api/rpc.ts +++ b/src/api/rpc.ts @@ -90,6 +90,24 @@ const defaultGetHistoryParams = { order: RequestOrder.DESC } +export const eth_traceTransaction = (hash: string) => { + return rpcAdapter(API_URL, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + jsonrpc: "2.0", + method: "trace_transaction", + id: 1, + params: [hash], + }), + }).then(data => { + if (data.error) { + throw new Error(data.error.message) + } + return data.result + }); +} + export const hmyv2_getTransactionsHistory = (params: IGetTxsHistoryParams[]) => { return rpcAdapter>(API_URL, { method: "POST", diff --git a/src/pages/TransactionPage/index.tsx b/src/pages/TransactionPage/index.tsx index 2592ac9..83d8c7d 100644 --- a/src/pages/TransactionPage/index.tsx +++ b/src/pages/TransactionPage/index.tsx @@ -93,7 +93,7 @@ export const TransactionPage = () => { trxInputSignature = await getTxInputSignature(trx) } } - + setTx((trx || {}) as RPCTransactionHarmony); setInputSignature(trxInputSignature) }; @@ -110,7 +110,10 @@ export const TransactionPage = () => { 0, "transaction_hash", tx.hash, - ]); + ], + // need to track fallback + tx.blockNumber + ); const methodSignatures = await Promise.all( txs.map((tx) => { return tx.input && tx.input.length > 10