exchange.IO("decode", ...)
Decodes data.
exchange.IO(k, dataFormat, data)Examples
-
The inverse operation of the
exchange.IO("encode", ...)function.javascriptfunction 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
encodePackedencoding on thepathparameter, because theexactOutputmethod call to be encoded later requirespathas a parameter. Then it performsencodeencoding on theexactOutputmethod of the router contract, which has only one parameter of typetuple. The method nameexactOutputis encoded as:0x09b81346. Finally, theexchange.IO("decode", ...)method is used to decode according to(bytes,address,uint256,uint256)to obtaindecodeRaw, whose fields are numbered by position asField1toField4, and the value of each field corresponds in order to the contents of the variabledataTuple.javascriptfunction 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
| Type | Description |
string / number / bool / object / array | When |
Arguments
| Name | Type | Required | Description |
k | string | Yes | Function of the |
dataFormat | string | Yes | Types and order of the data to be decoded. |
data | string | Yes | Data to decode, a hex string with an optional |
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", ...).