Developing Smart Contract for Businesses with Solidity + Truffle + Ganache + web3.js

A real life case study for developing Ethereum smart contract for businesses: Building a smart contract to manage a burger store.

Ahmet Özlü
4 min readApr 24, 2021
Learn how to develop smart contracts for businesses.

In this post, you’ll learn mostly about the practical background of developing Ethereum smart contract to create solution for different work-flows and business logic. If you need to get basic theoretical background, you can check this post.

Technologies, Tools and Frameworks

First, let us check the technologies and tools that are used to develop and perform tests our smart contract.

  • Solidity: Solidity is an object-oriented programming language for writing smart contracts. It is used for implementing smart contracts on various blockchain platforms, most notably, Ethereum.
  • web3.js: web3. js is a collection of libraries which allow you to interact with a local or remote Ethereum node, using a HTTP or IPC connection.
  • Ganache: Ganache is a personal blockchain for rapid Ethereum and Corda distributed application development. You can use Ganache across the entire development cycle; enabling you to develop, deploy, and test your DApps in a safe and deterministic environment.
  • Truffle: Truffle is a development environment, testing framework, and asset pipeline all rolled into one. It is based on Ethereum Blockchain and is designed to facilitate the smooth and seamless development of DApps (Distributed Applications).

Case Study: Building a smart contract to manage a Burger Store

I love hamburger!

The Burger Store!
Our business is manufacturing burgers :). We’ll develop a custom smart contract to manage a burger store! Basically, we’ll provide functions to send order, send price, send safe payment, send invoice and so on. We’ll provide totally safe, reliable and robust cash and burger menu order flow!

The business work-flow is given below for our Burger Store case study!

Business work-flow for our Burger Store case study.

First of all, let us define out variables and structs. Basically, we need to have burger store and customer addresses, and order and invoice structs.

We need to have order and invoice lists so we use mapping in solidity for it.

This is our no-arg constructor for our smart contract.

And, here are the events are triggered for the activities.

Here are the functions to provide order delivery, safe payment and get invoice.

And, here is our test class for our smart contract. Basically a JavaScript codes using web3.js which is collections of libraries which allow you to interact with a local or remote Ethereum node, using a HTTP or IPC connection.

Quick Demo

You can clone this repository to play with our smart contract on your local.

First, clone the repository and build the project using truffle by this command:

truffle build

After building the project and starting ganache which provides ethereum blockchain network on your local, our smart contract can be deployed using truffle by this command:

truffle migrate --reset

We can use developed unit test class to perform unit testing:

truffle test

Summary

In this quick tutorial, you learnt about how can you develop a smart contract to manage any types of professional businesses. We have been focused a Burger Store in this tutorial but you can easily adopt this case for your custom cases. Blockchain is the next big thing and smart contracts provide a super big potentials to build future! And, by this post, you learnt the basics of this technology with practical and theoretical stuff! You can find all the source codes in this GitHub repo!

--

--

Ahmet Özlü

I am a big fan of Real Madrid CF and I love computer science!