-
Notifications
You must be signed in to change notification settings - Fork 40
/
interest_rate.go
51 lines (43 loc) · 1.76 KB
/
interest_rate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/elys-network/elys/x/stablestake/types"
)
func (k Keeper) InterestRateComputation(ctx sdk.Context) sdk.Dec {
params := k.GetParams(ctx)
if params.TotalValue.IsZero() {
return params.InterestRate
}
interestRateMax := params.InterestRateMax
interestRateMin := params.InterestRateMin
interestRateIncrease := params.InterestRateIncrease
interestRateDecrease := params.InterestRateDecrease
healthGainFactor := params.HealthGainFactor
prevInterestRate := params.InterestRate
moduleAddr := authtypes.NewModuleAddress(types.ModuleName)
depositDenom := k.GetDepositDenom(ctx)
balance := k.bk.GetBalance(ctx, moduleAddr, depositDenom)
borrowed := params.TotalValue.Sub(balance.Amount)
targetInterestRate := healthGainFactor.
Mul(sdk.NewDecFromInt(borrowed)).
Quo(sdk.NewDecFromInt(params.TotalValue))
interestRateChange := targetInterestRate.Sub(prevInterestRate)
interestRate := prevInterestRate
if interestRateChange.GTE(interestRateDecrease.Mul(sdk.NewDec(-1))) && interestRateChange.LTE(interestRateIncrease) {
interestRate = targetInterestRate
} else if interestRateChange.GT(interestRateIncrease) {
interestRate = prevInterestRate.Add(interestRateIncrease)
} else if interestRateChange.LT(interestRateDecrease.Mul(sdk.NewDec(-1))) {
interestRate = prevInterestRate.Sub(interestRateDecrease)
}
newInterestRate := interestRate
if interestRate.GT(interestRateMin) && interestRate.LT(interestRateMax) {
newInterestRate = interestRate
} else if interestRate.LTE(interestRateMin) {
newInterestRate = interestRateMin
} else if interestRate.GTE(interestRateMax) {
newInterestRate = interestRateMax
}
return newInterestRate
}