Skip to content

Commit

Permalink
[Stablestake]: Fix USDC earn amount and borrow ratio (#830)
Browse files Browse the repository at this point in the history
* add account interest migration

* add migration
  • Loading branch information
amityadav0 authored Oct 1, 2024
1 parent 0954068 commit 95feaae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
1 change: 0 additions & 1 deletion x/stablestake/keeper/begin_blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions x/stablestake/keeper/debt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions x/stablestake/migrations/v6_migration.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions x/stablestake/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down

0 comments on commit 95feaae

Please sign in to comment.