Skip to content

Instantly share code, notes, and snippets.

@Hazelwu2
Last active April 18, 2023 15:09
Show Gist options
  • Select an option

  • Save Hazelwu2/4bb5b93f7507c99859f31de010eeb9b8 to your computer and use it in GitHub Desktop.

Select an option

Save Hazelwu2/4bb5b93f7507c99859f31de010eeb9b8 to your computer and use it in GitHub Desktop.
Buy Me A Coffee
// 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