abi.encode encodes data into bytes. abi.decode decodes bytes back into data. // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; contract AbiDecode { struct MyStruct { string name; uint[2] nums; } function encode( uint x, address addr, uint[] calldata arr, MyStruct calldata myStruct ) external pure returns (bytes memory) { return abi.encode(x, addr, arr, myStruct); } function decode(bytes calldata data) external pure returns ( uint x, address addr, uint[] memory arr, MyStruct memory myStruct ) { // (uint x, address addr, uint[] memory arr, MyStruct myStruct) = ... (x, addr, arr, myStruct) = abi.decode(data, (uint, address, uint[], MyStruct)); } }
原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/278127.html