The exchange.IO("api", ...)
function is called in a way that is used to call the methods of the smart contract.
The exchange.IO("api", ...)
function returns the return value of the called smart contract method.
string, number, bool, object, array, null and all other types supported by the system
exchange.IO(k, address, method) exchange.IO(k, address, method, …args) exchange.IO(k, address, method, value, …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 address
parameter is used to specify the address of the smart contract.
address
true
string
The method
parameter is used to specify the method of the smart contract to be called.
method
true
string
The value
parameter is used to set the amount of ETH to be sent. The stateMutability
attribute of the smart contract method to be executed is payable
, then the value
parameter needs to be passed. The "stateMutability": "payable"
attribute can be viewed from the ABI. The exchange.IO()
function will determine the required parameter based on the stateMutability
attribute in the ABI that has been registered. If the stateMutability
attribute is nonpayable
, then the value
parameter does not need to be passed.
value
false
number、string
The arg
parameter is used to specify the parameters of the method of the smart contract to be called. There may be more than one arg
parameter, and the type and number of arg
parameters depends on the method of the smart contract to be called.
arg
false
string, number, bool, and all other types supported by the system
function main(){
var tokenAddress = "0x111111111117dC0aa78b770fA6A738034120C302" // The contract address of the token, the token is 1INCH in the example
Log(exchange.IO("api", tokenAddress, "decimals")) // Query, print 1INCH tokens with precision index of 18
}
The decimals
method is a constant
method of ERC20 that does not incur gas consumption and it can query the precision data of a token. The decimals
method has no parameters. Return value: the precision data of the token.
function main(){
// The contract address of the token, in the example the token is 1INCH
var tokenAddress = "0x111111111117dC0aa78b770fA6A738034120C302"
// For example, the query yields 1000000000000000000, divided by the precision unit of the token 1e18, the wallet to which the current exchange object is bound has authorized 1 1INCH to the spender address
Log(exchange.IO("api", tokenAddress, "allowance", "owner", "spender"))
}
The allowance
method is a constant
method of ERC20 that does not generate gas consumption and can query the authorized amount of a token for a certain contract address. The allowance
method takes 2 parameters, the first one is the wallet address and the second one is the authorized address. Return value: the authorization amount of the token.
owner
: the address of the wallet, the example is replaced by the string “owner”, the actual use needs to fill in the specific address. spender
: the address of the authorized contract, the example is replaced by the string “spender”, the actual use needs to fill in the specific address, for example, it can be Uniswap V3 router v1
address.
function main(){
// The contract address of the token, the token is 1INCH in the example
var tokenAddress = "0x111111111117dC0aa78b770fA6A738034120C302"
// The hexadecimal string of the authorization amount: 0xde0b6b3a7640000 , the corresponding decimal string: 1e18 , 1e18 divided by the precision unit of the token, i.e. 1 token amount, so this refers to the authorization of one token
Log(exchange.IO("api", tokenAddress, "approve", "spender", "0xde0b6b3a7640000"))
}```
The ```approve``` method is a non-```constant``` method of ERC20, which generates gas consumption and is used to authorize the operation amount of a token to a contract address. The ```approve``` method takes 2 parameters, the first one is the address to be authorized and the second one is the amount to be authorized. Return value: txid.
```spender```: the address of the authorized contract, the example is replaced by the string "spender", the actual use needs to fill in the specific address, for example, it can be ```Uniswap V3 router v1``` address. ```0xde0b6b3a7640000```: the number of authorizations, here is the hexadecimal string, the corresponding decimal value is 1e18, divided by the token precision unit in the example (i.e. 1e18). The result is that 1 token is authorized. The third parameter of the ```exchange.IO()``` function is passed to the method name ```approve```, which can also be written in the form of methodId, such as "0x571ac8b0". It is also possible to write the full standard method name, for example: "approve(address,uint256)".
```javascript
function main() {
var ContractV3SwapRouterV2 = "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45"
var tokenInName = "ETH"
var amountIn = 0.01
var options = {gasPrice: 5000000000, gasLimit: 21000, nonce: 100} // This is an example, depending on the actual scene settings
var data = "" // The encoded data, here is the empty string, depending on the actual scene settings
var tx = exchange.IO("api", ContractV3SwapRouterV2, "multicall(uint256,bytes[])", (tokenInName == 'ETH' ? amountIn : 0), (new Date().getTime() / 1000) + 3600, data, options || {})
}
The multicall
method is a non-constant
method of Uniswap V3
that generates gas consumption and is used to redeem tokens in multiple ways.
The multicall
method may have various ways of passing parameters, you can check the ABI that contains the method specifically, you need to register the ABI before calling the method. Return value: txid.
For specific examples of multicall
method calls, you can refer to the platform’s publicly available “Uniswap V3 Trade” Template
Some details are described here using pseudo-code:
exchange.IO("api", ContractV3SwapRouterV2, "multicall(uint256,bytes[])", value, deadline, data)
ContractV3SwapRouterV2
: address of router v2 of Uniswap V3.
value
: the amount of ETH to transfer, set to 0 if the tokenIn token of the exchange operation is not ETH.
deadline
: deadline
is the parameter of the multicall
method, which can be set to (new Date().getTime() / 1000) + 3600, indicating that it is valid for one hour.
data
: data
is the parameter of the multicall
method, the data of the packing operation to be performed.
Similar to exchange.IO("api", "eth", "send", "toAddress", toAmount)
, the gasLimit/gasPrice/nonce
setting of the method call can be specified when calling the multicall
method.
Again, we use pseudo-code to describe:
exchange.IO("api", ContractV3SwapRouterV2, "multicall(uint256,bytes[])", value, deadline, data, {gasPrice: 123456, gasLimit: 21000})
The parameter {gasPrice: 11, gasLimit: 111, nonce: 111}
can be set according to specific needs, which is set to the last parameter of the exchange.IO()
function.
You can omit the nonce
and use the system default value, or leave gasLimit/gasPrice/nonce
unset and use the system default value for all.