Created
November 11, 2021 18:30
-
-
Save Amogh-Bharadwaj/6dd32288957d1b20c2cf5c2f2d86f83e 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=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 total_price = tokenPrice * amount; | |
// Checking if customer can afford the purchase. | |
if (EntryBalance[customer] < total_price) { | |
return "Insufficient balance."; | |
} | |
// 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."; | |
} | |
// Public function which allows users to use a given amount of tokens to enter the shop. | |
function UseTokens (uint256 amount) public returns(string memory){ | |
// Checking to see if customer has that many tokens. | |
if (TokenBalance[customer] < amount){ | |
return "Insufficient tokens possessed."; | |
} | |
// Deducting from token balance | |
TokenBalance[customer] = TokenBalance[customer] - amount; | |
return "Successfully spent."; | |
} | |
// Public, payable function to sell a given amount RETs. | |
function SellTokens (uint256 amount) public payable returns (string memory){ | |
// Checking to see if customer has that many tokens. | |
if (TokenBalance[customer] < amount){ | |
return "Insufficient tokens possessed."; | |
} | |
uint256 selling_price = tokenPrice * amount; | |
// Checking to see if Dexter can afford this. | |
if (EntryBalance[Dexter] < selling_price){ | |
return "Insufficient balance at Dexter's address."; | |
} | |
// Executing the transaction. | |
EntryBalance[customer] = EntryBalance[customer] + selling_price; | |
EntryBalance[Dexter] = EntryBalance[Dexter] - selling_price; | |
return "Successfully sold."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment