mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-14 03:55:19 +00:00
0efe7f2281
* add annualized_reward query proto * use sdkmath.LegacyDec to match RPS param... * add AnnualizedRewards grpc query * add changelog entry * simplify calculation & expand test cases
28 lines
1.2 KiB
Go
28 lines
1.2 KiB
Go
package keeper
|
|
|
|
import (
|
|
sdkmath "cosmossdk.io/math"
|
|
)
|
|
|
|
const SecondsPerYear = 365 * 24 * 3600
|
|
|
|
// CalculateStakingAnnualPercentage returns the annualized staking reward rate.
|
|
// It assumes that staking comes from one of two sources depending on if inflation is enabled or not.
|
|
func CalculateStakingAnnualPercentage(totalSupply, totalBonded sdkmath.Int, inflationRate, communityTax, rewardsPerSecond sdkmath.LegacyDec) sdkmath.LegacyDec {
|
|
// no rewards are given if no tokens are bonded, in addition avoid division by zero
|
|
if totalBonded.IsZero() {
|
|
return sdkmath.LegacyZeroDec()
|
|
}
|
|
|
|
// the percent of inflationRate * totalSupply tokens that are distributed to stakers
|
|
percentInflationDistributedToStakers := sdkmath.LegacyOneDec().Sub(communityTax)
|
|
|
|
// the total amount of tokens distributed to stakers in a year
|
|
amountGivenPerYear := inflationRate.
|
|
MulInt(totalSupply).Mul(percentInflationDistributedToStakers). // portion provided by inflation via mint & distribution modules
|
|
Add(rewardsPerSecond.Mul(sdkmath.LegacyNewDec(SecondsPerYear))) // portion provided by community module
|
|
|
|
// divide by total bonded tokens to get the percent return
|
|
return amountGivenPerYear.QuoInt(totalBonded)
|
|
}
|