2 unstable releases
new 0.2.0 | Apr 3, 2025 |
---|---|
0.0.0 | Jan 16, 2025 |
#33 in #reward
141 downloads per month
Used in 3 crates
3.5MB
56K
SLoC
FRAME Staking Rewards Pallet
Allows accounts to be rewarded for holding fungible
asset/s, for example LP tokens.
Overview
Initiate an incentive program for a fungible asset by creating a new pool.
During pool creation, a 'staking asset', 'reward asset', 'reward rate per block', 'expiry block', and 'admin' are specified.
Once created, holders of the 'staking asset' can 'stake' them in a corresponding pool, which creates a Freeze on the asset.
Once staked, rewards denominated in 'reward asset' begin accumulating to the staker, proportional to their share of the total staked tokens in the pool.
Reward assets pending distribution are held in an account unique to each pool.
Care should be taken by the pool operator to keep pool accounts adequately funded with the reward asset.
The pool admin may increase reward rate per block, increase expiry block, and change admin.
Disambiguation
While this pallet shares some terminology with the staking-pool
and similar native staking
related pallets, it is distinct and is entirely unrelated to native staking.
Permissioning
Currently, pool creation and management restricted to a configured Origin.
Future iterations of this pallet may allow permissionless creation and management of pools.
Note: The permissioned origin must return an AccountId. This can be achieved for any Origin by
wrapping it with EnsureSuccess
.
Implementation Notes
Internal logic functions such as update_pool_and_staker_rewards
were deliberately written
without side-effects.
Storage interaction such as reads and writes are instead all performed in the top level pallet Call method, which while slightly more verbose, makes it easier to understand the code and reason about how storage reads and writes occur in the pallet.
Rewards Algorithm
The rewards algorithm is based on the Synthetix StakingRewards.sol smart contract.
Rewards are calculated JIT (just-in-time), and all operations are O(1) making the approach scalable to many pools and stakers.
Resources
- This video series, which walks through the math of the algorithm.
- This dev.to article, which explains the algorithm of the SushiSwap MasterChef staking. While not identical to the Synthetix approach, they are quite similar.
Dependencies
~19–35MB
~576K SLoC