本页目录
eth_sendTransaction
创建交易并提示用户确认。成功后返回交易哈希。
参数
Array 包含一个交易对象:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
from | string | 是 | 发送方地址 |
to | string | 否 | 接收方地址(部署合约时省略) |
value | string | 否 | 发送金额,单位 wei(十六进制) |
data | string | 否 | 合约调用数据或字节码 |
gas | string | 否 | Gas 限制(十六进制) |
gasPrice | string | 否 | Gas 价格,用于传统交易(十六进制) |
maxFeePerGas | string | 否 | 最大 Gas 费用,用于 EIP-1559(十六进制) |
maxPriorityFeePerGas | string | 否 | 最大优先费用,用于 EIP-1559(十六进制) |
nonce | string | 否 | 交易 nonce(十六进制) |
返回值
string - 交易哈希(32 字节十六进制字符串)
示例
发送 ETH
const txHash = await window.$onekey.ethereum.request({
method: 'eth_sendTransaction',
params: [{
from: '0xYourAddress...',
to: '0xRecipientAddress...',
value: '0xDE0B6B3A7640000', // 1 ETH in wei
}]
})
console.log('Transaction hash:', txHash)调用合约方法
const txHash = await window.$onekey.ethereum.request({
method: 'eth_sendTransaction',
params: [{
from: '0xYourAddress...',
to: '0xContractAddress...',
data: '0xa9059cbb...', // Encoded function call
}]
})EIP-1559 交易
const txHash = await window.$onekey.ethereum.request({
method: 'eth_sendTransaction',
params: [{
from: '0xYourAddress...',
to: '0xRecipientAddress...',
value: '0xDE0B6B3A7640000',
maxFeePerGas: '0x2540BE400', // 10 Gwei
maxPriorityFeePerGas: '0x3B9ACA00', // 1 Gwei
}]
})错误码
| 错误码 | 消息 | 说明 |
|---|---|---|
| 4001 | User rejected the request | 用户拒绝交易 |
| -32000 | Insufficient funds | 余额不足 |
| -32602 | Invalid params | 交易参数无效 |
说明
- 所有数值都使用带
0x前缀的十六进制字符串 - 如未提供
gas,钱包会自动估算 - 对于支持 EIP-1559 的链,建议使用
maxFeePerGas而非gasPrice from地址必须是用户已连接的账户
不要将合约调用的 gas 上限直接设为原生币转账常见的 21,000。应让钱包估算,或调用 eth_estimateGas。示例地址和调用数据均为占位值。返回交易哈希不代表执行成功,请在同一网络查询交易回执。
最后更新于