add transaction view page
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { post } from './call'
|
||||
import { getChainID, getURL } from './network'
|
||||
import { inspect } from 'util'
|
||||
import type { Transaction } from './types'
|
||||
import { numberToHex } from 'viem'
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
isHex,
|
||||
formatEther,
|
||||
type PublicClient,
|
||||
formatGwei,
|
||||
decodeFunctionData,
|
||||
} from 'viem'
|
||||
|
||||
export type TransactionDetails = {
|
||||
hash: string
|
||||
from: string
|
||||
to: string | null
|
||||
value: string
|
||||
blockNumber: string | null
|
||||
fee: string
|
||||
gasPrice: string
|
||||
status: 'success' | 'reverted'
|
||||
type: string
|
||||
nonce: number
|
||||
input: string
|
||||
}
|
||||
|
||||
export const getTransactionDetails = async (
|
||||
client: PublicClient,
|
||||
hash: string,
|
||||
): Promise<TransactionDetails | null> => {
|
||||
if (!isHex(hash)) return null
|
||||
|
||||
const [tx, receipt] = await Promise.all([
|
||||
client.getTransaction({ hash }),
|
||||
client.getTransactionReceipt({ hash }),
|
||||
])
|
||||
|
||||
return {
|
||||
hash: tx.hash,
|
||||
from: tx.from,
|
||||
to: tx.to,
|
||||
value: formatEther(tx.value),
|
||||
blockNumber: receipt.blockNumber?.toString() ?? null,
|
||||
fee: (
|
||||
parseFloat(formatGwei(receipt.gasUsed)) *
|
||||
parseFloat(formatGwei(tx.gasPrice || BigInt(0)))
|
||||
).toString(),
|
||||
gasPrice: formatGwei(tx.gasPrice || BigInt(0)),
|
||||
status: receipt.status === 'success' ? 'success' : 'reverted',
|
||||
type: tx.type,
|
||||
nonce: tx.nonce,
|
||||
input: tx.input,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { bsc, mainnet, polygon, type Chain } from 'viem/chains'
|
||||
import type { ChainID } from '../alchemy/network'
|
||||
|
||||
const chains: Record<ChainID, Chain> = {
|
||||
'1': mainnet,
|
||||
'56': bsc,
|
||||
'137': polygon,
|
||||
}
|
||||
|
||||
export const getChain = (id: ChainID) => {
|
||||
return chains[id]
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { createPublicClient, http } from 'viem'
|
||||
import { getChainID, getURL } from '../alchemy/network'
|
||||
import { getChain } from './getChain'
|
||||
|
||||
export const getClient = (chainId: string, apiKey: string) => {
|
||||
const id = getChainID(chainId)
|
||||
|
||||
if (!id) {
|
||||
throw new Error()
|
||||
}
|
||||
|
||||
const url = getURL({ chainId: id, key: apiKey })
|
||||
const publicClient = createPublicClient({
|
||||
chain: getChain(id),
|
||||
transport: http(url),
|
||||
})
|
||||
|
||||
return publicClient
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './getClient'
|
||||
export * from './get-transaction-details'
|
||||
Reference in New Issue
Block a user