0.0.4 • Published 1 year ago

@lenddao/lend-stake v0.0.4

Weekly downloads
-
License
AGPL-3.0
Repository
-
Last release
1 year ago

License: AGPL v3 Build pass codecov

LendDAO Incentives contracts

Sets of smart contracts to enable stake of LendDAO-related assets and rewards distribution based on them.

Specification

Architecture

General architecture

A common parent contract LendDistributionManager is used to keep the "accounting" logic for a set of children front contracts taking care of each type of incentive; being these, initially, a StakedLend contract for stake of LendDAO tokens on the LendDAO SM (Security Module) which will which be used as security fund in the near future, and a LendIncentivesController contract through which the LendDAO protocol will interact in order to provider rewards as incentives to users. The rationale of this inheritance chain in 2 main layers is the clearly conceptual separation between a common part of configurations of the distributions and mathematical calculation, and one specific part for each type of incentive (locking funds in a stake, providing liquidity to a protocol and potentially more in the future). Additionally, there will be a Rewards vault where the LEND rewards will be keep, to distribute through the StakedLend and the LendIncentivesController. Both will be granted in advance with allowance to pull funds from. The following sections will go in detail on the specification of both the LendDistributionManager and the different front contracts.

DistributionManager

Core contract for calculation of multiple distributions with different configurations. All the front contracts which users/lend protocol will interact with inherit from the DistributionManager. It calculates how many rewards belong to a certain user depending on the user's situation defined by the front contract of the distribution. This calculation is done by using a distribution index representing the accumulation of rewards from an emission per second and snapshoting that index on each user to take into account how much of the total belongs to him.

Data

  • assets. Mapping of AssetData structs which, for each front contract connected to the LendDistributionManager stores 1 or more of:
    • emissionPerSecond: Amount of rewards per second distribution-wide. It's used to calculate the raw amount of rewards to distribute in a time delta since the last update of the following described index.
    • index: Variable representing the accumulated rewards distributed distribution-wide per unit of token used in the specific child contract of the distribution (per unit of staked LEND in the case of the StakedLend child contract). The next index is calculated by the formula on _getNormalizedDistribution() emissionPerSecond timeDelta 10^PRECISION / balanceOnFrontContract + previousIndex, scaling it up multiplying by 10^18 in order to not lose precision.
    • lastUpdateTimestamp: timestamp when the struct was updated.
    • userIndexes: mapping user address => index snapshotted on the user from the one of the distribution.

For the child StakedLend, they key of the mapping used is the address of the StakedLend itself. In the case of the LendIncentivesController, there is a different key and struct for each lToken and debtToken submitted to incentives.

Logic

This contract allows to do the following:

  • Configuration of multiple distributions: only allowed to a trusted EMISSION_MANAGER, allows to list an specific distribution, with some emission per second and front contract.
  • Update of user/distribution state on interaction: called by the child contract when something happened concerning the situation on the user, for example when he stakes on StakedLend, redeems, deposit on the LendDAO protocol, etc...
  • Get the unclaimed rewards of an user: self-explanatory, used by the children contracts to check how much rewards were accrued for an user and store the data if needed on their side, by interacting with the claimRewards() function.
  • Query information about distributions/users: by using the different view functions available.

StakedLend

Contract to stake LEND token, to be connected with a slashing mechanism in the near future in order to secure the LendDAO protocol, forming the so called LendDAO SM (Security Module). Holders of LendDAO tokens stae them in this contract, they receive equivalent amount in stkLEND tokens and start accruing rewards in LEND; rewards previously configured on the father contract LendDistributionManager by the a trusted EMISSION_MANAGER. Once they accrued LEND rewards, they can claim them at any moment but, to withdraw their staked LEND tokens, they need to activate and wait a cooldown period, and withdraw just after it, during a withdrawal time window.

Data

  • stakerRewardsToClaim: mapping storing the accrued rewards accrued and stored for an user, not taking into account those accrued but not stored yet.
  • stakersCooldowns: mapping the timestamp of activation of cooldown period for an user, if activated.

Logic

This contract allows to do the following:

  • Stake LEND tokens to start accruing rewards: through the stake() function. The LEND tokens will be locked in this same contract, and stkLEND tokens will be minted for the user in the same proportion as LEND staked, the state in the father LendDistributionManager will be updated and the timestamp of the cooldown will be updated too.
  • Withdraw staked LEND tokens: if an user has stkLEND, he can call the redeem() function, burning the stkLEND and receiving the same proportion of previously staked LEND. The withdrawal will only suceed if the user in on the withdrawal window after the cooldown period.
  • Activate the cooldown period: self-explanatory, calling the cooldown() function and needed to withdraw the staked LEND.
  • Claim the accrued rewards: by calling the claimRewards() function, used to update the state and transfer to the user the accrued rewards, consequence of the time he was/is staking.
  • Query information about users: about their rewards or cooldown period.

Cooldown period

The main objective of the cooldown period is to avoid situations on the future Security Module when, if an slashing event happens, people starts withdrawing in mass their staked funds, leaving the protocol uncover and removing the utility on the stake itself. To achieve this, the most important condition to be fullfilled on any state update/operation involving the StakedLend contract is that, if a user staking withdraws, he already respected a cooldown period, which leads that movement of funds should only affect "negatively" the cooldown period. Depending on the type of operation, the cooldown period is affected in the following way:

  • If an user stakes LEND with/without having any fund staked before, if he didn't have the cooldown activated, it remains the same way.
  • If an user stakes LEND holding already stkLEND and with cooldown period activated:
    • If the cooldown is expired, remains expired.
    • If the cooldown is still valid, using the amount staked and the current timestamp, it does the weighted average with the current cooldown timestamp of the user.
  • If the user redeems LEND, the cooldown timestamp is set to 0.
  • If the user claims rewards, the cooldown timestamp is not affected.
  • On transfer of stkLEND:
  • The cooldown timestamp of the sender remains as it is.
  • On the recipient:
    • If the recipient is on a valid cooldown period finishing before that the one of the sender, we do the same weighted average as in stake().
    • If the recipient has an expired cooldown timestamp, his cooldown timetamp is set to 0.
    • If both sender and recipient have valid cooldown period activated and the one of the sender ends before than the recipient, the recipient keeps his own.

LendIncentivesController

Contract in charge of the incentives for activity on the LendDAO protocol, inheriting from the LendDistributionManager. Each time an action involving any incentive for an user happens on the LendDAO protocol, this contract is called to manage the update of the incentives state.

Data

  • _usersUnclaimedRewards: mapping storing the accrued rewards accrued and stored for an user, not taking into account those accrued but not stored yet.

Logic

This contract allows to do the following:

  • Communication LendDAO protocol -> incentives: through the whitelisted function handleAction(), only callable by the LendDAO lending pool. For every asset and user, one call to this function needs to be done, which will trigger a state update in both the rewards of the user and the distribution data.
  • Claim of user rewards: by claimRewards() function, transferring to the user the LEND rewards. If the user tries to claim his rewards with the StakedLend as target, a bonus will applied on the rewards accumulated from his activity on the protocol, and the stake() function on the StakedLend will be called.
  • Query information about users: mainly about the state of their rewards.

Audits

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago