0% found this document useful (0 votes)
11 views

Assignment 3

Uploaded by

hgtxdcymhn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Assignment 3

Uploaded by

hgtxdcymhn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Capital University of Science & Technology

Department of Software Engineering

Zohaib Ahmed (BSE203017)


Name Moiz Ahmed (BSE203146)
Muhammad Hassaan (BSE203193)

Course BlockChain Technology

Section 2

Due Date 27th May, 2024

Assignment # 03
Question # 1
Create a fungible token with ERC20 (extended version ERC223) or a non-fungible token with
ERC 721 (extended version ERC1155) by writing the code and description, along with
screenshots.

Answer:
To create a token, we first installed the MetaMask Browser extension and created our wallet
there. At start, the wallet got 0 ETH as shown in the picture.

To get the token created, we were required to have a minimum ETH amount in our MetaMask
wallet. So, we installed Ganache which provided a Testnet environment having multiple accounts
and a complete network as shown in the image below.
We took the account private key to add the Balance ETH to the MetaMask by clicking on the
Key button in any of the given addresses in the above image.
This private key helped us getting a sample amount of ETH in testnet as shown in the following
image.
Now, the main step is to create the Token with ERC20. We have used remix.ethereum.org to
develop the code file and deploy our own Token to the Ethereum network.

After writing the code, we compiled it successfully and deployed our first token as can be seen in
below image.

The token properties are as follows


[vm]
from: 0x5B3...eddC4
to: ZohaibAhmedToken.(constructor)
value: 0 wei
data: 0x608...00000
logs: 1
hash: 0x169...27fd3
Debug
status 0x1 Transaction mined and execution succeed
transaction hash 0x169d303482da13c51f2b5a19397378725b974a98f971044539f724c1d2827fd3
block hash 0x1b77618d4330e62ad18336a1988620f1fefcd59ab0f070888e27783d92a174b8
block number 1
contract address 0xd9145CCE52D386f254917e481eB44e9943F39138
from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
to ZohaibAhmedToken.(constructor)
gas 1089853 gas
transaction cost 947698 gas
execution cost 804998 gas
input 0x608...00000
decoded input { "string _name": "Zohaib Smart Token", "string _symbol": "ZST" }
decoded output -
[ { "from": "0xd9145CCE52D386f254917e481eB44e9943F39138", "topic":
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "event":
"Transfer", "args": { "0": "0x0000000000000000000000000000000000000000", "1":
"0x5B38Da6a701c568545dCfcB03FcB875f56beddC4", "2": "1000000000000000000000",
"from": "0x0000000000000000000000000000000000000000", "to":
"0x5B38Da6a701c568545dCfcB03FcB875f56beddC4", "value":
logs "1000000000000000000000" } } ]

And here is the contract address 0xd9145CCE52D386f254917e481eB44e9943F39138


Upon importing the contract address to meta mask, we successfully deployed our first token as
can be seen below.
Question # 2

Use RSA, generate two prime numbers less than 20 each, generate public and private keys from
the numbers, encrypt and decrypt numbers 1 to 5 using the keys.

Answer:
Question # 3
To incentivize people to come for vote, government has introduced a coin, calling it a VoteCoin,
as cryptocurrency. The government has implemented the incentive system as Blockchain
network
with the following terms:
a. Everyone who votes get 10 VoteCoin, where each VoteCoin is pegged against PKR 100.
b. In case a person is a head of a family and if another of his household votes the voter gets
2 additional VoteCoin for each person to a maximum of 10 coins.
c. In case all the members of his house (the same address in NADRA) votes, the person
gets 5 additional coins.
These coins can only be used to pay electricity bill.
 Design logic for smart contract to implement the terms.
 Install Solidity, write code to implement the aforementioned logic, and take screenshots.

Answer:
Following is the code snippet.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract VoteCoin {
string public name = "VoteCoin";
string public symbol = "VTC";
uint8 public decimals = 0;
uint256 public totalSupply;
address public government;

mapping(address => uint256) public balanceOf;


mapping(address => address) public headOfFamily;
mapping(address => uint256) public familyVotes;
mapping(address => bool) public hasVoted;

event Transfer(address indexed from, address indexed to, uint256 value);

modifier onlyGovernment() {
require(msg.sender == government, "Only government can execute this");
_;
}

constructor() {
government = msg.sender;
}
function vote(address voter, address familyHead) external onlyGovernment {
require(!hasVoted[voter], "Already voted");

uint256 reward = 10;


balanceOf[voter] += reward;
totalSupply += reward;
emit Transfer(address(0), voter, reward);

hasVoted[voter] = true;

if (familyHead != address(0)) {
headOfFamily[voter] = familyHead;
familyVotes[familyHead] += 1;

if (familyVotes[familyHead] <= 5) {
balanceOf[familyHead] += 2;
totalSupply += 2;
emit Transfer(address(0), familyHead, 2);
}
}
}

function finalizeFamilyBonus(address familyHead, uint256 householdSize) external


onlyGovernment {
require(familyVotes[familyHead] == householdSize, "Not all members voted");

balanceOf[familyHead] += 5;
totalSupply += 5;
emit Transfer(address(0), familyHead, 5);
}

function transfer(address to, uint256 value) external {


require(balanceOf[msg.sender] >= value, "Insufficient balance");

balanceOf[msg.sender] -= value;
balanceOf[to] += value;
emit Transfer(msg.sender, to, value);
}
}

Question # 4
Create a Private Blockchain Network using Geth & Ganache. Write the smart contract in remix,
and deploy the contract on Metamask connected to the created blockchain. Once the contract is
deployed, do the compilation, migration and testing using the truffle suite.
Answer: We are using Geth & Ganache for creating Private Blockchain Network.

Creating a private blockchain network using geth


Importing account to MetaMask by the private key of the account address.
Contract deployed and transaction fee is deducted as can be seen in the following image.
Installed truffle successfully as can be seen in the following image.

Testing with truffle:

You might also like