Gauge
Gauge is a voting contract for liquidity pools, with each liquidity pool having its own Gauge contract.
rescueERC20
Rescues tokens stuck in the contract.
Parameters:
tokenAddress
address
Address of the token to be rescued.
to
address
Address that will receive the tokens.
amount
uint
Amount of tokens to be rescued.
setParams
Sets parameters (weight
, base
, and slope
) of the Gauge contract.
weight
: Weight determines the how much reward user can earn in Farm contract. The higher the weight, the lower the reward.base
: Base reward rate.slope
: Slope of reward rate.
Please refer to Point voting to learn more about ratePoint
calculation.
Parameters:
_weight
uint
New weight.
_base
uint
New base reward rate.
_slope
uint
New slope of the reward rate.
balanceOf
Retrieves the user's latest balance, i.e., balance recorded in the user's latest checkpoint.
Parameters:
account
address
User's address.
Return Values:
balance
uint
User's latest balance.
balanceOfAt
Retrieves the user's balance at a given week. If no checkpoint is recorded at the given week, it searches for the latest checkpoint among previous ones.
Parameters:
account
address
User's address.
week
uint
The week to find out the user's balance.
Return Values:
balance
uint
User's balance at the given week.
totalSupplyAt
Total supply at a given week.
Parameters:
week
uint
The week to find out the total supply.
Return Values:
totalSupply
uint
Total supply.
tick
This function serves the purpose of updating the contract's state, specifically handling the transition to a new week. Below is an introduction to what the tick
function does:
Week Update:
The function first calculates the current week based on the current timestamp divided by the duration of a week (1 week). The result is stored in the
_week
variable.
Checkpoint and Total Supply Update:
If the calculated
_week
is greater than the currentthisWeek
stored in the contract, it means a new week has begun.For each week between the current
thisWeek
and the new_week
(exclusive), the function updates the total supply checkpoint (_totalSupplyAt
) with the currenttotalSupply
.The
thisWeek
is then updated to the new_week
.
Reward Rate Update:
The function calls the internal
_updateRewardRate
function to update the reward rate recorded in the associated Farm contract based on the latest total supply and the configured slope and base values.
updateTotalSupply
Update latest total supply and trigger tick
.
Parameters:
_totalSupply
uint
New total supply.
nextRewardRate
Compute new reward rate base on latest totalSupply
, slope
and base.
The formula is as follows:
totalSupply
: This represents the total supply of the sGov token in the Gauge contract. It's the sum of all the sGov tokens deposited by users.slope
: This is a configurable parameter that determines the rate at which the reward rate increases based on the total supply. A higher slope means a faster increase in the reward rate.REWARD_RATE_BASE_UNIT
: This is a constant value set as1e18
representing the base unit for the reward rate. It's used to ensure that the result is in the correct units.base
: This is another configurable parameter that represents the base reward rate. It's the minimum reward rate that the contract will provide, irrespective of the total supply.
Please refer to Point voting to learn more about reward rate calculation.
Return Values:
newRewardRate
uint
New reward rate.
deposit
Deposits sGOV tokens on behalf of a user. This function plays a crucial role in the liquidity mining mechanism, where users deposit sGov tokens to earn additional rewards. Below is an introduction to what the deposit
function does:
Token Transfer:
Transfers the specified amount of sGov tokens from the sender's address to the Gauge contract. This requires prior approval of the sGov token by the user.
Checkpoint Update:
Records a new checkpoint for the user, updating their balance with the deposited amount. This allows the contract to keep track of the user's historical balances.
Total Supply Update:
Updates the total supply of sGov tokens in the Gauge by adding the deposited amount. This is essential for calculating the reward rates accurately.
Please refer to Boost section to learn more about performing a deposit to Gauge.
Parameters:
amount
uint
Amount of sGOV tokens to deposit.
to
address
Address that owns the amount of sGOV tokens.
applyWithdrawal
This function allows users to initiate the process of withdrawing their sGov tokens from the Gauge. This function introduces a delay of one week before the actual withdrawal occurs. Users must call this function to express their intent to withdraw, and the withdrawal will be processed after the specified delay. Below is an introduction to what the applyWithdrawal
function does:
Checkpoint Update:
Updates the user's checkpoint by reducing their recorded balance by the withdrawal amount. This reflects the intention to withdraw in the historical balance records.
Total Supply Update:
Decreases the total supply of sGov tokens in the Gauge, accounting for the pending withdrawal. This ensures accurate reward rate calculations.
Pending Withdrawal Record:
Records the withdrawal amount as pending for the user. This amount will be available for withdrawal after the specified delay.
Week to Withdraw Update:
Sets the week when the user can complete the withdrawal. The withdrawal delay is one week, so it will be processed in the subsequent week.
Parameters:
amount
uint
Amount of sGOV tokens to withdraw.
withdraw
The withdraw
function in the Gauge contract allows users to complete their withdrawal of sGov tokens. This function is typically called after a one-week delay from the time the user initiated the withdrawal by calling the applyWithdrawal
function. The purpose of the delay is to allow for a period during which the user's withdrawal request can be processed and confirmed. Below is an introduction to what the withdraw
function does:
Requirements:
Ensures that the withdrawal can only be completed after the specified one-week delay has passed. Also Ensures that the withdrawal amount is non-zero.
Amount Calculation:
Retrieves the amount of sGov tokens that was previously marked as pending for withdrawal.
Pending Withdrawal Clearing:
Clears the pending withdrawal amount for the user after successfully completing the withdrawal.
Token Transfer:
Transfers the withdrawn sGov tokens from the Gauge contract to the user's address.
Return Values:
amount
uint
Amount of sGOV tokens withdrawn.
bonus
This function calculates the bonus ratio for a user based on their latest balance of sGov tokens. The bonus ratio is designed to provide additional rewards to users with smaller balances, encouraging broader participation. The formula for calculating the bonus is as follows:
balance
: Represents the user's latest balance of sGov tokens.BONUS_MULTIPLIER
: A constant factor used to scale the bonus calculation which is set as22.5e36
.totalSupply
: Refers to the total supply of sGov tokens in the Gauge contract._bonus
: The calculated bonus ratio for the user.MAX_BONUS:
The max bonus ratio is set as1.5e18
. Bonus ratio approaches max when the user's balance gets closer to 1/10 of total supply.
The formula computes a preliminary bonus value, adjusts it using the square root operation, and then caps the final bonus ratio at a specified maximum value (MAX_BONUS
). The intention is to incentivize users with smaller balances by providing a non-linear bonus that diminishes as the user's balance increases.
Parameters:
user
address
User's address.
Return Values:
_bonus
uint
User's bonus ratio.
Last updated