Setting up Coding Environment
Jump to navigation
Jump to search
Go into the directory that has the project
npm init
Accepts all defaults Install javascript libraries needed for this projects
npm install web3@0.20.0 solc@0.4.18
Compile Code
Start Ganache Go the the root of your project Start a node console
node Web3 = require('web3') web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545")) //last line creates a new web3 object and connect it to the local ganache node web3.eth.accounts // this will now list he 10 accounts on ganache
Load source and compile it load library from npm dependencies
solc = require('solc') sourceCode = fs.readFileSync('greetings.sol').toString()
// this loads the source code into a string
Now we compile it
compileCode = solc.compile(sourceCode)
Get the ABI interface
contractABI = JSON.parse(compiledCode.contracts[':Greetings'].interface) // The greetings is one in the constructor, thats why its upper case
steps to Deploy contract to Ethereum Node
greetingsContract = web3.eth.contract(contractABI)
extract bytecode
byteCode = compileCode.contracts[':Greetings'].bytecode
Deploy Contract
greetingsDeployed = greetingsContract.new({data:byteCode, from: web3.eth.accounts[0],gas: 4700000})
Create an instance of the contract
greetingsInstance = greetingsContrace.at(greetingsDeployed.address)
now we can interact with the contract
greetingsInstance.getGreetings() greetingsInstance.setGreetings("hello", {from: web3.eth.accounts[0]})
Exit NPM NODE
.exit