Skip to Content
dApp 接入
本页目录

eth_getTransactionByHash

通过交易哈希返回交易信息。


参数

索引类型说明
0string交易哈希(32 字节)

返回值

object | null - 交易对象,如果未找到则为 null

字段类型说明
hashstring交易哈希
noncestring发送方 nonce(十六进制)
blockHashstring区块哈希(待处理时为 null)
blockNumberstring区块号(待处理时为 null)
transactionIndexstring区块中的索引(待处理时为 null)
fromstring发送方地址
tostring接收方地址(合约创建时为 null)
valuestring转账金额(十六进制 wei)
gasPricestringGas 价格(十六进制)
gasstringGas 限制(十六进制)
inputstring交易数据
vstringECDSA 恢复 ID
rstringECDSA 签名 r
sstringECDSA 签名 s

示例

const tx = await window.$onekey.ethereum.request({ method: 'eth_getTransactionByHash', params: ['0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b'] }) if (tx) { console.log('From:', tx.from) console.log('To:', tx.to) console.log('Value:', BigInt(tx.value).toString()) console.log('Status:', tx.blockHash ? 'Mined' : 'Pending') }

发送后追踪交易

async function sendAndTrack(txParams, timeoutMs = 60000) { const deadline = Date.now() + timeoutMs // Send transaction const txHash = await window.$onekey.ethereum.request({ method: 'eth_sendTransaction', params: [txParams] }) console.log('Transaction sent:', txHash) // Poll for transaction details const checkTx = async () => { if (Date.now() >= deadline) throw new Error("Transaction lookup timed out") const tx = await window.$onekey.ethereum.request({ method: 'eth_getTransactionByHash', params: [txHash] }) if (tx && tx.blockHash) { console.log('Transaction mined in block:', parseInt(tx.blockNumber, 16)) return tx } // Still pending, check again await new Promise(r => setTimeout(r, 2000)) return checkTx() } return checkTx() }

错误码

错误码消息说明
-32602Invalid params交易哈希无效

说明

  • 如果交易未找到,返回 null
  • 待处理的交易 blockHash 和 blockNumber 为 null
  • 要获取交易状态(成功/失败),请使用 eth_getTransactionReceipt
  • 交易数据在交易被确认后仍然持久存在
最后更新于