Skip to content

solidsnakedev/raffle-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cardano Raffle

1. Introduction

A raffle is a game of chance in which each player purchases one or more tickets, each of which is assigned a unique number. The winner is determined by a random drawing of numbers. Raffles are a popular form of gambling, and they are often used to raise funds for charitable causes. They are also used as promotional tools by businesses, in which the prize is usually a product or service offered by the business.

2. Raffle in Cardano

Due to the deterministic nature of the Extended UTXO Model (EUTXO) it is hard to generate a random number and/or to trust an entity to provide such value. the following solutions are proposed in order to find a simple and practical implementation in Cardano.

3. Solutions

3.1 Secret-Reveal-Draw method :

This process can be divided in 3 periods

Secret Period

Every user commits to:

  1. send funds to pay for the ticket
  2. generate a hashed random number
  3. save the hashed number in the list of secret numbers.

$$ \text{secret numbers =} \begin{bmatrix} s_0 \\ s_1 \\ \vdots \\ s_i \end{bmatrix} $$

where :

$s$ = hashed secret number

Reveal Period

After secret period deadline ends, all participants should reveal their numbers and store it in a new list

$$ \text{revealed numbers =} \begin{bmatrix} r_0 \\ r_1 \\ \vdots \\ r_i \end{bmatrix} $$

Draw Winner

After the reveal period ends, just the users who reveal the secret number should participate and compute the random number using a function like modulo.

$$ \begin{equation} \text{random number =} \left(\sum_{i=0}^{j} r_i\right) \bmod k \end{equation} $$

$where :$

$j$ = array length

$i$ = array index

$k$ = number of participants that reveal the secret number

Conclusion

Although this approach looks feasible and truly random, this can be quite expensive and slow, due to the fact that every single user must reveal the secret number by making a new transaction.

3.2 Oracle method

An oracle is a good way to delegate the responsibility of drawing a random number, therefore there must be a verifiable mechanism to trust a third party provider to prove the number is randomly generated. A decent solution could be having a oracle script using the VRF function to generate a random number and at the same time the player can prove this number was correctly generated. More on this. link to VRF

Conclusion:

Currently there's no such a thing like VRF function in plutus, therefore you can not build this solution yet.

3.3 Minimum Hash Method :

Why a Hash?

A hash is a math function that takes an input with arbitrary length, and returns a encrypted output with a fixed length, a hash function like SHA256 returns a fixed 32 byte length output. This function can be used to compute the minimum hash in a list of arbitrary inputs.

Why minimum Hash?

sha256 function returns a unique value of 32 byte fixed length, therefore this value can be used to compare it with others

In order to calculate the min hash, participants should commit to use an immutable value, a mutable value and an unique value.

  • There are values shared with all participants
    • raffle seed (immutable value)
    • sold tickets (mutable value)
  • There is one value coming from the participant
    • token name (unique value)

Function to compute minimum hash :

$$ \begin{equation} \text{minimum hash =} Hash[tn [st \quad rs]] \end{equation} $$

$where :$

$\text{tn = token name}$

$\text{st = sold tickets}$

$\text{rs = random seed}$

Plutus representation

sha2_256 (appendByteString ticketName $ consByteString soldTickets randomSeed)

Conclusion :

Any user claim to have the minimum hash without the need to submit another transaction, this considerably reduce the amount of interactions with the script as this computation happens off-chain.

4. Cardano Raffle - Minimum hash method

This method can be divided in 4 Periods

4.1 Start Raffle

Datum schema is used as a shared state of the game where :

  • ticketPrice : the value in ADA the user has to paid to get a ticket
  • randomSeed : random bytestring used to compute the minimum hash
  • maxTickets : maximum amount of ticket to be sold
  • soldTickets : mutable value
  • minimumHash : value inserted by the user claiming to have the min hash
  • ticketList : list of sold tickets
  • Intervals : intervals to delimit buy-claim-close period

The raffle initiator commits to:

  1. Set the ticketPrice in Lovelace
  2. Set the maxTickets > 0
  3. Set a randomSeed as arbitrary Bytestring
  4. Set the minimumHash as empty Bytestring
  5. Set ticketList to as empty List
  6. Set both intervals in PosixTime

4.2 Buy Period

Every user commits to :

  1. Send funds to pay for the ticket
  2. Use the consumed utxo to generate a unique ticket name
  3. Mint only 1 ticket
  4. Keep the initial raffle settings
  5. increment datum soldTickets by 1

Function to mint an unique token name :

The minting ticket policy force the user to pick the spent utxo, and hash it to get an unique token name

sha2_256 (consByteString (txOutRefIdx utxo) ((getTxId . txOutRefId) utxo))

4.3 Claim Period

Once the buy period ends any user can claim to have the minimumhash by using the following parameters:

  1. random seed
  2. sold tickets
  3. ticket name

The only value that is not be deterministic is the number of sold tickets.

if an user claims to have the minimum hash value, this one is allowed to update the datum field of minimumhash. if another user claim to have a lower minimum hash value than the previous user, this one can update the datum field of minimumhash.

4.4 Close Period

Just the user holding the token that computes the minimum hash can consumed the funds from the script

requirements:

  1. Hold the ticket that computes the minimum hash
  2. Claim period already ended

5. Compile Code

cabal run raffle-project e1c0c4132b498ae79762c7df068834b7e2b4841e6ce934084a1ecf94dccbff97#0 raffle

6. Front End - NextJS

Install nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

reference link

Install Node and NPM

nvm install --lts

Go to front-end folder

npm install
npm run dev

7. Roadmap 🐢

On chain

  • Combine mint raffle with start raffle
  • Enable time intervals in lottery validator
  • Evaluate and Implement -> On chain association list with constant time
  • Evaluate and Implement Merke Tree for ticket list -> Plutus Merkle Tree
  • Migrate to Plutarch
  • Implement V2 features
  • Add multiple benerifiaries to Contract
  • Split prize between beneficiaries
  • Let users decide the prize currency symbol

Off chain

  • Add datum schema text field to start raffle
  • Update raffle component when tx is sent

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published