The exchange.IO("decode", ...)
function is called in a way that is used for decoding.
The exchange.IO("decode", ...)
function returns the decoded data. Returns a string when there is only one data specified by the dataFormat
parameter. Returns an array when there is more than one data specified by the dataFormat
parameter.
array、string
exchange.IO(k, dataFormat, data)
The k
parameter is used to set the function of the exchange.IO()
function, and setting it to "decode"
means that the function is used for data decoding.
k
true
string
The dataFormat
parameter is used to specify the type and order of the decoded data.
dataFormat
true
string
The data
parameter is used to set the data to be decoded.
data
true
string
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)
var rawData = exchange.IO("decode", types, ret)
Log("decode:", rawData)
}
Inverse operation of the exchange.IO("encode", ...)
function:
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"}]`
exchange.IO("abi", routerAddress, abi) // abi only uses the contents of the local exactOutput method, the full abi can be searched on the Internet
// 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)) // 09b81346
Log("params hash:", rawData.slice(8))
// decode exactOutput params
var decodeRaw = exchange.IO("decode", "tuple(path bytes,recipient address,amountOut uint256,amountInMaximum uint256)", rawData.slice(8))
Log("decodeRaw:", decodeRaw)
}
The following example first performs an encodePacked
operation on the path
parameter processing, because the exactOutput
method call that needs to be encoded later requires path
as a parameter. Then encode
the exactOutput
method of the routing contract, which has only one parameter of type tuple
. The method name exactOutput
is encoded as: 0x09b81346
, and using the exchange.IO("decode", ...)
method to decode the resulting decodeRaw
, consistent with the variable dataTuple
.
For data processing, the exchange.IO()
function supports not only encoding, but also decoding.