Skip to Content
dApp 接入
本页目录

eth_sendTransaction

创建交易并提示用户确认。成功后返回交易哈希。


参数

Array 包含一个交易对象:

字段类型必填说明
fromstring是发送方地址
tostring否接收方地址(部署合约时省略)
valuestring否发送金额,单位 wei(十六进制)
datastring否合约调用数据或字节码
gasstring否Gas 限制(十六进制)
gasPricestring否Gas 价格,用于传统交易(十六进制)
maxFeePerGasstring否最大 Gas 费用,用于 EIP-1559(十六进制)
maxPriorityFeePerGasstring否最大优先费用,用于 EIP-1559(十六进制)
noncestring否交易 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 }] })

错误码

错误码消息说明
4001User rejected the request用户拒绝交易
-32000Insufficient funds余额不足
-32602Invalid params交易参数无效

说明

  • 所有数值都使用带 0x 前缀的十六进制字符串
  • 如未提供 gas,钱包会自动估算
  • 对于支持 EIP-1559 的链,建议使用 maxFeePerGas 而非 gasPrice
  • from 地址必须是用户已连接的账户

不要将合约调用的 gas 上限直接设为原生币转账常见的 21,000。应让钱包估算,或调用 eth_estimateGas。示例地址和调用数据均为占位值。返回交易哈希不代表执行成功,请在同一网络查询交易回执。

最后更新于