From d40b32503e757ffe21134583b7e5ad898d724054 Mon Sep 17 00:00:00 2001 From: kaladinlight <35275952+kaladinlight@users.noreply.github.com> Date: Fri, 10 Nov 2023 12:57:35 -0700 Subject: [PATCH] chore: update avalanche explorer api --- .../avalanche/api/src/controller.ts | 2 +- node/coinstacks/common/api/src/evm/service.ts | 32 ++++++++++++++----- node/coinstacks/common/api/src/evm/types.ts | 18 ++++++++++- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/node/coinstacks/avalanche/api/src/controller.ts b/node/coinstacks/avalanche/api/src/controller.ts index 31375f8e4..955794632 100644 --- a/node/coinstacks/avalanche/api/src/controller.ts +++ b/node/coinstacks/avalanche/api/src/controller.ts @@ -45,7 +45,7 @@ export const gasOracle = new GasOracle({ logger, provider, coinstack: 'avalanche export const service = new Service({ blockbook, gasOracle, - explorerApiUrl: 'https://api.snowtrace.io/api', + explorerApiUrl: 'https://api.routescan.io/v2/network/mainnet/evm/43114/etherscan/api', provider, logger, rpcUrl: RPC_URL, diff --git a/node/coinstacks/common/api/src/evm/service.ts b/node/coinstacks/common/api/src/evm/service.ts index ecd426315..2a557ec4f 100644 --- a/node/coinstacks/common/api/src/evm/service.ts +++ b/node/coinstacks/common/api/src/evm/service.ts @@ -17,7 +17,15 @@ import { TokenMetadata, TokenType, } from './models' -import { Cursor, NodeBlock, DebugCallStack, ExplorerApiResponse, ExplorerInternalTx, TraceCall } from './types' +import { + Cursor, + NodeBlock, + DebugCallStack, + ExplorerApiResponse, + TraceCall, + ExplorerInternalTxByHash, + ExplorerInternalTxByAddress, +} from './types' import { GasOracle } from './gasOracle' import { formatAddress, handleError } from './utils' import { validatePageSize } from '../utils' @@ -555,13 +563,17 @@ export class Service implements Omit, API { } private async fetchInternalTxsByTxid(txid: string): Promise | undefined> { - const { data } = await axios.get>>( + const { data } = await axios.get>>( `${this.explorerApiUrl}?module=account&action=txlistinternal&txhash=${txid}&apikey=${this.explorerApiKey}` ) if (data.status === '0') return [] - return data.result.map((t) => ({ from: formatAddress(t.from), to: formatAddress(t.to), value: t.value })) + return data.result.reduce>((prev, t) => { + // filter out all 0 index trace ids as these are the normal initiating tx calls that are returned by routescan.io for some reason + if (t.index === 0) return prev + return [...prev, { from: formatAddress(t.from), to: formatAddress(t.to), value: t.value }] + }, []) } private async getInternalTxs( @@ -631,14 +643,18 @@ export class Service implements Omit, API { pageSize: number, from?: number, to?: number - ): Promise | undefined> { - const { data } = await axios.get>>( - `${this.explorerApiUrl}?module=account&action=txlistinternal&address=${address}&page=${page}&offset=${pageSize}&startblock=${from}&endblock=${to}&sort=desc&apikey=${this.explorerApiKey}` - ) + ): Promise | undefined> { + let url = `${this.explorerApiUrl}?module=account&action=txlistinternal&address=${address}&page=${page}&offset=${pageSize}&sort=desc` + if (from) url += `&startblock=${from}` + if (to) url += `&endblock=${to}` + if (this.explorerApiKey) url += `&apikey=${this.explorerApiKey}` + + const { data } = await axios.get>>(url) if (data.status === '0') return [] - return data.result + // filter out all 0 index trace ids as these are the normal initiating tx calls that are returned by routescan.io for some reason + return data.result.filter((internalTx) => internalTx.traceId !== '0') } private async getTxs( diff --git a/node/coinstacks/common/api/src/evm/types.ts b/node/coinstacks/common/api/src/evm/types.ts index 680df207a..17e6d60d4 100644 --- a/node/coinstacks/common/api/src/evm/types.ts +++ b/node/coinstacks/common/api/src/evm/types.ts @@ -86,7 +86,23 @@ export interface ExplorerApiResponse { result: T } -export interface ExplorerInternalTx { +export interface ExplorerInternalTxByHash { + index?: number + blockNumber: string + timeStamp: string + from: string + to: string + value: string + contractAddress: string + input: string + type: string + gas: string + gasUsed: string + isError: string + errCode: string +} + +export interface ExplorerInternalTxByAddress { blockNumber: string timeStamp: string hash: string