Skip to content

Instantly share code, notes, and snippets.

@bodrovis
Created October 25, 2021 16:59
Show Gist options
  • Save bodrovis/eb5e7211205432797c053ca059acb284 to your computer and use it in GitHub Desktop.
Save bodrovis/eb5e7211205432797c053ca059acb284 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MopedShop {
mapping (address => bool) buyers;
uint256 public price = 2 ether;
address public owner;
address public shopAddress;
bool fullyPaid; // false
event ItemFullyPaid(uint _price, address _shopAddress);
constructor() {
owner = msg.sender;
shopAddress = address(this);
}
function getBuyer(address _addr) public view returns(bool) {
require(owner == msg.sender, "You are not an owner!");
return buyers[_addr];
}
function addBuyer(address _addr) public {
require(owner == msg.sender, "You are not an owner!");
buyers[_addr] = true;
}
function getBalance() public view returns(uint) {
return shopAddress.balance;
}
function withdrawAll() public {
require(owner == msg.sender && fullyPaid && shopAddress.balance > 0, "Rejected");
address payable receiver = payable(msg.sender);
receiver.transfer(shopAddress.balance);
}
receive() external payable {
require(buyers[msg.sender] && msg.value <= price && !fullyPaid, "Rejected");
if(shopAddress.balance == price) {
fullyPaid = true;
emit ItemFullyPaid(price, shopAddress);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment