A quick tutorial to get your first DApp up and running without any frameworks!
By Ben Stewart
Last Updated:2/18/2018
Install Dependencies:
- ganache-cli
npm install ganache-cli [email protected]
- solc
npm install solc
Directions:
- Git Clone this repository
- Run ganache-cli
node_modules/.bin/ganace-cli
- Open up Nodejs console
node
- Compile the contract
code = fs.readFileSync('Voting.sol').toString()
solc = require('solc')
compiledCode = solc.compile(code)
- Deploy the contract
abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
VotingContract = web3.eth.contract(abiDefinition)
byteCode = compiledCode.contracts[':voting'].bytecode
deployedContract = VotingContract.new(['Bill','Tom','Janice'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
contractInstance = VotingContract.at(deployedContract.address)
Interacting with contract through nodejs console
> contractInstance.totalVotesFor.call('Bill')
Output: { [String: '0'] s: 1, e: 0, c: [ 0 ] }
> contractInstance.voteForCandidate('Tom', {from: web3.eth.accounts[0]})
Output: '0xdedc7ae544c3dde74ab5a0b07422c5a51b5240603d31074f5b75c0ebc786bf53'
> contractInstance.voteForCandidate('Janice', {from: web3.eth.accounts[0]})
Output: '0x02c054d238038d68b65d55770fabfca592a5cf6590229ab91bbe7cd72da46de9'
> contractInstance.voteForCandidate('Janice', {from: web3.eth.accounts[0]})
Output: '0x3da069a09577514f2baaa11bc3015a16edf26aad28dffbcd126bde2e71f2b76f'
> contractInstance.totalVotesFor.call('Bill').toLocaleString()
Output: '3'
Interacting with contract via front GUI
- Update the contract instance address in index.js
- Can run command
contractInstance.address
to find address
- Open index.html in your browser