Skip to content

Commit

Permalink
feat(mint): implement token model v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Dec 4, 2023
1 parent 36a965f commit 473221d
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 322 deletions.
2 changes: 1 addition & 1 deletion app/upgrades/v6/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v5
package v6

import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/cosmos/cosmos-sdk v0.47.3
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.1.0
github.com/dustinxie/ecc v0.0.0-20210511000915-959544187564
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/grpc-ecosystem/grpc-gateway v1.16.0
Expand Down Expand Up @@ -101,7 +102,6 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dustinxie/ecc v0.0.0-20210511000915-959544187564 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.15.0 // indirect
Expand Down
13 changes: 8 additions & 5 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ import (
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

// fetch stored minter & params
minter := k.GetMinter(ctx)
// fetch stored params
params := k.GetParams(ctx)

// recalculate inflation rate
totalSupply := k.TokenSupply(ctx, params.MintDenom)
bondedRatio := k.BondedRatio(ctx)
minter.Inflation = minter.NextInflation(params, bondedRatio)
minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply)

minter, err := types.NewMinterWithInflationCoef(params.InflationCoef, bondedRatio, totalSupply)
if err != nil {
panic(err)
}

k.SetMinter(ctx, minter)

// mint coins, update supply
mintedCoin := minter.BlockProvision(params)
mintedCoins := sdk.NewCoins(mintedCoin)

err := k.MintCoins(ctx, mintedCoins)
err = k.MintCoins(ctx, mintedCoins)
if err != nil {
panic(err)
}
Expand Down
31 changes: 4 additions & 27 deletions x/mint/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import (

// Simulation parameter constants.
const (
Inflation = "inflation"
InflationCoef = "inflation_coef"
BondingAdjustment = "bonding_adjustment"
TargetBondingRatio = "target_bonding_ratio"
Inflation = "inflation"
InflationCoef = "inflation_coef"
)

// GenInflation randomized Inflation.
Expand All @@ -31,16 +29,6 @@ func GenInflationCoefMax(_ *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(73, 3)
}

// GenBondingAdjustmentMax randomized AnnualReductionFactor.
func GenBondingAdjustmentMax(_ *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(25, 1)
}

// GenTargetBondingRatioMax randomized AnnualReductionFactor.
func GenTargetBondingRatioMax(_ *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(66, 2)
}

// RandomizedGenState generates a random GenesisState for mint.
//
//nolint:forbidigo
Expand All @@ -59,24 +47,13 @@ func RandomizedGenState(simState *module.SimulationState) {
simState.Cdc, InflationCoef, &inflationCoef, simState.Rand,
func(r *rand.Rand) { inflationCoef = GenInflationCoefMax(r) },
)
var targetBondingRatio sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, TargetBondingRatio, &targetBondingRatio, simState.Rand,
func(r *rand.Rand) { targetBondingRatio = GenTargetBondingRatioMax(r) },
)

var bondingAdjustment sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, BondingAdjustment, &bondingAdjustment, simState.Rand,
func(r *rand.Rand) { bondingAdjustment = GenBondingAdjustmentMax(r) },
)

mintDenom := sdk.DefaultBondDenom
blocksPerYear := uint64(60 * 60 * 8766 / 5)
params := types.NewParams(mintDenom, inflationCoef, bondingAdjustment, targetBondingRatio, blocksPerYear)
params := types.NewParams(mintDenom, inflationCoef, blocksPerYear)
annualProvision := inflation.MulInt(simState.InitialStake)

minter := types.InitialMinter(inflation)
minter := types.NewMinterWithInitialInflation(inflation)
minter.AnnualProvisions = annualProvision

mintGenesis := types.NewGenesisState(minter, params)
Expand Down
2 changes: 0 additions & 2 deletions x/mint/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account)
params := types.DefaultParams()
params.BlocksPerYear = uint64(simtypes.RandIntBetween(r, 1, 1000000))
params.InflationCoef = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2)
params.TargetBondingRatio = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2)
params.BondingAdjustment = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2)
params.MintDenom = simtypes.RandStringOfLength(r, 10)

return &types.MsgUpdateParams{
Expand Down
5 changes: 5 additions & 0 deletions x/mint/types/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

import sdkerrors "cosmossdk.io/errors"

var ErrBondedRatioIsZero = sdkerrors.Register(ModuleName, 1, "bonded ratio is zero")
2 changes: 1 addition & 1 deletion x/mint/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func ValidateGenesis(data GenesisState) error {
return err
}

return ValidateMinter(data.Minter)
return data.Minter.Validate()
}
160 changes: 30 additions & 130 deletions x/mint/types/mint.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 473221d

Please sign in to comment.