Skip to content

Instantly share code, notes, and snippets.

@bekatom
Last active February 22, 2018 11:56
Show Gist options
  • Save bekatom/0c4d2cfe2591bab914d15b65da8b3637 to your computer and use it in GitHub Desktop.
Save bekatom/0c4d2cfe2591bab914d15b65da8b3637 to your computer and use it in GitHub Desktop.
SMART-CONTRACT

ბლოკჩეინთან ურთიერთობა web3.js

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)

კონტრაქტთან უერთიერთობა, მეთოდების გამოძახება node კონსოლიდან

> 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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment