Skip to main content

Poolable

Poolable

Manages token distribution logic across different eras based on user levels and a halving mechanism.

This abstract contract provides the core functionalities for calculating token allocations, tracking user participation (levels and withdrawals) within specific eras, and managing the pool token supply. It is designed to be inherited by other pool contracts.

totalTokens

uint256 totalTokens

The total supply of tokens to be managed by this contract.

This value is set once during contract deployment and remains constant.

eras

mapping(uint256 => struct Era) eras

Era data includes: count of claims/withdrawals, total tokens claimed, and total active levels or difficulty.

Stores data for each era. Key is the era number.

eraLevels

mapping(uint256 => mapping(address => uint256)) eraLevels

Tracks the levels of each user per era. Mapping: eraNumber => userAddress => levels.

eraTokens

mapping(uint256 => mapping(address => uint256)) eraTokens

Tracks the tokens claimed by each user per era. Mapping: eraId => userAddress => eraTokens.

hasWithdrawn

mapping(uint256 => mapping(address => bool)) hasWithdrawn

Tracks withdrawals to ensure a user can only claim rewards once per era.

constructor

constructor(uint256 _totalTokens) public

Initializes the contract with the total amount of tokens for the pool.

Parameters

NameTypeDescription
_totalTokensuint256The total supply of tokens to be managed by this contract.

getEra

function getEra(uint256 era) external view returns (struct Era)

Returns the aggregated data for a specific era.

Provides access to claimsCount, tokens claimed, and levels for the requested era.

Parameters

NameTypeDescription
erauint256The number of the era to retrieve data for.

Return Values

NameTypeDescription
[0]struct EraEra The Era struct containing the aggregated details for the specified era.

tokensPerEra

function tokensPerEra(uint256 currentEpoch, uint256 halvingFactor) public view returns (uint256)

Tokens of actual ERA.

Returns the amount of tokens to be distributed to users in current era.

Parameters

NameTypeDescription
currentEpochuint256The current epoch number, used to determine halving mechanism.
halvingFactoruint256The number of eras for halving.

tokensPerEpoch

function tokensPerEpoch(uint256 currentEpoch) public view returns (uint256)

Tokens halve with each successive epoch: totalTokens / (2^currentEpoch).

Calculates the base amount of tokens to be distributed in an epoch.

Parameters

NameTypeDescription
currentEpochuint256The current epoch number.

Return Values

NameTypeDescription
[0]uint256The amount of tokens for the epoch.

_calculateUserEraTokens

function _calculateUserEraTokens(uint256 era, address to, uint256 _tokensPerEra) internal view returns (uint256)

Calculates the amount of tokens a user is eligible to withdraw in a specific era.

The calculation is based on the user's levels relative to the total levels in that era.

Parameters

NameTypeDescription
erauint256Era number.
toaddressUserAddress.
_tokensPerErauint256The total tokens available for distribution in this specific era.

Return Values

NameTypeDescription
[0]uint256The amount of tokens the user can claim. Returns 0 if the user has no levels.

_haveTokensToWithdraw

function _haveTokensToWithdraw(address delegate, uint256 era, uint256 _tokensPerEra) internal view returns (bool)

Private function to check if a user have tokens to withdraw at an era.

Parameters

NameTypeDescription
delegateaddressUser address.
erauint256User current era.
_tokensPerErauint256Pool tokensPerEra.

Return Values

NameTypeDescription
[0]boolbool True if have tokens to withdraw, false if will just update era.

_updateEraAfterWithdraw

function _updateEraAfterWithdraw(uint256 era, address user, uint256 numTokens) internal

This function should be called internally after a successful token withdrawal process. It increments the era's claims count and total tokens claimed.

Updates era data after a user withdraw.

Parameters

NameTypeDescription
erauint256The number of the era.
useraddressThe address of the user who claimed tokens.
numTokensuint256The amount of tokens claimed by the user.

_addPoolLevel

function _addPoolLevel(address to, uint256 levels, uint256 era) internal

Adds pool levels to a user for a specific era.

Parameters

NameTypeDescription
toaddressThe address of the user.
levelsuint256The amount of levels to add.
erauint256The number of the era.

_removePoolLevel

function _removePoolLevel(address _user, uint256 _era, uint256 _amountToRemove) internal

Removes pool levels users.

Parameters

NameTypeDescription
_useraddressThe address of the user.
_erauint256The number of the era.
_amountToRemoveuint256The amount of levels to remove.

hasWithdrawnEraModifier

modifier hasWithdrawnEraModifier(uint256 era, address delegate)

PoolLevelAdded

event PoolLevelAdded(address user, uint256 era, uint256 levelsAdded, uint256 newTotalEraLevels, uint256 newEraUserLevels)

Emitted when a user's pool levels are added for a specific era.

Parameters

NameTypeDescription
useraddressThe address of the user whose levels were added.
erauint256The era number where levels were added.
levelsAddeduint256The amount of levels added.
newTotalEraLevelsuint256The new total levels for the era.
newEraUserLevelsuint256The new total levels for the user in that era.

PoolLevelRemoved

event PoolLevelRemoved(address user, uint256 era, uint256 levelsRemoved, uint256 newTotalEraLevels, uint256 newEraUserLevels)

Emitted when a user's pool levels are removed for a specific era.

Parameters

NameTypeDescription
useraddressThe address of the user whose levels were removed.
erauint256The era number where levels were removed.
levelsRemoveduint256The amount of levels removed.
newTotalEraLevelsuint256The new total levels for the era.
newEraUserLevelsuint256The new total levels for the user in that era.

TokensWithdrawn

event TokensWithdrawn(address user, uint256 era, uint256 amount)

Emitted when a user successfully withdrawals tokens for a specific era.

Parameters

NameTypeDescription
useraddressThe address of the user who withdrew tokens.
erauint256The era number from which tokens were withdrawn.
amountuint256The amount of tokens withdrawn.