# โ๏ธDraw Mechanism

## Lifecycle

Each Pool Draw is automatically triggered based on the Pool Draw schedule configuration. Each minted block essentially checks for Pools ready to launch a new Draw and initiates the Draw process if one or more Pools are ready.

The following flow represents the key phases of a Draw:

## Prize Pool

The Prize Pool is the total amount of assets available in a Pool used to compute the Prizes. It evolves over time and each Draw computes the Prize amounts based on the current available Prize Pool.

**Prize Pool tokens sources:**

Fresh Staking rewards claimed during the Draw process (see above chart)

Undistributed rewards from previous Draws (equivalent to the sum of all Prizes with no winner)

Expired and Clawed back Prizes (see Prizes lifecycle for more information)

The Prize Pool available for a Draw is therefore the sum of (1) + (2) + (3) computed during the Draw phase.

## Time weighted balance (TWB)

The chances to win a Prize for a Depositor depends on the total amount they deposited into a Pool versus the total amount deposited by all Depositors within the Pool.

**Example 1 - without TWB:**

Assuming a Pool configured to have 1 possible Prize with 100% chance to be drawn and the following depositors:

Alice deposits 10 ATOM

Bob deposits 20 ATOM

Charlie deposits 70 ATOM

**Total deposited:**

100 ATOM

**Chances to win:**

Alice : 10/100

Bob : 20/100

Charlie : 70/100

Since the Prize Pool tokens come from Staking depositors assets, we must find a way to ensure that someone who deposits 7 days before a Draw has better odds to win a Prize compared to someone depositing 1 hour before the Draw happens. This is due to the fact that Staking rewards increase over time, this must therefore be reflected in the way we compute Depositors winning chances.

The Time Weighted Deposit Balance (TWB) is here to solve this issue and to ensure a fair Draw chance to all Depositors, regardless on when they deposited in the Pool. This also ensures that all Depositors can make their Deposit(s) whenever they see fit, without having to think about improving their chances to get a Prize early on.

Each Depositor final balance for a Draw is computed using the following formula:

`amount`

is the deposited amount`ฮด draw`

is the configured delta between Draws (ex: 7 days)`ฮด deposit`

is the time elapsed since the Deposit was made (ex: 3 days)

*N.B: TWB only matters for the first Draw happening after a Deposit was made, subsequent Draws will take the full Deposit amount into account since **ฮด deposit** will be greater than **ฮด draw**.*

**Example 1 - with TWB:**

Assuming a Pool configured to have 1 possible Prize with 100% chance to be drawn and the following depositors.

The Pool is configured to Draw every 10 days:

Alice deposits 10 ATOM on Day 0

Bob deposits 20 ATOM on Day 5

Charlie deposits 70 ATOM on Day 8

**Total deposited post TWB: **

10 * ((10-0)/10) + 20 * ((10-5)/10) + 70 * ((10-8)/10) =

**34**

**Chances to win:**

Alice : 10 * ((10-0)/10) / 34 = 10/34

Bob : 20 * ((10-5)/10) / 34 = 10/34

Charlie : 70 * ((10-8)/10) / 34 = 14/34

This examples shows that Charlie, despite depositing a way larger amount that Alice and Bob gets almost the same winning odds since he deposited very late in the Draw timeline.

## Prizes Draw

The Prizes Draw is the critical part of the Cosmos Millions protocol, it must abide to the following principles:

**Fast:**As blocks are minted every 5-6 seconds, its processing time must respect this order of magnitude.**Fair:**Everyone must have a shot, whatever the Deposit amount, and each Prize must have an independent probability to ensure large Deposits do not exponentially increase Depositors chance of winning.

### Algorithm choice

In order to abide to the first rule (be fast), the protocol first implementation is a **random drawing algorithm** which picks zero or one Winner per Prize. This ensures that the complexity of the Draw is not exponential based on the number of Depositors but rather logarithmic depending on the number or Prizes to draw and the number of Depositors: `O(log n)`

.

The protocol could also offer later on a** lottery algorithm** which would grant the capability to pick zero to N Winners per Prize, each Prize would then be divided among the Winners. This topic is up to discussions and technical investigations as it would likely lead to an exponential complexity per Draw based on the number or Prizes to draw and the number of Depositors.

### Random number generation

Random Number Generation (RNG) and blockchain technology is a long debated topic, as randomness and the need for blockchain consensus algorithms to be deterministic are quite opposite to each other.

In order to solve this, the Cosmos Millions protocol uses a simple, elegant and secure approach based on the previous block application hash alongside the current block timestamp coming from the block proposer to generate a pseudo-random seed.

The pseudo-random seed generated is then used subsequently to generate random numbers to draw each Prize with an independent probability.

The following pseudo-code summarizes the process:

### Draw algorithm

#### Base draw buffer

First, a buffer with all Deposits TWB is created such as all Deposits represent a fraction of the buffer based on their TWB value. It can be represented with a D*epositor Area* as shown below.

Example: 5 unique Depositors (as a matter of fact, we could see multiple deposits of `E`

or any other depositor here) with a total TWB Deposited amount of `107`

.

#### Prize Draw buffer

Secondly, each Prize Draw extends the draw buffer in order to account for the area in which the Prize cannot be won, based on its probability to find a winner.

The *Empty Area* in the chart below is computed using the following formula:

Example: Prize with a probability to win of 0.7 (70%)

#### Winner selection

Finally, the generated random number `[0, 1[`

for the Prize is used to compute a position within the buffer based on the buffer area length where `position = randomNumber * bufferArea`

.

**There is NO Winner IF:** `position`

is within the `Empty Area`

.

**There is ONE Winner IF:** `position`

is within the `Depositors Area`

, and naturally, the winner is the Depositor owning the area for this specific position.

Examples:

position

`15`

is owned by`C`

position

`50`

is owned by`E`

position

`110`

is part of the empty area (no owner = no winner)

## Winning chances

As explained in the previous section, the random drawing algorithm implemented by Cosmos Millions gives fair winning chances to each participant. Its inherent randomness makes it is impossible to predict which depositor will win but it is possible to compute the probabilities for a win to happen.

The following formula (exposed by the Cosmos Millions web application for each Pool) gives the probability (the chance) to win at least one prize during a Draw based on a total deposited amount:

For which:

**in**: the amount to deposit**TVL**: the total pool TVL**sponsor**: the total sponsored amount (part of the tvl)**prizes**: A batch of prizes (part of the prize strategy)**P(win)**: win probability for a prize of this batch**size**: number of prizes for this batch

*N.B: Time Weighted Balance is not included in this formula for the sake of simplicity but will be taken into account by the protocol. For instance, the in amount of the formula will become TWB(in).*

Last updated