Skip to Content
dApp 接入
本页目录

eth_getTransactionReceipt

通过交易哈希返回交易回执。回执包含交易执行的信息,包括状态和日志。


参数

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

返回值

object | null - 回执对象,如果尚未确认则为 null

字段类型说明
transactionHashstring交易哈希
transactionIndexstring区块中的索引(十六进制)
blockHashstring区块哈希
blockNumberstring区块号(十六进制)
fromstring发送方地址
tostring接收方地址
cumulativeGasUsedstring区块中累计使用的 Gas
gasUsedstring此交易使用的 Gas(十六进制)
contractAddressstring创建的合约地址(如适用)
logsarray日志对象数组
logsBloomstring日志的布隆过滤器
statusstring0x1(成功)或 0x0(失败)
effectiveGasPricestring实际支付的 Gas 价格(十六进制)

示例

const receipt = await window.$onekey.ethereum.request({ method: 'eth_getTransactionReceipt', params: ['0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b'] }) if (receipt) { const success = receipt.status === '0x1' console.log('Transaction', success ? 'succeeded' : 'failed') console.log('Gas used:', parseInt(receipt.gasUsed, 16)) console.log('Logs:', receipt.logs.length) }

等待回执

async function waitForReceipt(txHash, timeout = 60000) { const start = Date.now() while (Date.now() - start < timeout) { const receipt = await window.$onekey.ethereum.request({ method: 'eth_getTransactionReceipt', params: [txHash] }) if (receipt) { return receipt } await new Promise(r => setTimeout(r, 2000)) } throw new Error('Transaction receipt timeout') } // Usage const txHash = await window.$onekey.ethereum.request({ method: 'eth_sendTransaction', params: [txParams] }) const receipt = await waitForReceipt(txHash) console.log('Transaction status:', receipt.status === '0x1' ? 'Success' : 'Failed')

解析合约事件

// Set tokenAddress to the intended ERC-20 contract before parsing its Transfer logs. const transferTopic = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' const transferLogs = receipt.logs.filter( log => log.address.toLowerCase() === tokenAddress.toLowerCase() && log.topics[0] === transferTopic && log.topics.length === 3 ) transferLogs.forEach(log => { const from = '0x' + log.topics[1].slice(26) const to = '0x' + log.topics[2].slice(26) const value = BigInt(log.data).toString() console.log(`Transfer: ${from} -> ${to}: ${value}`) })

错误码

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

说明

  • 待处理的交易(尚未确认)返回 null
  • status 字段:0x1 = 成功,0x0 = 回滚
  • contractAddress 仅在合约创建交易中存在
  • 使用 logs 解析交易中触发的事件

解码日志时,请核对发出日志的合约和预期 ABI。仅凭 topic 匹配无法确认代币身份;ERC-721 也使用相同的 Transfer 事件签名,但 indexed 参数布局不同。

最后更新于