本页目录
eth_getTransactionByHash
通过交易哈希返回交易信息。
参数
| 索引 | 类型 | 说明 |
|---|---|---|
| 0 | string | 交易哈希(32 字节) |
返回值
object | null - 交易对象,如果未找到则为 null
| 字段 | 类型 | 说明 |
|---|---|---|
hash | string | 交易哈希 |
nonce | string | 发送方 nonce(十六进制) |
blockHash | string | 区块哈希(待处理时为 null) |
blockNumber | string | 区块号(待处理时为 null) |
transactionIndex | string | 区块中的索引(待处理时为 null) |
from | string | 发送方地址 |
to | string | 接收方地址(合约创建时为 null) |
value | string | 转账金额(十六进制 wei) |
gasPrice | string | Gas 价格(十六进制) |
gas | string | Gas 限制(十六进制) |
input | string | 交易数据 |
v | string | ECDSA 恢复 ID |
r | string | ECDSA 签名 r |
s | string | ECDSA 签名 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()
}错误码
| 错误码 | 消息 | 说明 |
|---|---|---|
| -32602 | Invalid params | 交易哈希无效 |
说明
- 如果交易未找到,返回
null - 待处理的交易
blockHash和blockNumber为 null - 要获取交易状态(成功/失败),请使用 eth_getTransactionReceipt
- 交易数据在交易被确认后仍然持久存在
最后更新于