From 95feaaebe232ac6800540c194826c826c5f56462 Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Tue, 1 Oct 2024 21:17:49 +0530 Subject: [PATCH] [Stablestake]: Fix USDC earn amount and borrow ratio (#830) * add account interest migration * add migration --- x/stablestake/keeper/begin_blocker.go | 1 - x/stablestake/keeper/debt.go | 25 ++++++++++++++++++++++++ x/stablestake/migrations/v6_migration.go | 12 ++++++++++++ x/stablestake/module.go | 4 ++-- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 x/stablestake/migrations/v6_migration.go diff --git a/x/stablestake/keeper/begin_blocker.go b/x/stablestake/keeper/begin_blocker.go index 66f39ceca..e33095c79 100644 --- a/x/stablestake/keeper/begin_blocker.go +++ b/x/stablestake/keeper/begin_blocker.go @@ -19,7 +19,6 @@ func (k Keeper) BeginBlocker(ctx sdk.Context) { k.SetParams(ctx, params) } k.SetInterest(ctx, uint64(ctx.BlockHeight()), types.InterestBlock{InterestRate: params.InterestRate, BlockTime: ctx.BlockTime().Unix(), BlockHeight: uint64(ctx.BlockHeight())}) - // Remove old data, should keep data of 2 years if numBlocks < int(ctx.BlockHeight()) { delBlock := ctx.BlockHeight() - int64(numBlocks) diff --git a/x/stablestake/keeper/debt.go b/x/stablestake/keeper/debt.go index 780b84e0a..11d54b55f 100644 --- a/x/stablestake/keeper/debt.go +++ b/x/stablestake/keeper/debt.go @@ -260,3 +260,28 @@ func (k Keeper) DeleteAllInterest(ctx sdk.Context) { store.Delete(iterator.Key()) } } + +func (k Keeper) AccountInterest(ctx sdk.Context) []types.Debt { + store := ctx.KVStore(k.storeKey) + params := k.GetParams(ctx) + + iterator := sdk.KVStorePrefixIterator(store, types.DebtPrefixKey) + defer iterator.Close() + + debts := []types.Debt{} + for ; iterator.Valid(); iterator.Next() { + debt := types.Debt{} + k.cdc.MustUnmarshal(iterator.Value(), &debt) + + // Set to 17% (as it couldn't be more over period of ~6 months) + // Note: These interest values were inflated due to a migration issue + val := sdk.NewDecFromInt(debt.Borrowed).Mul(sdk.NewDecWithPrec(17, 2)) + if debt.InterestStacked.GT(val.TruncateInt()) { + sub := debt.InterestStacked.Sub(val.TruncateInt()) + params.TotalValue = params.TotalValue.Sub(sub) + debt.InterestStacked = val.TruncateInt() + } + } + k.SetParams(ctx, params) + return debts +} diff --git a/x/stablestake/migrations/v6_migration.go b/x/stablestake/migrations/v6_migration.go new file mode 100644 index 000000000..ba2721520 --- /dev/null +++ b/x/stablestake/migrations/v6_migration.go @@ -0,0 +1,12 @@ +package migrations + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func (m Migrator) V6Migration(ctx sdk.Context) error { + + m.keeper.AccountInterest(ctx) + + return nil +} diff --git a/x/stablestake/module.go b/x/stablestake/module.go index c9ef1d15a..e3d8fb4b5 100644 --- a/x/stablestake/module.go +++ b/x/stablestake/module.go @@ -117,7 +117,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) m := migrations.NewMigrator(am.keeper) - err := cfg.RegisterMigration(types.ModuleName, 4, m.V5Migration) + err := cfg.RegisterMigration(types.ModuleName, 5, m.V6Migration) if err != nil { panic(err) } @@ -144,7 +144,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw } // ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1 -func (AppModule) ConsensusVersion() uint64 { return 5 } +func (AppModule) ConsensusVersion() uint64 { return 6 } // BeginBlock contains the logic that is automatically triggered at the beginning of each block func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {