Skip to content

Commit

Permalink
Added tutorial 18
Browse files Browse the repository at this point in the history
  • Loading branch information
willitscale committed Oct 10, 2017
1 parent 7c32d16 commit f9cac8c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ The companion to the Youtube tutorials
- [Learning Solidity : Tutorial 14 Transferring Ethereum between contracts](https://www.youtube.com/watch?v=ELWSKMcJfI8)
- [Learning Solidity : Tutorial 15 Public vs External](https://www.youtube.com/watch?v=Ii4g38mPPlg)
- [Learning Solidity : Tutorial 16 Time Based Events](https://www.youtube.com/watch?v=HGw-yalqdgs)
- [Learning Solidity : Tutorial 17 Polymorphism](https://www.youtube.com/watch?v=l_E5F5qnbtk)
- [Learning Solidity : Tutorial 17 Polymorphism](https://www.youtube.com/watch?v=l_E5F5qnbtk)
- [Learning Solidity : Tutorial 18 Randomness and Gambling](https://www.youtube.com/watch?v=3wY5PRliphE)
94 changes: 94 additions & 0 deletions tutorial-18/Casino.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
pragma solidity ^0.4.0;

contract Casino {

uint private start;

uint private buyPeriod = 1000;
uint private verifyPeriod = 100;
uint private checkPeriod = 100;

mapping(address => uint) private _tickets;
mapping(address => uint) private _winnings;

address[] _entries;
address[] _verified;

uint private winnerSeed;
bool private hasWinner;
address private winner;

function Casino()
public {
start = block.timestamp;
}

/**
* This should NOT be part of the contract!!
*/
function unsafeEntry(uint number, uint salt)
public
payable
returns (bool) {
return buyTicket(generateHash(number, salt));
}

function generateHash(uint number, uint salt)
public
pure
returns (uint) {
return uint(keccak256(number + salt));
}

function buyTicket(uint hash)
public
payable
returns (bool) {
// Within the timeframe
require(block.timestamp < start+buyPeriod);
// Correct amount
require(1 ether == msg.value);
// 1 entry per address
require(_tickets[msg.sender] == 0);
_tickets[msg.sender] = hash;
_entries.push(msg.sender);
return true;
}

function verifyTicket(uint number, uint salt)
public
returns (bool) {
// Within the timeframe
require(block.timestamp >= start+buyPeriod);
require(block.timestamp < start+buyPeriod+verifyPeriod);
// Has a valid entry
require(_tickets[msg.sender] > 0);
// Validate hash
require(salt > number);
require(generateHash(number, salt) == _tickets[msg.sender]);
winnerSeed = winnerSeed ^ salt ^ uint(msg.sender);
_verified.push(msg.sender);
}

function checkWinner()
public
returns (bool) {
// Within the timeframe
require(block.timestamp >= start+buyPeriod+verifyPeriod);
require(block.timestamp < start+buyPeriod+verifyPeriod+checkPeriod);
if (!hasWinner) {
winner = _verified[winnerSeed % _verified.length];
_winnings[winner] = _verified.length-10 ether;
hasWinner = true;
}
return msg.sender == winner;
}

function claim()
public {
// Has winnings to claim
require(_winnings[msg.sender] > 0);
msg.sender.transfer(_winnings[msg.sender]);
_winnings[msg.sender] = 0;
}
}
19 changes: 19 additions & 0 deletions tutorial-18/Random.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity ^0.4.0;

contract Random {

function unsafeBlockRandom()
public
returns (uint) {
return uint(block.blockhash(block.number-1)) % 100;
}

uint private _baseIncrement;

function unsafeIncrementRandom()
public
returns (uint) {
return uint(sha3(_baseIncrement++)) % 100;
}

}

0 comments on commit f9cac8c

Please sign in to comment.