Skip to content

Commit

Permalink
feat: mint calculator (#2159)
Browse files Browse the repository at this point in the history
* 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
robert-zaremba and gsk967 committed Jul 21, 2023
1 parent 1537007 commit d1b3c15
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
13 changes: 7 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Bug Fixes

- [2148](https://github.com/umee-network/umee/pull/2148) Fix MsgBeginUnbonding counting existing unbondings against max unbond twice.
- [2148](https://github.com/umee-network/umee/pull/2148) Fix MsgLeverageLiquidate CLI not actually allowing wildcard denoms.

### Features

- [2129](https://github.com/umee-network/umee/pull/2129) Emergency Group x/ugov proto.
- [2146](https://github.com/umee-network/umee/pull/2146) Add store GetTimeMs and SetTimeMs.
- [2146](https://github.com/umee-network/umee/pull/2146) Add store `GetTimeMs` and `SetTimeMs`.
- [2157](https://github.com/umee-network/umee/pull/2157) Add `x/metoken` module.
- [2145](https://github.com/umee-network/umee/pull/2145) Add hard market cap for token emission.

### Improvements

- [2134](https://github.com/umee-network/umee/pull/2134) Bump CometBFT to 34.29.

### Bug Fixes

- [2148](https://github.com/umee-network/umee/pull/2148) Fix MsgBeginUnbonding counting existing unbondings against max unbond twice.
- [2148](https://github.com/umee-network/umee/pull/2148) Fix MsgLeverageLiquidate CLI not actually allowing wildcard denoms.

### API Breaking

- [2140](https://github.com/umee-network/umee/pull/2140)
Expand Down
8 changes: 7 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

customante "github.com/umee-network/umee/v5/ante"
"github.com/umee-network/umee/v5/app/inflation"
appparams "github.com/umee-network/umee/v5/app/params"
"github.com/umee-network/umee/v5/swagger"
"github.com/umee-network/umee/v5/util/genmap"
Expand Down Expand Up @@ -672,6 +673,11 @@ func New(
// we prefer to be more strict in what arguments the modules expect.
skipGenesisInvariants := cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))

inflationClaculator := inflation.Calculator{
UgovKeeperB: app.UGovKeeperB,
MintKeeper: &app.MintKeeper,
}

// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
appModules := []module.AppModule{
Expand All @@ -694,7 +700,7 @@ func New(
app.interfaceRegistry,
),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, inflationClaculator.InflationRate),
// need to dereference StakingKeeper because x/distribution uses interface casting :(
// TODO: in the next SDK version we can remove the dereference
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, *app.StakingKeeper),
Expand Down
10 changes: 10 additions & 0 deletions app/inflation/expected_keepers.go
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
}
51 changes: 51 additions & 0 deletions app/inflation/inflatin.go
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
}

0 comments on commit d1b3c15

Please sign in to comment.