Type/to search
Built-in Functions
Structures
Built-in Variables and Constants
Conventions
Exchanges
Capability Matrix
Centralized Exchanges
Decentralized Exchanges (DEX)
Prediction Markets
Securities and Futures
On-chain & Custom

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 unwrapWETH9 method as an example.

    javascript
    function 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.encode in Solidity.

    javascript
    function 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 tuple and bytes, so two more arguments must be passed when calling exchange.IO() to encode:

      1. The variable corresponding to the tuple type:
      json
      [30, 20, "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"]
      The tuple's values are passed by position as an array; the number, order and types of its elements must all match (uint256,uint8,address) in the types argument. A literal tuple has no field names; when decoded, the fields are named by position as Field1, Field2, and so on.
      1. The variable corresponding to the bytes type:
      json
      "0011"
    javascript
    function 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.

    javascript
    function main() { var path = ["0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "0xdac17f958d2ee523a2206206994597c13d831ec7"] // ETH address, USDT address var ret = exchange.IO("encode", "address[]", path) Log("encode: ", ret) }

Returns

TypeDescription

string

The encoded data.

Arguments

NameTypeRequiredDescription

k

string

Yes

Fixed as encode, indicating data encoding.

address

string

No

Smart contract address. When calling exchange.IO("encode", ...), passing the address argument encodes a method call on the smart contract; if the address argument is not passed, the function encodes the data according to the specified type sequence, which is equivalent to abi.encode in Solidity.

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 dataFormat argument.
arg can be passed multiple times; their types and number are determined by the dataFormat argument. Tuples are passed as arrays.

See Also

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", ...).