StakedToken
Inherits: IStakedToken, ERC20Permit, IncreAccessControl, Pausable, ReentrancyGuard
Author: webthethird
Based on Aave's StakedToken, but with reward management outsourced to the SafetyModule
State Variables
UNDERLYING_TOKEN
Address of the underlying token to stake
COOLDOWN_SECONDS
Seconds that user must wait between calling cooldown and redeem
UNSTAKE_WINDOW
Seconds available to redeem once the cooldown period is fullfilled
safetyModule
Address of the SafetyModule contract
isInPostSlashingState
Whether the StakedToken is in a post-slashing state
Post-slashing state disables staking and further slashing, and allows users to redeem their staked tokens without waiting for the cooldown period
maxStakeAmount
Max amount of staked tokens allowed per user
exchangeRate
Exchange rate between the underlying token and the staked token, normalized to 1e18
Rate is the amount of underlying tokens held in this contract per staked token issued, so it should be 1e18 in normal conditions, when all staked tokens are backed 1:1 by underlying tokens, but it can be lower if users' stakes have been slashed for an auction by the SafetyModule
_stakersCooldowns
Timestamp of the start of the current cooldown period for each user
Functions
onlySafetyModule
Modifier for functions that can only be called by the SafetyModule contract
constructor
StakedToken constructor
Parameters
Name | Type | Description |
---|---|---|
|
| The underlying token to stake |
|
| The SafetyModule contract to use for reward management |
|
| The number of seconds that users must wait between calling |
|
| The number of seconds available to redeem once the cooldown period is fullfilled |
|
| The maximum amount of staked tokens allowed per user |
|
| The name of the token |
|
| The symbol of the token |
getUnderlyingToken
Returns the underlying ERC20 token
Returns
Name | Type | Description |
---|---|---|
|
| Underlying ERC20 token |
getCooldownSeconds
Returns the length of the cooldown period
Returns
Name | Type | Description |
---|---|---|
|
| Number of seconds in the cooldown period |
getUnstakeWindowSeconds
Returns the length of the unstake window
Returns
Name | Type | Description |
---|---|---|
|
| Number of seconds in the unstake window |
getCooldownStartTime
Returns the start time of the latest cooldown period for a given user
Parameters
Name | Type | Description |
---|---|---|
|
| Address of the user |
Returns
Name | Type | Description |
---|---|---|
|
| Timestamp when the user's latest cooldown period started |
previewStake
Returns the amount of staked tokens one would receive for staking an amount of underlying tokens
Parameters
Name | Type | Description |
---|---|---|
|
| Amount of underlying tokens to stake |
Returns
Name | Type | Description |
---|---|---|
|
| Amount of staked tokens that would be received at the current exchange rate |
previewRedeem
Returns the amount of underlying tokens one would receive for redeeming an amount of staked tokens
Parameters
Name | Type | Description |
---|---|---|
|
| Amount of staked tokens to redeem |
Returns
Name | Type | Description |
---|---|---|
|
| Amount of underlying tokens that would be received at the current exchange rate |
stake
Stakes tokens from the sender and starts earning rewards
Parameters
Name | Type | Description |
---|---|---|
|
| Amount of underlying tokens to stake |
stakeOnBehalfOf
Stakes tokens on behalf of the given address, and starts earning rewards
Tokens are transferred from the transaction sender, not from the onBehalfOf
address
Parameters
Name | Type | Description |
---|---|---|
|
| Address to stake on behalf of |
|
| Amount of underlying tokens to stake |
redeem
Redeems staked tokens, and stop earning rewards
Parameters
Name | Type | Description |
---|---|---|
|
| Amount of staked tokens to redeem for underlying tokens |
redeemTo
Redeems staked tokens, and stop earning rewards
Staked tokens are redeemed from the sender, and underlying tokens are sent to the to
address
Parameters
Name | Type | Description |
---|---|---|
|
| Address to redeem to |
|
| Amount of staked tokens to redeem for underlying tokens |
cooldown
Activates the cooldown period to unstake
Can't be called if the user is not staking
slash
Sends underlying tokens to the given address, lowers the exchange rate accordingly, and changes the contract's state to POST_SLASHING
, which disables staking, cooldown period and further slashing until the state is returned to RUNNING
Only callable by the SafetyModule contract
Parameters
Name | Type | Description |
---|---|---|
|
| Address to send the slashed underlying tokens to |
|
| Amount of staked tokens to slash |
Returns
Name | Type | Description |
---|---|---|
|
| Amount of underlying tokens slashed |
returnFunds
Transfers underlying tokens from the given address to this contract and increases the exchange rate accordingly
Only callable by the SafetyModule contract
Parameters
Name | Type | Description |
---|---|---|
|
| Address to transfer tokens from |
|
| Amount of underlying tokens to transfer |
settleSlashing
Sets isInPostSlashingState
to false, which re-enables staking, slashing and cooldown period
Only callable by the SafetyModule contract
getNextCooldownTimestamp
Calculates a new cooldown timestamp
*Calculation depends on the sender/receiver situation, as follows:
If the timestamp of the sender is "better" or the timestamp of the recipient is 0, we take the one of the recipient
Weighted average of from/to cooldown timestamps if:
The sender doesn't have the cooldown activated (timestamp 0).
The sender timestamp is expired
The sender has a "worse" timestamp
If the receiver's cooldown timestamp expired (too old), the next is 0*
Parameters
Name | Type | Description |
---|---|---|
|
| Cooldown timestamp of the sender |
|
| Amount of staked tokens to receive |
|
| Address of the recipient |
|
| Current balance of the receiver |
Returns
Name | Type | Description |
---|---|---|
|
| The new cooldown timestamp |
paused
Indicates whether staking and transferring are currently paused
Contract is paused if either this contract or the SafetyModule has been paused
Returns
Name | Type | Description |
---|---|---|
|
| True if paused, false otherwise |
setSafetyModule
Changes the SafetyModule contract used for reward management
Only callable by governance
Parameters
Name | Type | Description |
---|---|---|
|
| Address of the new SafetyModule contract |
setMaxStakeAmount
Sets the max amount of staked tokens allowed per user
Only callable by governance
Parameters
Name | Type | Description |
---|---|---|
|
| New max amount of staked tokens allowed per user |
pause
Pauses staking and transferring of staked tokens
Only callable by governance
unpause
Unpauses staking and transferring of staked tokens
Only callable by governance
_updateExchangeRate
Updates the exchange rate of the staked token, based on the current underlying token balance held by this contract and the total supply of the staked token
_transfer
Internal ERC20 _transfer
of the tokenized staked tokens
Updates the cooldown timestamps if necessary, and updates the staking positions of both users in the SafetyModule, accruing rewards in the process
Parameters
Name | Type | Description |
---|---|---|
|
| Address to transfer from |
|
| Address to transfer to |
|
| Amount to transfer |
_stake
_redeem
Last updated