Staking ProtocolrewardsDistributionpreStakeRewardDistribution

PreStakeRewardDistributionFacetStorage

This library manages the storage and logic for the PreStake Reward Distribution algorithm.

_This implementation is designed exclusively for campaigns that expect users to stake (or restake) within a predefined time window, specifically between the campaign's starting and ending timestamps. Rewards are distributed only after the campaign's ending timestamp and up until a predefined rewards ending timestamp.

Key Design Features:

  • Fixed Lock Period: All users should be locked for the same duration, eliminating the need for lock multipliers.
  • Restaking is permitted only within the campaign's staking window (startingTimestamp <= t_stake <= endingTimestamp).
  • Delayed Rewards: Rewards are not distributed instantly upon staking but are available after the campaign's ending timestamp.
  • InstaRewards Option:
    • If enabled, users can claim all their designated rewards immediately after the campaign's ending timestamp, though they remain locked until the rewards ending timestamp.
    • If disabled, users receive rewards proportionally based on the time elapsed since the campaign's ending timestamp. Full rewards are only claimable after the rewards ending timestamp.

Integration Considerations:

  1. Using ERC__RewardTransferFacet:

    • Rewards are transferred from the distributor to the RewardAssetHandler.
    • The reward distributor must approve the diamond for reward transfers before the PreStake configuration is executed.
    • The diamond contract will initiate transferFrom to retrieve the rewards.
  2. Using ERC__RewardMinterFacet:

    • Rewards are minted by the campaign's RewardAssetHandler, not the diamond itself.
    • The minter role must be assigned to the campaign's RewardAssetHandler._

CampaignNotOnCreationStateForSetting

error CampaignNotOnCreationStateForSetting(uint256 campaignId)

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

InvalidZeroTotalReward

error InvalidZeroTotalReward(uint256 campaignId)

Thrown at setter when attempting to set 0 total rewards.

InvalidZeroRewardProviderAddress

error InvalidZeroRewardProviderAddress(uint256 campaignId)

Thrown at setter when the zero address is specified as the reward provider.

NoRewardsYet

error NoRewardsYet(uint256 campaignId, uint256 rewardStartingTimestamp)

Thrown at calculateReward() when the campaign's reward period has not yet started.

STORAGE_SLOT

bytes32 STORAGE_SLOT

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

DIVIDER

uint256 DIVIDER

Used to normalize values for calculations with up to 18 decimal precision.

Layout

Struct for managing campaign information.

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

CampaignInfo

Struct containing local information for a staking campaign.

struct CampaignInfo {
  bool isInstaRewards;
  uint256 totalReward;
  uint256 rewardDuration;
  uint256 virtualTotalPacketsLocked;
}

layout

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

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

setCampaignRewardsDistribution

function setCampaignRewardsDistribution(struct PreStakeRewardDistributionFacetStorage.Layout l, uint256 campaignId, bytes campaignRewardsDistributionData) internal returns (uint256, bool, address)

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

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

Parameters

NameTypeDescription
lstruct PreStakeRewardDistributionFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
campaignRewardsDistributionDatabytesThe ABI encoded data.

applyStake

function applyStake(struct PreStakeRewardDistributionFacetStorage.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 PreStakeRewardDistributionFacetStorage.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 PreStakeRewardDistributionFacetStorage.Layout l, uint256 campaignId, uint256) internal returns (uint256)

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

Parameters

NameTypeDescription
lstruct PreStakeRewardDistributionFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
uint256

Return Values

NameTypeDescription
[0]uint2560 because rewards are provided after ending timestamp of campaign.

applyUnstake

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

Returns the claimable rewards for a position being unstaked.

Parameters

NameTypeDescription
lstruct PreStakeRewardDistributionFacetStorage.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 PreStakeRewardDistributionFacetStorage.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 PreStakeRewardDistributionFacetStorage.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(struct PreStakeRewardDistributionFacetStorage.Layout l, uint256 campaignId, uint256 nftId, uint256 rewardTimestamp) internal returns (uint256)

Calculates a given position's rewards based on the active period, total stakes and stake of position.

Parameters

NameTypeDescription
lstruct PreStakeRewardDistributionFacetStorage.Layout
campaignIduint256The unique identifier of the targeted staking campaign.
nftIduint256The unique identifier of the NFT associated with the position.
rewardTimestampuint256The duration for which the reward is calculated, expressed in seconds.

Return Values

NameTypeDescription
[0]uint256The calculated reward