Name: Express-Web3-API
Owner: ConsenSys
Description: Express Web3 API to access contract functions more easily in hybrid app build
Created: 2018-05-14 19:27:23.0
Updated: 2018-05-14 19:30:42.0
Pushed: 2018-05-14 19:30:36.0
Homepage: null
Size: 18971
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
A simple example of locally exposing API endpoints using Express that read data from Smart Contracts deployed on a local blockchain test network.
Clone the Repo from Git
Start Ganache from Blockchain-Dev directory (may have to run yarn install first, please let me know)
_modules/.bin/ganache-cli
In Another Terminal run Node Console
Deploy our example Smart Contract, Voting.sol in Node Console
b3 = require('web3')
b3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
b3.eth.accounts
de = fs.readFileSync('Voting.sol').toString()
lc = require('solc')
mpiledCode = solc.compile(code)
iDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
tingContract = web3.eth.contract(abiDefinition)
teCode = compiledCode.contracts[':Voting'].bytecode
ployedContract = VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
IMPORTANT! - Retrieve contract's address and save
ployedContract.address
ntractInstance = VotingContract.at(deployedContract.address)
In Another Terminal edit server.js and change the address to the address you just saved
t contractInstance = VotingContract.at('YOUR_ADDRESS_HERE');
And again in index.js …
ractInstance = VotingContract.at('YOUR_ADDRESS_HERE');
Start server from Blockchain-Dev directory
server
and test it works by navigating to http://localhost:5000/api/hello
Navigate to http://localhost:5000/api/votes/Rama to validate the API returns 0 votes.
In another Terminal open the index.html to see a simple UI
index.html
In this UI you can add votes to the candidates. Try 'Rama' and click Vote button. Go back to the http://localhost:5000/api/votes/Rama endpoint to confirm change.