Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Stablestake]: Fix USDC earn amount and borrow ratio #830

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
store.Delete(iterator.Key())
}
}

func (k Keeper) AccountInterest(ctx sdk.Context) []types.Debt {
store := ctx.KVStore(k.storeKey)
params := k.GetParams(ctx)

Check warning on line 266 in x/stablestake/keeper/debt.go

View check run for this annotation

Codecov / codecov/patch

x/stablestake/keeper/debt.go#L264-L266

Added lines #L264 - L266 were not covered by tests

iterator := sdk.KVStorePrefixIterator(store, types.DebtPrefixKey)
defer iterator.Close()

Check warning on line 269 in x/stablestake/keeper/debt.go

View check run for this annotation

Codecov / codecov/patch

x/stablestake/keeper/debt.go#L268-L269

Added lines #L268 - L269 were not covered by tests

debts := []types.Debt{}
for ; iterator.Valid(); iterator.Next() {
debt := types.Debt{}
k.cdc.MustUnmarshal(iterator.Value(), &debt)

Check warning on line 274 in x/stablestake/keeper/debt.go

View check run for this annotation

Codecov / codecov/patch

x/stablestake/keeper/debt.go#L271-L274

Added lines #L271 - L274 were not covered by tests

// 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()

Check warning on line 282 in x/stablestake/keeper/debt.go

View check run for this annotation

Codecov / codecov/patch

x/stablestake/keeper/debt.go#L278-L282

Added lines #L278 - L282 were not covered by tests
}
}
k.SetParams(ctx, params)
return debts

Check warning on line 286 in x/stablestake/keeper/debt.go

View check run for this annotation

Codecov / codecov/patch

x/stablestake/keeper/debt.go#L285-L286

Added lines #L285 - L286 were not covered by tests
}
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 @@
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)

Check warning on line 120 in x/stablestake/module.go

View check run for this annotation

Codecov / codecov/patch

x/stablestake/module.go#L120

Added line #L120 was not covered by tests
if err != nil {
panic(err)
}
Expand All @@ -144,7 +144,7 @@
}

// 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 }

Check warning on line 147 in x/stablestake/module.go

View check run for this annotation

Codecov / codecov/patch

x/stablestake/module.go#L147

Added line #L147 was not covered by tests

// 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
Loading