Skip to content

Instantly share code, notes, and snippets.

@agrcrobles
Last active January 28, 2018 15:11
Show Gist options
  • Save agrcrobles/e389e0fcaf65e18a379e38e6b1489842 to your computer and use it in GitHub Desktop.
Save agrcrobles/e389e0fcaf65e18a379e38e6b1489842 to your computer and use it in GitHub Desktop.
Voting contract in Ropsten
var Migrations = artifacts.require('./Migrations.sol');
module.exports = function(deployer) {
deployer.deploy(Migrations, { gas: 4612388 });
};
var Voting = artifacts.require('./Voting.sol');
module.exports = function(deployer) {
deployer.deploy(Voting, [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
]);
};

voting-deploy

Overview

When Voting.sol contract is deployed on the blockchain it initialized the list of super heroes as well as the total number of tokens available for "sale".

Getting started in localhost

# JSON-RPC node can be running and configured in `truffle.js` with your local network IP ( `127.0.0.1` )

npm i -g ganache-cli

# run local eth
ganache-cli

Build and deploy contracts

npm i -g truffle

# generates json files abstraction
# Writing artifacts to ./build/contracts
truffle compile

# deploy the contracts
truffle migrate --network development

Optional: copy local contracts to the mobile dapp

yarn copy-local

For more information about truffle see ganache-cli

Getting started in ropsten

  • An account and an API is necessary to getting started

  • Edit truffle.js configuration

  • Run

truffle compile
truffle migrate --network ropsten

See intructions: http://truffleframework.com/tutorials/using-infura-custom-provider

More info see:

https://medium.com/@guccimanepunk/how-to-deploy-a-truffle-contract-to-ropsten-e2fb817870c1

https://ethereum.stackexchange.com/questions/23279/steps-to-deploy-a-contract-using-metamask-and-truffle

Smart Contract on testnet

https://ropsten.etherscan.io/address/0x367f283aa2c1979eb400f98bc00b5ba21439e6a0

Running migration: 1_initial_migration.js
================================
BLOCK CHANGED: #26ae87 0x4a81567c88de674833a4ce3d49efcb5b2e3cdb0e48cc68fb60c2fcf3db34384e
================================
  Deploying Migrations...
  ... 0xf239321bdaf8a4f21b2b952340bd3164e7ddb3aebf34ae8d1ca19af57d4e9592
================================
BLOCK CHANGED: #26ae88 0xc45593b4177158c5108b29f46503e536c9e46cbbda30236e31ab2189a81b416d
================================
================================
BLOCK CHANGED: #26ae89 0xecae905cfd236ee4cb0e6f54496e91304a416af1386699760bd24bd65ebba232
================================
  Migrations: 0x367f283aa2c1979eb400f98bc00b5ba21439e6a0
Saving successful migration to network...
  ... 0x08c2fa2d86514532332b4afd8146496931a8dfa072098eb150eb20a41016bc3e
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying Voting...
================================
BLOCK CHANGED: #26ae8b 0x1da766d9f59df85eecd54a8d49e7eab4efad275dea34bd15e97be3fdb4fda792
================================
  ... 0x62b07c9ac3a261a99be23656d5ac7d3b5d7d775d7a82d7f8e34346c7680e2da4
================================
BLOCK CHANGED: #26ae8c 0xb93c9a44f81fab1e22bc7302c5f5af934185da59d9abf139f089f9c9a596bae0
================================
================================
BLOCK CHANGED: #26ae8d 0xfcec71debd017dd0d153897fa8ce15e60ac99359f93277a1a7aac6d51155d867
================================
  Voting: 0x0477fc2032a23845516789b5781543040acda2e9
Saving successful migration to network...
  ... 0x927ac98d1fe494fe5eace47e9db07c8d841a67b21bfef574e256d38edb12ff0f
================================
BLOCK CHANGED: #26ae8e 0x0794a25aca78e389d5c0a34adeb1550e3d34b67903220c2c9be73f0ebbad19f8
================================
================================
BLOCK CHANGED: #26ae8f 0xfa5bfd44e14701175fa939fe05b579f1446db79ef110a158c96ed3a452418113
================================
Saving artifacts...

License

MIT @ zetta

pragma solidity ^0.4.2;
contract Migrations {
address public owner;
uint public last_completed_migration;
modifier restricted() {
if (msg.sender == owner) _;
}
function Migrations() {
owner = msg.sender;
}
function setCompleted(uint completed) restricted {
last_completed_migration = completed;
}
function upgrade(address new_address) restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
var mnemonic = 'MY_MNEMONIC';
var HDWalletProvider = require('truffle-hdwallet-provider');
var bip39 = require('bip39');
var hdkey = require('ethereumjs-wallet/hdkey');
var ProviderEngine = require('web3-provider-engine');
var WalletSubprovider = require('web3-provider-engine/subproviders/wallet.js');
var Web3Subprovider = require('web3-provider-engine/subproviders/web3.js');
var Web3 = require('web3');
// Get our mnemonic and create an hdwallet
var hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic));
// Get the first account using the standard hd path.
var wallet_hdpath = "m/44'/60'/0'/0/";
var wallet = hdwallet.derivePath(wallet_hdpath + '0').getWallet();
var address = '0x' + wallet.getAddress().toString('hex');
var providerUrl = 'https://ropsten.infura.io/';
var engine = new ProviderEngine();
engine.addProvider(new WalletSubprovider(wallet, {}));
engine.addProvider(
new Web3Subprovider(new Web3.providers.HttpProvider(providerUrl))
);
// log new blocks
engine.on('block', function(block) {
console.log('================================');
console.log(
'BLOCK CHANGED:',
'#' + block.number.toString('hex'),
'0x' + block.hash.toString('hex')
);
console.log('================================');
});
// network connectivity error
engine.on('error', function(err) {
// report connectivity errors
console.error(err.stack);
});
engine.start(); // Required by the provider engine.
module.exports = {
networks: {
development: {
host: '127.0.0.1', // this url is local@home and should be changed
port: 8545,
network_id: '*',
},
ropsten: {
network_id: 3, // Official ropsten network id
provider: new HDWalletProvider(
mnemonic,
'https://ropsten.infura.io/'
),
from: address, // Use the address we derived
gas: 3000000,
},
},
};
{
"name": "voting-deploy",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"format": "prettier --ignore-path .gitignore --single-quote --use-tabs --trailing-comma es5 --write '**/*.js'",
"copy-local": "cp -R build/* ../react-native-ethereum-votes/build"
},
"author": "Alejandro Garcia <agrcrobles@gmail.com>",
"engines": {
"node": ">=6.0.0"
},
"license": "MIT",
"dependencies": {
"bip39": "^2.4.0",
"ethereumjs-wallet": "^0.6.0",
"truffle-hdwallet-provider": "0.0.3",
"web3": "^0.18.4",
"web3-provider-engine": "^8.6.1",
"zeppelin-solidity": "^1.2.0"
},
"devDependencies": {
"prettier": "^1.10.2"
}
}
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;
}
function allCandidates() view public returns (bytes32[]) {
return candidateList;
}
// 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, uint8 votes) public {
require(validCandidate(candidate));
votesReceived[candidate] += votes;
}
function validCandidate(bytes32 candidate) view public returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment