exchange.IO("encode", ...)
Encodes data using ABI encoding.
exchange.IO(k, dataFormat, ...args)
exchange.IO(k, address, dataFormat)
exchange.IO(k, address, dataFormat, ...args)Examples
-
Encoding a call to the
unwrapWETH9method as an example.javascriptfunction main() { // ContractV3SwapRouterV2 mainnet address : 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45 // unwrapWETH9 is not an ERC20 method: register the ABI first (built-in template) var router = "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45" exchange.IO("abi", router, "uniswapV3SwapRouter02") // 1 is amountMinimum, the minimum amount to unwrap, in wei (not 1 WETH): the router unwraps // all the WETH it holds into ETH and sends it to the recipient (the current wallet here); // the transaction fails if the amount is less than amountMinimum var data = exchange.IO("encode", router, "unwrapWETH9(uint256,address)", 1, exchange.IO("address")) Log(data) // Output: call data starting with 49404b7c (without 0x) } -
An encoding example equivalent to
abi.encodeinSolidity.javascriptfunction main() { var x = 10 var address = "0x02a5fBb259d20A3Ad2Fdf9CCADeF86F6C1c1Ccc9" var str = "Hello World" var array = [1, 2, 3] // uint is equivalent to uint256; you can also write uint directly var ret = exchange.IO("encode", "uint256,address,string,uint256[]", x, address, str, array) Log("ret:", ret) // Output: ret is a hexadecimal string formed by concatenating the following 32-byte fields in order // 000000000000000000000000000000000000000000000000000000000000000a x // 00000000000000000000000002a5fbb259d20a3ad2fdf9ccadef86f6c1c1ccc9 address // 0000000000000000000000000000000000000000000000000000000000000080 offset of str // 00000000000000000000000000000000000000000000000000000000000000c0 offset of array // 000000000000000000000000000000000000000000000000000000000000000b length of str // 48656c6c6f20576f726c64000000000000000000000000000000000000000000 data of str // 0000000000000000000000000000000000000000000000000000000000000003 length of array // 0000000000000000000000000000000000000000000000000000000000000001 first element of array // 0000000000000000000000000000000000000000000000000000000000000002 second element of array // 0000000000000000000000000000000000000000000000000000000000000003 third element of array } -
Supports encoding tuples or type sequences that contain tuples. In this example the type sequence consists of
tupleandbytes, so two more arguments must be passed when callingexchange.IO()to encode:-
- The variable corresponding to the tuple type:
The tuple's values are passed by position as an array; the number, order and types of its elements must all matchjson[30, 20, "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"](uint256,uint8,address)in thetypesargument. A literal tuple has no field names; when decoded, the fields are named by position asField1,Field2, and so on. -
- The variable corresponding to the
bytestype:
json"0011" - The variable corresponding to the
javascriptfunction main() { var types = "(uint256,uint8,address),bytes" var ret = exchange.IO("encode", types, [30, 20, "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"], "0011") Log("encode: ", ret) } -
-
Supports encoding arrays or type sequences that contain arrays.
javascriptfunction main() { var path = ["0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "0xdac17f958d2ee523a2206206994597c13d831ec7"] // ETH address, USDT address var ret = exchange.IO("encode", "address[]", path) Log("encode: ", ret) }
Returns
| Type | Description |
string | The encoded data. |
Arguments
| Name | Type | Required | Description |
k | string | Yes | Fixed as |
address | string | No | Smart contract address. When calling |
dataFormat | string | Yes | The method, types and order of the data to be encoded. |
arg | string / number / array / any | No | The specific data values matching the |
See Also
Web3 exchange.IO exchange.IO("decode") exchange.IO("encodePacked") exchange.IO("abi") exchange.IO("call")
Remarks
The exchange.IO() function wraps the encode method, which encodes a function call and returns it as a hex string. For swaps on Uniswap and PancakeSwap, you can use the Uniswap and PancakeSwap exchange objects directly, without encoding the calls yourself.
When encoding a method call on a smart contract, the method can be given as the method name, the full method signature or the method selector; standard ERC20 methods are built in, while other methods require registering the corresponding ABI first.
Returns a hexadecimal string without the 0x prefix. Values of type bytesN can be passed as a hexadecimal string (the 0x prefix is optional) or as an array of byte values; values of type address can be passed as TRON addresses starting with T.
exchange.IO("pack", ...) is an alias of exchange.IO("encode", ...).