Staking ProtocolrewardsDistributionrateBasedRewardDistributionrateBasedOpenRewardDistribution

RateBasedOpenRewardDistributionFacetStorage

This library manages the storage and logic for the Rate-Based Open Reward Distribution algorithm.

_This implementation is designed exclusively for no-lock campaigns and does not support instant stake rewards. Lock multipliers are also not applicable.

Rewards are calculated as the product of the respective reward rate(s) (i.e., campaignRate), and active period(s), and the virtual balance of positions.

IMPORTANT:

  • Rate-Based Reward Distribution Facets should only be used in conjuction with RewardMinter Facets for configuring reward assets in staking campaigns.

  • This implementation is designed for campaigns where positions are always unlockable and instant staking rewards are not featured. It does not support scenarios where stakeholders can lock their positions._

CampaignNotOnCreationStateForSetting

error CampaignNotOnCreationStateForSetting(uint256 campaignId)

Thrown when a targeted campaign is not "On Creation" state, which is required at configurations.

InvalidZeroCampaignRate

error InvalidZeroCampaignRate(uint256 campaignId)

Thrown when the campaign rate is set to 0, which is not allowed.

NotCampaignCreator

error NotCampaignCreator(uint256 campaignId, address unauthorizedAddress)

Thrown when an account not being the creator of a campaign attempts to change its reward rate.

STORAGE_SLOT

bytes32 STORAGE_SLOT

Unique identifier for the storage slot where the Layout struct is stored.

DIVIDER

uint256 DIVIDER

Used to normalize values (e.g., rates) for calculations with up to 18 decimal precision.

Layout

Struct for managing campaign information.

struct Layout {
  mapping(uint256 => struct RateBasedOpenRewardDistributionFacetStorage.CampaignInfo) campaignInfoLocal;
}

CampaignInfo

Struct containing local information for a staking campaign.

struct CampaignInfo {
  uint256 currentIndex;
  uint256[] campaignRates;
  uint256[] rateChangeSnapshots;
  mapping(uint256 => struct RateBasedOpenRewardDistributionFacetStorage.NftInfo) nftInfoLocal;
}

NftInfo

struct NftInfo {
  uint256 lastRewardTimestamp;
  uint256 snapshotStartingIndex;
}

layout

function layout() internal pure returns (struct RateBasedOpenRewardDistributionFacetStorage.Layout l)

Retrieves a reference to the Layout struct stored at the slot specified by STORAGE_SLOT unique identifier.

setCampaignRewardsDistribution

function setCampaignRewardsDistribution(struct RateBasedOpenRewardDistributionFacetStorage.Layout l, uint256 campaignId, bytes campaignRewardsDistributionData) internal returns (uint256)

Sets the rewards distribution schedule (rate of rewards) for a specified campaign.

This function can only be called during the campaign's creation (see CampaignCreationSkeleton.createCampaign()).

Parameters

NameTypeDescription
lstruct RateBasedOpenRewardDistributionFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
campaignRewardsDistributionDatabytesThe ABI encoded data containing the reward rate to set.

changeCampaignRate

function changeCampaignRate(struct RateBasedOpenRewardDistributionFacetStorage.Layout l, uint256 campaignId, uint256 newRate) internal

Changes the rate of rewards for a specified campaign.

Records the current block.timestamp when the change is applied. This function can only be called by the creator of the targeted campaign.

Parameters

NameTypeDescription
lstruct RateBasedOpenRewardDistributionFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
newRateuint256The new reward rate to be applied for the campaign (must be scaled with 1e18 at API level).

applyStake

function applyStake(struct RateBasedOpenRewardDistributionFacetStorage.Layout l, uint256 campaignId, uint256 nftId, uint256 virtualPacketsStaked, uint256 packetsStaked) internal

Creates a new staking position in the specified campaign.

If no amount multiplier is applied, virtualPacketsStaked will be equal to packetsStaked.

Parameters

NameTypeDescription
lstruct RateBasedOpenRewardDistributionFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
nftIduint256The unique identifier of the NFT associated with the position.
virtualPacketsStakeduint256The number of staked (input) packets adjusted by the applicable amount multiplier.
packetsStakeduint256The raw number of staked packets, without accounting for amount multipliers.

applyRestake

function applyRestake(struct RateBasedOpenRewardDistributionFacetStorage.Layout l, uint256 campaignId, uint256 nftId) internal returns (uint256)

Called when a specified position in the given campaign is increased.

Records the index of the last snapshot for the given position.

Parameters

NameTypeDescription
lstruct RateBasedOpenRewardDistributionFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
nftIduint256The unique identifier of the NFT associated with the position.

Return Values

NameTypeDescription
[0]uint2560 because unclaimed rewards (till restake) should be provided by getRestakeReward() (see facet).

applyUnstake

function applyUnstake(struct RateBasedOpenRewardDistributionFacetStorage.Layout l, uint256 campaignId, uint256 nftId) internal returns (uint256)

Returns the claimable rewards for a position being unstaked (either partially or fully).

See getReward() for details on reward calculation.

Parameters

NameTypeDescription
lstruct RateBasedOpenRewardDistributionFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
nftIduint256The unique identifier of the NFT associated with the position.

Return Values

NameTypeDescription
[0]uint256The calculated claimable rewards.

getReward

function getReward(struct RateBasedOpenRewardDistributionFacetStorage.Layout l, uint256 campaignId, uint256 nftId) internal returns (uint256)

Returns the claimable rewards for a specific position.

See _calculateReward() for details on reward calculation.

Parameters

NameTypeDescription
lstruct RateBasedOpenRewardDistributionFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
nftIduint256The unique identifier of the NFT associated with the position.

Return Values

NameTypeDescription
[0]uint256The rewards that can be claimed by the specified position.

_calculateReward

function _calculateReward(uint256 campaignRate, uint256 nftId, uint256 activePeriod) internal view returns (uint256)

Calculates a given position's rewards based on the specified reward rate, and active duration.

This function assumes that the campaignRate input value is scaled by 1e18.

Parameters

NameTypeDescription
campaignRateuint256The reward rate to use (should be scaled by 1e18).
nftIduint256The unique identifier of the NFT associated with the position.
activePerioduint256The duration for which the reward is calculated, expressed in seconds.

Return Values

NameTypeDescription
[0]uint256The calculated reward for the specified parameters, scaled down by DIVIDER.