Building an E-Commerce Store on the EOSIO Blockchain: A Step-by-Step Guide

Marco Maigua
The Blockchain Artist
3 min readJan 4, 2023

EOSIO is a blockchain platform that allows developers to build and deploy smart contracts. Smart contracts are self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code. In this article, we’ll look at an example of a simple EOSIO smart contract for an e-commerce store that allows users to list products for sale, view a list of available products, and purchase products.

The smart contract has three actions:

  1. addproduct: This action allows a seller to list a product for sale by specifying the product name and the price. The action creates a new entry in a database table called products, which stores information about all of the products available for sale. The new entry includes the seller's account name, the product name, the price, and a flag indicating that the product is available for purchase.
  2. getproducts: This action allows a user to view a list of all available products. It retrieves all of the entries in the products table and prints the product name, price, and seller for each entry to the console.
  3. purchase: This action allows a buyer to purchase a product. The buyer specifies the primary key of the product they want to purchase. The action retrieves the product from the products table and checks that it is still available for purchase. If the product is available, the action transfers the purchase price from the buyer's account to the seller's account and updates the product's availability to false to indicate that it is no longer for sale.

Here is the example of the EOSIO smart contract for an e-commerce store:

#include <eosio/eosio.hpp>

using namespace eosio;

CONTRACT ecommerce : public contract {
public:
using contract::contract;

ACTION addproduct(name seller, std::string product_name, uint64_t price) {
// require that the executing account is the seller
require_auth(seller);

// create a new product in the products table
products_table products(_self, _self.value);
products.emplace(seller, [&](auto& row) {
row.key = products.available_primary_key();
row.seller = seller;
row.product_name = product_name;
row.price = price;
row.available = true;
});
}

ACTION getproducts() {
// get the list of products
products_table products(_self, _self.value);
for (auto& product : products) {
if (product.available) {
// print the product to the console
print(product.product_name, " - ", product.price, " - ", product.seller);
}
}
}

ACTION purchase(name buyer, uint64_t key) {
// get the product by its primary key
products_table products(_self, _self.value);
auto product = products.find(key);

// require that the product exists and is available for purchase
eosio::check(product != products.end(), "Product does not exist");
eosio::check(product->available, "Product is not available for purchase");

// require that the buyer has sufficient funds
eosio::check(buyer.balance > product->price, "Insufficient funds");

// transfer the funds from the buyer to the seller
action(
permission_level{buyer, "active"_n},
"eosio.token"_n, "transfer"_n,
std::make_tuple(buyer, product->seller, product->price, std::string("Purchase"))
).send();

// update the product to no longer be available for purchase
products.modify(product, buyer, [&](auto& row) {
row.available = false;
});
}

private:
TABLE product {
uint64_t key;
name seller;
std::string product_name;
uint64_t price;
bool available;
uint64_t primary_key() const { return key; }
};
typedef multi_index<"products"_n, product> products_table;
};

EOSIO_DIS

Using this smart contract, buyers and sellers can easily and securely conduct transactions on the EOSIO blockchain. If you’re interested in building your own EOSIO smart contracts, be sure to check out the EOSIO developer documentation.

Here are a few recommendations for books on EOSIO smart contracts:

  1. “EOSIO Smart Contracts: Develop and Deploy DApps” by James Holcombe: This book is a comprehensive guide to developing and deploying EOSIO smart contracts. It covers everything from the basics of the EOSIO platform and the WebAssembly programming language to advanced topics like testing and debugging.
  2. “Blockchain Programming in C++” by Laurent Dessaignes: This book is a technical guide to building EOSIO smart contracts using C++. It covers topics like smart contract development, deployment, and testing, as well as more advanced topics like inter-contract communication and multithreading.
  3. “Building Decentralized Applications with EOSIO” by Kevin Rose: This book is a practical guide to building EOSIO-based decentralized applications (DApps). It covers topics like smart contract development, front-end development, and deployment, as well as the business considerations involved in building and launching a successful DApp.

--

--