From 1293973a1bda202a692bf5f30d6f44a2a5b75be5 Mon Sep 17 00:00:00 2001 From: bt3gl <1130416+bt3gl@users.noreply.github.com> Date: Thu, 29 Sep 2022 05:03:11 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=AB=20add=20ABI=20encoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solidity/abi_encoding.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 solidity/abi_encoding.md diff --git a/solidity/abi_encoding.md b/solidity/abi_encoding.md new file mode 100644 index 0000000..1894d17 --- /dev/null +++ b/solidity/abi_encoding.md @@ -0,0 +1,24 @@ +## ABI encoding + +* the solidity built-in function `abi.encode` encodes solidity types into raw bytes, that can be interpreted directly by the EVM. + + +``` +contract StringEncoding { + bytes public encodedString = abi.encode("hacking"); +} +``` + +* this is what happens: + 1. 1st (32 bytes) word = offset → indicates at which bytes index the string starts. If you count 32 from the beginning (= index 32), you will reach the starting point of where the actual encoded string starts. + 2. 2nd (32 bytes) word = string length → in the case of the string, this indicates how many characters (including whitespaces) are included in the string. + 3. 3rd (32 bytes) word = the actual utf8 encoded string → each individual bytes corresponds to hex notation of a letter / character encoded in utf8. + +
+ +#### other ABI Encodings + +* address payable -> address +* contract -> address +* enum -> uint8 +* struct -> tuple of elementry types