This is the example from A Beginner’s Guide to Blockchain Programming written by Febin John James.
1.Install dependencies. ganache-cli、web3.js、solc
$ npm install
$ node_modules/.bin/ganache-cli
2.Compile solidity code.
$ node
$ code = fs.readFileSync('Grades.sol').toString()
$ solc = require('solc')
$ compiledCode = solc.compile(code)
3.Deploy the code on ethererum praivate newtwork by using web3.js.
$ Web3 = require('web3')
$ web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
$ abiDefinition = JSON.parse(compiledCode.contracts[':Grades'].interface)
$ GradesContract = web3.eth.contract(abiDefinition)
$ byteCode = compiledCode.contracts[':Grades'].bytecode
$ deployedContract = GradesContract.new(['John','James'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
Give Grade A+ to John by follwing code.
$ deployedContract.giveGradeToStudent('John', 'A+', {from: web3.eth.accounts[0]})
Call john's grade.
$ deployedContract.getGradeForStudent.call('John')
'A+'