Giter Club home page Giter Club logo

eosio-project-boilerplate-simple's Introduction

⚠️ Important! Since Jan 1st 2019, eosio/eos-dev docker image in docker hub is deprecated. Starting from that time, eosio-project-boilerplate-simple is building its own docker image based on eos and eosio.cdt instead of pulling eosio/eos-dev. ⚠️

Overview

NoteChain demonstrates the eosio platform running a blockchain as a local single node test net with a simple DApp, NoteChain. NoteChain allows users to create and update notes. This guide uses scripts, containing relevant commands, which will show you how to install, build and run NoteChain, and by doing so will demonstrate:

  • Downloading and running eosio in docker;
  • Managing your docker container;
  • Setting up and running a local single node testnet;
  • Setting up wallets, keys, and accounts;
  • Writing and deploying a smart contract;
  • Implementing a web based UI using React;
  • Connecting the UI to the blockchain using eosjs;
  • Styling the UI using Material-UI.

Github eosio-project-boilerplate-simple (https://github.com/EOSIO/eosio-project-boilerplate-simple) contains the UI and Smart Contract code, as well as setup scripts which will initialise and start all the necessary components.

The sample DApp demonstrates storing data in multi index table and retrieving this data into the web based UI. NoteChain is a simple note taking application, where notes are tied to user accounts. For this example, all accounts are pre-created by scripts and the account details are displayed at the bottom of the NoteChain UI.

Each account can then be used to add a note to the blockchain. The individual notes are saved in a multi-index table and for simplicity are of fixed width. Each account may have one note attached to it, adding a note to an account with an existing note will replace the existing note with a new note.

Any private keys you see in this repository are for demo purposes only. For a real DApp NEVER expose the private keys.

Prerequisites

Make sure Docker and Node.js are installed

The DApp and eosio will occupy the ports 3000, 8888 and 9876. Make sure nothing else is already running on these ports.

Clone the repository:

git clone https://github.com/EOSIO/eosio-project-boilerplate-simple.git

The following guide assumes you are using macOS.

Quick start - Run the DApp

In this section we provide a single command script to run all the commands needed to start both the blockchain and UI. For more detail on each component see the Detailed guide below.

To start

./quick_start.sh

The above command will execute the following in sequence:

  1. first_time_setup.sh
  2. start_eosio_docker.sh
  3. start_frontend.sh

To stop, press ctrl+c on your keyboard, and execute:

docker stop eosio_notechain_container

Detailed guide

In this section we will describe in detail each script used to run the NoteChain environment in details.

Initial setup

./first_time_setup.sh

Executing the above shell script verifies that docker and node.js are installed. It then builds eosio-notechain docker image if it has never been built before (which contains a full version of the eosio blockchain), removes any previous instances of this docker container and installs node packages for the frontend react app.

Initialise and start blockchain and DApp

After the initialisation, two terminal windows are required, both opened in the repository directory

  • The first terminal window is for blockchain process.
  • The second terminal window is for frontend react app.

running the blockchain

For the first (blockchain) terminal window, running

./start_eosio_docker.sh

will:

  • Start the eosio blockchain
  • Create smart contract owner account,
  • Deploy smart contract
  • Pre-create 7 user accounts with hard coded keys.

The log of blockchain will be displayed on your screen. eosio is now running and starts producing blocks.

running the DApp

For the second (frontend) terminal window, running

./start_frontend.sh

will open a browser session connecting to http://localhost:3000/ showing the react app. You can try to add or remove notes using one of the pre-created accounts with its key on the website. This react app will interact with the smart contract, performing transactions, which are written to the blockchain, which stores note data in the multi index table of the smart contract running on your local nodeos.

Stopping blockchain or DApp

stopping the blockchain

In the first (blockchain) terminal window, press ctrl+c on your keyboard, the log will stop printing. And then execute:

docker stop eosio_notechain_container

This action will take a few seconds. The blockchain will be stopped.

stopping the DApp

In the second (frontend) terminal window, press ctrl+c on your keyboard. The frontend react app will be stopped.

Restarting blockchain or DApp

restarting the blockchain

In the first (blockchain) terminal window, execute this command:

./start_eosio_docker.sh

The blockchain will be resumed automatically and the log will be outputted to the terminal.

restarting the DApp

In the second (frontend) terminal window, you can restart the frontend react app by executing:

./start_frontend.sh

Reset blockchain data

First, you need to stop the blockchain (as above) and then execute:

./first_time_setup.sh

This removes all data on the blockchain, including accounts, deployed smart contracts, etc... The block count will be reset when you start the blockchain again.

Project structure

noteChain // project directory
├── eosio_docker
   ├── * contracts // this folder will be mounted into docker
      └── notechain
          └── notechain.cpp // the main smart contract
   ├── * data // blockchain data, generated after first_time_setup.sh
      ├── blocks
      ├── state
      └── initialized // to indicate whether the blockchain has been initialized or not
   └── * scripts // scripts and utilities for docker container
       ├── accounts.json // pre-create account names, public and private keys (for demo only)
       ├── continue_blockchain.sh // continue the stopped blockchain
       ├── create_accounts.sh // create account data
       ├── deploy_contract.sh // deploy contract
       └── init_blockchain.sh // script for creating accounts and deploying contract inside docker container
└── frontend
    ├── node_modules // generated after npm install
    ├── public
       └── index.html // html skeleton for create react app
    ├── src
       ├── pages
          └── index.jsx // an one-pager jsx, include react component and Material-UI
       └── index.js // for react-dom to render the app
    ├── package-lock.json // generated after npm install
    └── package.json // for npm packages

* means the directory will be mounted to the docker container. Whenever the file changes on the local machine, it will be automatically reflected in the docker environment.

DApp development

The DApp consists of two parts. eosio blockchain and frontend react app. These can be found in:

  • eosio_docker
    • eosio block producing node (local node) wrapped in a docker container
      • 1 smart contract
      • auto smart contract deployment
      • auto create 7 user accounts
  • frontend

Users interact with the UI in client and sign the transaction in frontend. The signed transaction (which is an update action in this demo DApp) is sent to the blockchain directly. After the transaction is accepted in blockchain, the note is added into the multi index table in blockchain.

The UI, index.jsx, reads the notes data directly from nodeos using 'getTableRows()'. The smart contract, notechain.cpp, stores these notes in the multi index table using 'emplace()'' and 'modify()'.

Docker usage

Docker is used to wrap the eosio software inside and run a container (instance) from an image (eosio-notechain). To work with the blockchain directly, by running the scripts or using a cleos command line, you need to go into the container bash.

Go into container bash:

docker exec -it eosio_notechain_container bash

We have already set the container working directory to /opt/eosio/bin/, you could run cleos command in this directory directly. For documentation of cleos: https://developers.eos.io/eosio-nodeos/docs/cleos-overview

You can also look at the init_blockchain.sh or deploy_contract.sh scripts for examples of cleos command lines.

To exit from inside the container bash:

exit

Smart contract (Blockchain):

The smart contract can be found at eosio_docker/contracts/notechain/notechain.cpp(host environment), you can edit this smart contract. You will then need to compile and deploy the contract to the blockchain.

To save time, we prepared some scripts for you. Execute the scripts in the container bash (see above.)

The following script will help you to unlock the wallet, compile the modified contract and deploy to blockchain. 1st parameter is the contract name; 2nd parameter is the account name of the contract owner, 3rd and 4th parameter references wallet related information that was created during the Initial setup:

Inside docker container

./scripts/deploy_contract.sh notechain notechainacc notechainwal $(cat notechain_wallet_password.txt)

After running this script the modified smart contract will be deployed on the blockchain.

Remember to redeploy the NoteChain contract each time you modify it using the steps above!

Frontend:

The UI code can be found at frontend/src/pages/index.jsx(host environment), once you have edited this code the frontend react app compile automatically and the page on browser will be automatically refreshed. You can see the change on the browser once the browser finishes loading.

Docker commands

If you are more familiar with docker, you could use the docker commands below to have better control with the whole environment. Below are the explanations of each of the commands:

Execute below command in /eosio_docker:

Run container from eosio-notechain image by mounting contracts / scripts to the container with running the init_blockchain.sh script as the process. The init_blockchain.sh script run the local node of the blockchain and initializes wallets / contract / data.

docker run --rm --name eosio_notechain_container \
-p 8888:8888 -p 9876:9876 \
--mount type=bind,src="$(pwd)"/contracts,dst=/opt/eosio/bin/contracts \
--mount type=bind,src="$(pwd)"/scripts,dst=/opt/eosio/bin/scripts \
--mount type=bind,src="$(pwd)"/data,dst=/mnt/dev/data \
-w "/opt/eosio/bin/" eosio-notechain:eos2.0.5-cdt1.6.2 /bin/bash -c "./scripts/init_blockchain.sh"

Output and follow docker console logs:

docker logs eosio_notechain_container --follow

Remove the container (will remove all wallets / contracts / data), useful if you want to re-init the whole DApp.

docker rm -f eosio_notechain_container

Stop the container (see below troubleshoot section to see how to pause and continue the blockchain):

docker stop eosio_notechain_container

eosio-project-boilerplate-simple's People

Contributors

andriantolie avatar christiandunst avatar dependabot[bot] avatar jcardenas9x avatar jeffreyssmith2nd avatar jlamarr22 avatar mjk90 avatar rickykung avatar sergmetelin avatar taokayan avatar taratritt avatar terrylks avatar zhuboao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eosio-project-boilerplate-simple's Issues

Error response from daemon: No such container: eosio_notechain_container

$ export DOCKER_HOST=:2375
eosio-project-boilerplate-simple$ ./first_time_setup.sh
=== start of first time setup ===
=== pull eosio/eos-dev image v1.2.5 from docker hub ===
v1.2.5: Pulling from eosio/eos-dev
6b98dfc16071: Already exists
4001a1209541: Already exists
6319fc68c576: Already exists
b24603670dc3: Already exists
97f170c87c6f: Already exists
989c07c6edff: Already exists
3cb5f57fd105: Already exists
17e8c5aa3340: Already exists
865a94241630: Already exists
78c0dba4c581: Already exists
80dfca640586: Already exists
e56cf35e4a67: Already exists
e6e28f56a275: Pull complete
82d13d3b1596: Pull complete
54b694f46b8e: Pull complete
e0c3c86f8dbf: Pull complete
8bf8762fa7a4: Pull complete
97beabd34cd5: Pull complete
Digest: sha256:150cab925ca326293a82518bcd7d1a3cbbd5720f7e12f4545c6a616819083ab2
Status: Downloaded newer image for eosio/eos-dev:v1.2.5
=== setup/reset data for eosio_docker ===
Error response from daemon: No such container: eosio_notechain_container
Error response from daemon: No such container: eosio_notechain_container
=== npm install packpage for frontend react app ===
: not found/nodejs/npm: 3: /mnt/d/Apps/nodejs/npm:
: not found/nodejs/npm: 5: /mnt/d/Apps/nodejs/npm:
/mnt/d/Apps/nodejs/npm: 6: /mnt/d/Apps/nodejs/npm: Syntax error: word unexpected (expecting "in")

Make `./quickstart` such that an error in the contract can be handled

Currently, the setup is such that the scripts don't work if the contract on the first run has an error. After fixing the error in the contract, quick_start.sh fails (because the initialized file was not yet written but some parts of the init process already done.

It should be possible to run the scripts several times until the contract is fixed.

cookiecutter-eos

I've done a cookiecutter's template to allow create skeletons in a easy way:

pip install cookiecutter

cookiecutter --no-input gh:xusy2k/cookiecutter-eos

Improvements

fatal error: 'eosiolib/eosio.hpp' file not found

All of this work is on https://github.com/xusy2k/cookiecutter-eos

Just a typo

Line 34 of first_time_setup.sh script.
echo "=== npm install packpage for frontend react app ==="

Proposal: Use docker-compose

Couldn't docker-compose be used to have both processes opened at the same time?

I could help create a PR for this, and also perhaps help out with a makefile to start things off a little be easier if there's interest perhaps?

Install System Contracts or include note on installed Contracts in README

If anyone is attempting to use this boilerplate with any other examples we have, it may be confusing that this chain is deploying custom contracts. For example, there was confusion caused by the lack of buyrambytes contracts in this EOSJS issue. We should consider adding a note that these contracts are different than the system contracts or install the system contracts as well, so this can immediately be used as a baseline for other examples that utilize system contracts.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.