Surge Women

View Original

An Overview of Smart Contracts for Developers

By Linda Ikechukwu

Contracts are legally-binding agreements between two or more parties, in which there is a promise to exchange one element for another, based on pre-defined terms.

An example is a freelancer contract, in which the employer promises to pay or provide certain perks to the freelancer in exchange for specific deliverables.

Smart contracts are similar to traditional contracts in that they are also agreements between two or more parties. The difference is that they are enforced through code rather than by a country’s law. 

In this article, we’ll dive deep into what smart contracts are, how they work, and what you need to know to start writing your own smart contracts. 

What Is a Smart Contract?

In the simplest of terms, a smart contract is a piece of code (or a program) hosted on the blockchain that runs only when predetermined conditions are met (much like an if-then block). It defines the terms of an agreement and automates its execution. 

In the development of dApps, think of a smart contract as the backend component.

Automated and hosted on a decentralized network, smart contracts eliminate the need to depend on or trust any human administrator or enforcer. Smart contracts remove intermediaries like banks or lawyers.

For example, let's consider that freelancer contract again. In a traditional implementation, the freelancer works on the assigned task, hands over the expected deliverables to the employer, and then the employer pays the freelancer. But what if the employer refuses to pay the freelancer? A smart contract eliminates the possibility of that happening.

You can write a smart contract to rule out manipulation from the employer. The freelancer will upload their deliverables, and the smart contract will verify that the deliverable is as expected. If it is, the smart contract automatically transfers funds to the freelancer. If it isn't, the smart contract returns funds to the employer. 

In comparison to traditional contracts, smart contracts require less trust and provide more assurances - due to the immutability and decentralized nature of blockchain networks.

Anatomy of a Smart Contract

Like every other computer program, smart contracts are made up of building blocks. Below is a brief, high-level overview of what makes up a smart contract program to help you understand how they work. 

An account

A smart contract is deployed within an account (or what is popularly known as a wallet address) on the blockchain network. Just like your regular user accounts, a smart contract account can hold, send and receive cryptocurrency tokens. 

Functions

Like regular JavaScript programs, a smart contract is made up of functions that are executable units of code. They get and set data in response to transaction requests. 

A transaction request is an instruction to execute a smart contract code initiated by another account, e.g., transfer X cryptocurrency tokens to Y account. 

Events

Events are how smart contracts communicate with their corresponding frontend interface or another smart contract. When something meaningful happens within a smart contract, the smart contract can emit an event so that the frontend can process it. 

For example, when a smart contract transfers X cryptocurrency tokens to Y account, it can emit an event containing all the data about the transfer, so that the frontend can display it. 

Data

A smart contract program is also made up of data that includes but is not limited to environment, state, and function variables.

Storage

Each account has a persistent key-value pair storage. You can store things like state, derived calculations, and transactions. A smart contract cannot write to any storage apart from its own. 

Writing Smart Contracts

Because smart contracts are deployed on a blockchain network, the blockchain you intend to deploy on is crucial in determining how you build a smart contract.

The Ethereum blockchain is currently the most popular blockchain for deploying smart contracts. Other blockchains like Solana, Cardano, Polkadot, and Algorand also support smart contracts.  Since the Ethereum blockchain has the most documentation and trial and error with smart contracts, we recommend that you get started there before moving on to other blockchains. Worth noting is that many blockchains support the Ethereum Virtual Machine (EVM), so your Ethereum skills will be transferable. 

To write a smart contract, you need a smart contract programming language. For that, Ethereum core contributors created a language called Solidity

Solidity is an object-oriented, statically-typed, high-level programming language used to create smart contracts. It has a similar syntax to C++ and JavaScript, which makes it relatively easy to adopt. 

For example, here’s a demo greeting smart contact program written in Solidity: 

There are other smart contract development languages like Vyper and Yul. However, we're going with Solidity because it is the most widely used and has a large, accessible developer community. 

Setting up a smart contract Solidity development environment

To start writing your own smart contract, we can use a web-based IDE such as Remix, EthFiddle, or SettleMint if you just want to get your feet wet. But, for more flexibility, you need to set up a smart contract development environment locally on your computer. 

To set up a local smart contract development environment, install either the Truffle or Hardhat blockchain development tooling. These tools are to smart contract development what create-react-app is to React frontend development. They provide abstractions that simplify handling compilation, automated testing, deployments, and client-side development, allowing for smooth end-to-end dApp development.

Both Truffle and Hardhat are excellent blockchain development toolchains for JavaScript developers. Hardhat is more recent and possesses somewhat superior features. But, Truffle is more popular and is used in most tutorials online.

Another thing you can do to make local development easier for you is to install the official Solidity extension or plugin of your code editor. For example, here’s the link to the official VSCode Solidity extension. This will give you access to things like syntax highlighting, linting, snippets, and shortcuts.

Notable Smart Contract Development Resources and Tutorials

Here's a list of top-notch curated free resources that'll help you learn how to write smart contracts in Solidity and build your own dApps. 

Hello World Smart Contract for Beginners

This quick guide will teach you to how to write, test and deploy a simple ‘Hello World’ smart contract.

Intro to Web3.js · Ethereum Blockchain Developer Crash Course

This 8-part tutorial series shows you how to use the web3.js library to write code that allows you to read and write data to the blockchain with smart contracts. 

Solidity for Beginners · Smart Contract Development Crash Course

This course gives you a soft-landing introduction to the Solidity programming language. It will walk you through how to write your first Ethereum smart contracts with Solidity as a beginner. You don't have to know anything about the language to follow this tutorial.

Cryptozombies.io

Cryptozombies.io is an interactive web-based learning platform that offers step-by-step lessons to take you from the very basics of Solidity to writing your first smart contract, to creating your own fully-functional blockchain-based game.

How To Build A Blockchain App with Ethereum, Web3.js & Solidity Smart Contracts

This 3-hour video course takes you from basics to building a fully-fledged crypto-based marketplace with Ethereum, Web3.js, and Solidity smart contracts. You don't have to know anything about blockchain to follow along. 

Start Building the Future of the Internet

Whether you're a dev or a total newbie that's curious about web3 but not sure where to start, you've come to the right place. 

Surge is a global community focused on learning, educating, and securing women's place in Web3. Come geek out and learn about Web3 alongside other devs and enthusiasts. 

Join our Discord, follow us on Twitter, and subscribe to our newsletter below.