In the Introduction to Ethereum series, we explored the simple vehicle buying
use-case (involving the buyer, the dealer, and the dmv) using a single node on a private
Ethereum network.
In this article, we demonstrate the interaction between the dealer and the buyer by building a web-based
application using HTML/CSS/JavaScript for the User Interface (UI) and the popular
server-side JavaScript frameworks
Web3.js,
Node.js and
Express for the Backend.
Installation
The installation is on a Ubuntu 16.04 LTS (and above) based Linux desktop.
We will assume a hypothetical user called polarsparc with the home directory
located at /home/polarsparc.
Assuming that we are logged in as polarsparc and the current working directory
is the home directory /home/polarsparc, we will setup a directory structure by
executing the following commands:
$ mkdir -p Ethereum/app
$ mkdir -p Ethereum/app/static
Now, change the current working directory to the directory /home/polarsparc/Ethereum.
ASSUMPTION:
We assume the installation and setup from the articles Part 1 and Part 2 are followed prior to this installation.
To install the current stable version (8.x) of the popular Node.js platform, execute the
following commands:
Once the installation complete, execute the following command to check everything was ok:
$ node -v
The following would be the typical output:
Output.1
v8.9.4
To install the Express module, execute the following command:
$ sudo npm install -g express
To install the body-parser module, execute the following command:
$ sudo npm install -g body-parser
To install the Solidity module, execute the following command:
$ sudo npm install -g solc
To install the web3 module, execute the following command:
$ sudo npm install -g web3
The above command can result in an error as shown below:
Output.2
npm WARN deprecated fs-promise@2.0.3: Use mz or fs-extra^3.0 with Promise Support
npm WARN deprecated tar.gz@1.0.7: ⚠️ WARNING ⚠️ tar.gz module has been deprecated and your
application is vulnerable. Please use tar module instead: https://npmjs.com/tar
npm WARN deprecated minimatch@0.3.0: Please update to minimatch 3.0.2 or higher to avoid
a RegExp DoS issue
npm ERR! code 1
npm ERR! Command failed: /usr/bin/git clone --depth=1 -q -b browserifyCompatible
git://github.com/frozeman/WebSocket-Node.git /home/polarsparc/.npm/_cacache/tmp/git-clone-b0ed4ad3
npm ERR! /home/polarsparc/.npm/_cacache/tmp/git-clone-b0ed4ad3/.git: Permission denied
npm ERR!
npm ERR! A complete log of this run can be found in:
npm ERR! /home/polarsparc/.npm/_logs/2018-27-30T21_11_32_065Z-debug.log
To fix the errors, execute the following commands:
Now we can install the web3 module by executing the following command (no need
for sudo):
$ npm install -g web3
Hands-on with Ethereum Web Application
The following is the CSS stylesheet named style.css
located in the directory /home/polarsparc/Ethereum/app/static:
The following is the JavaScript file named vehicle.js
located in the directory /home/polarsparc/Ethereum/app/static:
The following is the HTML file named index.html
located in the directory /home/polarsparc/Ethereum/app/static:
The following is the Solidity file named Vehicle2.sol
located in the directory /home/polarsparc/Ethereum/src:
The following is the Node server file named node-app.js
located in the directory /home/polarsparc/Ethereum/app:
Open a new terminal window (referred to as the node window) and execute the following commands:
$ cd $HOME/Ethereum/app
$ node node-app.js
The following would be the typical output:
Output.3
27 Jan 20:58:44 - >>>>> setup - Bytecode: 6060604052341561000f57600080fd5b60405160608061052d8339810160405280805190602001909190805190602001909190805190602001909190505060008081905550826001819055508160028190555080600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555033600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600460146101000a81548160ff021916908360018111156100f357fe5b02179055505050506104238061010a6000396000f300606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680634e69d56014610088578063893d20e8146100bf5780639cf6d1af14610114578063a4e7290214610169578063bd3e19d414610173578063bf20f9401461019c578063f9633930146101c5575b600080fd5b341561009357600080fd5b61009b6101ee565b604051808260018111156100ab57fe5b60ff16815260200191505060405180910390f35b34156100ca57600080fd5b6100d2610205565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561011f57600080fd5b61012761022f565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b610171610259565b005b341561017e57600080fd5b6101866103da565b6040518082815260200191505060405180910390f35b34156101a757600080fd5b6101af6103e4565b6040518082815260200191505060405180910390f35b34156101d057600080fd5b6101d86103ee565b6040518082815260200191505060405180910390f35b6000600460149054906101000a900460ff16905090565b6000600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6000600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60025480341015151561026b57600080fd5b6001600081905550600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156103d75760026000819055506001600460146101000a81548160ff021916908360018111156102f057fe5b0217905550600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc6002549081150290604051600060405180830381858888f19350505050151561035957600080fd5b7f3ccb2ab6980b218b1dd4974b07365cd90a191e170c611da46262fecc208bd661600154600254604051808381526020018281526020019250505060405180910390a1600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b50565b6000600254905090565b6000600154905090565b600080549050905600a165627a7a723058201675c46924bd35108501f10889a27f0eef6a30339f4b79bcf853b8231efe590d0029
27 Jan 20:58:44 - >>>>> setup - ABI: [{"constant":true,"inputs":[],"name":"getStatus","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getOwner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getDealer","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"buyVehicle","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"getCost","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getVin","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getFlag","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"vin","type":"uint256"},{"name":"cost","type":"uint256"},{"name":"buyer","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"vin","type":"uint256"},{"indexed":false,"name":"cost","type":"uint256"}],"name":"Bought","type":"event"}]
27 Jan 20:58:44 - >>>>> setup - Completed !!!
27 Jan 20:58:45 - -> Express server @localhost:9001
Open a new terminal window (referred to as the Ethereum terminal) and follow the commands
listed under the section Hands-on with Ethereum from the article
Part 1 till
the Output.8.
To start a single node in our private Ethereum network, execute the following command: