跳到主要内容

eth_sendTransaction

请求钱包创建并发送一笔交易。用户在 UKey Wallet 中确认后,Provider 会把交易广播到当前网络,并返回交易哈希。


入参

参数为数组,数组内包含一个交易对象:

字段类别必填说明
fromstring用户已授权的发送方地址
tostring接收方地址;部署合约时可省略
valuestring发送金额,单位 wei,十六进制格式
datastring合约调用数据或合约字节码
gasstringGas 上限,十六进制格式
gasPricestring传统交易 Gas 价格
maxFeePerGasstringEIP-1559 最大费用
maxPriorityFeePerGasstringEIP-1559 最大优先费用
noncestring交易 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用户取消了本次请求用户取消了交易确认
-32000Insufficient funds余额不足
-32602Invalid params交易参数无效

说明

  • 所有数值字段都应使用带 0x 前缀的十六进制字符串。
  • 如果没有提供 gas,钱包会尝试自动估算。
  • 支持 EIP-1559 的网络建议使用 maxFeePerGasmaxPriorityFeePerGas
  • from 必须是当前已授权账户,否则请求会失败。
  • 交易广播后仍需等待链上确认,请使用 RPC 或区块浏览器跟踪哈希状态。