This contract implements a simple DICE game between two players with 50/50 odds of winning.
Before playing all players deposit funds into their @dice account just like the @exchange contract
- Player 1 proposes to bet 1 EOS and submits SHA256(secret1)
- Player 2 proposes to bet 1 EOS and submits SHA256(secret2)
Because Player 1 and 2 bet equal amounts their orders are matched and the game begins.
- A Player reveales their secret
- A 5 minute deadline starts whereby the first to reveal automatically wins unless the other player reveals
- The other player reveals and a winner is chosen and paid based upon the value of sha256( cat(secret1,secret2) )
- After the deadline anyone can trigger a default claim and the rewards
A variation on this game would be to add an additional information on offer creation that will get paid a commission when the player wins. With this commission in place there is financial incentive for a service provider to continue to execute the game in a timely manner as well as provide quality and entertaining interfaces on top of this game.
This same basic model can be used to build more robust games.
- Block Producers may exclude reveal transaction
- Losers may force winner to wait 5 minutes to get rewards
- Service providers may fail to auto-reveal on your behalf
- You may lose internet connectivity mid-game
- A blockhain reorganization could cause some havock if secrets are revealed too quickly
- @dice could protect users by rejecting reveals until a game creation is irreversible (about 45 seconds max)
- users could take risk themselves by deciding how many confirmations are required
- for small amounts it probably doesn't matter
- under normal operation of DPOS chains there are few if any chain reorganizations
-
Wallet must be unlock and have at least the following private keys
5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 5Jmsawgsp1tQ3GD6JyGCwy1dcvqKZgX6ugMVMdjirx85iv5VyPR
cleos set contract eosio build/contracts/eosio.bios -p eosio
cleos create account eosio eosio.token EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
cleos set contract eosio.token build/contracts/eosio.token -p eosio.token
cleos create account eosio dice EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
cleos set contract dice build/contracts/dice -p dice
cleos push action eosio.token create '[ "eosio", "1000000000.0000 EOS", 0, 0, 0]' -p eosio.token
cleos create account eosio alice EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
cleos create account eosio bob EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
cleos push action eosio.token issue '[ "alice", "1000.0000 EOS", "" ]' -p eosio
cleos push action eosio.token issue '[ "bob", "1000.0000 EOS", "" ]' -p eosio
cleos set account permission alice active '{"threshold": 1,"keys": [{"key": "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4","weight": 1}],"accounts": [{"permission":{"actor":"dice","permission":"active"},"weight":1}]}' owner -p alice
cleos set account permission bob active '{"threshold": 1,"keys": [{"key": "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4","weight": 1}],"accounts": [{"permission":{"actor":"dice","permission":"active"},"weight":1}]}' owner -p bob
cleos push action dice deposit '[ "alice", "100.0000 EOS" ]' -p alice
cleos push action dice deposit '[ "bob", "100.0000 EOS" ]' -p bob
openssl rand 32 -hex
28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905
echo -n '28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905' | xxd -r -p | sha256sum -b | awk '{print $1}'
d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883
cleos push action dice offerbet '[ "3.0000 EOS", "alice", "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883" ]' -p alice
openssl rand 32 -hex
15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12
echo -n '15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12' | xxd -r -p | sha256sum -b | awk '{print $1}'
50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129
cleos push action dice offerbet '[ "3.0000 EOS", "bob", "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129" ]' -p bob
cleos get table dice dice account
{
"rows": [{
"owner": "alice",
"eos_balance": "97.0000 EOS",
"open_offers": 0,
"open_games": 1
},{
"owner": "bob",
"eos_balance": "97.0000 EOS",
"open_offers": 0,
"open_games": 1
}
],
"more": false
}
cleos get table dice dice game
{
"rows": [{
"id": 1,
"bet": "3.0000 EOS",
"deadline": "1970-01-01T00:00:00",
"player1": {
"commitment": "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883",
"reveal": "0000000000000000000000000000000000000000000000000000000000000000"
},
"player2": {
"commitment": "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129",
"reveal": "0000000000000000000000000000000000000000000000000000000000000000"
}
}
],
"more": false
}
cleos push action dice reveal '[ "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129", "15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12" ]' -p bob
cleos get table dice dice game
{
"rows": [{
"id": 1,
"bet": "3.0000 EOS",
"deadline": "2018-04-17T07:45:49",
"player1": {
"commitment": "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883",
"reveal": "0000000000000000000000000000000000000000000000000000000000000000"
},
"player2": {
"commitment": "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129",
"reveal": "15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12"
}
}
],
"more": false
}
cleos push action dice reveal '[ "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883", "28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905" ]' -p alice
cleos get table dice dice account
{
"rows": [{
"owner": "alice",
"eos_balance": "103.0000 EOS",
"open_offers": 0,
"open_games": 0
},{
"owner": "bob",
"eos_balance": "97.0000 EOS",
"open_offers": 0,
"open_games": 0
}
],
"more": false
}
cleos push action dice withdraw '[ "alice", "103.0000 EOS" ]' -p alice
cleos get currency balance eosio.token alice eos
1003.0000 EOS