npm install
- use
./node_modules/.bin/ganache-cli
start a ganache test chain - use
./node_modules/.bin/solcjs --abi --bin Voting.sol
to get the abi and code bin file - in the node-cli
var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
- use
web3.isConnected()
test whether the web3 connected the ganache chain or not - copy the content of the abi and the bin
var abi ={the content of the abi}
var bytecode={the content of the bin}
var VotingContract = web3.eth.contract(abi)
contractInst = VotingContract.new(["Alice","Bob","Cary"],{data:bytecode,from:web3.eth.accounts[0],gas:5000000})
after this you can see a json data about the contract in the cli- get the address of the contract
contractInst.address
Anyway,my abi is like this:
[{"constant":true,"inputs":[{"name":"candidateName","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"candidateName","type":"bytes32"}],"name":"vote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"candidateListName","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
bin:
608060405234801561001057600080fd5b506040516103c53803806103c5833981018060405281019080805182019291905050508060009080519060200190610049929190610050565b50506100c8565b828054828255906000526020600020908101928215610092579160200282015b82811115610091578251829060001916905591602001919060010190610070565b5b50905061009f91906100a3565b5090565b6100c591905b808211156100c15760008160009055506001016100a9565b5090565b90565b6102ee806100d76000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632f265cf7146100675780637021939f146100b2578063a69beaba146100fd578063b13c744b1461012e575b600080fd5b34801561007357600080fd5b506100966004803603810190808035600019169060200190929190505050610177565b604051808260ff1660ff16815260200191505060405180910390f35b3480156100be57600080fd5b506100e160048036038101908080356000191690602001909291905050506101bd565b604051808260ff1660ff16815260200191505060405180910390f35b34801561010957600080fd5b5061012c60048036038101908080356000191690602001909291905050506101dd565b005b34801561013a57600080fd5b506101596004803603810190808035906020019092919050505061023a565b60405180826000191660001916815260200191505060405180910390f35b60006101828261025d565b151561018d57600080fd5b60016000836000191660001916815260200190815260200160002060009054906101000a900460ff169050919050565b60016020528060005260406000206000915054906101000a900460ff1681565b6101e68161025d565b15156101f157600080fd5b6001806000836000191660001916815260200190815260200160002060008282829054906101000a900460ff160192506101000a81548160ff021916908360ff16021790555050565b60008181548110151561024957fe5b906000526020600020016000915090505481565b600080600090505b6000805490508160ff1610156102b75760008160ff1681548110151561028757fe5b906000526020600020015460001916836000191614156102aa57600191506102bc565b8080600101915050610265565b600091505b509190505600a165627a7a723058207a1e42461e716a028925ddade6f5985292c8fa9657086ffeff465dc0f669bd460029