ganache-cli - ბლოკჩეინის სიმულატორი
კონსოლიდან web3js ის მეშვეობით დაკავშირება ბლოკჩეინზე, წინასწარ უნდა იყოს გაშვებული ბლოკჩიენის სიმულატორი ganache-cli
node
Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
რომ დავრწმუნდეთ web3 გააკეთა ინიციალიზაცია და შეგვიძლია თუ არა უკვე კომუნიკაცია ბლოკჩეინთან გავაკეთოთ შემდეგი რამ.
web3.eth.accounts
ეს მოგვცემს მიმდინარე ექაუნთებს (ganache-cli გაშვებული ვირტუალირი ნეთვორქიდან)
code = fs.readFileSync('Voting.sol').toString()
solc = require('solc')
compiledCode = solc.compile(code)
> abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
> VotingContract = web3.eth.contract(abiDefinition)
> byteCode = compiledCode.contracts[':Voting'].bytecode
> deployedContract = VotingContract.new(['Giga','Beka','Nika'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
> deployedContract.address
> contractInstance = VotingContract.at(deployedContract.address)
> contractInstance.totalVotesFor.call('Beka')
{ [String: '0'] s: 1, e: 0, c: [ 0 ] }
> contractInstance.voteForCandidate('Beka', {from: web3.eth.accounts[0]})
'0xdedc7ae544c3dde74ab5a0b07422c5a51b5240603d31074f5b75c0ebc786bf53'
> contractInstance.voteForCandidate('Beka', {from: web3.eth.accounts[0]})
'0x02c054d238038d68b65d55770fabfca592a5cf6590229ab91bbe7cd72da46de9'
> contractInstance.voteForCandidate('Beka', {from: web3.eth.accounts[0]})
'0x3da069a09577514f2baaa11bc3015a16edf26aad28dffbcd126bde2e71f2b76f'
> contractInstance.totalVotesFor.call('Beka').toLocaleString()
'3'