Smart contracts code
Donuts Vending Machine
Lottery
Car & New car
Donuts Vending machine
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.19;
//creating contract
contract Vending_machine {
//declaring state variables
address public owner; //setting variable ower
mapping (address => uint) public donutBalances; //setting mapping of the eth addresses which have donuts
constructor () {
owner = msg.sender; //this will be the address of the deployer of the contract
donutBalances[address(this)] = 100; //setting balance for the vending machine
}
//setting function to get the balance of the vending machine
function getVendingMachineBalance() public view returns (uint){
return donutBalances[address(this)]; //return the value of the donuts saved in the current contact
}
//setting function to allow the owner to restoke donuts
function restoke (uint amount) public {
require(msg.sender == owner, "Only the owner can restoke this machine"); //using restriction, so only the owner can restoke
donutBalances[address(this)] += amount;
}
//setting function for purchase
function purchase (uint amount) public payable {
//setting the donuts price; will check whether the ether send is enough for the paymenrt t be completed
require(msg.value >= amount* 2 ether, "You must pay atleast 2 etehr per donut");
//checking the balanc eof the current contract
require(donutBalances[address(this)] >= amount, "Not enough donuts in stock to fulfill the purchase request");
donutBalances[address(this)] -= amount; //reducing the acc balance for the buyer
donutBalances[msg.sender] += amount; //increasing thr acc balance for the owner
}
}
Lottery
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.19;
contract Lottery{
//declaring state variables
address public owner;
//declare array to store the players records,
address payable [] public players;
uint public lotteryId;
//setting an object with key-value pairs to keep track of the winners
mapping (uint => address payable) public lotteryHistory;
constructor(){
owner = msg.sender; // setting the person who deploy the contarct as owner
lotteryId = 1;
}
//setting function to get
function getWinnerByLottery(uint lottery) public view returns (address payable){
return lotteryHistory[lottery];
}
//settingfunction to get balance
function getBalance() public view returns (uint) {
return address(this).balance;
}
//settingfunction to get players, will be stored in memory for the duration of the current gane cycle
function getPlayers() public view returns (address payable[] memory){
return players;
}
//setting function
function enter() public payable {
//using the require statement to check
require(msg.value > 0.01 ether);
//setting the address of the player entering he lottery
players.push(payable(msg.sender));
}
//setting function to get random number
function getRandomNumber() public view returns (uint){
//using abi.encode... to concatinate the owner address and the block, this will be hashed and used and random number
return uint(keccak256(abi.encodePacked(owner, block.timestamp)));
}
//setting function to choose the winner
function picWinner() public {
//get index from the array, will choose random number form the array
uint index = getRandomNumber() % players.length;
//once the random number is chosen, a transfer will be made to its account
players[index].transfer(address(this).balance);
//
lotteryHistory[lotteryId] = players[index];
//set a protection from a reentrancy attack; will incremetn the lotteryId before reseting the players array
lotteryId ++;
//set update every time when we have a new winner
players = new address payable[](0);
}
//setting reusable modifier
modifier onlyOwner(){
//to restrict the use of this function only to the owner of the contract
require(msg.sender == owner);
//the code in the function will run only after the require statement above is satisfied
_;
}
}
Car & New Car
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.19;
contract Car {
address public owner;
string public model;
address public carAddr;
constructor(address _owner, string memory _model) payable {
owner = _owner;
model = _model;
carAddr = address(this);
}
}
contract carFactory {
//create array to keep track on the car contracts
Car[] public cars;
//create new contract car
function create(address _owner, string memory _model) public {
Car car = new Car (_owner, _model);
//when new contract is created, will be stored in the array
cars.push(car);
}
//to send ETH to new car contract
function createAndSendEther(address _owner, string memory _model, bytes32 _salt) public {
Car car = (new Car) {salt: _salt}(_owner, _model);
cars.push(car);
}
function create2AndSendEther(
address _owner,
string memory _model,
bytes32 _salt) public payable
{
Car car = (new Car){value: msg.value, salt: _salt}(_owner, _model);
cars.push(car);
}
function getCar(uint _index) public view returns (address owner, string memory model, address carAddr, uint balance){
Car car = cars[_index];
return (car.owner(), car.model(), car.carAddr(), address(car).balance);
}
}