B.E Report BL LAB
B.E Report BL LAB
USING BLOCKCHAIN
By
Guide
Prof. Aruna Kadam
1
APPROVAL FOR BLOCKCHAIN MINI-PROJECT REPORT FOR
FINAL YEAR SEMESTER VIII
BLOCKCHAIN LAB
Examiners
1.__________________________
2. __________________________
Guide
1.__________________________
Date:
2
Abstract
3
Contents
1 Introduction 6
2 System design 8
4 Conclusion 22
5 References 23
4
List of Figures
7 Picking of Winner 20
5
Chapter 1
Introduction
Lotteries have been a prevalent form of gambling and fundraising for centuries, with
governments, organizations, and individuals utilizing them for various purposes. However,
traditional lottery systems often suffer from issues such as lack of transparency, susceptibility
to fraud, and centralized control, leading to distrust among participants and stakeholders.
The motivation behind our project stems from the need to address these shortcomings and
revolutionize lottery systems using emerging technologies. By leveraging blockchain
technology and smart contracts, we aim to create a transparent, secure, and decentralized lottery
system that fosters trust and integrity among participants. This project seeks to harness the
potential of blockchain to enhance transparency, fairness, and efficiency in lottery operations,
ultimately benefiting participants, organizations, and society as a whole.
Traditional lottery systems are plagued by various issues, including lack of transparency,
susceptibility to fraud, and reliance on centralized authorities. These shortcomings undermine
trust and integrity, leading to dissatisfaction among participants and stakeholders. Moreover,
the absence of a secure and transparent mechanism for conducting lotteries hampers the
potential for widespread adoption and acceptance of such systems. In light of these challenges,
there is a pressing need for a solution that addresses the shortcomings of traditional lottery
systems while leveraging emerging technologies to enhance transparency, security, and trust.
Blockchain technology, with its decentralized and immutable ledger, coupled with smart
contracts' self-executing capabilities, presents a promising avenue for revolutionizing lottery
operations.
6
1.3 Proposed Solution
Smart Lottey contract provides the functionalities of participating in lottery using Metamask
account and allowing only the owner of the contract to pick the winner. These functionalities
are provided to the user using metamask. Here, React app is connected with the MetaMask on
the web browser and the MetaMask accounts are imported with ethers from faucets.chain.link
which provides the ethers for carrying out the transactions. So as soon as the user tries
performing the above mentioned functionalities, the metamask needs to be logged in and the
user needs to confirm the transaction to get it done. As soon as the user clicks on confirm, the
respective operation gets performed and the amount gets debited/credited from the metamask
account.
The rest of the report is organized as follows. Chapter 2 presents the summary of the literature
review carried out. Chapter 3 discusses the system design of the proposed model. In Chapter
4, we present the results and implementation steps for the system. And finally, Chapter 5
includes the conclusion followed by references.
7
Chapter 2
System design
The above diagram Fig 1 shows the system design of the Smart Lottery Contract.
The user needs to be logged in with the metamask in order to carry out further operations. Once
the user is successfully logged in he/she will be able to enter lottery and claim prize if winner.
8
Application Areas of Project
● Government Lotteries
● Event Ticketing
● Crowdfunding Campaigns
● Charitable organizations
9
Chapter 3
Implementation and results
Flowchart
In Fig 4, The user needs to log in with a metamask in order to carry out the Lottery functionalities.
Once logged in, he/she will be able to select the required operation. After the operation is selected,
the user needs to provide the input to get the operation performed.
10
Code
Lottery.sol
pragma solidity ^0.8.0;
// SPDX-License-Identifier: UNLICENSED
contract Lottery{
address public manager;
address payable[] public players; // in order to send ether to or recieve from contract to account
or likewise
address payable winner;
bool public isComplete;
bool public claimed;
constructor() {
manager = msg.sender;
isComplete = false;
claimed = false;
}
modifier onlyManager(){
require(msg.sender == manager);
_;
}
function getManager() public view returns(address){
return manager;
}
function getWinner() public view returns(address){
return winner;
}
function status() public view returns (bool) {
return isComplete;
}
function enter() public payable{
require(msg.value >= 0.001 ether);
require(!isComplete);
players.push(payable(msg.sender));
}
function pickWinner() public onlyManager {
require(players.length > 0);
require(!isComplete);
winner = players[randomNumber() % players.length];
isComplete = true;
}
function randomNumber() private view returns (uint){
11
return uint(keccak256(abi.encodePacked(block.prevrandao, block.timestamp,
players.length)));
}
function claimPrize() public {
require(msg.sender == winner);
require(isComplete);
winner.transfer(address(this).balance);
claimed = true;
}
function getPlayers() public view returns (address payable[] memory) {
return players;
}
}
Home.js
import React, { useState, useEffect } from "react";
import { ethers } from 'ethers';
import constants from './constants';
function Home() {
const [currentAccount, setCurrentAccount] = useState("");
const [contractInstance, setContractInstance] = useState('');
const [status, setStatus] = useState(false);
const [isWinner, setIsWinner] = useState('');
useEffect(() => {
const fetchData = async () => {
if (contractInstance) {
const status = await contractInstance.status();
setStatus(status);
const winner = await contractInstance.getWinner();
setIsWinner(winner);
const owner = await contractInstance.getManager();
setOwner(owner);
if (owner === currentAccount) {
setisOwnerConnected(true);
} else {
setisOwnerConnected(false);
}
}
};
fetchData();
12
}, [contractInstance, currentAccount]);
useEffect(() => {
// window.ethereum.enable()
try {
const signer = provider.getSigner();
const address = await signer.getAddress();
console.log(address);
setCurrentAccount(address);
window.ethereum.on('accountsChanged', (accounts) => {
setCurrentAccount(accounts[0]);
console.log(currentAccount)
})
} catch (err) {
console.log(err);
}
}
else {
alert('Please install Metamask to use this application')
}
};
13
if (winner === currentAccount) {
setIsWinner(true);
}
else {
setIsWinner(false);
}
loadBlockchainData();
contract();
}, [currentAccount])
return (
<div className="container">
<h1>Lottery Page</h1>
<div className="button-container">
{
status ? (isWinner ? (<button className="enter-button" onClick={claimPrize}>
Claim Prize </button>) : (<p>You are not the winner</p>)) : (<button className="enter-button"
onClick={enterLottery}> Enter Lottery </button>)
}
</div>
</div>
)
}
export default Home;
PickWinner.js
import React, { useState, useEffect } from "react";
import { ethers } from 'ethers';
import constants from './constants';
function PickWinner() {
14
const [owner, setOwner] = useState('');
const [contractInstance, setContractInstance] = useState('');
const [currentAccount, setCurrentAccount] = useState('');
const [isOwnerConnected, setisOwnerConnected] = useState(false);
const [winner, setWinner] = useState('');
const [status, setStatus] = useState(false);
useEffect(() => {
const fetchData = async () => {
if (contractInstance) {
const status = await contractInstance.status();
setStatus(status);
const winner = await contractInstance.getWinner();
setWinner(winner);
const owner = await contractInstance.getManager();
setOwner(owner);
if (owner === currentAccount) {
setisOwnerConnected(true);
} else {
setisOwnerConnected(false);
}
}
};
fetchData();
}, [contractInstance, currentAccount]);
useEffect(() => {
const loadBlockchainData = async () => {
if (typeof window.ethereum !== 'undefined') { // metamask is installed
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send('eth_requestAccounts', []); // <- this promps user to connect
metamask
// window.ethereum.enable()
try {
const signer = provider.getSigner();
const address = await signer.getAddress();
console.log(address);
setCurrentAccount(address);
window.ethereum.on('accountsChanged', (accounts) => {
setCurrentAccount(accounts[0]);
console.log(currentAccount)
})
15
} catch (err) {
console.log(err);
}
}
else {
alert('Please install Metamask to use this application')
}
};
loadBlockchainData();
contract();
}, [currentAccount])
return (
<div className='container'>
<h1>Result Page</h1>
<div className='button-container'>
16
{status ? (<p>Lottery Winner is : {winner}</p>) :
(isOwnerConnected ? (<button className="enter-button" onClick={pickWinner}>
Pick Winner </button>) :
(<p>You are not the owner</p>))
}
</div>
</div>
)
}
export default PickWinner;
17
Output:
Fig 3: Shows the home page of the Smart Lottery Contract where Account 1 can carry out the
its operations as it is the owner of contract and also shows the transaction carried out via
Metamask
Fig 4: Shows the transaction carried out via Metamask from Account 1
18
Fig 5: Shows the transaction carried out via Metamask from Account 2
Fig 6: Shows that Account 2 is not the owner, so it cannot pick the winner
19
Fig 7: Shows the transaction carried out via Metamask after picking the winner
20
Fig 9: Shows the transaction carried out via Metamask for Account 1 after claiming the prize
21
Chapter 4
Conclusion
In conclusion, the development and implementation of a smart lottery contract project using
blockchain technology hold significant promise for revolutionizing lottery systems across
various domains. Through our project, we have demonstrated the feasibility and benefits of
leveraging blockchain's decentralized ledger and smart contract capabilities to address the
shortcomings of traditional lottery systems. By integrating blockchain technology, we have
tackled key challenges such as transparency, security, and trust, which are paramount in
lottery operations. The immutable nature of blockchain ensures transparent and auditable
lottery transactions, fostering trust among participants and stakeholders. Moreover, the use of
smart contracts enables automated and self-executing lottery processes, eliminating the need
for intermediaries and reducing the risk of fraud or manipulation.
22
References
[1] A. Dixit, A. Trivedi and W. W. Godfrey, "Blockchain Based Secure Lottery Platform by
Using Smart Contract," 2022 IEEE 6th Conference on Information and Communication
Technology (CICT), Gwalior, India, 2022, pp. 1-5, doi: 10.1109/CICT56698.2022.9997830.
[2] A. Hire, H. Lanjewar, P. Haridas, M. Jadhav and M. Rane, "Decentralized Lottery Using
Blockchain," 2023 3rd International Conference on Pervasive Computing and Social
Networking (ICPCSN), Salem, India, 2023, pp. 1035-1041, doi:
10.1109/ICPCSN58827.2023.00176.
[4] Nikhil, S. Panday, A. Saini and N. Gupta, "Instigating Decentralized Apps with Smart
Contracts," 2022 International Conference on Advances in Computing, Communication and
Applied Informatics (ACCAI), Chennai, India, 2022, pp. 1-5, doi:
10.1109/ACCAI53970.2022.9752568.
23