-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: mint calculator * add inflation package * remove not needed interface * simplify mint keeper interface * add cf function * changelog * fix * lint & fix * Apply suggestions from code review --------- Co-authored-by: Sai Kumar <17549398+gsk967@users.noreply.github.com>
- Loading branch information
1 parent
1537007
commit d1b3c15
Showing
4 changed files
with
75 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package inflation | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
type MintKeeper interface { | ||
StakingTokenSupply(ctx sdk.Context) math.Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package inflation | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" | ||
|
||
ugovkeeper "github.com/umee-network/umee/v5/x/ugov/keeper" | ||
) | ||
|
||
type Calculator struct { | ||
UgovKeeperB ugovkeeper.Builder | ||
MintKeeper MintKeeper | ||
} | ||
|
||
func (c Calculator) InflationRate( | ||
ctx sdk.Context, minter minttypes.Minter, p minttypes.Params, bondedRatio sdk.Dec) sdk.Dec { | ||
|
||
maxSupply, _ := sdk.NewIntFromString("21_000_000_000_000_000000") | ||
|
||
totalSupply := c.MintKeeper.StakingTokenSupply(ctx) | ||
if totalSupply.GTE(maxSupply) { | ||
// supply is already reached the maximum amount, so inflation should be zero | ||
return sdk.ZeroDec() | ||
} | ||
|
||
// TODO: here we need to use a new inflation function and check if we need to go to the | ||
// next inflation cycle | ||
minter.Inflation = c.calculateInflation(ctx, minter, p, bondedRatio) | ||
return readjustSupply(totalSupply, maxSupply, minter, p) | ||
} | ||
|
||
func (c Calculator) calculateInflation( | ||
ctx sdk.Context, minter minttypes.Minter, p minttypes.Params, bondedRatio sdk.Dec) sdk.Dec { | ||
return minttypes.DefaultInflationCalculationFn(ctx, minter, p, bondedRatio) | ||
} | ||
|
||
// TODO: add unit tests to this function | ||
func readjustSupply(totalSupply, maxSupply math.Int, minter minttypes.Minter, params minttypes.Params) sdk.Dec { | ||
minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply) | ||
newSupply := minter.BlockProvision(params).Amount | ||
newTotalSupply := totalSupply.Add(newSupply) | ||
if newTotalSupply.GT(maxSupply) { | ||
overdraft := newTotalSupply.Sub(maxSupply) | ||
maxNewSupply := newSupply.Sub(overdraft) | ||
factor := sdk.NewDecFromInt(maxNewSupply).QuoInt(newSupply) | ||
minter.Inflation = minter.Inflation.Mul(factor) | ||
} | ||
|
||
return minter.Inflation | ||
} |