Last active
April 18, 2023 15:09
-
-
Save Hazelwu2/4bb5b93f7507c99859f31de010eeb9b8 to your computer and use it in GitHub Desktop.
Buy Me A Coffee
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
| // SPDX-License-Identifier: UNLICENSED | |
| pragma solidity ^0.8.18; | |
| contract BuyMeACoffee { | |
| uint256 totalCoffee; | |
| address payable public owner; | |
| constructor() payable { | |
| owner = payable(msg.sender); | |
| } | |
| event NewCoffee ( | |
| address indexed from, | |
| uint256 timestamp, | |
| string message, | |
| string name | |
| ); | |
| struct Coffee { | |
| address sender; | |
| string message; | |
| string name; | |
| uint256 timestamp; | |
| } | |
| Coffee[] coffee; | |
| function getAllCoffee() public view returns (Coffee[] memory) { | |
| return coffee; | |
| } | |
| function getTotalCoffee() public view returns (uint256) { | |
| return totalCoffee; | |
| } | |
| function buyCoffee( | |
| string memory _message, | |
| string memory _name | |
| ) payable public { | |
| require(msg.value == 0.01 ether, "You need to pay 0.01 ether to buy a coffee!"); | |
| totalCoffee += 1; | |
| coffee.push(Coffee(msg.sender, _message, _name, block.timestamp)); | |
| (bool success,) = owner.call{value: msg.value}(""); | |
| require(success, "Fail to send Ether to owner"); | |
| emit NewCoffee( | |
| msg.sender, | |
| block.timestamp, | |
| _message, | |
| _name | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment