eth_sendTransaction
请求钱包创建并发送一笔交易。用户在 UKey Wallet 中确认后,Provider 会把交易广播到当前网络,并返回交易哈希。
入参
参数为数组,数组内包含一个交易对象:
| 字段 | 类别 | 必填 | 说明 |
|---|---|---|---|
from | string | 是 | 用户已授权的发送方地址 |
to | string | 否 | 接收方地址;部署合约时可省略 |
value | string | 否 | 发送金额,单位 wei,十六进制格式 |
data | string | 否 | 合约调用数据或合约字节码 |
gas | string | 否 | Gas 上限,十六进制格式 |
gasPrice | string | 否 | 传统交易 Gas 价格 |
maxFeePerGas | string | 否 | EIP-1559 最大费用 |
maxPriorityFeePerGas | string | 否 | EIP-1559 最大优先费用 |
nonce | string | 否 | 交易 nonce,通常交给钱包处理 |
结果
string - 交易哈希,32 字节十六进制字符串。
示范
发送 ETH
const txHash = await window.$ukey.ethereum.request({
method: "eth_sendTransaction",
requestParams: [
{
from: '0x已授权账户地址...',
to: '0x收款地址...',
value: "0x2386f26fc10000", // 示例 ETH 金额,对应 wei
},
],
});
console.log("交易哈希:", txHash);
调用合约方法
const txHash = await window.$ukey.ethereum.request({
method: "eth_sendTransaction",
requestParams: [
{
from: '0x已授权账户地址...',
to: '0x目标合约地址...',
data: "0xa9059cbb...", // ABI 编码后的函数调用数据
gas: "0x5208",
},
],
});
EIP-1559 交易
const txHash = await window.$ukey.ethereum.request({
method: "eth_sendTransaction",
requestParams: [
{
from: '0x已授权账户地址...',
to: '0x收款地址...',
value: "0x2386f26fc10000",
maxFeePerGas: "0x1dcd65000", // 费用上限示例
maxPriorityFeePerGas: "0x77359400", // 优先费用示例
},
],
});
错误码
| 错误码 | 消息 | 说明 |
|---|---|---|
| 4001 | 用户取消了本次请求 | 用户取消了交易确认 |
| -32000 | Insufficient funds | 余额不足 |
| -32602 | Invalid params | 交易参数无效 |
说明
- 所有数值字段都应使用带
0x前缀的十六进制字符串。 - 如果没有提供
gas,钱包会尝试自动估算。 - 支持 EIP-1559 的网络建议使用
maxFeePerGas和maxPriorityFeePerGas。 from必须是当前已授权账户,否则请求会失败。- 交易广播后仍需等待链上确认,请使用 RPC 或区块浏览器跟踪哈希状态。