From ee61e3976969e0c190905fd77b97a6b003ef9e36 Mon Sep 17 00:00:00 2001 From: Solovyov1796 Date: Mon, 17 Jun 2024 11:02:26 +0800 Subject: [PATCH] Merge pull request #34 from 0glabs/vesting calc inflation based on staking apy --- app/app.go | 10 ++++-- chaincfg/mint.go | 84 ++++++++++++++++++++++++++++++++---------------- go.mod | 5 ++- go.sum | 8 ++--- localtestnet.sh | 10 +++--- 5 files changed, 75 insertions(+), 42 deletions(-) diff --git a/app/app.go b/app/app.go index 48c41cd9..83b1836c 100644 --- a/app/app.go +++ b/app/app.go @@ -23,6 +23,7 @@ import ( authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/auth/vesting" + vestingkeeper "github.com/cosmos/cosmos-sdk/x/auth/vesting/keeper" vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" "github.com/cosmos/cosmos-sdk/x/authz" authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" @@ -248,6 +249,7 @@ type App struct { bep3Keeper bep3keeper.Keeper pricefeedKeeper pricefeedkeeper.Keeper committeeKeeper committeekeeper.Keeper + vestingKeeper vestingkeeper.VestingKeeper mintKeeper mintkeeper.Keeper dasignersKeeper dasignerskeeper.Keeper @@ -299,6 +301,7 @@ func NewApp( minttypes.StoreKey, counciltypes.StoreKey, dasignerstypes.StoreKey, + vestingtypes.StoreKey, ) tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey, evmtypes.TransientKey, feemarkettypes.TransientKey) memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey) @@ -364,11 +367,14 @@ func NewApp( bankSubspace, app.loadBlockedMaccAddrs(), ) + app.vestingKeeper = vestingkeeper.NewVestingKeeper(app.accountKeeper, app.bankKeeper, keys[vestingtypes.StoreKey]) + app.stakingKeeper = stakingkeeper.NewKeeper( appCodec, keys[stakingtypes.StoreKey], app.accountKeeper, app.bankKeeper, + app.vestingKeeper, stakingSubspace, ) app.authzKeeper = authzkeeper.NewKeeper( @@ -590,7 +596,7 @@ func NewApp( upgrade.NewAppModule(app.upgradeKeeper), evidence.NewAppModule(app.evidenceKeeper), transferModule, - vesting.NewAppModule(app.accountKeeper, app.bankKeeper), + vesting.NewAppModule(app.accountKeeper, app.vestingKeeper), authzmodule.NewAppModule(appCodec, app.authzKeeper, app.accountKeeper, app.bankKeeper, app.interfaceRegistry), issuance.NewAppModule(app.issuanceKeeper, app.accountKeeper, app.bankKeeper), bep3.NewAppModule(app.bep3Keeper, app.accountKeeper, app.bankKeeper), @@ -599,7 +605,7 @@ func NewApp( committee.NewAppModule(app.committeeKeeper, app.accountKeeper), evmutil.NewAppModule(app.evmutilKeeper, app.bankKeeper, app.accountKeeper), // nil InflationCalculationFn, use SDK's default inflation function - mint.NewAppModule(appCodec, app.mintKeeper, app.accountKeeper, chaincfg.CustomInflationCalculateFn), + mint.NewAppModule(appCodec, app.mintKeeper, app.accountKeeper, chaincfg.NextInflationRate), council.NewAppModule(app.CouncilKeeper, app.stakingKeeper), dasigners.NewAppModule(app.dasignersKeeper, app.stakingKeeper), ) diff --git a/chaincfg/mint.go b/chaincfg/mint.go index 1ecfe409..754dd86b 100644 --- a/chaincfg/mint.go +++ b/chaincfg/mint.go @@ -1,45 +1,73 @@ package chaincfg import ( - "github.com/tendermint/tendermint/libs/log" + "github.com/shopspring/decimal" sdk "github.com/cosmos/cosmos-sdk/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" ) -func CustomInflationCalculateFn(ctx sdk.Context, minter minttypes.Minter, params minttypes.Params, bondedRatio sdk.Dec) sdk.Dec { - logger := ctx.Logger() - if logger == nil { - panic("logger is nil") - } - return customInflationCalculateFn(logger, minter, params, bondedRatio) +var ( + Xmax, _ = sdk.NewDecFromStr("1.0") // upper limit on staked supply (as % of circ supply) + Ymin, _ = sdk.NewDecFromStr("0.05") // target APY at upper limit + + Xmin, _ = sdk.NewDecFromStr("0.2") // lower limit on staked supply (as % of circ supply) + Ymax, _ = sdk.NewDecFromStr("0.15") // target APY at lower limit + + decayRate, _ = sdk.NewDecFromStr("10") +) + +func decExp(x sdk.Dec) sdk.Dec { + xDec := decimal.NewFromBigInt(x.BigInt(), -18) + expDec, _ := xDec.ExpTaylor(18) + expInt := expDec.Shift(18).BigInt() + return sdk.NewDecFromBigIntWithPrec(expInt, 18) } -func customInflationCalculateFn(logger log.Logger, minter minttypes.Minter, params minttypes.Params, bondedRatio sdk.Dec) sdk.Dec { - // The target annual inflation rate is recalculated for each previsions cycle. The - // inflation is also subject to a rate change (positive or negative) depending on - // the distance from the desired ratio (67%). The maximum rate change possible is - // defined to be 13% per year, however the annual inflation is capped as between - // 7% and 20%. +func NextInflationRate(ctx sdk.Context, minter minttypes.Minter, params minttypes.Params, bondedRatio sdk.Dec, circulatingRatio sdk.Dec) sdk.Dec { + X := bondedRatio.Quo(circulatingRatio) - // (1 - bondedRatio/GoalBonded) * InflationRateChange - inflationRateChangePerYear := sdk.OneDec(). - Sub(bondedRatio.Quo(params.GoalBonded)). - Mul(params.InflationRateChange) - inflationRateChange := inflationRateChangePerYear.Quo(sdk.NewDec(int64(params.BlocksPerYear))) - - // adjust the new annual inflation for this next cycle - inflation := minter.Inflation.Add(inflationRateChange) // note inflationRateChange may be negative - if inflation.GT(params.InflationMax) { - inflation = params.InflationMax - } - if inflation.LT(params.InflationMin) { - inflation = params.InflationMin + var apy sdk.Dec + if X.LT(Xmin) { + apy = Ymax + } else { + exp := decayRate.Neg().Mul(Xmax.Sub(Xmin)) + c := decExp(exp) + d := Ymin.Sub(Ymax.Mul(c)).Quo(sdk.OneDec().Sub(c)) + expBonded := decayRate.Neg().Mul(X.Sub(Xmin)) + cBonded := decExp(expBonded) + e := Ymax.Sub(d).Mul(cBonded) + apy = d.Add(e) } - logger.Info( - "calculated new annual inflation", + inflation := apy.Mul(bondedRatio) + + // // The target annual inflation rate is recalculated for each previsions cycle. The + // // inflation is also subject to a rate change (positive or negative) depending on + // // the distance from the desired ratio (67%). The maximum rate change possible is + // // defined to be 13% per year, however the annual inflation is capped as between + // // 7% and 20%. + + // // (1 - bondedRatio/GoalBonded) * InflationRateChange + // inflationRateChangePerYear := sdk.OneDec(). + // Sub(bondedRatio.Quo(params.GoalBonded)). + // Mul(params.InflationRateChange) + // inflationRateChange := inflationRateChangePerYear.Quo(sdk.NewDec(int64(params.BlocksPerYear))) + + // // adjust the new annual inflation for this next cycle + // inflation := minter.Inflation.Add(inflationRateChange) // note inflationRateChange may be negative + // if inflation.GT(params.InflationMax) { + // inflation = params.InflationMax + // } + // if inflation.LT(params.InflationMin) { + // inflation = params.InflationMin + // } + + ctx.Logger().Info( + "nextInflationRate", "bondedRatio", bondedRatio, + "circulatingRatio", circulatingRatio, + "apy", apy, "inflation", inflation, "params", params, "minter", minter, diff --git a/go.mod b/go.mod index b15e5c7a..d6c38257 100644 --- a/go.mod +++ b/go.mod @@ -212,14 +212,13 @@ replace ( // Use rocksdb 7.9.2 github.com/cometbft/cometbft-db => github.com/kava-labs/cometbft-db v0.7.0-rocksdb-v7.9.2-kava.1 // Use cosmos-sdk fork with backported fix for unsafe-reset-all, staking transfer events, and custom tally handler support - // github.com/cosmos/cosmos-sdk => github.com/0glabs/cosmos-sdk v0.46.11-kava.3 - github.com/cosmos/cosmos-sdk => github.com/0glabs/cosmos-sdk v0.46.11-0glabs.4 + github.com/cosmos/cosmos-sdk => github.com/0glabs/cosmos-sdk v0.46.11-0glabs.5 // See https://github.com/cosmos/cosmos-sdk/pull/13093 github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2 // Use go-ethereum fork with precompiles github.com/ethereum/go-ethereum => github.com/evmos/go-ethereum v1.10.26-evmos-rc2 // Use ethermint fork that respects min-gas-price with NoBaseFee true and london enabled, and includes eip712 support - github.com/evmos/ethermint => github.com/0glabs/ethermint v0.21.0-0g.v2.0.1 + github.com/evmos/ethermint => github.com/0glabs/ethermint v0.21.0-0g.v2.0.2 // See https://github.com/cosmos/cosmos-sdk/pull/10401, https://github.com/cosmos/cosmos-sdk/commit/0592ba6158cd0bf49d894be1cef4faeec59e8320 github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0 // Use the cosmos modified protobufs diff --git a/go.sum b/go.sum index 96dfdfd0..4329d648 100644 --- a/go.sum +++ b/go.sum @@ -202,10 +202,10 @@ filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmG filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw= git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9/go.mod h1:BVJwbDfVjCjoFiKrhkei6NdGcZYpkDkdyCdg1ukytRA= -github.com/0glabs/cosmos-sdk v0.46.11-0glabs.4 h1:NYKYgJIilexHR8VE1EAl7Tv2wMQGPwdzKiLV2DnIAwg= -github.com/0glabs/cosmos-sdk v0.46.11-0glabs.4/go.mod h1:jwgWoeAWxqMF5pZUZ4N+G4rD3q6oOLulq3/dGCFLEX4= -github.com/0glabs/ethermint v0.21.0-0g.v2.0.1 h1:loFnZAEZ8tboo3JO3+AE+1gJcUm6hkYuwcn+ZHBhjxE= -github.com/0glabs/ethermint v0.21.0-0g.v2.0.1/go.mod h1:peUmQT71k9BOBgoWoIRWRrM/O01mffVjIH0RLnoaFuI= +github.com/0glabs/cosmos-sdk v0.46.11-0glabs.5 h1:/7zqU8Az6n3UpKnypKQ92Yw8AgrE1v1AfatrL8elajs= +github.com/0glabs/cosmos-sdk v0.46.11-0glabs.5/go.mod h1:jwgWoeAWxqMF5pZUZ4N+G4rD3q6oOLulq3/dGCFLEX4= +github.com/0glabs/ethermint v0.21.0-0g.v2.0.2 h1:mFVOMra9lmeNk+CgL0UsBxqiXHx5JWVeiURJFgQmHzY= +github.com/0glabs/ethermint v0.21.0-0g.v2.0.2/go.mod h1:KPLRino6lVDPV/cZCbQf7dZdR1nsVgptr0Htf6ZxZe8= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4= github.com/Azure/azure-sdk-for-go/sdk/azcore v0.21.1/go.mod h1:fBF9PQNqB8scdgpZ3ufzaLntG0AG7C1WjPMsiFOmfHM= diff --git a/localtestnet.sh b/localtestnet.sh index 189ec3b2..a5137590 100755 --- a/localtestnet.sh +++ b/localtestnet.sh @@ -16,7 +16,7 @@ userMnemonic="news tornado sponsor drastic dolphin awful plastic select true liz # 0x7Bbf300890857b8c241b219C6a489431669b3aFA # kava10wlnqzyss4accfqmyxwx5jy5x9nfkwh6qm7n4t -relayerMnemonic="never reject sniff east arctic funny twin feed upper series stay shoot vivid adapt defense economy pledge fetch invite approve ceiling admit gloom exit" +vestingMnemonic="never reject sniff east arctic funny twin feed upper series stay shoot vivid adapt defense economy pledge fetch invite approve ceiling admit gloom exit" # 0xa2F728F997f62F47D4262a70947F6c36885dF9fa # kava15tmj37vh7ch504px9fcfglmvx6y9m70646ev8t @@ -64,11 +64,11 @@ $BINARY add-genesis-account $evmFaucetKeyName 1000000000000000000000ua0gi userKeyName="user" printf "$userMnemonic\n" | $BINARY keys add $userKeyName --eth --recover -$BINARY add-genesis-account $userKeyName 1000000000000000000000ua0gi,1000000000usdx +$BINARY add-genesis-account $userKeyName 1000000000000000000000ua0gi -relayerKeyName="relayer" -printf "$relayerMnemonic\n" | $BINARY keys add $relayerKeyName --eth --recover -$BINARY add-genesis-account $relayerKeyName 1000000000000000000000ua0gi +vestingKeyName="vesting" +printf "$vestingMnemonic\n" | $BINARY keys add $vestingKeyName --eth --recover +$BINARY add-genesis-account $vestingKeyName 1000000000000000000000ua0gi --vesting-amount 1000000000000000000000ua0gi --vesting-start-time 1717200000 --vesting-end-time 1719791999 storageContractAcc="0g1vsjpjgw8p5f4x0nwp8ernl9lkszewcqqss7r5d" $BINARY add-genesis-account $storageContractAcc 1000000000000000000000ua0gi