A smart contract and its bro

A smart contract and its bro

ABİ means big brother, the short form of ağabey in Turkish. Pun intended.

Hello, fren! I hope you are happy and well! Today’s article will draw a super simplified outline of what a smart contract is, as well as its not-so-famous helper, the ABI. Everyone pays credit to Batman for always saving Gotham, but what is Batman without Robin?

Let’s dive in!

Blockchain, Fintech, Web3, CrossFit, smart contracts - a mouthful of cringe buzzwords, eh?

I am more than aware that you recognize these terms. Youtube and TikTok bombard all of us with them. But do you know about ABI - Application Binary Interface? Of course, you do! And if not - don’t be repelled - it is way simpler than it sounds.

Smart Contracts

I want to give you a definition of a smart contract that avoids sounding too academic but is also correct and not misleading or confusing or another appropriate word. Here it goes:

A program, executed in the “world computer” that the Ethereum protocol represents. It can be found by its address (which looks like every other wallet address).

Kinda simplified, right? Would you like another one? It’s a teeny-tiny bit more “complex”, but if you have had any exposure to programming, you will get me:

An "object" (the Object Oriented Programming type of object) that contains data and functions which can be called when a transaction is sent to it.

Cool fact about smart contracts that you may never have realized:

Smart contracts are dormant - they never “run” on their own in the background. In other words, they don’t work like the other 29 tabs that you have currently opened in your Chrome browser. They “wake up” only when they need to execute commands that people demand (in the form of transactions).

You can think of them as the chess from "Harry Potter and the Philosopher's stone" - the chess never made any move unless Harry declares "Horse to E4".

The last thing to remember about smart contracts is that they are immutable. This means that once a software developer deploys a contract on the Ethereum blockchain, he cannot simply find it on a website like etherscan.io and edit its code. But he can write the code in such a way that the contract may be malicious or prone to exploits, hacks, and more. As a proud veteran of the 2021 memecoin hype, I will give you the most common example of a scammy contract. Please stay with me and don’t be afraid of the code:

