Have you ever wondered how to create your blockchain application? When it comes to Ethereum, it starts with smart contracts.
In this article, we will learn how to build a simple, smart contract on Ethereum and test it using the Truffle framework. Our smart contract will perform essential create, read, update, and delete (CRUD) operations.
We’ll focus on smart contracts written in Solidity language. We’ll use the Truffle Suite to deploy a local version of the Ethereum blockchain and compile smart contracts using Ethereum Virtual Machine (EVM).
Prerequisites
For this tutorial, the following software and packages are required:
- Node and its package manager,
npm
. We run the commandnode -v
&&npm -v
to verify we have them installed, or install them from here - Alternatively, we can use another package manager, Yarn
- An Ethereum blockchain, a smart-contract compiler
- A JavaScript library for communication, Web3.js
What is a smart contract?
In simple words, A smart contract is a piece of code that controls some digital asset. It defines rules for transferring assets and penalties like a traditional contract. The best thing is that it automatically performs these transfers and penalties based on pre-coded conditions without needing an intermediary.
What is Solidity?
Solidity is one of the most famous languages to code smart contracts on Ethereum. It is designed for smart contract programming. It’s syntactically similar to javascript.
What is the Truffle Suite?
Ethereum is a blockchain that allows applications to run on it. The code is written in Solidity language in the form of smart contracts. To compile these contracts, we need an Ethereum compiler, which converts smart contracts to machine-readable code.
The Truffle Suite is a collection of tools made specifically for blockchain development on Ethereum. The suite includes three pieces of software:
- Truffle, a framework for smart contract development
- Ganache, which enables you to set a personal Ethereum blockchain on the local network for testing and development
- Drizzle, which is used for creating DApp user interfaces and includes a collection of ready-to-use components
Installing Ganache
The smart contracts run on the Ethereum blockchain, so we require one for deployment and testing. We could also deploy on a live chain, but that would cost us Ether as a gas fee. So let’s set up a local chain and do our testing there. When you’re sure about the code and ready to distribute your application, you can deploy it on the live chain.
Ganache is the local chain installed on our computers and runs on localhost. Download Ganache from the Truffle Suite website.
We can see that Ganache provided ten accounts with 100 ETH each. These are fake Ethers, so don’t be too excited. Also, the chain is running on 127.0.0.1 at 7545 port. We will use these accounts to deploy our smart contracts on this chain. Ethers will help us pay gas fees.
Installing Truffle
Truffle provides the compiler for smart contracts. We need it to convert the Solidity code into machine-readable code that can be deployed on the Ganache blockchain.
Install Truffle using the following command:
$ npm install truffle -g
Yarn:
$ yarn add truffle
Creating smart contracts
To create smart contracts, we first need to create a project directory where we will keep all the Solidity files. Let’s create one with the name crud-app
and move to the directory in the terminal using cd crud-app
.
Right now, our project is empty. To work with it, we need some boilerplate code. For example, if we wish to create the UI in React, we’ll need to install React.
Truffle already provides some packages called boxes. These packages are bundles of different frameworks, such as Truffle, Ganache, React, Web3, and Redux, and there is one for Vue.js developers. Together, they complete the end-to-end application development, from client UI to blockchain smart contracts.
This article will use the React box provided by Truffle.
Installing the React box
To…
Read More: web3.hashnode.com