Verified contractPartial
Keno
Address0xdb7b25bc934d815af76aba0beb9b047f36e891c2
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.5.0
- Same code
1 more contract
Details
- Compiler
- solc 0.5.0+commit.1d4f565a
- Language
- solidity
- Optimization
- Yes, 200 runs
- License
- Not stated
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 7,342,928
- Creation transaction
- 0x2b15bec082…439ca8610c
Keno.sol
pragma solidity ^0.5.0;
library SafeCast {
/**
* Cast unsigned a to signed a.
*/
function castToInt(uint a) internal pure returns(int) {
assert(a < (1 << 255));
return int(a);
}
/**
* Cast signed a to unsigned a.
*/
function castToUint(int a) internal pure returns(uint) {
assert(a >= 0);
return uint(a);
}
}
library SafeMath {
/**
* @dev Multiplies two unsigned integers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Multiplies two signed integers, throws on overflow.
*/
function mul(int256 a, int256 b) internal pure returns (int256) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
int256 c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two unsigned integers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return a / b;
}
/**
* @dev Integer division of two signed integers, truncating the quotient.
*/
function div(int256 a, int256 b) internal pure returns (int256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// Overflow only happens when the smallest negative int is multiplied by -1.
int256 INT256_MIN = int256((uint256(1) << 255));
assert(a != INT256_MIN || b != - 1);
return a / b;
}
/**
* @dev Subtracts two unsigned integers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Subtracts two signed integers, throws on overflow.
*/
function sub(int256 a, int256 b) internal pure returns (int256) {
int256 c = a - b;
assert((b >= 0 && c <= a) || (b < 0 && c > a));
return c;
}
/**
* @dev Adds two unsigned integers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
/**
* @dev Adds two signed integers, throws on overflow.
*/
function add(int256 a, int256 b) internal pure returns (int256) {
int256 c = a + b;
assert((b >= 0 && c >= a) || (b < 0 && c < a));
return c;
}
}
interface GameInterface {
function maxBet(uint _num, uint _bankRoll) external view returns(uint);
function resultNumber(bytes32 _serverSeed, bytes32 _userSeed, uint _num) external view returns(uint);
function userProfit(uint _num, uint _betValue, uint _resultNum) external view returns(int);
function maxUserProfit(uint _num, uint _betValue) external view returns(int);
}
contract Utilities {
using SafeCast for int;
using SafeCast for uint;
using SafeMath for int;
using SafeMath for uint;
uint constant public PROBABILITY_DIVISOR = 10000;
uint constant public HOUSE_EDGE = 150;
uint constant public HOUSE_EDGE_DIVISOR = 10000;
/**
* @dev Calc max bet we allow
* We definitely do not allow bets greater than kelly criterion would allow.
* => The max bet is limited to the max profit of houseEdge * bankroll.
* => maxBet = houseEdge / (1/p * (1 - houseEdge) - 1) * bankroll, with p is win probability.
* The max bet can be further restricted on backend.
* @param _winProbability winProbability.
* @return max allowed bet.
*/
function maxBetFromProbability(uint _winProbability, uint _bankRoll) public pure returns(uint) {
assert(0 < _winProbability && _winProbability < PROBABILITY_DIVISOR);
uint tmp1 = PROBABILITY_DIVISOR.mul(HOUSE_EDGE_DIVISOR).div(_winProbability);
uint tmp2 = PROBABILITY_DIVISOR.mul(HOUSE_EDGE).div(_winProbability);
uint enumerator = HOUSE_EDGE.mul(_bankRoll);
uint denominator = tmp1.sub(tmp2).sub(HOUSE_EDGE_DIVISOR);
return enumerator.div(denominator);
}
/**
* Calculate user profit from total won.
* @param _totalWon user winnings.
* @param _betValue bet value.
* @return user profit.
*/
function calcProfitFromTotalWon(uint _totalWon, uint _betValue) public pure returns(int) {
uint houseEdgeValue = _totalWon.mul(HOUSE_EDGE).div(HOUSE_EDGE_DIVISOR);
return _totalWon.castToInt().sub(houseEdgeValue.castToInt()).sub(_betValue.castToInt());
}
/**
* @dev Generates a 256 bit random number by combining server and user seed.
* @param _serverSeed server seed.
* @param _userSeed user seed.
* @return random number generated by combining server and user seed.
*/
function generateRandomNumber(bytes32 _serverSeed, bytes32 _userSeed) public pure returns(uint) {
bytes32 combinedHash = keccak256(abi.encodePacked(_serverSeed, _userSeed));
return uint(combinedHash);
}
}
contract Keno is GameInterface, Utilities {
using SafeCast for uint;
using SafeMath for uint;
/// @dev divider for PAY_OUT and MAX_BET
uint public constant DIVIDER = 1000;
/// @dev #selectable fields
uint public constant SELECTABLE_FIELDS = 10;
/// @dev #available fields
uint public constant FIELDS = 40;
/// @dev max bet table as fraction of bankroll
uint16[11] public MAX_BET = [0, 5, 10, 7, 5, 4, 4, 2, 2, 2, 1];
/// @dev payout multiplier table, first index: selected fields, second: hits
uint24[][11] public PAY_OUT;
event LogGameCreated(uint num);
constructor() public {
// setup payout table
PAY_OUT[0] = [0];
PAY_OUT[1] = [0, 3940];
PAY_OUT[2] = [0, 2000, 3740];
PAY_OUT[3] = [0, 1000, 3150, 9400];
PAY_OUT[4] = [0, 800, 1700, 5300, 24500];
PAY_OUT[5] = [0, 250, 1400, 4000, 16600, 42000];
PAY_OUT[6] = [0, 0, 1000, 3650, 7000, 16000, 46000];
PAY_OUT[7] = [0, 0, 460, 3000, 4400, 14000, 39000, 80000];
PAY_OUT[8] = [0, 0, 0, 2250, 4000, 11000, 30000, 67000, 90000];
PAY_OUT[9] = [0, 0, 0, 1550, 3000, 8000, 14000, 37000, 65000, 100000];
PAY_OUT[10] = [0, 0, 0, 1400, 2200, 4400, 8000, 28000, 60000, 120000, 200000];
}
modifier onlyValidNum(uint _betNum) {
require(_betNum > 0 && _betNum < (1 << FIELDS) && getSelectedBits(_betNum) <= SELECTABLE_FIELDS, "Invalid num");
_;
}
modifier onlyValidResultNum(uint _resultNum) {
require(_resultNum < (1 << FIELDS) && getSelectedBits(_resultNum) == SELECTABLE_FIELDS);
_;
}
function maxBet(uint _betNum, uint _bankRoll) external onlyValidNum(_betNum) view returns(uint) {
uint fields = getSelectedBits(_betNum);
return uint(MAX_BET[fields]).mul(_bankRoll).div(DIVIDER);
}
function resultNumber(bytes32 _serverSeed, bytes32 _userSeed, uint _betNum) external onlyValidNum(_betNum) view returns(uint) {
uint resultNum = 0;
bytes32 seed = keccak256(abi.encodePacked(_serverSeed, _userSeed));
for (uint i = 0; i < SELECTABLE_FIELDS; i++) {
uint randNum = uint(seed) % (FIELDS - i);
uint pos = 0;
uint resultPos = 0;
for (;;) {
if (resultNum & (1 << resultPos) == 0) {
if (pos == randNum) {
break;
}
pos++;
}
resultPos++;
}
resultNum |= 1 << resultPos;
// update seed
seed = keccak256(abi.encodePacked(seed));
}
return resultNum;
}
function userProfit(uint _betNum, uint _betValue, uint _resultNum)
external
onlyValidNum(_betNum)
onlyValidResultNum(_resultNum)
view
returns(int)
{
uint hits = getSelectedBits(_betNum & _resultNum);
uint selected = getSelectedBits(_betNum);
return calcProfit(_betValue, selected, hits);
}
function maxUserProfit(uint _betNum, uint _betValue) external onlyValidNum(_betNum) view returns(int) {
uint selected = getSelectedBits(_betNum);
return calcProfit(_betValue, selected, selected);
}
function calcProfit(uint _betValue, uint _selected, uint _hits) private view returns(int) {
assert(_hits <= _selected);
assert(_selected <= SELECTABLE_FIELDS);
uint payoutMultiplier = PAY_OUT[_selected][_hits];
uint payout = _betValue.mul(payoutMultiplier).div(DIVIDER);
return payout.castToInt().sub(_betValue.castToInt());
}
function getSelectedBits(uint _num) private pure returns(uint) {
uint selectedBits = 0;
// Could be calculated more efficient.
// But as it's only needed if a conflict arises, let's keep it simple.
for (uint i = 0; i < FIELDS; i++) {
if (_num & (1 << i) > 0) {
selectedBits += 1;
}
}
return selectedBits;
}
}Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
}
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.