Create your first smart contract using Solidity in Remix IDE
In this article, we will how to write a simple voting smart contract. We will write our contract in Remix IDE using Solidity.
A closer look at terms used above :
- Smart contract is a computer program or a transaction protocol which is intended to automatically execute, control or document legally relevant events and actions according to the terms of a contract or an agreement.
- Remix IDE (Integrated Development Environment) is a web application that can be used to write, debug, and deploy Ethereum Smart Contracts.
- Solidity is a contract-oriented, high-level language for implementing smart contracts.
Let’s get started
Step 1 :
Go to https://remix.ethereum.org/. Under the contracts folder, you will find some default contracts already given to us by Remix.
As we use Solidity to write our smart contracts, .sol
extension is used. Let’s now create a new contract.
For that, right-click on the contracts
and select New File
. Name our file as Voting
and type the following code.
Let’s now understand the code
pragma solidity ^0.6.6;
Using the above line we are specifying what version of Solidity we are using. In this, we are using 0.6.6, as it is a stable version.
We are naming our contract — Voting
in Line 3
struct Candidate{
uint id;
string name;
uint voteCount;
}
In the above lines, we are defining a new complex datatype called Candidate
using the struct
keyword. Our newly created datatype consists of three attributes — id
which uniquely identifies the particular candidate, name
which refers to the name of the candidate and voteCount
which contains the no of votes that the candidates gained.
In Solidity, uint is actually an alias for uint256, a 256-bit unsigned integer.
After this, we are creating two mappings to keep track of the candidates and citizens.
Mapping in Solidity acts like a hash table or dictionary in any other language. These are used to store the data in the form of key-value pairs, a key can be any of the built-in data types.
In the Candidates
mapping, we have the key as type uint
. As said already, this will be the ID to identify the candidate.
In the Citizen
mapping, we are using the key as the address and boolean as the value. So initially, the boolean value for a citizen will be false and once they have cast their vote, it will change to true. By this, we can make sure that each citizen can cast their vote only once.
function addCandidate(string memory _name) private{
candidatecount++;
candidates[candidatecount] = Candidate(candidatecount, _name, 0);
}
The above function is used to add a Candidate as its name suggests. We are taking in the name
as the parameter, increasing the candidate by one and storing the new candidate in Candidate
mapping with their voteCount as 0.
We are keeping the scope of the function as private
not everyone should be able to add candidates.
function vote(uint _candidateid) public{
require(!citizen[msg.sender]);
citizen[msg.sender] = true;
candidates[_candidateid].voteCount ++;
}
This function is used to handle voting. The require(!citizen[msg.sender])
is used to check if the citizen is already voted. Cause in case they have already voted their boolean value will be True, so the condition will fail not allowing the citizen to vote again.
If this is their first vote, we change the boolean value of the citizen as True and increment the voteCount of the particular candidate that the city chose to vote with the help of the candidateid
constructor() public{
addCandidate("Godlin");
addCandidate("Hilda");
}
As most of you are already aware, a constructor
is called at the beginning of a program. In our case, the constructor is called when we deploy our contract. So when we are deploying, we are adding two candidates named — Godlin and Hilda.
Now that we have understood what is happening in our code, let us go ahead and deploy it.
Step 2 :
Click on the solidity compiler present on the left
Now from the drop-down list, select version 0.6.6.
Then click on Compile Voting.sol
You can select Auto-compile so our contract automatically compiles when we do some changes.
Step 3 :
After compiling our contract, now is the time to deploy our simple contract. For that click on run and deploy transactions on the left
Then click on Deploy
After your contract is successfully deployed, you will able to see your contract under the deployed contracts
Click on the down arrow to expand and see the different functions and mapping inside our contract. As we set addCandidate function as private we won't be able to see it but other than that all will be displayed.
Since we have already added two Candidates when we deployed the contract with the help of the constructor if you press on the candidatecount
— you will 2 as output.
Now we can access the information of the Candidate using the id. You will see the candidate mapping will require uint256
as input.
If you give the input as 1, you will be to see the details of our first candidate. In our case, the first candidate is Godlin.
Similarly, if you want to get the details of Hilda (second candidate) enter 2.
Now by default, Remix IDE will give us 10 test accounts with 100 dummy ethers. Let’s now play around with those accounts to understand the working of our project.
On the top of the column, you will see ACCOUNT
. Select the drop-down box to see the list of accounts.
You will notice that the first account will have less ether when compared to the rest, that is because by default Remix will the first account to deploy our contract. If we want to deploy a contract we have to pay some ether as gas.
Now select the second account and let’s cast our vote. Go to our deployed contract
present in the bottom and vote for your candidate.
Say if you want to vote for Godlin ( Candidate 1), under vote
enter 1 because vote function takes in uint(candidateid) as input and in this case, Godlin has 1 has the candidateid. Click on transact to run the function.
Let’s go and check if our vote is cast. In order to do that we have to go check the voteCount of Godlin. Initially, it was 0 but once since we voted the voteCount will become 1
Yay!! You finished your responsibility as a citizen. But if you try to vote
again using the same account (second account) you will get an error caused a citizen can vote only once.
Now, this is how you write and deploy a simple contract in Remix using Solidity. Hope you found this useful
Keep Exploring!!!