Skip to content

krunalvora/blockchain-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 

Repository files navigation

Table of Contents

  1. Set up local (in-memory) blockchain
    1. Using Ganache desktop application
    2. Using docker
    3. Using npm ganache-cli
  2. Install Solidity compiler
  3. NodeJS Dapp on Local Blockchain
    1. Compile voting contract
    2. Deploy application using web3.js - Ethereum JavaScript API
    3. Interact with the contract through NodeJS console
    4. Interact using a web page

Set up local (in-memory) blockchain

Using Ganache desktop application

https://www.trufflesuite.com/ganache

Using Docker

docker run -d -p 8545:8545 trufflesuite/ganache-cli:latest

Using npm ganache-cli

# Install/update npm to latest version
sudo npm install -g n

# As of this documentation, `ganache-cli` does not work with Node 14 (https://github.com/trufflesuite/ganache-cli/issues/732)
# Install Node 13
sudo n 13.11.0

# Install ganache-cli and web3
npm install ganache-cli

# Start Ganache in-memory blockchain
node_modules/.bin/ganache-cli

Install Solidity compiler

https://solidity.readthedocs.io/en/latest/installing-solidity.html#binary-packages

NodeJS Dapp on Local Blockchain

cd ethereum/voting-dapp/nodejs

Compile voting contract

Compile Voting.sol file to generate Voting.bin and Voting.abi files

solc -o ./ --bin --abi ../Voting.sol

Voting.bin -> compiled bytecode that is deployed to blockchain

Voting.abi -> tells user what methods are available in the contract

Deploy application using web3.js - Ethereum JavaScript API

npm install [email protected]
$ node

> Web3 = require('web3')
> web3 = new Web3("http://localhost:7545")  // Use 8545 if you are using ganache-cli
> web3.eth.getAccounts(console.log)
# Should list all 10 accounts

> bytecode = fs.readFileSync('Voting_sol_Voting.bin').toString()
> abi = JSON.parse(fs.readFileSync('Voting_sol_Voting.abi').toString())

> deployedContract = new web3.eth.Contract(abi)
> listOfCandidates = ['Puni', 'Anna', 'Bhavin']
> deployedContract.deploy({
  data: bytecode,
  arguments: [listOfCandidates.map(name => web3.utils.asciiToHex(name))]
}).send({
  from: '<ENTER 1 OF 10 ACCOUNT ADDRESSES like 0xfb3....>',
  gas: 1500000,
  gasPrice: web3.utils.toWei('0.00003', 'ether')
}).then((newContractInstance) => {
  deployedContract.options.address = newContractInstance.options.address
  console.log(newContractInstance.options.address)
});

Interact with the contract through NodeJS console

> deployedContract.methods.totalVotesFor(web3.utils.asciiToHex('Puni')).call(console.log)
> deployedContract.methods.voteForCandidate(web3.utils.asciiToHex('Puni')).send({from: 'YOUR ACCOUNT ADDRESS'}).then((f) => console.log(f))
> deployedContract.methods.totalVotesFor(web3.utils.asciiToHex('Puni')).call(console.log)

Interact using a web page

  • Update vonting-dapp.js with contract's address (returned by the deploy application code / available in the contract create block on Ganache desktop application)
  • Open voting-dapp.html on your browser

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published