Understand a smart contract; Web3 Basics

That is a great question! People have built great and often weird things on smart contracts:

  • Lending money to strangers
  • Communities where people get together (my own favourite!)
  • Verifying who owns a monkey on a yacht
  • Parts of video games

It is hard to come up with a single definition of what is a smart contract. Some have tried labelling it as a giant computer, or an automatic execution of instructions. These definitions are interesting, but they don't help us build a real understanding. To build that understanding, we will need to understand a few things. But first, we need to understand where they live and run: a blockchain. Let's define what a blockchain is: A decentralized fault-tolerant 😴😴... It's complicated 😅.

Really smart people are at work to create the best blockchains possible. I won't go into the technical aspects of how they work, but we still to understand the basics:

Blockchain: People coming together to agree on who owns what

At its most fundamental level, this is what a blockchain is. A blockchain keeps track of what people transfer to each other. To make this work, we use servers that are connected and reach an agreement on who owns an asset. These servers approve of people sending money to each other.

A smart contract plays the role of an intermediary

If a blockchain is people connected to each other, you can think of a smart contract as a set of rules to facilitate how we connect to each other. People sending money to each other is simple and does not require a smart contract, but to make a blockchain more useful, smart contracts were invented. Let me present my own definition of a smart contract:

Smart contracts: Code that is executed on the blockchain to facilitate exchanges

If the blockchain is great at tracking who owns what, it lacks features. It cannot replace fully a bank or a real estate marketplace. This is why we have smart contracts to expand the capacities of blockchains. They simply are code that is executed on the blockchain so that our exchanges on it are more interesting!

Smart contract vocabulary

Creating a smart contract will feel really familiar to coding a regular backend. Be careful with this comparison! It is a different way of thinking! Now that you understand what a smart contract is, here are a few words to be familiar with before starting:

  • Solidity: The most popular programming language to write a smart contract. It contains a familiar syntax, but it also contains features that are a gateway to the Ethereum or similar blockchain.
  • Smart contract address: A smart contract behaves the same way as an automated wallet. A smart contract can send and receive funds and has an external address. (See my post on wallets and authentication)
  • ABI: A smart contract contains code that is uploaded. This code must be understood by libraries and the blockchain. The ABI contains a JSON representation of every function in the smart contract.
  • Gas: Since the smart contract runs on a shared network, you must pay to interact with it! You pay a fee called Gas. This is normally paid using the cryptocurrency native to the blockchain you are using.
  • ERC: Ethereum Request for Comments is a standard used by the Ethereum network to describe the functionalities of the blockchain. Smart contracts often follow ERC standards.

Different platforms have different caveats and programming languages to develop smart contracts, but this post is meant to get you started instead of teaching you how to code. Instead of exploring Solidity as a language, let's look at the tooling needed to deploy a smart contract on Ethereum.

Solidity Tooling

The Solidity ecosystem when compared to other programming languages is in its infancy. Nonetheless, the ecosystem of tools gives fairly good coverage of a developer's needs. I will cover what I consider the standard set of tools I use to develop smart contracts. These tools normally build upon the JavaScript ecosystem.

Smart contracts have a slow development cycle!

This is mostly due to the fact that a deployed smart contract cannot be rewritten. Once it lives on the blockchain, there is no way to correct a bug or typo.

The realization that we cannot iterate easily on a smart contract once it is deployed will affect our choice of tooling! We are looking for tools that will let us run assertions and powerful security checks on our contracts. Here are the tools that I consider standard and well accepted by the community.

Hardhat => Task runner and orchestration

Hardhat is mostly a task runner. It lets you configure your development environment in order to execute scripts, tests and start a local Ethereum test network. It mostly acts as the glue to the different tools we will discuss later. Its most useful feature is the ability to start and deploy contracts to a local network and debug their execution. Interestingly for JS developers, Hardhat also supports adding console.log statements to your smart contracts.

Jest => Testing library

Hardhat normally comes bundled with the Chai testing library. Here I diverge and always install Jest instead of Chai. This is a personal preference. I choose Jest as interactions with smart contracts are mostly asynchronous in nature and Chai requires additional libraries to work with Javascript promises.

Using Jest or Chai is a powerful idea in developing your smart contracts. It gives you the ability when paired with Hardhat to work in a test-driven development environment.

Ethers => Javascript Ethereum client library

Hardhat and Jest alone are useless when it comes to working with smart contracts. For this reason, we use Ethers.js to interact with smart contracts. Ethers.js contains the code necessary to deploy a smart contract and even call functions from a deployed smart contract. Hardhat ships with a customized version of Ethers to allow users to assume quickly the role of specific wallets while testing.

Typechain => Type generation for smart contracts

Lastly, we have my favourite tool: TypeChain. Smart contracts are difficult to interact with from a UI. TypeChain lets you generate types and classes that you can use as a convenient wrapper over Ethers.

Conclusion

It is not possible to get into a single post on all you need to become a smart contract developer! This will be the subject of a full course I intend to launch! In the meantime, I open-sourced a repository of a test NFT mint I built. As always, thank you for reading!

Questions? Reach me on Twitter!