Blockchain & Solidity Program Lab Manual
ISBN 9788119221646

Highlights

Notes

  

Prog. 15:: Simple Solidity Program in Ethereum

Step 1: Download Sublime Text3 – https://www.sublimetext.com/3

Step 2: Open Sublime Text3 editor

Step 3: Go https://packagecontrol.io/packages/ethereum

Click on Installation Tab on right corner of webpage

Step 4: Go to Preference tab and click package control > install package

Step 5: Download the Truffle Framework and Ganache from www.trufflesuite.com

Step 6: Open the Node.js command prompt and make project directory

Step 7: Install the Truffle Framework in project directory

Step 8: Then Unzip all Truffle framework dependency in our project directory

Step 9: Check the truffle version

Step 10: Type the Solidity Program in Sublime Text3 pragma solidity 0.5.16;

//creating the contract contract Contest {

//creating strcture to model the contestant struct Contestant {

uint id; 
string name;

uint voteCount;

}

//use mapping to get or fetch the contestant details

mapping(uint => Contestant) public contestants;

//to save the list of users/accounts who already casted vote 
mapping(address => bool) public voters;

//add a public state variable to keep track of contestant Count 
uint public contestantsCount;

event votedEvent 
(

uint indexed _contestantId

);

constructor() public

{ 
addContestant(“Tom”); 
addContestant(“Jerry”);

}

//add a function to add contestant

function addContestant (string memory _name) private 
{ 
contestantsCount ++;

contestants[contestantsCount] = Contestant(contestantsCount, _name, 0);

}

function vote (uint _contestantId) public 
{

//restricting the person who already casted the vote require(!voters[msg.sender]);

//require that the vote is casted to a valid contestant require(_contestantId > 0 && _contestantId <= contestantsCount);

//increase the contestant vote count contestants[_contestantId].voteCount ++;

//set the voter’s voted status to true

voters[msg.sender] = true;

//trigger the vote event

emit votedEvent(_contestantId);

}

}

Step 11: After typing above solidity programs, go to the migration folder of sublime text editor and right click on 1_initial_migration file and select Open Containing Folder.

Copy and Paste 1_initial_migration file in the same folder and rename the pasted file 2_deploy_contacts.

Open the 2_deploy_contacts.js file and change the contents var Contest = artifacts.require(“./Contest.sol”);

module.exports = function(deployer) 
{ 
deployer.deploy(Contest);

};

Step 12: Then Compile the Solidity Program using following command

Step 13: Open the truffle-config.js file and check code as below

module.exports = 
{

// See <http://truffleframework.com/docs/advanced/configuration>

// for more about customizing your Truffle configuration! networks: 
{

development:

{

host: “127.0.0.1”,

port: 7545,

network_id: “*”

// Match any network id

},

develop:

{

port: 8545

}

}

};

Step 14: Open the Node.js command prompt and type truffle console command

Step 14: Then Deployed the Solidity program on Ethereum using following command