Write Your First Smart Contract In Ethereum Using Solidity



Welcome to the second video in the series on Ethereum Fundamentals.
Today we will learn about writing smart contracts using Solidity.

In previous videos we saw:
How to create a private blockchain,
How to mine ether on your private blockchain,
We also had an overview of smart contracts.

Now, let’s review our goals for this video:
We will develop a simple voting system, that will give you a hands-on look at the Ethereum virtual machine.
This voting system is based on the example provided by Ethereum on its Solidity documentation site.
We will implement this system by creating a smart contract using Solidity.
We will deploy this smart contract into your private blockchain.
Finally, we will test this smart contract by running a few transactions.

Before we begin, let’s make sure you are set up to write this smart contract:
Go to GitHub using the URL shown on the screen,
Click “Clone Or Download”, then “Download ZIP”,
Unzip this downloaded file on your local drive,
Lastly, Use geth to create two accounts with some mined ether.
By now, you should already know how to create accounts and mine ether on your private blockchain.
If you need a refresher, review some of the prior videos on this series, or read the readme file in the downloaded zip.

Ok, now let’s design our voting system.
We will first jot down, what the system needs to keep track of, in other words, its data.
We will then figure out, what the system needs to do, meaning, its methods.

Let’s start with the data piece.
A simple voting system needs to track four things:
One, it needs to track the voters – their address, which is their account,
have they voted yet,
and who they voted for.
Second, it needs to track who the chairperson of the voting process is, who is also a voter.
Third, it needs to track candidates, which are called proposals.
Here, we track the name of the proposal, and how many votes each proposal received.

Next, let’s look at the actions that this system will perform.

First, it needs to be initialized with names of all the proposals or candidates running,
Next, it should only allow authorized voters to cast their vote,
Third, and most importantly, it should let those people vote,
Finally, it should be able to tally those votes, and announce the winner.

Having designed this simple voting system, let’s look at how we actually create and compile this smart contract.

First, ensure that you are in the “voting 2” directory.
Open the “ballot.sol” file, and view/edit the source code.
You will see the code for our voting system written in a javascript-like language called Solidity. You can look at the Solidity documentation if you are interested in more details. For now, treat it as a variation of Javascript.

Next, we need to compile the Solidity file into EVM bytecode.
Load the browser Solidity compiler by going to the URL shown on the screen.

Once the page is loaded, paste the contents of “ballot dot sol” into the web page.
The compiler does a couple of things now. It first compiles the Solidity code into EVM bytecode.
It also creates a Javascript function that Geth can use to deploy the contract.

Now, the Javascript function will show up on the right hand side of the page, in the section called “Web3 deploy section”.

Copy this function, and save as the file “ballot dot j s”.

Lastly, add the array of proposal names to the top of the file. This is used to initialize the voting system.

source

Exit mobile version