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

Decodes data.

exchange.IO(k, dataFormat, data)

Examples

  • The inverse operation of the exchange.IO("encode", ...) function.

    javascript
    function main() { // A tuple is written in parentheses, with fields arranged by position; here is a three-field // tuple plus a bytes parameter var types = "(uint256,uint8,address),bytes" // Tuple values are passed as an array by position var ret = exchange.IO("encode", types, [30, 20, "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"], "0011") Log("encode: ", ret) // A literal tuple has no field names, so fields are numbered by position as Field1, Field2... // To use meaningful field names, // you need to register the contract ABI first with exchange.IO("abi", ...), refer to the next // example var rawData = exchange.IO("decode", types, ret) Log("decode:", rawData) // Output: decode: [{"Field1":"30","Field2":20,"Field3":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"}, "0011"] }
  • The following example first performs encodePacked encoding on the path parameter, because the exactOutput method call to be encoded later requires path as a parameter. Then it performs encode encoding on the exactOutput method of the router contract, which has only one parameter of type tuple. The method name exactOutput is encoded as: 0x09b81346. Finally, the exchange.IO("decode", ...) method is used to decode according to (bytes,address,uint256,uint256) to obtain decodeRaw, whose fields are numbered by position as Field1 to Field4, and the value of each field corresponds in order to the contents of the variable dataTuple.

    javascript
    function main() { // register SwapRouter02 abi var walletAddress = "0x398a93ca23CBdd2642a07445bCD2b8435e0a373f" var routerAddress = "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45" var abi = `[{"inputs":[{"components":[{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMaximum","type":"uint256"}],"internalType":"struct IV3SwapRouter.ExactOutputParams","name":"params","type":"tuple"}],"name":"exactOutput","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"payable","type":"function"}]` // The abi only contains the partial content of the exactOutput method; the complete abi can be // found by searching online exchange.IO("abi", routerAddress, abi) // encode path var fee = exchange.IO("encodePacked", "uint24", 3000) var tokenInAddress = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" var tokenOutAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7" var path = tokenInAddress.slice(2).toLowerCase() path += fee + tokenOutAddress.slice(2).toLowerCase() Log("path:", path) var dataTuple = { "path" : path, "recipient" : walletAddress, "amountOut" : 1000, "amountInMaximum" : 1, } // encode SwapRouter02 exactOutput var rawData = exchange.IO("encode", routerAddress, "exactOutput", dataTuple) Log("method hash:", rawData.slice(0, 8)) // Output: 09b81346 Log("params hash:", rawData.slice(8)) // decode exactOutput params var decodeRaw = exchange.IO("decode", "(bytes,address,uint256,uint256)", rawData.slice(8)) Log("decodeRaw:", decodeRaw) }

Returns

TypeDescription

string / number / bool / object / array

When dataFormat has a single type, the decoded value is returned directly; with several types, an array is returned. Integers of uint64/int64 width or narrower are numbers and wider ones are decimal strings (in JavaScript and Python, an integer whose absolute value is 2^53 or more is also returned as a decimal string so that no precision is lost); address is a lowercase address starting with 0x (also on TRON); bytes is a hex string without 0x; fixed-size bytesN such as bytes32 is an array of byte values; a literal tuple is an object keyed Field1, Field2 and so on.

Arguments

NameTypeRequiredDescription

k

string

Yes

Function of the exchange.IO() function. When set to decode, it indicates that the function is used for data decoding.

dataFormat

string

Yes

Types and order of the data to be decoded.

data

string

Yes

Data to decode, a hex string with an optional 0x prefix.

See Also

Remarks

In terms of data processing, the exchange.IO() function supports not only encoding (encode) but also decoding (decode).

exchange.IO("unpack", ...) is an alias of exchange.IO("decode", ...).