Smart contracts 101

I've noticed that smart contracts can be confusing, even for developers diving into web3.

Let's go back to the basics and explore how Ethereum smart contracts are created, deployed, and executed.

What are smart contracts?

Smart contracts are just programs on the Ethereum blockchain. It's a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.

Data = how many NFTs I have

Code = how to transfer NFT from A to B

How are smart contracts created?

To create a smart contract, developers usually write code in a programming language called Solidity. It's a high-level language designed specifically for Ethereum, inspired by C++, Python, and JavaScript.

You'll also see terms like Hardhat and Foundry when talking about smart contracts development. Those are special development environments that simplify the process of building, deploying, and testing smart contracts.

In Solidity, when we compile the code, we get bytecode and ABI.

Bytecode is the machine-readable format that the Ethereum Virtual Machine (EVM) can understand and execute. Bytecode consists of concise numeric codes, constants, and various data elements.

ABIs are application binary interfaces. They define the variables and methods that are available in a smart contract. Thanks to ABI, we know how to interact with that smart contract.

ABIs are represented as JSON and can look like this:

ABI
ABI

How are smart contracts deployed?

After the contract is compiled, it's time for deployment. Deployment means sending the contract to the Ethereum blockchain. It's done by creating a special transaction with the contract bytecode as data and no recipient address.

The contract is officially deployed when the deployment transaction is included in a block. The contract gets its own unique address on the blockchain, derived from the sender's address and nonce (number of transactions sent from a given address).

Fun facts:

  • You can calculate a smart contract address before deployment.

  • You can deploy a smart contract to multiple EVM chains to the same address.

Here is a Clone X contract deployment transaction. Notice the absence of a "To" address – this indicates that a new contract is being created (RTFKT: CloneX Token). The Input Data contains a lengthy string, which is the source code of the contract.

Deployment transaction
Deployment transaction

How to interact with smart contracts?

Now that the contract is deployed, anyone can interact with it through its functions.

There are two types of functions:

  • write functions that require transaction and cost gas

  • read functions that are free and don't require transaction

Write functions modify the contract state. These changes need to be propagated across the network, verified, and stored by validators in a new block. Validators require compensation for their work, which is why gas fees are associated with write functions.

Read functions don't require any changes to the blockchain state; they only access the current data. Since they don't involve modifying the blockchain, they can be executed locally on any node without needing a transaction or consensus. This makes them free of cost.

Validators pick up the transaction, execute the function within the EVM, and include the transaction in a block. The results of the function execution are recorded in the blockchain as part of the transaction receipt.

Conclusion

I hope this info helps demystify Ethereum smart contracts a bit. As smart contracts become more vital in shaping our digital future, it's important to understand how they work. Keep exploring and stay informed!

Subscribe to 0xMarko
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.