1
Step 1
. Install MetaMask on your browser.
To create a new wallet, you have to install the extension first. here they are Chrome, Firefox,
and Opera.
just search MetaMask Extension on your favorite search engine, and you can find it there.
Click on Install MetaMask as a Google Chrome extension.
Click Add to Chrome.
Click Add Extension.
2
And it’s as easy as that to install the extension on your browser, continue reading the next step to
figure out how to create an account.
Step 2. Create an account.
Click on the extension icon in the upper right corner to open MetaMask.
To install the latest version and be up to date, click Try it now.
3
Click Continue.
You will be prompted to create a new password. Click Create.
Proceed by clicking Next and accept the Terms of Use.
Click Reveal Secret Words. There you will see a 12 words seed phrase. This is really important
and usually not a good idea to store digitally, so take your time and write it down.
4
5
Verify your secret phrase by selecting the previously generated phrase in order. Click
Confirm.
And that’s it; now you have created your MetaMask account successfully..
6
Receiving Bitcoin & Sending Bitcoin
Buying bitcoin
7
Procedure:
Creating a local Ethereum network and compiling it locally :
1. Open the [Link] file and add the following lines of code to the networks
section:
[Link] a new file called [Link] in the contracts directory and add the following code:
This smart contract has a totalWaves variable that keeps track of the number of waves sent to
the contract, and a wave function that increments this variable by 1 every time it's called.
3. Compile the smart contract by running npx hardhat compile.
8
4. Create a new file called [Link] in the scripts directory and add the following
code:
This script deploys the WavePortal smart contract to the local network.
[Link] the smart contract by running npx hardhat run scripts/[Link] --network hardhat.
[Link] we can interact with the contract using the Hardhat console. Run npx hardhat console to
start the console, and then run the following commands to send a wave to the contract and check
the current number of waves:
9
Example
Try the following code to understand how the string works in Solidity.
pragma solidity ^0.5.0;
contract SolidityTest {
constructor() public{
}
function getResult() public view returns(string memory){
uint a = 1;
uint b = 2;
uint result = a + b;
return integerToString(result);
}
function integerToString(uint _i) internal pure
returns (string memory) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len - 1;
while (_i != 0) {
bstr[k--] = byte(uint8(48 + _i % 10));
_i /= 10;
10
}
return string(bstr);
}
}
Run the above program using steps provided in Solidity First Application chapter.
Output
0: string: 3
Arrays:
Creating an Array
To declare an array in Solidity, the data type of the elements and the number of elements should
be specified. The size of the array must be a positive integer and data type should be a valid
Solidity type
Syntax:
<data type> <array name>[size] = <initialization>
Fixed-size Arrays
uint[6] data1;
Dynamic Array:
The size of the array is not predefined when it is declared.
int[] data1=[10,20,30];
Array Operations
1. Accessing Array Elements: The elements of the array are accessed by using the index. If you
want to access ith element then you have to access (i-1)th index.
Example: In the below example, the contract Types first initializes an array[data] and then
retrieves the value at specific index 2.
11
// Solidity program to demonstrate
// accessing elements of an array
pragma solidity ^0.5.0;
// Creating a contract
contract Types {
// Declaring an array
uint[6] data;
// Defining function to
// assign values to array
function array_example(
) public payable returns (uint[6] memory){
data
= [uint(10), 20, 30, 40, 50, 60];
return data;
}
// Defining function to access
// values from the array
// from a specific index
function array_element(
) public payable returns (uint){
uint x = data[2];
return x;
}
}
Output :
12
2. Length of Array:
Example: In the below example, the contract Types first initializes an array[data] and then the
length of the array is calculated.
Solidity
// Solidity program to demonstrate
// how to find length of an array pragma solidity ^0.5.0;
// Creating a contract contract Types {
// Declaring an array uint[6] data;
// Defining a function to
// assign values to an array function array_example(
) public payable returns (uint[6] memory){ data = [uint(10), 20, 30, 40, 50,
60];
return data;
}
// Defining a function to
// find the length of the array function array_length(
) public returns(uint) { uint x = [Link]; return x;
}
}
Output :
13
3. Push: Push is used when a new element is to be added in a dynamic array. The new element
is always added at the last position of the array.
Example: In the below example, the contract Types first initializes an array[data], and then more
values are pushed into the array.
Solidity
// Solidity program to demonstrate
// Push operation pragma
solidity ^0.5.0;
// Creating a contract
contract Types {
// Defining the array
uint[] data = [10, 20, 30, 40, 50];
// Defining the function to push
// values to the array function
array_push(
) public returns(uint[] memory){
[Link](60);
[Link](70);
[Link](80);
return data;
}
}
Output :
14
1. Initialise your project: a. Mkdir folder_name
[Link] the folder terminal
[Link] the contract [Link]
[Link] the solidty code in the file
[Link] packages:
First install yarn package manager
npm I -g install yarn
packages to :
Install solc
Yarn add solc@0.8.7-fixed
Yarn add ethers
Yarn add fs-extra
15
3..Compiling the contract:
To compile a contract that imports other contracts via relative paths:
solcjs --bin –abi --include-path node_modules/ --base-path .[Link]
Use the --base-path and --include-path options to describe the layout of your project. --
[Link] a [Link] javascript file :
16
[Link] [Link] need to provide private key of your metamask account for this:
Go to metamask->click on 3 dots ->open account details->export private key by putting
password for metamask
17
.
Input:
● Solidity Functions
1. Modifiers
/**
* @notice To check if the voter's age is greater than or equal to 18
* @param currentTime_ Current epoch time of the voter
*/
modifier votingLinesAreOpen(uint256 currentTime_) {
require(currentTime_ >= votingStartTime);
require(currentTime_ <= votingEndTime);
_;
}
/**
* @notice To check if the voter's age is greater than or equal to 18
* @param voterAadhar_ Aadhar number of the current voter
* @param nominationNumber_ Aadhar number of the candidate
*/
modifier isEligibleVote(uint256 voterAadhar_, uint256 nominationNumber_) {
[Link] memory voter_ = voter[voterAadhar_];
[Link] memory politician_ = candidate[nominationNumber_];
require(voter_.age >= 18);
require(voter_.isAlive);
require(voter_.votedTo != 0);
require(
(politician_.stateCode == voter_.stateCode &&
politician_.constituencyCode == voter_.constituencyCode)
);
_;
}
18
/**
* @notice To check if the user is Election Chief or not
*/
modifier isElectionChief() {
require([Link] == electionChief);
_;
}
2. Voting timelines
● Voting will only be opened between a particular date(s) & election chief has the right to
update the start & end dates of the voting process.
● start date can only be updated only if the voting process is not started yet
● Can extend the voting end date after the voting is started & before the voting is ended
/**
* @dev used to update the voting start & end times
* @param startTime_ Start time that needs to be updated
* @param currentTime_ Current time that needs to be updated
*/
function updateVotingStartTime(uint256 startTime_, uint256 currentTime_)
public
isElectionChief
{
require(votingStartTime > currentTime_);
votingStartTime = startTime_;
}
/**
* @dev To extend the end of the voting
* @param endTime_ End time that needs to be updated
* @param currentTime_ Current time that needs to be updated
*/
function extendVotingTime(uint256 endTime_, uint256 currentTime_)
public
isElectionChief
{
19
require(votingStartTime < currentTime_);
require(votingEndTime > currentTime_);
votingEndTime = endTime_;
}
3. Results
Everyone can check the voting results once the voting lines are closed
function getResults(uint256 currentTime_)
public
view
returns ([Link][] memory)
{
require(votingEndTime < currentTime_);
[Link][] memory resultsList_ = new [Link][](
[Link]
);
// Since the candidates will be lesser in count than voter.
// So looping is acceptable.
for (uint256 i = 0; i < [Link]; i++) {
resultsList_[i] = [Link]({
name: candidates[i].name,
partyShortcut: candidates[i].partyShortcut,
partyFlag: candidates[i].partyFlag,
nominationNumber: candidates[i].nominationNumber,
stateCode: candidates[i].stateCode,
constituencyCode: candidates[i].constituencyCode,
voteCount: votesCount[candidates[i].nominationNumber]
});
}
return resultsList_;
}
20
Output:
21
22