Support for encode
The exchange.IO() function encapsulates the encode method, which can encode function calls and return them as hex string format. For specific usage, please refer to the platform's public "Uniswap V3 Trading Library" template.
The following example shows how to encode the unwrapWETH9 method call:
javascript
function main() {
// ContractV3SwapRouterV2 mainnet address: 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45
// Calling the unwrapWETH9 method requires registering the ABI first, registration steps are omitted here
// "owner" represents the wallet address, you need to fill in the actual address, 1 represents the unwrap amount, unwrapping one WETH to ETH
var data = exchange.IO("encode", "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45", "unwrapWETH9(uint256,address)", 1, "owner")
Log(data)
}
When calling the exchange.IO("encode", ...) function, if the second parameter (string type) starts with 0x, it indicates encoding a method call on a smart contract.
If it doesn't start with 0x, it is used to encode the specified type sequence, functionally equivalent to abi.encode in solidity. Please refer to the following example.
javascript
function main() {
var x = 10
var address = "0x02a5fBb259d20A3Ad2Fdf9CCADeF86F6C1c1Ccc9"
var str = "Hello World"
var array = [1, 2, 3]
var ret = exchange.IO("encode", "uint256,address,string,uint256[]", x, address, str, array) // uint means uint256, type length must be specified on FMZ
Log("ret:", ret)
/*
000000000000000000000000000000000000000000000000000000000000000a // x
00000000000000000000000002a5fbb259d20a3ad2fdf9ccadef86f6c1c1ccc9 // address
0000000000000000000000000000000000000000000000000000000000000080 // offset of str
00000000000000000000000000000000000000000000000000000000000000c0 // offset of array
000000000000000000000000000000000000000000000000000000000000000b // length of str
48656c6c6f20576f726c64000000000000000000000000000000000000000000 // str data
0000000000000000000000000000000000000000000000000000000000000003 // length of array
0000000000000000000000000000000000000000000000000000000000000001 // first element of array
0000000000000000000000000000000000000000000000000000000000000002 // second element of array
0000000000000000000000000000000000000000000000000000000000000003 // third element of array
*/
}
Supports encoding of tuples or type sequences containing tuples:
javascript
function main() {
var types = "tuple(a uint256,b uint8,c address),bytes"
var ret = exchange.IO("encode", types, {
a: 30,
b: 20,
c: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
}, "0011")
Log("encode: ", ret)
}
This type sequence consists of tuple and bytes, so when calling the exchange.IO() function for encode, two parameters need to be passed:
- Variable corresponding to tuple type:
The passed parameters must match the structure and types of thejson{ a: 30, b: 20, c: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" }tuple, as defined in thetypesparameter:tuple(a uint256,b uint8,c address). - Variable corresponding to bytes type:string"0011"
Supports encoding of arrays or type sequences containing arrays:
javascript
function main() {
var path = ["0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "0xdac17f958d2ee523a2206206994597c13d831ec7"] // ETH address, USDT address
var ret = exchange.IO("encode", "address[]", path)
Log("encode: ", ret)
}