Skip to main content

TieredLiquidityDistributor

Git Source

Author: PoolTogether Inc.

A contract that distributes liquidity according to PoolTogether V5 distribution rules.

Constants

MINIMUM_NUMBER_OF_TIERS

uint8 constant MINIMUM_NUMBER_OF_TIERS = 4;

MAXIMUM_NUMBER_OF_TIERS

uint8 constant MAXIMUM_NUMBER_OF_TIERS = 11;

NUMBER_OF_CANARY_TIERS

uint8 constant NUMBER_OF_CANARY_TIERS = 2;

State Variables

TIER_ODDS_0

The odds for each tier and number of tiers pair. For n tiers, the last three tiers are always daily.

SD59x18 internal immutable TIER_ODDS_0;

TIER_ODDS_EVERY_DRAW

SD59x18 internal immutable TIER_ODDS_EVERY_DRAW;

TIER_ODDS_1_5

SD59x18 internal immutable TIER_ODDS_1_5;

TIER_ODDS_1_6

SD59x18 internal immutable TIER_ODDS_1_6;

TIER_ODDS_2_6

SD59x18 internal immutable TIER_ODDS_2_6;

TIER_ODDS_1_7

SD59x18 internal immutable TIER_ODDS_1_7;

TIER_ODDS_2_7

SD59x18 internal immutable TIER_ODDS_2_7;

TIER_ODDS_3_7

SD59x18 internal immutable TIER_ODDS_3_7;

TIER_ODDS_1_8

SD59x18 internal immutable TIER_ODDS_1_8;

TIER_ODDS_2_8

SD59x18 internal immutable TIER_ODDS_2_8;

TIER_ODDS_3_8

SD59x18 internal immutable TIER_ODDS_3_8;

TIER_ODDS_4_8

SD59x18 internal immutable TIER_ODDS_4_8;

TIER_ODDS_1_9

SD59x18 internal immutable TIER_ODDS_1_9;

TIER_ODDS_2_9

SD59x18 internal immutable TIER_ODDS_2_9;

TIER_ODDS_3_9

SD59x18 internal immutable TIER_ODDS_3_9;

TIER_ODDS_4_9

SD59x18 internal immutable TIER_ODDS_4_9;

TIER_ODDS_5_9

SD59x18 internal immutable TIER_ODDS_5_9;

TIER_ODDS_1_10

SD59x18 internal immutable TIER_ODDS_1_10;

TIER_ODDS_2_10

SD59x18 internal immutable TIER_ODDS_2_10;

TIER_ODDS_3_10

SD59x18 internal immutable TIER_ODDS_3_10;

TIER_ODDS_4_10

SD59x18 internal immutable TIER_ODDS_4_10;

TIER_ODDS_5_10

SD59x18 internal immutable TIER_ODDS_5_10;

TIER_ODDS_6_10

SD59x18 internal immutable TIER_ODDS_6_10;

TIER_ODDS_1_11

SD59x18 internal immutable TIER_ODDS_1_11;

TIER_ODDS_2_11

SD59x18 internal immutable TIER_ODDS_2_11;

TIER_ODDS_3_11

SD59x18 internal immutable TIER_ODDS_3_11;

TIER_ODDS_4_11

SD59x18 internal immutable TIER_ODDS_4_11;

TIER_ODDS_5_11

SD59x18 internal immutable TIER_ODDS_5_11;

TIER_ODDS_6_11

SD59x18 internal immutable TIER_ODDS_6_11;

TIER_ODDS_7_11

SD59x18 internal immutable TIER_ODDS_7_11;

ESTIMATED_PRIZES_PER_DRAW_FOR_4_TIERS

The estimated number of prizes given X tiers.

uint32 internal immutable ESTIMATED_PRIZES_PER_DRAW_FOR_4_TIERS;

ESTIMATED_PRIZES_PER_DRAW_FOR_5_TIERS

uint32 internal immutable ESTIMATED_PRIZES_PER_DRAW_FOR_5_TIERS;

ESTIMATED_PRIZES_PER_DRAW_FOR_6_TIERS

uint32 internal immutable ESTIMATED_PRIZES_PER_DRAW_FOR_6_TIERS;

ESTIMATED_PRIZES_PER_DRAW_FOR_7_TIERS

uint32 internal immutable ESTIMATED_PRIZES_PER_DRAW_FOR_7_TIERS;

ESTIMATED_PRIZES_PER_DRAW_FOR_8_TIERS

uint32 internal immutable ESTIMATED_PRIZES_PER_DRAW_FOR_8_TIERS;

ESTIMATED_PRIZES_PER_DRAW_FOR_9_TIERS

uint32 internal immutable ESTIMATED_PRIZES_PER_DRAW_FOR_9_TIERS;

ESTIMATED_PRIZES_PER_DRAW_FOR_10_TIERS

uint32 internal immutable ESTIMATED_PRIZES_PER_DRAW_FOR_10_TIERS;

ESTIMATED_PRIZES_PER_DRAW_FOR_11_TIERS

uint32 internal immutable ESTIMATED_PRIZES_PER_DRAW_FOR_11_TIERS;

_tiers

The Tier liquidity data.

mapping(uint8 tierId => Tier tierData) internal _tiers;

grandPrizePeriodDraws

The frequency of the grand prize

uint24 public immutable grandPrizePeriodDraws;

tierShares

The number of shares to allocate to each prize tier.

uint8 public immutable tierShares;

canaryShares

The number of shares to allocate to each canary tier.

uint8 public immutable canaryShares;

reserveShares

The number of shares to allocate to the reserve.

uint8 public immutable reserveShares;

tierLiquidityUtilizationRate

The percentage of tier liquidity to target for utilization.

UD60x18 public immutable tierLiquidityUtilizationRate;

prizeTokenPerShare

The number of prize tokens that have accrued per share for all time.

This is an ever-increasing exchange rate that is used to calculate the prize liquidity for each tier.

Each tier holds a separate tierPrizeTokenPerShare; the delta between the tierPrizeTokenPerShare and the prizeTokenPerShare tierShares is the available liquidity they have.*

uint128 public prizeTokenPerShare;

numberOfTiers

The number of tiers for the last awarded draw. The last tier is the canary tier.

uint8 public numberOfTiers;

_lastAwardedDrawId

The draw id of the last awarded draw.

uint24 internal _lastAwardedDrawId;

lastAwardedDrawAwardedAt

The timestamp at which the last awarded draw was awarded.

uint48 public lastAwardedDrawAwardedAt;

_reserve

The amount of available reserve.

uint96 internal _reserve;

Functions

constructor

Constructs a new Prize Pool.

constructor(
uint256 _tierLiquidityUtilizationRate,
uint8 _numberOfTiers,
uint8 _tierShares,
uint8 _canaryShares,
uint8 _reserveShares,
uint24 _grandPrizePeriodDraws
);

Parameters

NameTypeDescription
_tierLiquidityUtilizationRateuint256The target percentage of tier liquidity to utilize each draw
_numberOfTiersuint8The number of tiers to start with. Must be greater than or equal to the minimum number of tiers.
_tierSharesuint8The number of shares to allocate to each tier
_canarySharesuint8The number of shares to allocate to each canary tier
_reserveSharesuint8The number of shares to allocate to the reserve.
_grandPrizePeriodDrawsuint24The number of draws between grand prizes

_awardDraw

Adjusts the number of tiers and distributes new liquidity.

function _awardDraw(uint24 _awardingDraw, uint8 _nextNumberOfTiers, uint256 _prizeTokenLiquidity) internal;

Parameters

NameTypeDescription
_awardingDrawuint24The ID of the draw that is being awarded
_nextNumberOfTiersuint8The new number of tiers. Must be greater than minimum
_prizeTokenLiquidityuint256The amount of fresh liquidity to distribute across the tiers and reserve

_computeNewDistributions

Computes the liquidity that will be distributed for the next awarded draw given the next number of tiers and prize liquidity.

function _computeNewDistributions(
uint8 _numberOfTiers,
uint8 _nextNumberOfTiers,
uint128 _currentPrizeTokenPerShare,
uint256 _prizeTokenLiquidity
) internal view returns (uint96 deltaReserve, uint128 newPrizeTokenPerShare);

Parameters

NameTypeDescription
_numberOfTiersuint8The current number of tiers
_nextNumberOfTiersuint8The next number of tiers to use to compute distribution
_currentPrizeTokenPerShareuint128The current prize token per share
_prizeTokenLiquidityuint256The amount of fresh liquidity to distribute across the tiers and reserve

Returns

NameTypeDescription
deltaReserveuint96The amount of liquidity that will be added to the reserve
newPrizeTokenPerShareuint128The new prize token per share

getTierPrizeSize

Returns the prize size for the given tier.

function getTierPrizeSize(uint8 _tier) external view returns (uint104);

Parameters

NameTypeDescription
_tieruint8The tier to retrieve

Returns

NameTypeDescription
<none>uint104The prize size for the tier

getTierPrizeCount

Returns the estimated number of prizes for the given tier.

function getTierPrizeCount(uint8 _tier) external pure returns (uint32);

Parameters

NameTypeDescription
_tieruint8The tier to retrieve

Returns

NameTypeDescription
<none>uint32The estimated number of prizes

_getTier

Retrieves an up-to-date Tier struct for the given tier.

function _getTier(uint8 _tier, uint8 _numberOfTiers) internal view returns (Tier memory);

Parameters

NameTypeDescription
_tieruint8The tier to retrieve
_numberOfTiersuint8The number of tiers, should match the current. Passed explicitly as an optimization

Returns

NameTypeDescription
<none>TierAn up-to-date Tier struct; if the prize is outdated then it is recomputed based on available liquidity and the draw ID is updated.

getTotalShares

Computes the total shares in the system.

function getTotalShares() external view returns (uint256);

Returns

NameTypeDescription
<none>uint256The total shares

computeTotalShares

Computes the total shares in the system given the number of tiers.

function computeTotalShares(uint8 _numberOfTiers) public view returns (uint256);

Parameters

NameTypeDescription
_numberOfTiersuint8The number of tiers to calculate the total shares for

Returns

NameTypeDescription
<none>uint256The total shares

_computeReclamationStart

Determines at which tier we need to start reclaiming liquidity.

function _computeReclamationStart(uint8 _numberOfTiers, uint8 _nextNumberOfTiers) internal pure returns (uint8);

Parameters

NameTypeDescription
_numberOfTiersuint8The current number of tiers
_nextNumberOfTiersuint8The next number of tiers

Returns

NameTypeDescription
<none>uint8The tier to start reclaiming liquidity from

_consumeLiquidity

Consumes liquidity from the given tier.

function _consumeLiquidity(Tier memory _tierStruct, uint8 _tier, uint104 _liquidity) internal;

Parameters

NameTypeDescription
_tierStructTierThe tier to consume liquidity from
_tieruint8The tier number
_liquidityuint104The amount of liquidity to consume

_computePrizeSize

Computes the prize size of the given tier.

function _computePrizeSize(
uint8 _tier,
uint8 _numberOfTiers,
uint128 _tierPrizeTokenPerShare,
uint128 _prizeTokenPerShare
) internal view returns (uint104);

Parameters

NameTypeDescription
_tieruint8The tier to compute the prize size of
_numberOfTiersuint8The current number of tiers
_tierPrizeTokenPerShareuint128The prizeTokenPerShare of the Tier struct
_prizeTokenPerShareuint128The global prizeTokenPerShare

Returns

NameTypeDescription
<none>uint104The prize size

isCanaryTier

Returns whether the given tier is a canary tier

function isCanaryTier(uint8 _tier) public view returns (bool);

Parameters

NameTypeDescription
_tieruint8The tier to check

Returns

NameTypeDescription
<none>boolTrue if the passed tier is a canary tier, false otherwise

_numShares

Returns the number of shares for the given tier and number of tiers.

function _numShares(uint8 _tier, uint8 _numberOfTiers) internal view returns (uint8);

Parameters

NameTypeDescription
_tieruint8The tier to compute the number of shares for
_numberOfTiersuint8The number of tiers

Returns

NameTypeDescription
<none>uint8The number of shares

getTierRemainingLiquidity

Computes the remaining liquidity available to a tier.

function getTierRemainingLiquidity(uint8 _tier) public view returns (uint256);

Parameters

NameTypeDescription
_tieruint8The tier to compute the liquidity for

Returns

NameTypeDescription
<none>uint256The remaining liquidity

_getTierRemainingLiquidity

Computes the remaining tier liquidity.

function _getTierRemainingLiquidity(uint128 _tierPrizeTokenPerShare, uint128 _prizeTokenPerShare, uint8 _tierShares)
internal
pure
returns (uint256);

Parameters

NameTypeDescription
_tierPrizeTokenPerShareuint128The prizeTokenPerShare of the Tier struct
_prizeTokenPerShareuint128The global prizeTokenPerShare
_tierSharesuint8The number of shares for the tier

Returns

NameTypeDescription
<none>uint256The remaining available liquidity

estimatedPrizeCount

Estimates the number of prizes for the current number of tiers, including the first canary tier

function estimatedPrizeCount() external view returns (uint32);

Returns

NameTypeDescription
<none>uint32The estimated number of prizes including the canary tier

estimatedPrizeCountWithBothCanaries

Estimates the number of prizes for the current number of tiers, including both canary tiers

function estimatedPrizeCountWithBothCanaries() external view returns (uint32);

Returns

NameTypeDescription
<none>uint32The estimated number of prizes including both canary tiers

reserve

Returns the balance of the reserve.

function reserve() external view returns (uint96);

Returns

NameTypeDescription
<none>uint96The amount of tokens that have been reserved.

estimatedPrizeCount

Estimates the prize count for the given number of tiers, including the first canary tier. It expects no prizes are claimed for the last canary tier

function estimatedPrizeCount(uint8 numTiers) public view returns (uint32);

Parameters

NameTypeDescription
numTiersuint8The number of prize tiers

Returns

NameTypeDescription
<none>uint32The estimated total number of prizes

estimatedPrizeCountWithBothCanaries

Estimates the prize count for the given tier, including BOTH canary tiers

function estimatedPrizeCountWithBothCanaries(uint8 numTiers) public view returns (uint32);

Parameters

NameTypeDescription
numTiersuint8The number of tiers

Returns

NameTypeDescription
<none>uint32The estimated prize count across all tiers, including both canary tiers.

_estimateNumberOfTiersUsingPrizeCountPerDraw

Estimates the number of tiers for the given prize count.

function _estimateNumberOfTiersUsingPrizeCountPerDraw(uint32 _prizeCount) internal view returns (uint8);

Parameters

NameTypeDescription
_prizeCountuint32The number of prizes that were claimed

Returns

NameTypeDescription
<none>uint8The estimated tier

_sumTierPrizeCounts

Computes the expected number of prizes for a given number of tiers.

Includes the first canary tier prizes, but not the second since the first is expected to be claimed.

function _sumTierPrizeCounts(uint8 _numTiers) internal view returns (uint32);

Parameters

NameTypeDescription
_numTiersuint8The number of tiers, including canaries

Returns

NameTypeDescription
<none>uint32The expected number of prizes, first canary included.

getTierOdds

Computes the odds for a tier given the number of tiers.

function getTierOdds(uint8 _tier, uint8 _numTiers) public view returns (SD59x18);

Parameters

NameTypeDescription
_tieruint8The tier to compute odds for
_numTiersuint8The number of prize tiers

Returns

NameTypeDescription
<none>SD59x18The odds of the tier

Events

ReserveConsumed

Emitted when the reserve is consumed due to insufficient prize liquidity.

event ReserveConsumed(uint256 amount);

Parameters

NameTypeDescription
amountuint256The amount to decrease by

Structs

Tier

Struct that tracks tier liquidity information.

struct Tier {
uint24 drawId;
uint104 prizeSize;
uint128 prizeTokenPerShare;
}

Properties

NameTypeDescription
drawIduint24The draw ID that the tier was last updated for
prizeSizeuint104The size of the prize for the tier at the drawId
prizeTokenPerShareuint128The total prize tokens per share that have already been consumed for this tier.