Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views7 pages

Blockchain Assignment

The document outlines three smart contracts implemented on the Ethereum blockchain: an ERC-20 token contract for minting and burning tokens, a voting system that allows users to vote for candidates while ensuring one vote per address, and a simple storage contract for storing and retrieving an integer value. Each section includes key features, code implementations, and deployment instructions. The contracts utilize Solidity programming language and incorporate essential functionalities for their respective purposes.

Uploaded by

phabhishek6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Blockchain Assignment

The document outlines three smart contracts implemented on the Ethereum blockchain: an ERC-20 token contract for minting and burning tokens, a voting system that allows users to vote for candidates while ensuring one vote per address, and a simple storage contract for storing and retrieving an integer value. Each section includes key features, code implementations, and deployment instructions. The contracts utilize Solidity programming language and incorporate essential functionalities for their respective purposes.

Uploaded by

phabhishek6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Blockchain Assignment 2

1 ERC-20 Tokens
1.1 Introduction

This smart contract implements an ERC-20 token named Blocktokens (BLK) on the Ethereum
blockchain. The contract allows users to mint and burn tokens in exchange for ETH at a fixed rate.

1.2 Key Features


• Minting Tokens:
– Users can call the mint function and specify the number of tokens they want to mint.
– The function requires the user to send TOKEN PRICE * amount in ETH.
– If the correct ETH amount is provided, the contract mints amount tokens to the sender’s
address.
• Withdrawing ETH (Burning Tokens):
– Users can call the withdraw function and specify the number of tokens they want to redeem
for ETH.
– The function checks if the user has enough tokens.
– If they do, the specified tokens are burned, and the equivalent ETH is sent back to the user.
• Checking Contract Balance:
– The contractBalance function allows anyone to check the amount of ETH stored in the
contract.

1.3 Code Implementation


1 // SPDX - License - I d e n t i f i e r : MIT
2 pragma solidity ^0.8.20;
3
4 import " @openzeppelin / contracts / token / ERC20 / ERC20 . sol " ;
5
6 contract MintableToken is ERC20 {
7 uint256 public constant TOKEN_PRICE = 1000 wei ;
8
9 constructor () ERC20 ( " BlockTokens " , " BLK " ) {}
10
11 function mint ( uint256 _amount ) public payable {
12 require ( msg . value == TOKEN_PRICE * _amount , " Incorrect ETH amount sent " ) ;
13 _mint ( msg . sender , _amount ) ;
14 }
15
16 function withdraw ( uint _amount ) public {
17 require ( balanceOf ( msg . sender ) >= _amount , " Insufficient token balance to
withdraw " ) ;
18 _burn ( msg . sender , _amount ) ;
19 payable ( msg . sender ) . transfer ( TOKEN_PRICE * _amount ) ;
20 }
21
22 function c o nt ra ct B al an ce () public view returns ( uint256 ) {
23 return address ( this ) . balance ;
24 }
25 }

Listing 1: Solidity Smart Contract

1
1.4 Deployments
• Mint Function

• Check Balance

• Withdraw

2
2 Voting System
2.1 Introduction

This smart contract implements a simple voting system on the Ethereum blockchain. The contract
allows users to vote for candidates while ensuring that each address can vote only once.

2.2 Key Features


• Voting Mechanism:
– Users can vote for a candidate by calling the vote function.
– Each address is allowed to vote only once.
– The contract ensures the candidate is valid before accepting the vote.
• Retrieving Results:
– Users can check the number of votes a candidate has received using the getResult function.
– The function ensures the requested candidate exists before providing the vote count.
• Candidate Validation:
– The contract maintains a list of valid candidates.
– The isValidCandidate function checks whether a candidate is in the list before allowing
votes or retrieving results.

2.3 Code Implementation


1 // SPDX - License - I d e n t i f i e r : MIT
2 pragma solidity ^0.8.0;
3
4 contract Voting {
5 mapping ( string = > uint ) private voteCount ; // Stores vote count for each c a n d i d a t e
6 mapping ( address = > bool ) private hasVoted ; // Tracks if an address has voted
7
8 string [] public candidates ; // List of c a n d i d a t e s
9
10 event Voted ( address indexed voter , string candidate ) ;
11
12 constructor ( string [] memory _candidates ) {
13 candidates = _candidates ;
14 }
15
16 function vote ( string memory candidate ) public {
17 require (! hasVoted [ msg . sender ] , " You have already voted . " ) ;
18 require ( i s V a l i d Ca n d i d a t e ( candidate ) , " Invalid candidate . " ) ;
19

3
20 voteCount [ candidate ]++;
21 hasVoted [ msg . sender ] = true ;
22
23 emit Voted ( msg . sender , candidate ) ;
24 }
25
26 function getResult ( string memory candidate ) public view returns ( uint ) {
27 require ( i s V a l i d Ca n d i d a t e ( candidate ) , " Invalid candidate . " ) ;
28 return voteCount [ candidate ];
29 }
30
31 function i s V a l i d C a nd i d a t e (
32 string memory candidate
33 ) internal view returns ( bool ) {
34 for ( uint i = 0; i < candidates . length ; i ++) {
35 if (
36 keccak256 ( abi . encodePacked ( candidates [ i ]) ) ==
37 keccak256 ( abi . encodePacked ( candidate ) )
38 ) {
39 return true ;
40 }
41 }
42 return false ;
43 }
44 }

Listing 2: Solidity Voting Smart Contract

2.4 Deployment
• Contract Deployment

• Vote Function

• View Results

4
3 Contract:2 Storage Contract
3.1 Introduction

This smart contract implements a simple storage mechanism on the Ethereum blockchain. The contract
allows users to store and retrieve a single integer value.

3.2 Key Features


• Storing a Value:
– Users can store a new integer value by calling the store function.
– The contract emits an event whenever a new value is stored.
• Retrieving the Stored Value:
– Users can retrieve the stored value using the retrieve function.
– The function returns the last stored integer.

3.3 Code Implementation


1 // SPDX - License - I d e n t i f i e r : GPL -3.0
2 pragma solidity >=0.8.2 <0.9.0;
3 /* *
4 * @title Storage
5 * @dev Store & r e t r i e v e value in a v a r i a b l e
6 * @custom : dev - run - script ./ scripts / d e p l o y _ w i t h _ e t h e r s . ts
7 */
8 contract Storage {
9
10 event Stored ( uint256 value ) ;
11
12 uint256 number ;
13
14 /* *
15 * @dev Store value in v a r i a b l e
16 * @param num value to store
17 */
18 function store ( uint256 num ) public {
19 number = num ;
20 emit Stored ( num ) ;
21 }
22
23 /* *
24 * @dev Return value
25 * @return value of ’ number ’
26 */
27 function retrieve () public view returns ( uint256 ) {
28 return number ;

5
29 }
30 }

Listing 3: Solidity Storage Smart Contract

3.4 Deployment
• Contract Deployment

• Store Function

• Retrieve Function

6
7

You might also like