Skip to content

Instantly share code, notes, and snippets.

@laurogripa
Last active May 17, 2018 21:46
Show Gist options
  • Save laurogripa/f568285bcd5fe6ca116ce5562ee9a37f to your computer and use it in GitHub Desktop.
Save laurogripa/f568285bcd5fe6ca116ce5562ee9a37f to your computer and use it in GitHub Desktop.
Ethereum Blockchain Workshop

Content

  • How to create a local network
  • How to manage accounts and execute transactions
  • How to create and compile Smart Contracts in Solidity
  • How to deploy and interact with contracts in a local network

Dependencies

  • node
  • npm
  • solc
  • web3@0.20.5
  • ganache-cli
  • truffle

Setup

npm install ganache-cli web3@0.20.5 solc
node_modules/.bin/ganache-cli
node

Node console

Managing accounts

// web3 instance
Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))

// accounts
web3.eth.accounts
mainAddress = web3.eth.accounts[0]
web3.eth.getBalance(mainAddress).toString()

// new account
newAddress = web3.personal.newAccount('nopasswd')

Sending your first transaction

transactionValue = web3.toWei(1)
web3.eth.sendTransaction({from: mainAddress, to: newAddress, value: transactionValue})

Contract

You can test it on Remix

pragma solidity ^0.4.18;
// We have to specify what version of compiler this code will compile with

contract Voting {
  /* mapping field below is equivalent to an associative array or hash.
  The key of the mapping is candidate name stored as type bytes32 and value is
  an unsigned integer to store the vote count
  */

  mapping (bytes32 => uint8) public votesReceived;

  /* Solidity doesn't let you pass in an array of strings in the constructor (yet).
  We will use an array of bytes32 instead to store the list of candidates
  */

  bytes32[] public candidateList;

  /* This is the constructor which will be called once when you
  deploy the contract to the blockchain. When we deploy the contract,
  we will pass an array of candidates who will be contesting in the election
  */
  function Voting(bytes32[] candidateNames) public {
    candidateList = candidateNames;
  }

  // This function returns the total votes a candidate has received so far
  function totalVotesFor(bytes32 candidate) view public returns (uint8) {
    require(validCandidate(candidate));
    return votesReceived[candidate];
  }

  // This function increments the vote count for the specified candidate. This
  // is equivalent to casting a vote
  function voteForCandidate(bytes32 candidate) public {
    require(validCandidate(candidate));
    votesReceived[candidate] += 1;
  }

  function validCandidate(bytes32 candidate) view public returns (bool) {
    for(uint i = 0; i < candidateList.length; i++) {
      if (candidateList[i] == candidate) {
        return true;
      }
    }
    return false;
  }
}

Deploy

code = fs.readFileSync('Voting.sol').toString()
solc = require('solc')
compiledCode = solc.compile(code)

Contract Instance

abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
VotingContract = web3.eth.contract(abiDefinition)
byteCode = compiledCode.contracts[':Voting'].bytecode
deployedContract = VotingContract.new(['Biscoito','Bolacha'], {data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
deployedContract.address
contractInstance = VotingContract.at(deployedContract.address)

Interaction

contractInstance.totalVotesFor.call('Bolacha').toString()
contractInstance.voteForCandidate('Bolacha', {from: web3.eth.accounts[0]})
contractInstance.voteForCandidate('Bolacha', {from: web3.eth.accounts[0]})
contractInstance.voteForCandidate('Bolacha', {from: web3.eth.accounts[0]})
contractInstance.totalVotesFor.call('Bolacha').toString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment