The resource loading... loading...

exchange.IO("encode", …)

The exchange.IO("encode", ...) function is called for data encoding.

The exchange.IO("encode", ...) function returns the encoded data. string

exchange.IO(k, dataFormat, …args) exchange.IO(k, address, dataFormat) exchange.IO(k, address, dataFormat, …args)

The k parameter is used to set the function of the exchange.IO() function, set to "encode" means the function is used for data encoding. k true string The address parameter is used to set the address of the smart contract. When calling the exchange.IO("encode", ...) function, passing in the address parameter indicates encoding the method call on the smart contract. When calling the exchange.IO("encode", ...) function, if the address parameter is not passed, the function is used to encode the specified type order and is functionally equivalent to abi.encode in Solidity. address false string The dataFormat parameter is used to specify the method, type, and order of the encoded data. dataFormat true string The arg parameter is used to specify the specific data value that matches the dataFormat parameter. There may be more than one arg parameter, and the type and number of arg parameters depends on the dataFormat parameter setting. arg false string, number, tuple, array, and all other types supported by the system

function main() {
    // Main network address of ContractV3SwapRouterV2: 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45
    // Calling the unwrapWETH9 method requires registering the ABI first, which is omitted here
    // "owner" represents the wallet address, which need to fill in the specific, 1 represents the number of unpacking, unpacking a WETH into ETH
    var data = exchange.IO("encode", "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45", "unwrapWETH9(uint256,address)", 1, "owner")
    Log(data)
}

For example, calling the encoding method unwrapWETH9:

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 i.e. uint256 , the type length needs to be specified on FMZ
    Log("ret:", ret)
    /*
    000000000000000000000000000000000000000000000000000000000000000a    // x
    00000000000000000000000002a5fbb259d20a3ad2fdf9ccadef86f6c1c1ccc9    // address
    0000000000000000000000000000000000000000000000000000000000000080    // Offset of str
    00000000000000000000000000000000000000000000000000000000000000c0    // Offset of array
    000000000000000000000000000000000000000000000000000000000000000b    // The length of str
    48656c6c6f20576f726c64000000000000000000000000000000000000000000    // str data
    0000000000000000000000000000000000000000000000000000000000000003    // The length of the array
    0000000000000000000000000000000000000000000000000000000000000001    // array the first data
    0000000000000000000000000000000000000000000000000000000000000002    // array the second data
    0000000000000000000000000000000000000000000000000000000000000003    // array the third data
    */
}

It’s equivalent to the encoding example of abi.encode in Solidity:

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)
}

It supports encoding a tuple or a type order containing a tuple. This type order consists of tuple, bytes, so when calling exchange.IO() for encoding, you need to continue passing two parameters:

    1. Variables corresponding to the tuple type:
    
    {
        a: 30,
        b: 20,
        c: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
    }
    
    

    The parameters passed in must also be consistent with the structure and type of tuple, as defined in the types parameter of the form: tuple(a uint256,b uint8,c address).

    1. Variables corresponding to type bytes:
    "0011"
    
function main() {
    var path = ["0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "0xdac17f958d2ee523a2206206994597c13d831ec7"]   // ETH address, USDT address
    var ret = exchange.IO("encode", "address[]", path)
    Log("encode: ", ret)
}

It supports for sequential encoding of arrays or types containing arrays:

The exchange.IO() function encapsulates the encode method, which can return the function call code to hex string format. For specific use, you can refer to the platform’s publicly available “Uniswap V3 Trade” Template. When coding method calls on smart contracts, the corresponding ABI needs to be registered first.

exchange.IO("api", "eth", ...) exchange.IO("encodePacked", ...)