The calling method of exchange.IO("api", "eth", ...)
function is used to call the Ethereum RPC method.
The exchange.IO("api", "eth", ...)
function returns the return value of the called RPC method.
string, number, bool, object, array, null and all other types supported by the system
exchange.IO(k, blockChain, rpcMethod) exchange.IO(k, blockChain, rpcMethod, …args)
The k
parameter is used to set the function of the exchange.IO()
function, set to "api"
indicates that the function is used to extend the call request.
k
true
string
The blockChain
parameter is used to set the function of the exchange.IO()
function, set to "eth"
indicates that the function is used for RPC method calls on the Ethereum Network.
blockChain
true
string
The rpcMethod
parameter is used to set the RPC method to be called by the exchange.IO()
function.
rpcMethod
true
string
The arg
parameter is used to specify the parameters of the RPC method to be called. There may be more than one arg
parameter. The type and number of arg
parameters depend on the RPC method specified by the rpcMethod
parameter.
arg
false
string, number, bool, object, array, function, null, and all other types supported by the system
function main() {
// "owner" needs to be replaced with the specific wallet address
// Parameter labels for the "latest" string position: 'latest', 'earliest' or 'pending', please refrer to https://eth.wiki/json-rpc/API#the-default-block-parameter
// The return value ethBalance is a hexadecimal string: 0x9b19ce56113070
var ethBalance = exchange.IO("api", "eth", "eth_getBalance", "owner", "latest")
// ETH has a precision unit of 1e18
var ethDecimal = 18
// Because of the JavaScript language precision, it is necessary to use the system underlying package function BigInt, BigDecimal to process
// Convert ethBalance to readable amount, 0x9b19ce56113070 to 0.043656995388076145
Log(Number((BigDecimal(BigInt(ethBalance))/BigDecimal(Math.pow(10, ethDecimal))).toString()))
}
Check the balance of ETH in your wallet:
function mian() {
// ETH has a precision unit of 1e18
var ethDecimal = 18
// Number of transfers, readable amount e.g. 0.01 ETH
var sendAmount = 0.01
// Due to the JavaScript language precision, it is necessary to use the system underlying encapsulated functions BigInt, BigDecimal to process, and to convert the readable amount to the data processed on the chain
var toAmount = (BigDecimal(sendAmount)*BigDecimal(Math.pow(10, ethDecimal))).toFixed(0)
// "toAddress" is the address of the recipient's ETH wallet at the time of the transfer, which needs to be filled in specifically, and toAmount is the number of transfers
exchange.IO("api", "eth", "send", "toAddress", toAmount)
}
For ETH transfers, you can set the {gasPrice: 11, gasLimit: 111, nonce: 111}
parameter, which is set on the last parameter of the exchange.IO()
function, according to your specific needs. You can omit the nonce
and use the system default, or leave gasLimit/gasPrice/nonce
unset and use the system default value for all.
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)
}
Query gasPrice
:
function toAmount(s, decimals) {
// The toAmount function can convert hex-encoded values to decimal values
return Number((BigDecimal(BigInt(s))/BigDecimal(Math.pow(10, decimals))).toString())
}
function main() {
// Coding approve (authorization) method calls
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)
}
Query eth_estimateGas
:
The second parameter of the exchange.IO()
function with "eth"
can directly call the RPC methods available to the Ethereum node server.
{@fun BigDecimal}, {@fun BigInt}
exchange.IO("abi", ...) exchange.IO("encode", ...)