Last active
April 7, 2024 01:02
-
-
Save apogiatzis/0bdd8140b085f646155e0826c071c77b to your computer and use it in GitHub Desktop.
Dice rolling game smart contract skeleton
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
pragma solidity ^0.5.0; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.0/contracts/token/ERC20/IERC20.sol"; | |
contract Dice { | |
IERC20 public nativeGameToken; | |
mapping(address => UserBet) private userBets; | |
struct UserBet { | |
IERC20 betToken; | |
uint8 betValue; | |
uint256 amount; | |
bool isBetPlaced; | |
} | |
/** | |
* Initializes the contract with an address of an ERC-20 token | |
*/ | |
constructor(IERC20 _nativeGameToken) public{ | |
// TODO: Complete this | |
// [OPTIONAL] Ensure that _nativeGameToken is valid | |
// Store _nativeGameToken to contracts Storage | |
} | |
/** | |
* Allows a user to place a bet on a dice value using a specific ERC-20 token amount. | |
* Returns the amount that was bet. | |
*/ | |
function placeBet(IERC20 betToken, uint256 amount, uint8 betValue) external returns(uint256) { | |
// TODO: Complete this | |
// Ensure that User has not placed a bet already | |
// Ensure that thet betValue is between 1-6 | |
// Create a new user bet for the sender | |
// Return the amount of the bet | |
} | |
/** | |
* Returns whether the caller has already placed a bet. | |
*/ | |
function isBetPlaced() public view returns(bool){ | |
// TODO: Complete this | |
// Return True if bet from sender was placed, False otherwise | |
} | |
/** | |
* Rolls the dice and transfers the funds to the winner appropriately. | |
* Returns a pair of values: | |
* (a) A boolean that indicates if the caller has won, | |
* (b) The roll of the dice. | |
*/ | |
function rollDice() external returns(bool, uint8) { | |
// TODO: Complete this | |
// Ensure that sender has already placed a bet | |
// Ensure that contract is able to send at least 6*betAmount Tokens on behalf of the user | |
// Ensure that contract can send at least bet amount of Tokens on behalf of the sender | |
// Toggle isBetPlaced for the sender's bet to False | |
// Roll the dice | |
// Check if sender has won. | |
// If won send tokens bet amount*6 from owner to ender | |
// Otherwise send bet amount of tokens from sender to owner | |
// Return if the sender has won or not and the roll of the dice | |
} | |
/** | |
* Generates a NOT cryptographically secure pseudo random number in the range of 1-6 (inclussive) | |
*/ | |
function random() private view returns (uint8) { | |
// WARNING! | |
// This random generator is not secure! | |
// This is only for the sake of the exercise! | |
uint256 blockValue = uint256(blockhash(block.number-1 + block.timestamp)); | |
return uint8(blockValue % 5) + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment