EOSIO Smart Contract Database Walkthrough

EOS.IO is a blockchain protocol that emulates most of the attributes of a real computer, powered by the native cryptocurrency EOS. According to the official documentation,

The EOS.IO software introduces a new blockchain architecture designed to enable vertical and horizontal scaling of decentralized applications. This is achieved by creating an operating system-like construct upon which applications can be built. The software provides accounts, authentication, databases, asynchronous communication, and the scheduling of applications across many of CPU cores or clusters. The resulting technology is a blockchain architecture that may ultimately scale to millions of transactions per second, eliminates user fees, and allows for quick and easy deployment and maintenance of decentralized applications, in the context of a governed blockchain.

In this article, we will learn the basics of building an EOSIO smart contract database by covering the following topics:

  1. Create
  2. Query
  3. Modify
  4. Delete

Create

Generally, the emplace constructor is used to create database objects.

.cpp

Precautions:

  1. In the definition of a database object, the first parameter _self indicates the contract owner. The second parameter user indicates the database payer, namely, the account to which the database storage belongs.
  2. The emplace constructor receives a payer parameter and a lamada expression. This structure is fixed.

Let’s view the definition of the test_da class.

.hpp

  1. All smart contracts are inherited from contract. test_da( account_name self ):contract(self){} is the constructor of the test_da contract.
  2. The row following test_da( account_name self ):contract(self){} is a declaration of the create function.
  3. The rows following the create function define data fields. Here we define the data structure da.
  4. The primary_key function defines the primary key.
  5. The row following the primary_key function defines a secondary key, that is, return poster.
  6. The first parameter of the macro EOSLIB_SERIALIZE is the data structure, and the other parameters are data members in the data structure.
  7. typedef defines the das type, which is a database object. In this example, we define a database object containing a primary key and a secondary key.

.abi

abi is very important. An incorrect abi will cause contract execution failure.

Data Creation Example

Publish a contract.

Create data.

Query the data table.

The query result indicates that data has been created successfully. Will it be successful if we create data using another account?

Query the data table.

Great, it works! Here we shall have no questions about data creation.

Query

The most important function of a database is data query. If there is no query function, data in the database cannot be presented and therefore becomes meaningless. The database can be queried by primary key or secondary index.

In this document, all data is stored in one table to diversify the table data.

The preceding das datable( _self, user); in .cpp is replaced with das datable( _self, _self);. In this way, all data is stored in the table under the contract account.

Query data by primary key

In this example, a method is added to query and print data.

The .abi file is also modified accordingly.

Run the following commands:

Query data by secondary index

Add the following code to query data by secondary index:

Obtain the secondary index and use it to query data. In this example, only the find function is used for query.

Run getd.

The data query result is as follows:

Modify

Modify data in the database.

The existing data in the database is as follows:

Use the following action code to modify the data:

The first few rows of the code have been described before. We only describe the modify method in the code. The first parameter post is the object you have found for modification, and the second parameter user indicates the payer.

Run the following commands:

Query the database data.

The query result indicates that the data record containing post_id=1 has been modified.

Delete

The following action function is added to delete data:

The erase method is called to delete data, and the parameter is a data object. Check the command output.

Query the database data.

The query result indicates that the data record containing post_id=1 has been deleted.

Reference:https://www.alibabacloud.com/blog/eosio-smart-contract-database-walkthrough_594032?spm=a2c41.12103156.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store