Rewards
Authors: Aloe Labs, Inc., Inspired by Yield Protocol
Implements logic for staking rewards
State Variables
_REWARDS_SLOT
bytes32 private constant _REWARDS_SLOT = keccak256("aloe.ii.rewards");
Functions
setRate
Sets the pool's rewards rate. May be 0.
function setRate(Storage storage store, uint160 accumulated, uint64 rate) internal;
Parameters
Name | Type | Description |
---|---|---|
store | Storage | The rewards storage pointer |
accumulated | uint160 | Up-to-date poolState.accumulated , i.e. the output of _accumulate |
rate | uint64 | The rewards rate, specified as [token units per second]. Keep between 10^17 and 10^28 token units per year for smooth operation -- between 0.1 and 10 billion tokens, assuming 18 decimals. |
claim
function claim(
Storage storage store,
uint160 accumulated,
address user,
uint256 balance
) internal returns (uint96 earned);
updatePoolState
Ensures that changes in the pool's totalSupply
don't mess up rewards accounting. Should
be called anytime totalSupply
changes.
Use Rewards.load()
to easily obtain the first two arguments
function updatePoolState(Storage storage store, uint160 accumulated) internal;
Parameters
Name | Type | Description |
---|---|---|
store | Storage | The rewards storage pointer |
accumulated | uint160 | Up-to-date poolState.accumulated , i.e. the output of _accumulate |
updateUserState
Tracks how much reward a user
earned while holding a particular balance
. Should be
called anytime their balance changes.
Use Rewards.load()
to easily obtain the first two arguments
function updateUserState(Storage storage store, uint160 accumulated, address user, uint256 balance) internal;
Parameters
Name | Type | Description |
---|---|---|
store | Storage | The rewards storage pointer |
accumulated | uint160 | Up-to-date poolState.accumulated , i.e. the output of _accumulate |
user | address | The user whose balance (# of shares) is about to change |
balance | uint256 | The user's balance (# of shares) -- before it changes |
previewUserState
function previewUserState(
Storage storage store,
uint160 accumulated,
address user,
uint256 balance
) internal view returns (UserState memory userState);
getRate
function getRate() internal view returns (uint64);
load
Returns arguments to be used in updatePoolState
and updateUserState
. No good semantic
meaning here, just a coincidence that both functions need this information.
function load(uint256 totalSupply) internal view returns (Storage storage store, uint160 accumulator);
_accumulate
Accumulates rewards based on the current rate
and time elapsed since last update
function _accumulate(PoolState memory poolState, uint256 totalSupply) private view returns (uint160);
_storage
Diamond-pattern-style storage getter
function _storage() private pure returns (Storage storage store);
Events
RewardsRateSet
event RewardsRateSet(uint64 rate);
RewardsClaimed
event RewardsClaimed(address indexed user, uint96 amount);
Structs
PoolState
struct PoolState {
uint160 accumulated;
uint32 lastUpdated;
uint64 rate;
}
UserState
struct UserState {
uint96 earned;
uint160 checkpoint;
}
Storage
struct Storage {
PoolState poolState;
mapping(address => UserState) userStates;
}