Skip to content

Commit

Permalink
Add BasicHotelManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ghareeb-falazi committed Oct 25, 2024
1 parent 767ab2f commit 8a1023d
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 0 deletions.
140 changes: 140 additions & 0 deletions BasicHotelManager.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"scdl_version": "2.0.0",
"address": "http://localhost:9090?blockchain-id=eth-0&blockchain=ethereum&address=0xFdc5e6941140020100fE798E7668677F01F239D1",
"creation_date": "2024-10-25T10:51:47.0799584Z",
"bc_type": "ethereum",
"bc_version": "dencun",
"bc_internal_address": "0xFdc5e6941140020100fE798E7668677F01F239D1",
"sc_name": "BasicHotelManager",
"sc_state": "READY",
"sc_version": "1.0",
"sc_author": "Ghareeb Falazi",
"sc_language": "solidity",
"sc_language_version": "0.8.17",
"sc_code_hash": "0dcf5aeb50e007d401552fb4cf50ad584add90b226b99c01e9979d1b147ff106",
"functions": [
{
"name": "isRoomAvailable",
"only_external": false,
"side_effects": false,
"supports_tx": false,
"inputs": [],
"outputs": [
{
"name": "return",
"type": {
"type": "boolean"
}
}
],
"events": []
},
{
"name": "queryRoomPrice",
"only_external": true,
"side_effects": false,
"supports_tx": false,
"inputs": [],
"outputs": [
{
"name": "return",
"type": {
"type": "integer",
"minimum": 0,
"maximum": 115792089237316195423570985008687907853269984665640564039457584007913129639935
},
"description": "uint256"
}
],
"events": []
},
{
"name": "queryClientBalance",
"only_external": false,
"side_effects": false,
"supports_tx": false,
"inputs": [],
"outputs": [
{
"name": "return",
"type": {
"type": "integer",
"minimum": 0,
"maximum": 115792089237316195423570985008687907853269984665640564039457584007913129639935
},
"description": "uint256"
}
],
"events": []
},
{
"name": "changeRoomPrice",
"only_external": true,
"side_effects": true,
"supports_tx": false,
"inputs": [{
"name": "newPrice",
"type": {
"type": "integer",
"minimum": 0,
"maximum": 115792089237316195423570985008687907853269984665640564039457584007913129639935
},
"description": "uint256"
}],
"outputs": [],
"events": []
},
{
"name": "addToClientBalance",
"only_external": true,
"side_effects": true,
"supports_tx": false,
"inputs": [{
"name": "amountToAdd",
"type": {
"type": "integer",
"minimum": 0,
"maximum": 115792089237316195423570985008687907853269984665640564039457584007913129639935
},
"description": "uint256"
}],
"outputs": [],
"events": []
},
{
"name": "bookRoom",
"only_external": true,
"side_effects": true,
"supports_tx": false,
"inputs": [],
"outputs": [],
"events": []
},
{
"name": "hasReservation",
"only_external": true,
"side_effects": false,
"supports_tx": false,
"inputs": [],
"outputs": [
{
"name": "return",
"type": {
"type": "boolean"
}
}
],
"events": []
},
{
"name": "checkout",
"only_external": true,
"side_effects": true,
"supports_tx": false,
"inputs": [],
"outputs": [],
"events": []
}
],
"Events": []
}
85 changes: 85 additions & 0 deletions BasicHotelManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.17;

import "./IResourceManager.sol";
import {StringUtils} from "./StringUtils.sol";

contract BasicHotelManager {
mapping (string => string) private clientBalances;
string private roomOwner;
string private roomPrice;

function isRoomAvailable() public view returns (bool) {
return StringUtils.isEmpty(roomOwner);
}

function queryRoomPrice() external view returns (uint256) {
// default price is 500
uint256 result = 500;

if (!StringUtils.isEmpty(roomPrice)) {
result = StringUtils.stringToUint(roomPrice);
}

return result;
}

function queryClientBalance() public view returns (uint256) {
string memory varName = formulateClientBalanceVarName(tx.origin);
string memory balance = clientBalances[varName];

if (!StringUtils.isEmpty(balance)) {
return StringUtils.stringToUint(balance);
}

// initial balance
return 1000;
}

function changeRoomPrice(uint256 newPrice) external {
string memory priceS = StringUtils.uintToString(newPrice);
roomPrice = priceS;
}

function addToClientBalance(uint256 amountToAdd) external {
require(amountToAdd > 0, "The amount must be a positive value!");
string memory varName = formulateClientBalanceVarName(tx.origin);
uint256 balance = queryClientBalance();
uint256 newBalance = balance + amountToAdd;
string memory newBalanceS = StringUtils.uintToString(newBalance);
clientBalances[varName] = newBalanceS;
}

function bookRoom() external {
bool available = isRoomAvailable();
require(available, "the room must be available!");
uint256 price = this.queryRoomPrice();
deductFromClientBalance(price);
roomOwner = StringUtils.addressToHexString(tx.origin);
}

function hasReservation() external view returns (bool) {
string memory currentClient = StringUtils.addressToHexString(tx.origin);
return StringUtils.compareStrings(roomOwner, currentClient);
}

function checkout() external {
bool gotReservation = this.hasReservation();
require(gotReservation, "you must have a reservation in order to checkout!");
roomOwner = "";
}

function deductFromClientBalance(uint256 amountToDeduct) internal {
string memory varName = formulateClientBalanceVarName(tx.origin);
uint256 balance = queryClientBalance();
uint256 newBalance = balance - amountToDeduct;
require(newBalance >= 0, "The amount to deduct cannot be larger than the current balance!");
string memory newBalanceS = StringUtils.uintToString(newBalance);
clientBalances[varName] = newBalanceS;
}

function formulateClientBalanceVarName(address client) private pure returns (string memory) {
return StringUtils.addressToHexString(client);
}

}

0 comments on commit 8a1023d

Please sign in to comment.