AlphaX-IBS / blockchain-ai-demo

Research & develop some demo in blockchain and A.I
1 stars 0 forks source link

D.App Demo

Ethereum Pet-shop with Truffle, Nodejs Express

Pre-requisite

How to deploy GreenX smart contract?

Find the details here

About this guide

This is a sample project based on Truffle's official tutorial (pet-shop)
By following the above tutorial you should have a good understanding of Truffle, and how Truffle can simplify the deployment and testing of smart contracts on development environment
This sample extends the pet-shop by hooking Nodejs's Express module into it

Step-by-step

  1. Download this sample
    package.json

     "main": "app.js",
     "dependencies": {
       "express": "^4.16.3",
       "truffle-contract": "^3.0.5",
       "web3": "^0.20.1"
     }

    Notice the main has changed to app.js which was created as our Node application's entry point

  2. Execute npm install
    This will install Express, truffle-contract & web3 defined in dependencies
    note: web3 must be version 0.2x.x, not 1.0.0@beta, truffle-contract doesn't work well with beta version

  3. Start your local blockchain (ganache, geth...)
    The port number should be default 8545
    If you want to store data of previous runs on your local machine, specify the path when starting the program
    _Ganache-cli: ganache --db D:\my_block_chaindata --mnemonic "your mnemonic phrase"
    _Geth: geth --rpc --datadir D:\my_block_chaindata

  4. Deploy your contracts
    Execute

    truffle deploy

    or

    truffle compile
    truffle migrate

    should create Adoption.json inside /build/contract folder, this json file stores all contract information we need, we then pass this entire file to truffle-contract and a contract instance will be created, we interact with our deployed contracts with this instance. Read more about truffle-contract here

  5. Run your application
    Execute npm run start or node app.js
    Since our app's base route '/' is returning the index.html
    Open your browser and enter http://localhost:3000 will give you the pet-shop page as the official Truffle tutorial

  6. Testing
    As we're listening for allEvents in app.js, adopt some pets and it should display the transaction info on the console.

Debugging

Block-chain do not allow real-time debugging, instead, you can debug a completed transaction. Follow this guide here for more details:
How to debug Ethereum transactions
More examples