• This repository has been archived on 23/Jun/2019
  • Stars
    star
    450
  • Rank 97,143 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ’ฐ Retail Store that runs on Ethereum

Retail Store on Blockchain

About

This is a Smart Contract that runs on Ethereum

It is written in Solidity and represents a retail store. It supports customer and product registrations. Every registered customer owns a shopping cart to collect products before checking out.

Dapp

In this early version there's no proper web interface available and you'll have to use truffle console to access the contract. In future I'll provide a web-app written in Angular 4.x. The ultimate goal is to not only produce a web-site but a complete web-platform behind it. Embedding a real-world business model into something like a DApp implies certain functionalities:

  • database [you certainly don't want to store your customers personal data on the blockchain]

  • error handling [there's no error-handling in Ethereum but your business isn't Ethereum]

  • transactions [Ethereum transactions aren't your business transactions]

  • unavoidable updates [no code is eternal]

  • automatic backups [I'm repeating myself...see databases above]

  • backend APIs [for example: detailed product infos, currency conversions, geo-locations etc.]

...and many other things.

Giving customers an interface where they can add or remove products to/from their shopping carts is important but not the ultimate goal. The shopping experience on the UI and a sophisticated business logic in the backend must both exist to support each other. As long as we can't put a non-public & fast database on Ethereum we'll have to maintain it somewhere else. And to achieve this goal our Dapp will rely on backend APIs.

Currently, a simple demo to play around with web3-API is available. To get the above demo working please follow these steps:

  • Compile the contracts with truffle compile
  • Then move the newly created build folder to src
  • Now you can boot the app via npm run start:hmr

Tokens

Store Tokens will soon be supported. One could use them to purchase goods in the store or for initial coin offerings. For example: you're planning to open a store that deals with certain popular goods but you're unsure how many potential customers are out there. Now you could simply buy some ethers or other coins to finance your store (to pay goods in advance, hire a dev to code a proper Dapp for your customers etc.). Now everything depends on how successful your business will be. You may or may not be able to sustain it.

As we all know there are always certain risks to take care of and that's why people try to convince other people to support their business ideas. So, you decide to sell shares of your nascent business to interested parties. You create a proper business info material, for example a web-site that describes your business, how it should look like, what are potential risks etc. You generate a certain amount of tokens based on a price that could be fixed or not. Let's say you sell 1 MyStoreToken for 0.001 ETH. Additionally you can determine certain limits and how long your ICO will last. Of course there's no obligation to create all of your tokens in advance. You could easily define a dynamic token supply that depends on incoming ETHs.

Until the first working version gets out you can test the current development in truffle console:

Declare a reference variable for deployed token contract.

var token;

Get its reference via JS Promise.

BaseStoreToken.deployed().then(d => token = d);

Display initial token supply.

token.initialSupply();

Transfer 10 tokens from default address to web3.eth.accounts[1]

token.transfer(web3.eth.accounts[1], 10);

API

Name Group Signature Usage Returns
transferOwnership owner address store.transferOwnership(new_owner_address)
registerProduct owner uint256, bytes32, bytes32, uint, uint store.registerProduct(id, name, description, price, default_amount) bool
deregisterProduct owner uint256 store.deregisterProduct(id) bool
getProduct customer uint256 store.getProduct(id) (bytes32 name, bytes32 description, uint256 price, uint256 default_amount)
registerCustomer owner address, bytes32, uint256 store.registerCustomer(cust_address, cust_name, cust_balance) bool
deregisterCustomer owner address store.deregisterCustomer(cust_address) bool
insertProductIntoCart customer uint256 store.insertProductIntoCart(prod_id) (bool success, uint256 position_in_prod_mapping)
removeProductFromCart customer uint store.removeProductFromCart(prod_position_in_mapping) fires an event on successful removal
getCart customer store.getCart() (uint256[] memory product_ids, uint256 completeSum)
checkoutCart customer store.checkoutCart() bool
emptyCart customer store.emptyCart() bool
getBalance customer store.getBalance() uint256
renameStoreTo owner bytes32 store.renameStoreTo(new_store_name) bool

Usage

First, activate a local test-blockchain with testrpc. If you don't have it just type npm install -g ethereumjs-testrpc and let NPM install it for you.

Second, go into the root of this project and execute truffle compile and truffle migrate (when changing the code during live-testing use truffle migrate --reset instead).

Third, jump into truffle's console with truffle console. Now you can use the local Blockchain to play with the Store ๐Ÿ˜„

Interactive Testing

I've created this project to learn a bit about Solidity & Ethereum. Expect no sophisticated code here. And lots of bugs.

Here's how I interact with it:

First, we'll need two addresses: a customer and a seller. By default testrpc registers ten Ethereum accounts at our disposal.

For more information about the namespace web3.eth consult truffle docs and also Ethereum JavaScript API.

var seller = web3.eth.accounts[0];

var customer = web3.eth.accounts[1];

We also need a reference to our Store.

var store;

We get this reference asynchronously by executing this snippet.

Store.deployed().then(d => store = d);

Now we register a new Customer with a certain amount of money. The original signature of registerCustomer in Solidity differs a bit from the one used below. This is because we want to execute this API from our seller account. All available API calls can be expanded by using similar options that let Ethereum know which account should pay for the execution of the code. As you already know the smart contracts don't get executed for free. You have to pay the miners. You can also set the amount of gas that can be used. More information regarding these options can be found here.

store.registerCustomer(customer, "Harris", 100, {from: seller});

Our customers will hopefully buy some of our products. Now let's register one by using registerProduct. Note that I'm not using {from: seller} here. By default truffle executes transactions under the first available account address. Only when we explicitely want to have a transaction being executed under a different address, like in the shopping cart checkout below, we'll have to provide it.

store.registerProduct(0, "t-shirt", "lacoste", 40, 1);

Now, as a customer we take a T-Shirt with id == 0 and put it into our cart.

store.insertProductIntoCart(0, {from: customer});

Let's see what's in the cart. Note that we don't execute a transaction here. A transaction would try to change the state on the blockchain that makes no sense in this case. Instead we execute a .call() that returns the product ids and total sum.

store.getCart.call({from: customer});

We also want to take care of proper event handling...

var allStoreEvents = store.allEvents().watch({}, '');

...by registering an event handler that'll siphon them all.

allStoreEvents.watch(function(err, res) { console.log("Error: " + err); console.log("Event: " + res.event); });

Let's try to check out. ๐Ÿ˜„

store.checkoutCart({from: customer});

Finally, let's see our balance after the checkout.

store.getBalance.call({from: customer});

Automatic Testing

The tests are written in Solidity. Simply enter truffle test in your console.

Thanks

Many thanks to the nice Ethereum community from reddit.

Special thanks to cintix for the advice regarding unbounded iterations.

List of used images

Cash Register - CC0 Public Domain

Ethereum Logo - free for non-commercial use via Google Filter Settings

License

MIT

More Repositories

1

OpenCV

๐Ÿ“ท Computer-Vision Demos
C#
263
star
2

Sinkholes

๐Ÿ› Malware Sinkhole List in various formats
Python
96
star
3

Angular-VR-Starter

๐Ÿ‘“ Virtual Reality with A-Frame & Angular
JavaScript
84
star
4

Svelte-TypeScript-WebPack-Starter

Svelte-Framework Starter with TypeScript, WebPack, Bootstrap, Font-Awesome, and some other goodies
JavaScript
54
star
5

BiB

๐Ÿ“š Library Management Application for Elementary Schools
TypeScript
39
star
6

purescript-redux

๐Ÿ‘ฝ Redux Bindings for PureScript
JavaScript
36
star
7

ZeroMQ

๐Ÿš€ Client/Server & Pub/Sub Examples with ZeroMQ & Boost
C++
36
star
8

purescript-pouchdb

๐Ÿ“ฆ PouchDB Bindings for PureScript
JavaScript
24
star
9

TwitterFlink

A simple Twitter-Streaming Application for Apache Flink
Scala
21
star
10

BackbonePlayground

๐Ÿ”Œ MarionetteJS, Backbone.Radio, Gulp & more
JavaScript
20
star
11

Angular-Articles

๐Ÿ’พ Projects from my Angular article series
JavaScript
20
star
12

Cpp_REST

๐ŸŒ REST with C++ SDK (Casablanca)
C++
19
star
13

Data-Science-for-Losers

๐Ÿ“ˆ Articles on Data Science, Jupyter, and Pandas
18
star
14

bazaar

My unordered pile of scripts & readmes
Jupyter Notebook
18
star
15

HPX_Projects

โšก High-Performance-Computing with C++
C++
15
star
16

OpenLok

๐Ÿšˆ React.js app to access the Deutsche Bahn API (German Railway Corporation)
CSS
15
star
17

Inferno.js-Starter

๐Ÿ”ฅ Starter for Inferno.js, +WebPack, +Redux, +Mocha
JavaScript
15
star
18

miniDevOps

DevOps Toolbox that runs completely in Docker (Alpine Linux)
Dockerfile
12
star
19

RactAmp

RactiveJS components & AmpersandJS models
JavaScript
12
star
20

HDA_with_Cpp

Hypermedia-driven application based on htmx and Drogon C++ web framework
C++
10
star
21

Keycloak_on_Kubernetes

Running Keycloak with PostgreSQL on Kubernetes
Smarty
9
star
22

Using-Thrill

A complete Thrill environment with demo code
C++
8
star
23

Mithril_Kendo

Mithril, Kendo UI, Firebase & ASP.NET
C#
7
star
24

Amoklauf

A WebApp built with AmpersandJS and RiotJS
HTML
6
star
25

purescript-fedger

Fedger-API bindings for PureScript
JavaScript
6
star
26

Angular2-Playground

๐Ÿ†– 2 environment with Gulp, TypeScript, TSLint & Hapi
JavaScript
6
star
27

Intro-to-Assembly

Source code from "Into to Assembly" article series
Assembly
6
star
28

IHaveNoClue

Using Rust with Visual Studio
C
5
star
29

RactiveFalcorFabric

WebApp built with RactiveJS, Falcor & Office UI Fabric
CSS
4
star
30

nickel-demo

A web app built with Rust / Nickel.rs and RactiveJS
CSS
4
star
31

Bitcoin_Full_Node_on_Raspberry_Pi3

Shell scripts, configs & firewall setup
Shell
4
star
32

TwitterClient

๐Ÿ’ป An unfancy console-based Twitter client with DB & color support
Python
4
star
33

JScripty

๐Ÿชฒ REPL for transpiling ES6 to ES5
CSS
4
star
34

WebStreams-with-PureScript

How to use Web Streams with PureScript
JavaScript
3
star
35

Perl6-Bitcoin-Client

Client to access the Bitcoin REST API
Perl 6
3
star
36

OpenUI5_Table_Demo

MVC & Master-Detail Apps with OpenUI5 & KendoUI
JavaScript
3
star
37

Julia-Articles

Code examples from my articles on the Julia programming language
3
star
38

RactiveComponents

Source code from http://blog.brakmic.com/creating-components-with-ractive-js/
CSS
3
star
39

Blinker2

A simple Hardware Design for Xilinx Spartan-6 FPGA written in VHDL
VHDL
3
star
40

Angular_InCode_Tutorial

Angular.js by Example
JavaScript
3
star
41

ServiceWatcher

Reactive Extensions on ASP.NET & Azure
C#
3
star
42

RactiveArticles

Code snippets & projects from my RactiveJS articles
CSS
2
star
43

SparkNotebooks

A collection of Scala codes written with Spark-Notebook
2
star
44

React_Kendo

KendoUI Grid as a ReactJS Component
JavaScript
2
star
45

FlyWeb-Emoji-Tables

๐Ÿš Writing MicroApps with the FlyWeb API
JavaScript
2
star
46

a2g

๐Ÿ”จ Generate Angular2 boilerplate code like a boss!
TypeScript
2
star
47

pony-language-server

Pony Language Server | Work In Progress |
TypeScript
1
star
48

purescript-hoodie

Hoodie Bindings for PureScript
JavaScript
1
star
49

UnzeefMyLinks

A simple XML-Link Extractor to get your links out of Zeef-Pages.
C#
1
star
50

shipcloud-cpp

ShipCloud Bindings for C++
C++
1
star
51

API-Projects

Various API projects based on .NET Core 3.x and JavaScript.
C#
1
star
52

DistributedCachingDemo

C#
1
star
53

BarcodeScanner

A simple barcode scanner using Cordova, KnockoutJS & a RESTful API
Java
1
star
54

WebComponents

Web Components with DevExpress, ChartJS & KnockoutJS
JavaScript
1
star
55

htmx_server

Showcase: HTMX with CrowCpp web server
Meson
1
star
56

shipcloud-js

ShipCloud Bindings for JavaScript
JavaScript
1
star
57

Scala-Crash-Course

Code examples from my Scala Crash Course article series
Scala
1
star
58

TroAPI

A project to convert Troisdorf' web pages into RESTful APIs
HTML
1
star