Introduction
Smart contracts are written in languages like Solidity, but then are compiled into lower-level machine instructions as they’re deployed on the blockchain. ABIs (Application Binary Interfaces) allow us to speak this low-level language when we tell smart contracts to do specific things.
Essentially, ABIs are the bridge between the Internet and smart contracts. Today’s write-up explains ABIs and how they can provide you with information to protect yourself in Web3. Enjoy.
Sections
Transaction Calldata
What ABIs Do
How to Use the ABI Database
ABIs in the Ethereum Ecosystem
In Conclusion
*Thank you to Ziad for proofreading.
Transaction Calldata
Smart contracts are blockchain applications that can often do several different things. 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.
For example, an NFT smart contract can be used to mint tokens using a “mint” function, but could also allow the owner to change the metadata with a different function. The “calldata” section includes information about which of the two functions is being called.
Information in the “calldata” section is expressed in bytecode, the language that smart contracts are expressed in on the blockchain. ABIs allow our transactions to provide instructions in bytecode so that we can tell smart contracts what to do in the language they understand.
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.”
Step 2: Copy the first 8 characters.
Step 3: Go to the Function Signatures tab on 4byte.directory and paste those 8 characters into the search bar.
In the above example, two results appear when searching for “0xa22cb465.” 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.
The “de facto” ABI format on Ethereum is as a JSON file which is generated when a smart contract is compiled. This file contains much of the information about the contract, including the functions it can perform. Calling on those functions becomes extremely easy using this information presented by the ABI.
In Conclusion
ABIs make transactions seamless by converting them into instructions the EVM can understand and execute. This allows the use of many programming languages other than Solidity to write smart contracts. Regardless of the language they’re written in, commands are translated into bytecode, and ABIs convey our instructions to the EVM in that language.
Thank You & Additional Reading!
Thanks a lot for reading! Here are some more resources if you'd like to dive deeper.
Function Signature Database by 4byte.directory
Please like this post and sign up for more simple write-ups on blockchain concepts.
If you’d like me to cover any topics or have any questions, reach out in the comments!
Stay kind. Stay curious.