exchange.IO("api", blockChain, ...)
exchange.IO("api", "eth", ...)函数的调用方式,用于调用以太坊RPC方法(需要在Web3交易所对象配置时选择eth)。
exchange.IO("api", "tron", ...)函数的调用方式,用于调用波场RPC方法(需要在Web3交易所对象配置时选择tron)。
exchange.IO(k, blockChain, rpcMethod)
exchange.IO(k, blockChain, rpcMethod, ...args)示例
-
查询钱包中ETH的余额:
javascriptfunction main() { // "owner" 需要替换为具体钱包地址 // "latest"字符串位置的参数标签:'latest'、'earliest'或'pending',参考https://eth.wiki/json-rpc/API#the-default-block-parameter // 返回值 ethBalance 为十六进制字符串:0x9b19ce56113070 var ethBalance = exchange.IO("api", "eth", "eth_getBalance", "owner", "latest") // ETH的精度单位为1e18 var ethDecimal = 18 // 由于JavaScript语言精度原因,需要使用系统底层封装的函数BigInt、BigDecimal处理 // 将ethBalance转换为可读数量,0x9b19ce56113070转换为0.043656995388076145 Log(Number((BigDecimal(BigInt(ethBalance))/BigDecimal(Math.pow(10, ethDecimal))).toString())) } -
ETH转账,可以根据具体需求设置
{gasPrice: 11, gasLimit: 111, nonce: 111}参数,该参数设置在exchange.IO()函数的最后一个参数上。可以省略其中的nonce使用系统默认值,或者不设置gasLimit/gasPrice/nonce,全部使用系统默认值。javascriptfunction mian() { // ETH的精度单位为1e18 var ethDecimal = 18 // 转账数量,可读的数量例如:0.01个ETH var sendAmount = 0.01 // 由于JavaScript语言精度原因,需要使用系统底层封装的函数BigInt、BigDecimal处理,并且将可读数量转换为链上处理的数据 var toAmount = (BigDecimal(sendAmount)*BigDecimal(Math.pow(10, ethDecimal))).toFixed(0) // "toAddress"为转账时接收方的ETH钱包地址,需要具体填写,toAmount为转账数量 exchange.IO("api", "eth", "send", "toAddress", toAmount) } -
查询
gasPrice:javascriptfunction toAmount(s, decimals) { return Number((BigDecimal(BigInt(s))/BigDecimal(Math.pow(10, decimals))).toString()) } function main() { var gasPrice = exchange.IO("api", "eth", "eth_gasPrice") Log("gasPrice:", toAmount(gasPrice, 0)) // 5000000000 , in wei (5 gwei) } -
查询
eth_estimateGas:javascriptfunction toAmount(s, decimals) { // toAmount函数可以将十六进制编码的数值转换为十进制数值 return Number((BigDecimal(BigInt(s))/BigDecimal(Math.pow(10, decimals))).toString()) } function main() { // 编码approve(授权)方法的调用 var data = exchange.IO("encode", "0x111111111117dC0aa78b770fA6A738034120C302", "approve", "0xe592427a0aece92de3edee1f18e0157c05861564", "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") Log("data:", data) var gasPrice = exchange.IO("api", "eth", "eth_gasPrice") Log("gasPrice:", toAmount(gasPrice, 0)) var obj = { "from" : "0x0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // walletAddress "to" : "0x111111111117dC0aa78b770fA6A738034120C302", "gasPrice" : gasPrice, "value" : "0x0", "data" : "0x" + data, } var gasLimit = exchange.IO("api", "eth", "eth_estimateGas", obj) Log("gasLimit:", toAmount(gasLimit, 0)) Log("gas fee", toAmount(gasLimit, 0) * toAmount(gasPrice, 0) / 1e18) }
返回值
| 类型 | 描述 |
string / number / bool / object / array / any (平台支持的任意类型) |
|
参数
| 名称 | 类型 | 必填 | 描述 |
k | string | 是 |
|
blockChain | string | 是 |
|
rpcMethod | string | 是 |
|
arg | string / number / bool / object / array / function / any (平台支持的任意类型) | 否 |
|
参考
备注
exchange.IO()函数的第二个参数为"eth"时,可以直接调用以太坊节点服务器可用的RPC方法。