本页目录
eth_getTransactionReceipt
通过交易哈希返回交易回执。回执包含交易执行的信息,包括状态和日志。
参数
| 索引 | 类型 | 说明 |
|---|---|---|
| 0 | string | 交易哈希(32 字节) |
返回值
object | null - 回执对象,如果尚未确认则为 null
| 字段 | 类型 | 说明 |
|---|---|---|
transactionHash | string | 交易哈希 |
transactionIndex | string | 区块中的索引(十六进制) |
blockHash | string | 区块哈希 |
blockNumber | string | 区块号(十六进制) |
from | string | 发送方地址 |
to | string | 接收方地址 |
cumulativeGasUsed | string | 区块中累计使用的 Gas |
gasUsed | string | 此交易使用的 Gas(十六进制) |
contractAddress | string | 创建的合约地址(如适用) |
logs | array | 日志对象数组 |
logsBloom | string | 日志的布隆过滤器 |
status | string | 0x1(成功)或 0x0(失败) |
effectiveGasPrice | string | 实际支付的 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}`)
})错误码
| 错误码 | 消息 | 说明 |
|---|---|---|
| -32602 | Invalid params | 交易哈希无效 |
说明
- 待处理的交易(尚未确认)返回
null status字段:0x1= 成功,0x0= 回滚contractAddress仅在合约创建交易中存在- 使用
logs解析交易中触发的事件
解码日志时,请核对发出日志的合约和预期 ABI。仅凭 topic 匹配无法确认代币身份;ERC-721 也使用相同的 Transfer 事件签名,但 indexed 参数布局不同。
最后更新于