function renounceOwnership() public onlyOwner {
 emit OwnershipRenounced(_owner);

To an inexperienced person, this function may look trustworthy, because you see the words “renounced” and “ownership”. In actuality, the only thing that it does is “emit” an event. This pretty much interprets the whole code as "when the owner executes this function, let everyone know that the ownership has been renounced".

And here comes the question - Will I become a millionaire if I simply declare to the entire Internet that I have become one?

Besides understanding that I need to grind hard in order to achieve millionaire status, an event in Solidity just emits information, it does not change or modify the contract in any way.

The correct and safe way to rewrite the code above would be:

function renounceOwnership() public onlyOwner {
_owner = address(0);
 emit OwnershipRenounced(_owner);

Adding the line in between basically says “Make the new owner of the contract the zero address”. A word on the zero address - think of it as a “black hole” - everything that it touches stays in it forever. This is because no one has access to its private key.

the zero address (1).png

Making the owner of the contract the zero address revokes access of the original owner to the contract code, as he cannot control it anymore.

Of course, all this can be further manipulated and there are many more clever ways to exploit a contract, but I just wanted to give you the most basic example.

The contract ABI

Hmm. Well, see - if you have read my article on the EVM, you would know that, although a contract is written in Solidity, it gets compiled by the Solidity compiler into bytecode. This bytecode is then uploaded to the blockchain. Remember, the majority of the internet is directly or indirectly written in Javascript! If you enter a website that is written in JS, with a button on its homepage that’s connected to a function of a smart contract, how does the website know that when you click on the button, the corresponding function of the smart contract ( written in Solidity ) should be called? To make it even simpler - how does the smart contract communicate with other programming languages like Javascript?

If you ask a programmer this question, he would probably reply with something like: “Via a library like web3.js”.

This answer is insufficient, because you are barely 10 days into your web3 programming path, so you keep asking. Eventually, the programmer will reach a point where he will start talking about the Application Binary Interface (ABI) of the smart contract. This web3.js library that he mentioned previously needs to look into the ABI and find the function corresponding to the button that you clicked on the website and " attach " it to the button.

Thus, an ABI can be defined as the set of callable functions of the smart contract, or a JSON file, that gives the ability of smart contracts to communicate and interact with external applications, websites, etc, which are stored off-chain.

Contract ABI is an interface that defines a standard scheme of how to call functions in a smart contract and get data back. It has a specification of its structure. (Feel free to dive even deeper into the ABI Specification.

I left the most interesting part for the end! Now we will briefly go through an example contract and its ABI and you will see that despite its “cluttered” look, full of brackets of various types, it is nothing more than a description of our smart contract’s function.

pragma solidity ^0.8.15;

contract AndrewTateTheOgSigma {
 function enterANumberPlateForYourBugatti (uint _number) public  pure returns (uint) {
     return _number;
 }
}

The Solidity syntax and keywords are a topic for multiple articles, but I will keep it short and concise here. Let’s go through what all this means:

The first line - pragma solidity ^0.8.15 With this line, the contract tells the Solidity compiler "Hey, compiler I would like you to compile me, but only if your version is 0.8.15 or above!"

contract AndrewTateTheOgSigma { This declares the start of the contract code and its name. Remember that contracts are frequently compared to objects in other languages. This is because their code is enclosed in { } braces.

function enterANumberPlateForYourBugatti (uint _number) external pure returns (uint) {

This is called a " function header " - it contains the name of the function, and what it accepts as an input (in this case a variable that I named "_number" of the type uint - unsigned integer). Public is one of the four visibility modifiers in Solidity - which means that this specific function can be called by other contracts and wallets (for comparison, there are also functions, labeled “private” - they are the exact opposite of “public” ) pure - another modifier, which simply means that this function does not modify the state of the contract i.e. it does neither write or read information from the contract.

returns (uint) { - being kind of self-explanatory, this states that the function will return an uint (an unsigned integer, also known as a number that cannot be negative). The curly brace indicates the start of the function body - this is where the code of this specific function is written.

return _number - being the only line in our function body, it returns the number that we used as an input (the variable named _number)

And now the ABI of this contract in a JSON format:

[
    {
        "inputs": [
            {
                "internalType": "uint256",
                "name": "_number",
                "type": "uint256"
            }
        ],
        "name": "enterANumberPlateForYourBugatti",
        "outputs": [
            {
                "internalType": "uint256",
                "name": "",
                "type": "uint256"
            }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
    }
]

There it is. Remember that this JSON that we are talking about is actually an array (enclosed in [ ] braces ) that contains objects ( surrounded with { } ). These objects represent the different functions of the contract. In this example, since the function is one, the object that represents it in this file is also one.

The brackets declare the start and the end of the object that contains our contract's function.png

The function object contains four properties, which are actually the parts of the function header! Is it starting to make sense now? The inputs property of the function object contains an array with a single object and that represents the properties of our input variable - it is named _number, of the type uint256, which is an alias for uint.

Then comes name - the name of the function, and outputs - what the function returns. In our case, this is an unnamed unsigned integer (unnamed because we have not defined a name and unsigned integer because we stated that the type of the variable that the function will return is of the uint type.

And we are done! Lets now sum it up in a TLDR format to fortify what we learnt today:

  1. A smart contract represents a program, executed in the “world computer” that the Ethereum protocol represents. It can be found by its address (which looks like every other wallet address.)

  2. The most important property of a smart contract is its immutability- which means that once deployed, its code cannot be changed or modified. Important to notice that although immutable, the code may be written in a way that makes the contract malicious, or gives its deployer permissions to secretly execute functions that he should not.

  3. The Application Binary Interface is Robin, of the Batman and Robin duo - almost no one talks about it, but a smart contract would be worthless if the ABI was not present, because the contract would not be able to communicate with other contracts, or with applications and websites, which are off-chain.

  4. The human-readable version of the ABI comes in a JSON file format. It represents an array that contains one object for every single function, event, or error descriptions.

P.S. Paragraphs from this article were edited and styled by an anonymous user by the nickname of "Christina" from the RaidGuild Discord. I am lost for words! I know that you did not want any recognition, but you deserve it! You are proof that Web3 is full of happy and good people!

So, to my anon fren reading this - What better motivation to speed up your learning?

WAGMI

Did you find this article valuable?

Support Petar Todorov by becoming a sponsor. Any amount is appreciated!