exchange.IO("api", "eth", ...)函数的调用方式,用于调用以太坊RPC方法(需要在Web3交易所对象配置时选择eth)。
exchange.IO("api", "tron", ...)函数的调用方式,用于调用波场RPC方法(需要在Web3交易所对象配置时选择tron)。
exchange.IO("api", "eth", ...)函数返回所调用RPC方法的返回值。
string / number / bool / object / array / any (平台支持的任意类型)
exchange.IO(k, blockChain, rpcMethod) exchange.IO(k, blockChain, rpcMethod, …args)
k参数用于设置exchange.IO()函数的功能,设置为"api"表示该函数用于扩展调用请求。
k
true
string
blockChain参数用于设置exchange.IO()函数的功能,设置为"eth"表示该函数用于调用以太坊网络的RPC方法;设置为"tron"表示该函数用于调用波场网络的RPC方法。
blockChain
true
string
rpcMethod参数用于设置exchange.IO()函数所要调用的RPC方法。
rpcMethod
true
string
arg参数用于指定所要调用的RPC方法的参数。
arg参数可能有多个,arg参数的类型和数量根据rpcMethod参数指定的RPC方法而定。
arg
false
string / number / bool / object / array / function / any (平台支持的任意类型)
”`javascript function 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的余额:
javascript
function 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)
}
ETH转账,可以根据具体需求设置{gasPrice: 11, gasLimit: 111, nonce: 111}参数,该参数设置在exchange.IO()函数的最后一个参数上。可以省略其中的nonce使用系统默认值,或者不设置gasLimit/gasPrice/nonce,全部使用系统默认值。
javascript
function 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)
}
查询gasPrice:
javascript
function 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)
}”
查询eth_estimateGas`:
exchange.IO()函数的第二个参数为"eth"时,可以直接调用以太坊节点服务器可用的RPC方法。
{@fun BigDecimal}, {@fun BigInt}