Application Binary Interfaces (ABIs) for Smart Contracts
Published: 2023-12-13 15:27:48

Application Binary Interfaces (ABIs) are essential to smart contracts on Ethereum and other blockchain platforms. 

An ABI defines how to interact with a smart contract at a low level, specifying the functions, arguments, return types, and other details required to call the contract and execute it as expected.

Understanding ABIs is important for developers building decentralized applications (dApps) with smart contracts. 

When your application needs to call a function in a smart contract, it uses the ABI to format the function call properly. The Ethereum Virtual Machine (EVM) then knows how to execute that call to run the corresponding smart contract code.


####Defining the ABI

To define what an ABI means, we must first understand how it works and its role in the EVM (Ethereum Virtual Machine). 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnsq1mviurdnca6wxmhj.png)


We will break down this code and see how the ABI operates. Find the full source code here.

This code snippet demonstrates how an application would use a smart contract's ABI (Application Binary Interface) to format a function call to the Ethereum Virtual Machine (EVM).
Let's break this down line-by-line:

```solidity
    scofield[] public listofScofieldcontract;
```
This declares an array of smart contracts based on the `scofield` contract definition imported from the `new1.sol` file. This array will keep track of all `scofield` contract instances deployed.

```solidity
    function get_new_contract() public {
      scofield newlist = new scofield();
      listofScofieldcontract.push(newlist); 
    }
```
When this `get_new_contract` function gets called, it will deploy a new instance of the `scofield` contract using the `new` keyword. It adds this instance to the `listofScofieldcontract` array to track it.

```solidity
    function store (uint _index, uint storeNumber) public {
      listofScofieldcontract[_index].store(storeNumber); 
    }
```
This `store` function call is a key example of using the ABI to format the function call to the EVM.
It takes in `_index` to reference which `scofield` contract in the array to call. Then, it calls the `store` function on that contract instance, passing in the `storeNumber` value.

The ABI for `scofield`, defined in its Solidity source code, specifies that `store` takes one `uint` parameter. So based on that, this code formats the call correctly for the EVM by packing the `storeNumber` argument into a transaction call to execute the `store` function.

The EVM uses the ABI specification to know how to unpack the call data, execute the `store` function code with the value passed, and update the contract's storage.

So in summary, the ABI acts as the interface specification that enables formatting function calls like `store` so the EVM understands how to execute the smart contract code as intended. The application leverages the ABI details to ensure proper encoding and decoding.

Now that we know what an ABI is, let’s dive into the EVM and see what lies behind the rabbit hole.


####Ethereum Virtual Machine 

If you want to understand what makes Ethereum tick, you need to understand the concept of the Ethereum Virtual Machine (EVM). The EVM is the runtime environment that handles the execution of all smart contracts and decentralized applications (dapps) on the Ethereum blockchain.

In simple terms, the EVM is a giant, global computer comprising thousands of individual nodes that all execute code synchronized. It allows developers to deploy bytecode applications that have predictable behavior, are resistant to tampering, and can access blockchain state data.

####The Role of the EVM
The EVM handles key functions that make Ethereum decentralized and secure:

- Runs bytecode when transactions trigger - The EVM executes smart contract code when a transaction invokes a function call. All nodes run the code to validate outcomes.
- Manages state and accounts - Contract data lives on the EVM in a Merkle Patricia tree data structure. User balances and contract states are preserved in this state database.
- Enforces determinism - The EVM always produces the same output for a given input, allowing 1000s of nodes to verify contract outputs.
- Provides security through isolation - Code runs in a virtual sandbox separated from the node's operating system for increased fault tolerance.
    
####EVM Runtime Life Cycle

When an Ethereum transaction gets created, here is the life cycle that occurs in the EVM:

1. Transaction gets propagated and included in a block
2. Transaction data gets passed to the EVM
3. EVM validates signatures and checks nonces to prevent double spends
4. The gas fee for transaction execution gets deducted
5. Contract bytecode begins running through the EVM instruction set
6. Execution reverts or succeeds; execution gas gets reconciled
7. Contract state gets updated, events are logged, Ether gets transferred
8. Output validated across nodes, state roots updated
    
####Understanding Gas

The concept of "gas" is important for the EVM. Because execution requires computational resources, each operation in the EVM is assigned a gas cost. When contracts run, gas is slowly depleted based on operations performed.

This prevents infinite loops, encourages efficiency, and incentivizes miners who execute the code. Gas allows allocating EVM resources efficiently.

####EVM vs. Web Assembly Comparison

The EVM has some similarities and differences compared to the popular web technology Web Assembly (WASM):

- Like WASM, the EVM uses a bytecode format for code execution. This allows any high-level language to compile down to EVM bytecode.
    
- Unlike WASM, the EVM was built specially for decentralized, tamper-proof, and transparent blockchain characteristics. This allows including hash checks, signatures, gas metering and state storage.
    
- Due to specialization for blockchain, EVM contracts tend to be simpler and smaller than typical WASM programs. The EVM design forces programmers to think differently.
    
####The EVM as World Computer

A helpful perspective is thinking of the EVM as a globally accessible computer anyone can use. Developers write code that gets deployed to this "world computer" that has unique properties:


- Reliable computation - Code executes exactly as written thanks to deterministic EVM rules
- Permanent storage - Unlike servers that can go down, data persists on thousands of nodes forever
- Open Access - Anyone can read data or execute code by sending transactions
- Verified outputs - Nodes check that execution results match consensus rules
EVM in Remix 

These steps will guide you if you want to see how the EVM works. 


1. Write Solidity Code
![](https://paper-attachments.dropboxusercontent.com/s_95614469E595EE0E6297214EF83102EAB8D30B0445E937312C7A12666FC3728D_1702478049800_Screenshot+2023-12-13+at+15.34.05.png)


Like any development environment, you can write and edit Solidity smart contracts code in Remix's file explorer.


2. Compile to Bytecode
![](https://paper-attachments.dropboxusercontent.com/s_95614469E595EE0E6297214EF83102EAB8D30B0445E937312C7A12666FC3728D_1702478092895_Screenshot+2023-12-13+at+15.34.47.png)


When ready, Remix will compile the Solidity code to EVM bytecode to be deployed and executed.


4. Local Simulation of Blockchain Environment
![](https://paper-attachments.dropboxusercontent.com/s_95614469E595EE0E6297214EF83102EAB8D30B0445E937312C7A12666FC3728D_1702478239996_Screenshot+2023-12-13+at+15.37.12.png)


Remix allows injecting a Web3 provider like Metamask to connect to a local blockchain like Ganache or a public testnet.

Remix simulates the blockchain transaction lifecycle by allowing setting things like block number, timestamps, account balances and nonces.

You can use a testnet faucet like Goerli or Sapolia, which works like real money, to simulate the real EVM. You must be connected to a wallet; one of the favorites is Metamask.


4. Transactions Trigger Code Execution
![](https://paper-attachments.dropboxusercontent.com/s_95614469E595EE0E6297214EF83102EAB8D30B0445E937312C7A12666FC3728D_1702478746161_Screenshot+2023-12-13+at+15.45.39.png)


A contract function call initiates a transaction to trigger code execution, just like on the actual EVM.

In conclusion, the Ethereum Virtual Machine is a giant, decentralized world computer, making it possible to create uncensorable blockchain applications. Understanding the EVM helps clarify Ethereum’s capabilities and future possibilities for Web3 developers.