-
Notifications
You must be signed in to change notification settings - Fork 0
Create Casino.sol #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gpersoon
wants to merge
1
commit into
main
Choose a base branch
from
Add-casino
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Load in remix: remix.loadurl("https://github.com/web3examples/ethereum/solidity_examples/Casino.sol") | ||
| // Carefull with gas-estimates: due to random this doesn''t allways work | ||
| pragma solidity >=0.7.0 <0.9.0; | ||
|
|
||
| /// @author Gerard Persoon | ||
| /// @title A simple casino | ||
| contract Casino { | ||
|
|
||
| event Won(bool win) ; // declaring event | ||
|
|
||
| /// @notice Setup an intial amount for the bank, supplied during the creation of the contract. | ||
| constructor() payable { | ||
| } | ||
|
|
||
| /// @notice Perform the bet and pay out if you win | ||
| /// @dev several temporary variables are created to make debugging easier | ||
| function betAndWin() public payable returns (bool) { // returning value isn't easy to retreive | ||
| address payable betPlacer = payable(msg.sender); | ||
| uint bet = msg.value; | ||
| uint payout = bet * 2; | ||
| uint balance = getBankBalance(); | ||
| require(bet > 0, "No money added to bet."); | ||
| require(payout <= balance, "Not enough money in bank for this bet."); // bet has already been added to bank balance | ||
| bool win = bool (getRandom()%2 == 0); | ||
| if (win) { | ||
| (bool success, /* bytes memory response*/) = betPlacer.call{value: payout}(''); | ||
| require(success, "Pay was not successful."); | ||
| } | ||
| emit Won(win);// logging event | ||
| return win; | ||
| } | ||
|
|
||
| /// @notice Check the balance of the bank | ||
| /// @return returns the balance | ||
| function getBankBalance() public view returns(uint256) { | ||
| return address(this).balance; | ||
| } | ||
|
|
||
| /// @notice Draw a random number | ||
| /// @dev this is not secure but only to demonstrate | ||
| /// @return a pseudo random number | ||
| function getRandom() public view returns(uint256) { | ||
| return uint256(keccak256(abi.encodePacked(block.difficulty, block.coinbase, block.timestamp))); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its dangerous to use this for random |
||
| } | ||
|
|
||
| /// @notice Deposit more funds for bank | ||
| /// @dev used when the bank runs out of money | ||
| receive() external payable { | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows for an old version of solidity!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documented in #3
So the conversion can be resolved