Better Programming

Advice for programmers.

Follow publication

Storing Data on Ethereum Blockchain With Node.js

Petros Demetrakopoulos
Better Programming
Published in
6 min readJan 19, 2022
Photo by Kanchanara on Unsplash

We are at the beginning of 2022 and everybody is talking about the “next big thing” on the internet: Web 3. But the majority of people who talk about NFTs, crypto assets, altcoins, and blockchain technologies in general, do not really know how they can use them in practice and they are unaware of their true capabilities.

This article is a walkthrough that will help you understand some basic concepts of blockchains and smart contracts. It will also provide a hands-on guide to actually use the Ethereum blockchain to store structured data as you would do with a classic SQL database or a less classic No-SQL database.

In this way, you can take advantage of blockchains and use them actively in you next web application.

First of all, let’s remember what blockchains are:

A blockchain in Layman’s terms is a kind of distributed database that offers more transparency and more security than other databases.

This means that their main role is to store data.

Why Use Ethereum Blockchain Specifically?

Ethereum blockchain is amazing because it was the first blockchain that offered the capability of running code over it with Smart contracts and Solidity, the language behind them.

But Solidity smart contracts may be a real hell even for experienced developers as they have some major technical drawbacks (at least for now).

The most important of them are the following:

  1. The development of smart contracts in Solidity is very time consuming,
  2. Smart contracts cannot be easily tested
  3. Solidity does not support many different data types.
  4. Solidity supports only a limited number of parameters that a developer can pass into a function
  5. Solidity lacks object-oriented concepts. It feels more like a very primitive programming language than a modern one that allows more complex data structures and functions.

However, Ethereum Smart contracts are still extremely useful for a variety of applications ranging from complex auction scenarios and decentralized web apps to simple data storage.

EthAir Balloons: The Missing Part of the Puzzle

The gap between the complex, difficult to develop, test and maintain smart contracts, and the actual use of the Ethereum blockchain in JS-based web apps are covered by EthAir Balloons.

EthAir Balloons is a strictly typed ORM (Object-Relational Mapper) library for the Ethereum blockchain. It allows developers to use Ethereum blockchain as persistent storage in an organized and model-oriented way without writing custom complex Smart contracts. We could say it is for Ethereum-based blockchains what Mongoose is for MongoDB.

Of course, EthAir Balloons do not restrict developers to using only the Ethereum blockchain. The library can be used with any Ethereum-based blockchain (which can be either private or public).

Actually, if you want to experiment with EthAirBalloons and data storage on the Ethereum blockchain, I would strongly suggest making any tests and experiments in a private instance of the Ethereum blockchain (you can run one locally using the ganache-cli tool). This is because, the public Ethereum blockchain would charge a massive amount of transaction fees (know as “gas”) for each write, update and delete operation.

But enough with the theoretical part: Let’s see it in action.

Storing Data on the Blockchain

This part of the article assumes that you are familiar with the basic concepts of JavaScript and Node.js.

First of all let’s create a new node project by typing the following commands on the terminal:

mkdir ethereum_data_storage
cd ethereum_data_storage
npm init

Complete the wizard that will ask for further info about the projects (name, version, license, author, etc).

Then install the EthairBalloons, Express library and body-parser by executing the following lines in the root directory of the project. We will use express in order to expose the CRUD operations for the data as REST API calls.

npm i --save ethairballoons
npm i --save express
npm i --save body-parser

After the installation of the libraries we need to create a file named index.js and a directory named contracts. This is where the auto-generated Solidity contracts will be stored.

touch index.js
mkdir contracts

In the index.js file,we should add the following lines in order to import the various dependencies of the project and declare the model of the data that we want to store on the blockchain.

Dependencies and model declaration

In the first 8 lines, we import and declare the dependencies of the API.

On line 10, we initiate an instance of ethairballoons (or what I like to call an ethAirBalloonsProvider) using only 2 arguments:

1. the URL of the Ethereum blockchain provider that we want to use (in the example it is set to a local ganache-cli provider),

2. the path where we want to save the automatically generated smart contracts of the models.

After the provider initialization, we can create new data schemas using the createSchema() function and pass the schema details in JS object format (lines 12–29). Of course, developers are able (as advised) to keep the schema definitions in separate JSON files and then import them using the require() statement at the top of the file.

Now that our data schema is set, it is time to deploy it in the blockchain, in this point I would like to remember that we do so in a local ganache-cli instance (which is an Ethereum blockchain simulator) and not in the actual Ethereum network.

As transaction fees may be huge, it is strongly advised to only deploy EthAirBalloons models in private Ethereum blockchains or locally using ganache-cli.

In order to achieve that, we declare a new endpoint on our API that is called GET /deploy

So when this endpoint gets called, a new smart contract will be automatically generated and deployed based on the Schema we declared earlier, without the need to write a single line of Solidity code.

This function (Car.deploy) returns a boolean indicating if the deployment is successful and an error object that will be undefined if the deployment is successful. After the deployment is completed we can call the other functions of the model.

CRUD operations

As we said earlier, EthairBalloons fully supports all CRUD (Create, Update, Delete) operations and even more.

Apart from creating, updating and deleting an instance of the model we declared, EthairBalloons provide the ability to find an instance by Id and find all currently stored instances of the model.

The endpoints exposing the CRUD operations are shown in the code below.

Running a Local Ethereum Network Simulator

Before running the server locally, using the well-known node . command, we should start the ganache-cli (assuming it is already installed, if not you can easily find out how to install it there).

When running ganache-cli we must be sure that it is initialized started with enough funds to support all the transactions we want to do through our CRUD API and EthairBalloons. So we set parameter -l (gasLimit) to 800000000000000 which is enough gas for some create and update actions. The final command should look like this:

ganache-cli -l 800000000000000

Now you are ready to call the endpoints of the API to generate and deploy the Smart Contract to the Ethereum blockchain and then Create, Update, Delete and Find any instance of your model.

That’s all

I hope you find it interesting and that it will be useful in future projects! You can find the complete project on this GitHub repository. Do not hesitate to ask questions related to EthairBalloons.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Petros Demetrakopoulos
Petros Demetrakopoulos

Written by Petros Demetrakopoulos

💻Code-blooded, 🌏 Traveler, . Lifelong learner 📚. Currently studying Data Science and AI at TU/e, Eindhoven, NL. https://petrosdemetrakopoulos.github.io

Responses (2)

Write a response