Skip to content

Instantly share code, notes, and snippets.

@Amogh-Bharadwaj
Created November 14, 2021 10:14
Show Gist options
  • Save Amogh-Bharadwaj/8e1d10a9d6e8d2ee4ba6964593062b21 to your computer and use it in GitHub Desktop.
Save Amogh-Bharadwaj/8e1d10a9d6e8d2ee4ba6964593062b21 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.7.0 <0.9.0;
// Group 43 Members:
// Amogh Bharadwaj [2019A7PS0086H]
// Anish Kacham [2019A7PS0091H]
// Kevin Kevin Biju Kizhake Kanichery [2019A7PS0045H]
contract Ticketing_System {
// Initialising values
uint256 tokenPrice;
// Mapping in Solidity: we can associate a field with the sender's address.
// This allows us to store the number of RETs an address possesses.
mapping(address => uint256) EntryBalance;
mapping(address => uint256) TokenBalance;
address customer;
// 'payable' keyword implies we can send ether to this object(address in this case).
address Dexter = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);
constructor() payable {
customer = msg.sender;
EntryBalance[customer] = customer.balance;
tokenPrice = 3;
TokenBalance[customer]=0;
}
//Public, payable function for users to buy Royal Entry Tokens.
function BuyTokens (uint256 amount) public payable returns (string memory, uint256){
// Edge case : Only the owner of the contract can perform transactions.
if(customer != msg.sender) return ("Unauthorised.",0);
uint256 total_price = tokenPrice * amount;
// Checking if customer can afford the purchase.
if (EntryBalance[customer] < total_price) {
return ("Insufficient balance.",0);
}
// Executing the transaction.
EntryBalance[customer] = EntryBalance[customer] - total_price;
EntryBalance[Dexter] = EntryBalance[Dexter] + total_price;
// Updating the token count of the address.
TokenBalance[customer] = TokenBalance[customer] + amount;
return ("Successful purchase.Token balance below",TokenBalance[customer]); // Decoded output shows updated token balance.
}
// Public function which allows users to use a given amount of tokens to enter the shop.
function UseTokens (uint256 amount) public returns(string memory,uint256){
// Edge case : Only the owner of the contract can perform transactions.
if(customer != msg.sender) return ("Unauthorised.",0);
// Edge case: Checking to see if customer has that many tokens.
if (TokenBalance[customer] < amount){
return ("Insufficient tokens possessed.",0);
}
// Deducting from token balance
TokenBalance[customer] = TokenBalance[customer] - amount;
return ("Successfully spent. Token balance below.",TokenBalance[customer]);
}
// Public, payable function to sell a given amount RETs.
function SellTokens (uint256 amount) public payable returns (string memory,uint256){
// Edge case : Only the owner of the contract can perform transactions.
if(customer != msg.sender) return ("Unauthorised.",0);
// Edge case: Checking to see if customer has that many tokens.
if (TokenBalance[customer] < amount){
return ("Insufficient tokens possessed.",0);
}
uint256 selling_price = tokenPrice * amount;
// Edge case: Checking to see if Dexter can afford this.
if (EntryBalance[Dexter] < selling_price){
return ("Insufficient balance at Dexter's address.",0);
}
// Executing the transaction.
EntryBalance[customer] = EntryBalance[customer] + selling_price;
EntryBalance[Dexter] = EntryBalance[Dexter] - selling_price;
//Deducting tokens
TokenBalance[customer] = TokenBalance[customer]-amount;
return ("Successfully sold. Token balance below.",TokenBalance[customer]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment