Intro to Ethereum: Deploy a lottery app on blockchain. (part 2)

If you haven’t read the previous blog post in this series, read it Here.

In the last part, we learnt the basics of Blockchain, Ethereum, smart contracts and account creation using Metamask. Now we will continue further and learn how to create a contract using solidity programming language and deploy it on the rinkeby blockchain.

First install NodeJS in your PC from this link. After installing it, create a directory named lottery now open CMD, change directory to the newly created lottery folder, the type command:

npm init

this will create a package.json file inside, now inside lottery folder, create a new folder named contracts. Inside contracts, create a new file Lottery.sol . This file will contains the source code for the lottery contract in solidity programming language. In this contract, we will code the logic behind working of the lottery, like how people will participate, how winner will be decided and how will the prize money (in ethers) be sent to the winner?

To write solidity code, I will recommend Remix, which is an awesome online solidity compiler having cool features to code, test and debug solidity code.

in Lottery.sol, paste the following code:

pragma solidity ^0.4.17;

contract Lottery {

    address public manager;
    address[] public players;
    uint public index;

    function Lottery() public {
        manager = msg.sender;
    }

    function enter() public payable {
        require(msg.value > .01 ether);
        players.push(msg.sender);
    }

    function random() private view returns (uint) {
        return uint(keccak256(block.difficulty, now, players));

    }

    function pickWinner() public restricted returns (uint){
        index = random() % players.length;
        players[index].transfer(this.balance);
        players = new address[](0);
        return index;
    }

    function getPlayers() public view returns (address[]) {
        return players;
    }

    modifier restricted(){
        require(msg.sender == manager);
        _;
    }

}

Explanations are given in the source code comments. Now save this file.

Note that traditional javascript cannot compile solidity, so we need to install a node module named “solc” which gives javascript the ability to compile solidity code. To install solc, CD to lottery folder and type cmd comand:

npm install –save solc

Now in lottery folder, create a file “compile.js” . This file will contain a javascript code to compile the above solidity code. Inside compile.js, add following code:

const path = require('path');
const fs = require('fs');
//use path library to create path to .sol file (irrespective of OS).
const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const solc = require('solc');
const source = fs.readFileSync(lotteryPath, 'utf8');
//log compile output
console.log(solc.compile(source, 1));

//now in cmd with project main folder as CD, type:

// in cmd log, we see a JSON returned which contains various info like bytecode, interface etc of solidity contract.
// the ABI of .sol file resides in JSON returned under contracts[':Lottery'] subtree,


module.exports = solc.compile(source, 1).contracts[':Lottery'];  //export the ABI generated by compile method.

Solidity code, after compilation, returns an ABI (Application Bytecode Interface) which is basically a simplified bytecode of the contract containing all information about contract like functions, variables etc.  This bytecode can be read by javascript, thus making it easier to access contract properties using javascript code.

Now we have created a contract and compiled it, the next step is to deploy this contract on blockchain. We will do it in part 3 of this series.

One Reply to “Intro to Ethereum: Deploy a lottery app on blockchain. (part 2)”

Leave a Reply

Your email address will not be published. Required fields are marked *