Last active
September 5, 2022 19:23
-
-
Save gwenf/359b1ec2c53f4cba3e01d8d3de1e7811 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
pragma solidity >=0.4.22 <0.7.0; | |
// phone company dapp | |
// charge telemarkers coins for calls | |
// whitelist people don't get charged | |
// anyone not on the whitelist has to pay a small fee | |
contract PhoneCompany { | |
uint public chargeAmount; | |
uint public coinPrice; | |
address companyOwner; | |
// a list of phone numbers attached to my address | |
// only the user can view their own phonebook | |
mapping(uint => uint[]) whitelisted; | |
struct User { | |
string name; | |
uint phoneNumber; | |
uint coins; | |
} | |
mapping(address => User) users; | |
constructor(uint amount, uint price) public { | |
companyOwner = msg.sender; | |
chargeAmount = amount; | |
coinPrice = price; | |
} | |
function updateAmount(uint amount) public { | |
require( | |
msg.sender == companyOwner, | |
"Only the company owner can update charge amount." | |
); | |
chargeAmount = amount; | |
} | |
function initNewUser(uint phoneNumber, string memory name) payable public returns(string memory) { | |
// check amount paid | |
require( | |
msg.value >= coinPrice * 100, | |
"You didn't send enough money to buy 100 coins." | |
); | |
users[msg.sender] = User({ | |
phoneNumber: phoneNumber, | |
name: name, | |
coins: 100 | |
}); | |
return 'Success! You now have 100 coins.'; | |
} | |
function coinBalance() public view returns(uint) { | |
return users[msg.sender].coins; | |
} | |
// add a phone number to a users phone book, whitelist | |
function addPhoneNumber(uint phoneNumber) public { | |
whitelisted[users[msg.sender].phoneNumber].push(phoneNumber); | |
} | |
function makeCall(uint phoneNumber) public returns(uint) { | |
bool isWhitelisted = false; | |
uint userPhone = users[msg.sender].phoneNumber; | |
for (uint i; i < whitelisted[userPhone].length; i++) { | |
if (whitelisted[userPhone][i] == phoneNumber) { | |
isWhitelisted = true; | |
break; | |
} | |
} | |
if (!isWhitelisted) { | |
users[msg.sender].coins = users[msg.sender].coins - chargeAmount; | |
} | |
return users[msg.sender].coins; | |
} | |
// users can restock their coins | |
function buyCoins() public payable returns(uint) { | |
require( | |
msg.value >= coinPrice, | |
"You didn't send enough money to buy coins." | |
); | |
uint coinNum = msg.value / coinPrice; | |
users[msg.sender].coins = users[msg.sender].coins + coinNum; | |
return users[msg.sender].coins; | |
} | |
// addLastCallerToWhitelist | |
// store info of last caller and return coins | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment