Getting Started with Blockchain Development Part-2

By November 23, 2020DApps
Click here to view original web page at www.thepragmaticoptimist.net

We have already understood the theory of what a dApp is in the last blog post. The focus of this blog post would be to build a simple e-voting decentralized web application. In the previous blog, we coded our own private Ethereum blockchain on our system.

Part-2: Deploying a Smart Contract and Getting Started with dApp Development

We have already understood the theory of what a dApp is in the last blog post. The focus of this blog post would be to build a simple e-voting decentralized web application.

In the last blog, we coded our own private Ethereum blockchain on our system. While it was necessary to understand the procedure and basic commands, it won't be needed always.

What is Ganache?

Ganache is a tool that can quickly fire up a personal Ethereum blockchain with 10 accounts which you can use to run tests, execute commands, and inspect state while controlling how the chain operates.

1. Download and Install Ganache:

We have already understood the theory of what Solidity is, what Smart Contracts are and what web3.js is in the last blog post. Now let's try to compile and deploy a Smart Contract.

2. Download the Smart Contract Solidity file Voting.sol:

We will be requiring Node.js runtime and npm package manager for our current task.

Node.js is an open-source, cross-platform, back-end, JavaScript runtime environment that executes JavaScript code outside a web browser and npm is it's package manager just like pip is to python.

3. Download Node.js:

4. Install web3.js library and Solidity Compiler using npm. Run the following command in your terminal:

npm install -g web3 solc

Now let's compile your Smart Contract!

5. Create a folder named evoting and paste Voting.sol in it.

6. Open the terminal in the evoting folder. For windows users, type in the following command in your terminal:

cd replacethisstringwiththeaddressofyourevotingfolder

7. Then type in the following command in your terminal to compile your smart contract:

solcjs --bin --abi Voting.sol

This will generate 2 new files in your evoting folder:

Voting_sol_Voting.bin: This is the bytecode you get when the source code in Voting.sol is compiled. This is the code that will be deployed to the blockchain.

Voting_sol_Voting.abi: This is an interface or template of the contract (called abi) which tells the contract user what methods are available in the contract. Whenever you have to interact with the contract in the future, you will need this abi definition.

Now let's deploy your Smart Contract and interact with it using the Web3 library.

8. Start Ganache and choose "Quickstart" to setup your private ethereum blockchain in a single click!

9. Type in the following commands in your terminal:

node

Web3 = require('web3')

bytecode = fs.readFileSync('Voting_sol_Voting.bin').toString()

abi = JSON.parse(fs.readFileSync('Voting_sol_Voting.abi').toString())

deployedContract = new web3.eth.Contract(abi)

listOfCandidates = ['BJP', 'Congress', 'NOTA']

//Replace from: "address" to any 1 of the 10 given in your ganache

deployedContract.deploy({

arguments: [listOfCandidates.map(name => web3.utils.asciiToHex(name))]

}).send({

from: '0x1CBCd86FaCCb6069f768f681F33D578b9032F601',

gas: 1500000,

gasPrice: web3.utils.toWei('0.00003', 'ether')

deployedContract.options.address = newContractInstance.options.address;

10. The output of the last step is the address of your smart contract. Copy it.

11. Download index.js from:

Paste it in your evoting folder.

Open it and update it with your contract address.

12. Download index.html from:

Paste it in your evoting folder.

13. Open index.html and try voting.

Every time you vote for a candidate, you get back a transaction id. This transaction id is the proof that this transaction occurred and you can refer back to this at any time in the future. This transaction is immutable. This immutability is one of the big advantages of blockchains such as Ethereum.

We will learn more about smart contracts in the later parts of this blog series, so don't worry right now if you don't understand how the Smart Contract Solidity code actually works. You are not expected to understand it right now. Believe me, if you were able to get your first dApp up and runing, it's huge for now! Give yourselves a pat on the back!

If you have any doubts, feel free to ask in the comment section and stay tuned for the next part.

Happy Learning!

References:

Leave a Reply