ABIs: How We Tell Smart Contracts What to Do - A Simple Explanation
Transaction Calldata
When someone performs a transaction with a smart contract, that transaction will include some data about itself in a section called “calldata.” This calldata determines which of the smart contracts functions are being used.
Information in the “calldata” section is expressed in bytecode, the language that smart contracts are expressed in on the blockchain. ABIs allow transactions to provide instructions in bytecode so that we can tell smart contracts what to do, in the language they understand.
https://preview.redd.it/pwil82lp0sia1.png?width=600&format=png&auto=webp&v=enabled&s=215ebb92257940feaf5c90fa9f86a93274760ef9
ABIs (Application Binary Interfaces)
An ABI standardizes how to translate instructions into bytecode. Once in bytecode, instructions appear as one long string of characters, but there is a structure that’s useful to understand.
- First 4 Bytes: The first 4 bytes, or 8 characters, in a transaction’s calldata section determine which function is being called in a smart contract. For example, the “mint” function.
- From the 5th Byte On: This is where any additional data is provided that the transaction may need. For example, how many tokens to mint.
Using the ABI Database
Since the first 4 bytes determine the function that’s being called, this information can be used to verify what a transaction is doing. The first 8 characters can be pasted into this database, which will give you information you can use to verify the transaction is performing an expected function.
Step 1: When presented with a transaction, click on “View full transaction details.”
https://preview.redd.it/lm20c0b41sia1.jpg?width=380&format=pjpg&auto=webp&v=enabled&s=6c4c58334164f5f3996b3c4ecfaf6cb6952cad41
Step 2: Copy the first 8 characters.
https://preview.redd.it/zj61zsa61sia1.jpg?width=380&format=pjpg&auto=webp&v=enabled&s=1abc30474d20caea33e14c290daf3cddac4a7a29
Step 3: Go to the Function Signatures tab on 4byte.directory and paste those 8 characters into the search bar.
https://preview.redd.it/iyr0b1r71sia1.jpg?width=1280&format=pjpg&auto=webp&v=enabled&s=0a17adbbd009059660a35af72bd45cdd847c607d
In this example, two results appear in our search. Since only 4 bytes are used to represent the function, some functions may share these first 4 bytes.
ABIs in the Ethereum Ecosystem
ABIs standardize how information to smart contracts should be structured, but they aren’t a part of the core Ethereum protocol (as opposed to the consensus algorithm, for example). The Ethereum protocol and EVM only look at the bytecode inputs, so ABIs that translate instructions into bytecode exist purely for our convenience. They are extremely useful in allowing us to perform smart contract operations easily.
This post is a summary of the full write-up on my newsletter. Check it out below!
Sign up to receive a simple explanation of a blockchain concept, delivered once a week.
Stay kind. Stay curious. 😶🌫️
https://ramiwrites.substack.com/p/abis-how-we-tell-smart-contracts?sd=pf