Git Product home page Git Product logo

algo-dices's Introduction

๐ŸŽฒ AlgoDices

Let's roll your dices on Algorand!

Overview

AlgoDices let players roll dices on-chain (both TestNet and MainNet), thanks the Randomness Beacon Application.

Starting from AVM 7, Algorand enables turstless randomness on chain, thanks to the use of VRF: with the vrf_verify opcode an oracle Smart Contract can prove that a pseudo-random value has been honestly computed off-chain through a VRF process for a given blockchain round in the future.

AlgoDices makes use of the Randomness Beacon App deployed on TestNet and MainNet, which verifies, stores and provides the randomness to other applications for a given (future) round according to ARC-21.

NOTE: There is a non-negligible risk the MainNet Randomness Beacon miss certain round or becomes unavailable. In the latter case a new version of AlgoDices will be released.

ABI

AlgoDices ABI exposes two methods:

  1. book_dices_roll
  2. roll_dices

AlgoDices App requires players to opt-in.

Book (future) dices roll

Players can book dices rolls for future round, committing to future randomness:

{
    "name": "book_dices_roll",
    "args": [
        {
            "type": "uint64",
            "name": "future_round",
            "desc": "Future round booked for dices roll"
        }
    ],
    "returns": {
        "type": "void"
    },
    "desc": "Book dices roll for a future round."
}

Future round committment

Althogh the more the round is distant in the future the better is for security, AlgoDices App does not impose any minimum future_round, leaving this check to the client. An external App interacting with AlgoDices, for example, could mandate future_round to be greater than current round plus N.

โš ๏ธ Calling book_dices_roll twice will overwrite the last booked round, even if the previous booked randomness is unused.

Let's roll dices!

Once the booked randomness is available, players can roll dices with "real" number of faces (d2, d4, d6, d8, d10, d12, d20, are common dices in board games).

Players can roll multiple dices together (e.g. 2d6, 1d8 and 1d20), up to 14.

โš ๏ธ Players should wait at least for future_round + 8 round to be sure that their booked randomness is available in the Randomness Beacon App.

{
    "name": "roll_dices",
    "args": [
        {
            "type": "application",
            "name": "randomness_beacon",
            "desc": "Randomness Beacon App ID (TestNet: 110096026, MainNet: 947957720)"
        },
        {
            "type": "uint8[]",
            "name": "dices",
            "desc": "Array of dices to roll (faces: 2, 4, 6, 8, 10, 12, 20)"
        }
    ],
    "returns": {
        "type": "uint8[]",
        "desc": "Dices roll result."
    },
    "desc": "Roll dices with a given number of faces. Fee: 3 * min_fee."
}

AlgoDice roll_dices returns an array containing the dices results.

๐ŸŽฐ Games

On-chain games can call AlgoDices imposing their own rules, for example: a GameApp calls roll_die two times (on behalf of players Alice and Bob participating in the game through proxy Contract Accounts controlled by GameApp) as Inner Transactions executed in the same GameApp Call, requiring dice = 6. The highest result wins the match.

AlgoDices is also a trustless dices roller to play board games and RPGs (like D&D) "over-the-phone": players can safely roll dices remotely, with no mean of cheating. A Dungeon Master could ask the ๐Ÿง๐Ÿปโ€โ™€๏ธ Wizard Elf to roll 8d6 to count ๐Ÿ”ฅ Fireball damages and be sure the result is correct!

CLI

You can easily roll dices with the Python ๐ŸŽฒ AlgoDices CLI.

Clone algo-dices repo an open it:

$ git clone [email protected]:cusma/algo-dices.git
$ cd algo-dices

Install the virtual environment described in Pipfile using pipenv:

$ pipenv install

Lounch ๐ŸŽฒ AlgoDices CLI:

$ pipenv run python3 algodices.py

๐ŸŽฒ AlgoDices, let's roll verifiable random dices on Algornd!

Usage:
  algodices.py optin [--test]
  algodices.py book <future_rounds> [--test]
  algodices.py roll <dice> ... [--test]

Commands:
  optin    Subscribe to AlgoDices.
  book     Book a die roll in future round.
  roll     Roll dices (e.g. 2d6, 1d8 and 1d20: roll 6 6 8 20).

Options:
  -h, --help
  -t, --test

โš ๏ธ AlgoDices is deployed both on TestNet and MainNet, use the -t option to play on TestNet.

โš ๏ธ AlgoDices CLI requires entering a mnemonic phrase formatted as: "word_1 ... word_25".

Example: roll 2d6, 1d8 and 1d20 on TestNet:

$ pipenv run python3 algodices.py roll 6 6 8 20 -t

 --- ๐ŸŽฐ RESULTS ๐ŸŽฐ
 --- ๐ŸŽฒ d6:     1
 --- ๐ŸŽฒ d6:     5
 --- ๐ŸŽฒ d8:     1
 --- ๐ŸŽฒ d20:    17

DappFlow

AlgoDices can be played also with DappFlow ABI Studio, either on TestNet or MainNet, by logging with your wallet and importing the AlgoDices contract.json.

State Diagram

stateDiagram

[*] --> AlgoDices : create

AlgoDices --> AlgoDices_User : opt_in

AlgoDices_User --> Booked_Randomness : book_dices_roll

Booked_Randomness --> Booked_Randomness : book_dices_roll

state randomness <<choice>>
	Booked_Randomness --> randomness : roll_dices(faces)
	randomness --> Dices_Result : randomess_available
	randomness --> Booked_Randomness : randomness_unavailable

Dices_Result --> AlgoDices_User

Future developments (community welcome)

  • Multiple dices rolls on single round (e.g. [2d6, 1d8, 1d20]);
  • Contract Account proxy for players;
  • Switch to 512 bits math allowing more than 14 dices;
  • Unlimited dices rolls (pay-per-roll).

Credits

Thanks to @ori-shem-tov, @fabrice102 for the Randomness Oracle Application and @barnjamin for the CoinFlipper example that inspired AlgoDices.

Another special thanks again to @fabrice102 for the precious and clever considerations on "safe" optimization of randomness source usage.

algo-dices's People

Contributors

cusma avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

bmscis

algo-dices's Issues

Providing `instant_roll` and `booked_roll`

I see. I think I will take the best of two worlds in a future PR, refactoring the methods as follows:

  • Turning dice_rolls into an internal subroutine;
  • Provide a instant_roll, which takes the round a player want as method input;
  • Provide a booked_roll, which takes the round a player as previously committed to.

Originally posted by @cusma in #1 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.