Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update avalanche explorer api #883

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion node/coinstacks/avalanche/api/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 24 additions & 8 deletions node/coinstacks/common/api/src/evm/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -555,13 +563,17 @@ export class Service implements Omit<BaseAPI, 'getInfo'>, API {
}

private async fetchInternalTxsByTxid(txid: string): Promise<Array<InternalTx> | undefined> {
const { data } = await axios.get<ExplorerApiResponse<Array<ExplorerInternalTx>>>(
const { data } = await axios.get<ExplorerApiResponse<Array<ExplorerInternalTxByHash>>>(
`${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<Array<InternalTx>>((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(
Expand Down Expand Up @@ -631,14 +643,18 @@ export class Service implements Omit<BaseAPI, 'getInfo'>, API {
pageSize: number,
from?: number,
to?: number
): Promise<Array<ExplorerInternalTx> | undefined> {
const { data } = await axios.get<ExplorerApiResponse<Array<ExplorerInternalTx>>>(
`${this.explorerApiUrl}?module=account&action=txlistinternal&address=${address}&page=${page}&offset=${pageSize}&startblock=${from}&endblock=${to}&sort=desc&apikey=${this.explorerApiKey}`
)
): Promise<Array<ExplorerInternalTxByAddress> | 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<ExplorerApiResponse<Array<ExplorerInternalTxByAddress>>>(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(
Expand Down
18 changes: 17 additions & 1 deletion node/coinstacks/common/api/src/evm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,23 @@ export interface ExplorerApiResponse<T> {
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
Expand Down