Last active
May 11, 2022 15:28
-
-
Save AndreiCalazans/af920a01b3d721cbd0cb4dbd1631811b to your computer and use it in GitHub Desktop.
WIP - GuessGame Solidity Smart Contract
This file contains 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: MIT | |
pragma solidity ^0.8.7; | |
// Start game with a passphrase & reward | |
// Contract creator needs to have funds? | |
// Each address can only guess once (use modifier for this) | |
// Once correct guess give reward | |
// Add a game over modifier | |
contract GuessGame { | |
uint public reward; | |
bytes32 private passphrase; | |
mapping(address => bool) private didAddressGuessAlready; | |
constructor( | |
string memory _passphrase | |
) payable { | |
reward = msg.value; | |
passphrase = keccak256(abi.encodePacked(_passphrase)); | |
} | |
modifier isGameOver() { | |
if (reward == 0) revert("Reward was already won. Game over."); | |
_; | |
} | |
modifier onlyOneGuessPerAddress() { | |
if (didAddressGuessAlready[msg.sender] == false) { | |
didAddressGuessAlready[msg.sender] = true; | |
} else { | |
revert("Only one guess per address is allowed."); | |
} | |
_; | |
} | |
function guess(string memory _guess) external payable isGameOver onlyOneGuessPerAddress returns (bool) { | |
if (keccak256(abi.encodePacked(_guess)) != passphrase) { | |
return false; | |
} | |
payable(msg.sender).transfer(reward); | |
reward = 0; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some learnings from this:
Calling modifiers with arguments: