header-langage
简体中文
繁體中文
English
Tiếng Việt
한국어
日本語
ภาษาไทย
Türkçe
Scan to Download the APP

Simple Introduction to Solidity|Lecture 27: ABI Encoding and Decoding

2022-09-09 09:30
Read this article in 9 Minutes
This article introduces 4 ABI encoding methods and 1 ABI decoding method.
Original title: " A Minimalist Introduction to Solidity: 27. ABI Encoding and Decoding "
Original author: 0xAA


I am re-learning solidity recently, to consolidate the details, and also write an "Introduction to Solidity Minimalist", For novices (programmers can find other tutorials), 1-3 lectures are updated every week.


All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity


ABI  ;(Application Binary Interface, Application Binary Interface) is a standard for interacting with Ethereum smart contracts. Data are encoded based on their type; and since encoding does not contain type information, their type needs to be noted when decoding.


In Solidity, ABI encoding has 4 functions: abi.encode, abi.encodePacked, abi.encodeWithSignature, abi.encodeWithSelector. And ABI decoding has a function: abi.decode, which is used to decode the data of abi.encode. In this lecture, we will learn how to use these functions.


ABI encoding


We will encode 4 variables, their types are uint256 , address, string, uint256[2]:



abi.encode


Use ABI for given parameters Rule encoding. ABI is designed to interact with smart contracts. It fills each parameter into 32 bytes of data and splices them together. If you want to interact with the contract, you will use abi.encode.



The encoded result is 0x0000000000000000000000000000000000000000000000000000000000000a000000000000000000000007a58c0be72be218b41 c608b7fe7c5bb630736c710000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000 00000000000000000000000000004307841410000000000000000000000000000000000000000000000000000000, because abi.encode fills each data with 32 bytes, there are many 0s in the middle.


abi.encodePacked


Encode the given arguments according to the minimum space they require. It is similar to  abi.encode, but it will omit many zeros for padding. For example, only 1 byte is used to encode the uint type. When you want to save space and do not interact with the contract, you can use abi.encodePacked, such as calculating the hash of some data.



encoded The result is 0x000000000000000000000000000000000000000000000000000000000000a7a58c0be72be218b41c608b7fe7c5bb630736c71307841410000000000 00000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000006, due to abi. encodePacked compresses the encoding, and the length is much shorter than abi.encode.


abi.encodeWithSignature


Similar to abi.encode function, except the first parameter Sign the function, e.g. "foo(uint256, address)". Can be used when calling other contracts.



encoded The result is 0xe87082f1000000000000000000000000000000000000000000000000000000000000a000000000000000000000007a58c0be72be218b41c608b7 fe7c5bb630736c71000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000 00000000000000000000004307841410000000000000000000000000000000000000000000000000000000 is equivalent to adding a 4-byte function selector before the abi.encode encoding result.


abi.encodeWithSelector


Similar to abi.encodeWithSignature, except for the first parameter For the function selector, the first 4 bytes of the Keccak hash of the function signature.



encoded The result is 0xe87082f1000000000000000000000000000000000000000000000000000000000000a000000000000000000000007a58c0be72be218b41c608b7 fe7c5bb630736c71000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000 0000000000000000000000043078414100000000000000000000000000000000000000000000000000000000, same result as abi.encodeWithSignature.


ABI decoding 


abi.decode


abi.decode is used to decode the binary code generated by abi.encode and restore it to the original parameters.



We input the binary code of abi.encode to decode, and the original parameters will be decoded:


  abi.decode decode bytecode


Summary


In Ethereum In , data must be encoded into bytecodes to interact with smart contracts. In this lecture, we introduced 4 abi encoding methods and 1 abi decoding method.


Original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
This platform has fully integrated the Farcaster protocol. If you have a Farcaster account, you canLogin to comment
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit