Contract Maths
Introduction
Ethlizard's staking contract is required to calculate the current share of a user and their weight amongst the total staked pool in order to distribute rewards fairly.
In order to keep track of inflation, a user's weight as well as the global total, LizardLounge assigns each regular Ethlizards a value of 100 shares, and each genesis Ethlizards a value of 200 shares, both in big number form. In order to accurately calculate the weight of the user's pools, we also need to keep track of the global shares. We only store the value of the global shares in the contract, as storing all the user's individual shares, which update dynamically would be extremely expensive.
When rewards are distributed, a pool is created and inflation will be reset. Users will have different weights and rewards based on different pools, and to calculate this, we create a pool structure that contains the value, timestamp and current global shares when created. Inflation being reset results in a reduction of 80% of the inflation accumulated prior to the pool's creation.
For the rest of these sections, we will refer to a user's percentage ownership as their weight and their stored values as shares.
Functions
getCurrentShareRaw
This function will return the current raw share of a user. There are 4 cases that can occur:
Case A: If the no pools have been created after the user is staked, this means we do not factor in resets.
We call
calculateShareFromTime
and return the result.
Case B: One or more pools were created, but the user was staked before the creation of all of them.
We call
calculateShareFromTime
andresetShareRaw
and then loop between the pools to calculate the shares between them, and then once again calculate the shares between the current time and the last pool.
Case C: The user was staked between 2 pools.
We iterate through the pools to find out which 2 pools the user is staked between, and calculate the shares between the first pool created after the token was staked, and the time the token was staked, via
calculateShareFromTime
. We then iterate over the remaining pools to calculate the shares between them as well as the last pool, and then once again calculate the shares between the current time and the last pool.
createPool
This will call updateGlobalShares
, create a new pool structure and reset the current pending rewards and then call resetGlobalShares.
resetGlobalShares
This will reset the existing global shares by slashing the current inflated shares by 80%.
updateGlobalShares
This will update the current global shares by calculating the days that have passed since the last update to shares, and then multiplying the result ofcalculateRebasePercentage
and the prior global shares to compute the new global shares.
claimCalculation
This will calculate the owed rewards a staked tokenId based on an inputted pool. There are 3 cases that can occur:
Case A: There is only 1 pool, which means we do not need to factor in any resets.
We call
calculateShareFromTime
and calculate the owed rewards and return it
Case B: There are one or more pools created, but the user was staked before all of them.
We call
calculateShareFromTime
andresetShareRaw
and then loop between the pools to calculate the shares between them, resetting whenever iterating over a new loop. We then compute and return the rewards.
Case C: The user was staked between 2 pools.
We iterate through the pools to find out which 2 pools the user is staked between, and calculate the shares between the first pool created after the token was staked, and the time the token was staked, via
calculateShareFromTime
. We then iterate over the remaining pools to calculate the shares between them and then calculate and return the amount of the owed rewards.
calculateShareFromTime
This takes in 2 different UNIX timestamps and returns the inflation-applied raw share of it. A rebase will only be valid if the current timestamp is a full day from the start of staking. For example, if staking started at 7 pm, a user deposited their rewards at 6 pm, they would rebase and have inflation applied to their shares. We call calculateRebasePercentage here to calculate the inflation, as our rebasing factor is stored in 64.64-bit binary fixed-point form.
calculateRebasePercentage
This function calculates the power of the rebasing factor to the inputted required rebases, and then returns the number in big number form. See the implementation section for a detailed explanation.
resetShareRaw
This will reset the inputted user's shares by slashing the current inflated shares by 80%.
Last updated