0% found this document useful (0 votes)
34 views11 pages

Blockchain Lab

nice to read and impotanat for lab also

Uploaded by

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

Blockchain Lab

nice to read and impotanat for lab also

Uploaded by

1631gunjanpatel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Deploy Smart Contract for E-Voting

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Voting {

address public owner;

mapping(string => uint256) public votes;

mapping(address => bool) public hasVoted;

event Voted(address indexed user, string candidate);

modifier onlyOwner() {

require([Link] == owner, "Only the owner can call this function");

_;

constructor() {

owner = [Link];

function vote(string memory _candidate) public {

require(!hasVoted[[Link]], "You have already voted");

require(bytes(_candidate).length > 0, "Candidate name cannot be empty");

votes[_candidate] += 1;

hasVoted[[Link]] = true;

emit Voted([Link], _candidate);

}
function getVotes(string memory _candidate) public view returns (uint256) {

return votes[_candidate];

Deploy a smart contract for arithmetic operations using JavaScript


VM , Injected Web3 and Web3Provider using Metamask and
Ganache.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract ArithmeticOperations {
function add(int256 a, int256 b) public pure returns (int256) {
return a + b;
}

function subtract(int256 a, int256 b) public pure returns (int256) {


return a - b;
}

function multiply(int256 a, int256 b) public pure returns (int256) {


return a * b;
}

function divide(int256 a, int256 b) public pure returns (int256) {


require(b != 0, "Division by zero");
return a / b;
}
}

Deploy a smart contract for printing “Hello World “ using JavaScript VM ,


Injected Web3 and Web3Provider using Metamask and Ganache.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

contract My {
string public str = "Hello World";

function change(string memory a ) public{


str = a;
}
}
Deploy a smart contract using MyEtherWallet (MEW).
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract FlightReservation {
struct Flight {
string flightNumber;
string origin;
string destination;
uint256 departureTime;
uint256 seatsAvailable;
}
struct Booking {
address passenger;
uint256 flightIndex;
bool isBooked;
}

Flight[] public flights;


mapping(uint256 => Booking) public bookings;
uint256 public bookingIdCounter;

event FlightAdded(string flightNumber, string origin, string destination,


uint256 departureTime, uint256 seatsAvailable);
event FlightBooked(uint256 bookingId, address passenger, string
flightNumber);
event BookingCanceled(uint256 bookingId, address passenger, string
flightNumber);

function addFlight(string memory _flightNumber, string memory _origin,


string memory _destination, uint256 _departureTime, uint256
_seatsAvailable) public {
require(_seatsAvailable > 0, "Seats available cannot be zero");

Flight memory newFlight = Flight({


flightNumber: _flightNumber,
origin: _origin,
destination: _destination,
departureTime: _departureTime,
seatsAvailable: _seatsAvailable
});

[Link](newFlight);
emit FlightAdded(_flightNumber, _origin, _destination, _departureTime,
_seatsAvailable);
}

function bookFlight(uint256 _flightIndex) public {


require(_flightIndex < [Link], "Flight does not exist");
Flight storage flight = flights[_flightIndex];
require([Link] > 0, "No seats available");

bookingIdCounter++;
bookings[bookingIdCounter] = Booking({
passenger: [Link],
flightIndex: _flightIndex,
isBooked: true
});

[Link]--;
emit FlightBooked(bookingIdCounter, [Link], [Link]);
}

function cancelBooking(uint256 _bookingId) public {


require(bookings[_bookingId].isBooked, "Booking does not exist or is
already canceled");
require(bookings[_bookingId].passenger == [Link], "Only the
passenger can cancel their booking");

uint256 flightIndex = bookings[_bookingId].flightIndex;


bookings[_bookingId].isBooked = false;
flights[flightIndex].seatsAvailable++;

emit BookingCanceled(_bookingId, [Link],


flights[flightIndex].flightNumber);
}

function getFlightDetails(uint256 _flightIndex) public view returns (string


memory flightNumber, string memory origin, string memory destination,
uint256 departureTime, uint256 seatsAvailable) {
require(_flightIndex < [Link], "Flight does not exist");
Flight storage flight = flights[_flightIndex];
return ([Link], [Link], [Link],
[Link], [Link]);
}

function getBookingDetails(uint256 _bookingId) public view returns


(address passenger, string memory flightNumber, bool isBooked) {
require(bookings[_bookingId].isBooked, "Booking does not exist or is
canceled");
Booking storage booking = bookings[_bookingId];
Flight storage flight = flights[[Link]];
return ([Link], [Link], [Link]);
}
}
How to build a smart contract that user book rooms and pay them with
cryptocurrency.

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

contract HotelBooking {
address public owner;
uint public roomCount;

struct Room {
uint id;
uint price;
bool isBooked;
}

mapping(uint => Room) public rooms;


mapping(uint => address) public bookings;

event RoomAdded(uint id, uint price);


event RoomBooked(uint id, address booker);
event PaymentReceived(uint id, address from, uint amount);

modifier onlyOwner() {
require([Link] == owner, "Only the owner can call this function");
_;
}

modifier roomExists(uint _roomId) {


require(_roomId > 0 && _roomId <= roomCount, "Room does not exist");
_;
}

constructor() {
owner = [Link];
}

function addRoom(uint _price) public onlyOwner {


roomCount++;
rooms[roomCount] = Room(roomCount, _price, false);
emit RoomAdded(roomCount, _price);
}

function bookRoom(uint _roomId) public payable roomExists(_roomId) {


Room storage room = rooms[_roomId];
require(![Link], "Room is already booked");
require([Link] >= [Link], "Not enough Ether provided");

[Link] = true;
bookings[_roomId] = [Link];

emit RoomBooked(_roomId, [Link]);


emit PaymentReceived(_roomId, [Link], [Link]);
}

function checkBookingStatus(uint _roomId) public view


roomExists(_roomId) returns (bool) {
return rooms[_roomId].isBooked;
}

function withdrawFunds() public onlyOwner {


payable(owner).transfer(address(this).balance);
}
}

Create a Smart Contract for a banking application in solidity which allows


users to do the following: Mint money into your account Withdraw money
from your account Send money from your account to smart contact address
Check balance After a contract is created, deploy the contract on Ethereum
Testnet network.

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

contract Banking {
address public owner;
mapping(address => uint256) public balances;

event Mint(address indexed user, uint256 amount);


modifier onlyOwner() {
require([Link] == owner, "Only the owner can call this function");
_;
}

constructor() {
owner = [Link];
}

function mint(address _to, uint256 _amount) public onlyOwner {


require(_to != address(0), "Invalid address");
require(_amount > 0, "Amount should be greater than zero");

balances[_to] += _amount;
emit Mint(_to, _amount);
}

function balanceOf(address _user) public view returns (uint256) {


return balances[_user];
}
}
Deploy a smart contract for FINDING LARGEST NUMBER OUT OF THREE
NUMBERS using Java Script VM, Injected Web3 and Web3Provider using
Metamask and Ganache.

Deploy the smart contract for RAFFLE DRAW GAME.

You might also like