Skip to content

Instantly share code, notes, and snippets.

@saxenanickk
Created September 24, 2022 05:54
Show Gist options
  • Save saxenanickk/c7b73a27c121e29627bbe08597b591ce to your computer and use it in GitHub Desktop.
Save saxenanickk/c7b73a27c121e29627bbe08597b591ce to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
contract BikeChain {
address owner;
constructor() {
owner = msg.sender;
}
// Add yourself as a renter
struct Renter {
address payable walletAddress;
string firstName;
string lastName;
bool canRent;
bool active;
uint balance;
uint due;
uint start;
uint end;
}
mapping (address => Renter) public renters;
function addRenter(address payable walletAddress, string memory firstName, string memory lastName, bool canRent, bool active, uint balance, uint due, uint start, uint end) public {
renters[walletAddress] = Renter(walletAddress, firstName, lastName, canRent, active, balance, due, start, end);
}
// Checkout bike
function checkOut(address walletAddress) public {
require(renters[walletAddress].due == 0, "You have a pending payment.");
require(renters[walletAddress].canRent == true, "You can not rent at this moment.");
renters[walletAddress].active = true;
renters[walletAddress].start = block.timestamp;
renters[walletAddress].canRent = false;
}
// Check in a bike
function checkIn(address walletAddress) public {
require(renters[walletAddress].active == true, "Please checkout a bike first.");
renters[walletAddress].active = false;
renters[walletAddress].end = block.timestamp;
setDue(walletAddress);
}
// Get total duration of bike use
function renterTimespan(uint start, uint end) internal pure returns(uint) {
return end - start;
}
function getTotalDuration(address walletAddress) public view returns(uint) {
require(renters[walletAddress].active == false, "Bike is currently checked out.");
// uint timespan = renterTimespan(renters[walletAddress].start, renters[walletAddress].end);
// uint timespanInMinutes = timespan / 60;
// return timespanInMinutes;
return 6;
}
// Get contract balance
function balanceOf() public view returns(uint) {
return address(this).balance;
}
// Get renter's balance
function balanceOfRenter(address walletAddress) public view returns(uint) {
return renters[walletAddress].balance;
}
// Set due amount
function setDue(address walletAddress) internal {
uint timespanInMinutes = getTotalDuration(walletAddress);
uint fiveMinutesIncrements = timespanInMinutes / 5;
renters[walletAddress].due = fiveMinutesIncrements * 5000000000000000;
}
// Can rent bike
function canRentBike(address walletAddress) public view returns(bool) {
return renters[walletAddress].canRent;
}
// Deposit
function deposit(address walletAddress) payable public {
renters[walletAddress].balance += msg.value;
}
// Make payment
function makePayment(address walletAddress) payable public {
require(renters[walletAddress].due > 0, "You do not have any due amount at this moment.");
require(renters[walletAddress].balance > msg.value, "You do not have enough balance at this moment.");
renters[walletAddress].balance -= msg.value;
renters[walletAddress].canRent = true;
renters[walletAddress].due = 0;
renters[walletAddress].start = 0;
renters[walletAddress].end = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment