Skip to content

Commit

Permalink
Usdc earn query fix (#372)
Browse files Browse the repository at this point in the history
* fix denom problem, add upgrade handler for stablestake

* Resolve the usage of deposit denom

* fix: stablestake migration

---------

Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com>
  • Loading branch information
jelysn and cosmic-vagabond authored Feb 14, 2024
1 parent 6750ddd commit 4ab7cbe
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 30 deletions.
27 changes: 27 additions & 0 deletions scripts/examples/stablestake/stablestake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

# query params
elysd query bank balances $(elysd keys show -a treasury --keyring-backend=test)

elysd query stablestake params
# params:
# deposit_denom: uusdc
# epoch_length: "1"
# health_gain_factor: "1.000000000000000000"
# interest_rate: "0.150000000000000000"
# interest_rate_decrease: "0.010000000000000000"
# interest_rate_increase: "0.010000000000000000"
# interest_rate_max: "0.170000000000000000"
# interest_rate_min: "0.120000000000000000"
# redemption_rate: "1.000000000000000000"
# total_value: "100000000"
elysd query stablestake borrow-ratio
# borrow_ratio: "0.000000000000000000"
# total_borrow: "0"
# total_deposit: "100000000"

elysd tx stablestake bond 100000000 --from=treasury --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000


# Testnet
elysd tx stablestake bond 112131 --from=t2a --keyring-backend=test --chain-id=elystestnet-1 --yes --gas=1000000 --node=https://rpc.testnet.elys.network:443 --fees=250uelys
8 changes: 4 additions & 4 deletions x/stablestake/keeper/debt.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (k Keeper) UpdateInterestStacked(ctx sdk.Context, debt types.Debt) types.De
}

func (k Keeper) Borrow(ctx sdk.Context, addr sdk.AccAddress, amount sdk.Coin) error {
params := k.GetParams(ctx)
if params.DepositDenom != amount.Denom {
depositDenom := k.GetDepositDenom(ctx)
if depositDenom != amount.Denom {
return types.ErrInvalidBorrowDenom
}
debt := k.UpdateInterestStackedByAddress(ctx, addr)
Expand All @@ -88,8 +88,8 @@ func (k Keeper) Borrow(ctx sdk.Context, addr sdk.AccAddress, amount sdk.Coin) er
}

func (k Keeper) Repay(ctx sdk.Context, addr sdk.AccAddress, amount sdk.Coin) error {
params := k.GetParams(ctx)
if params.DepositDenom != amount.Denom {
depositDenom := k.GetDepositDenom(ctx)
if depositDenom != amount.Denom {
return types.ErrInvalidBorrowDenom
}

Expand Down
3 changes: 2 additions & 1 deletion x/stablestake/keeper/interest_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func (k Keeper) InterestRateComputation(ctx sdk.Context) sdk.Dec {
prevInterestRate := params.InterestRate

moduleAddr := authtypes.NewModuleAddress(types.ModuleName)
balance := k.bk.GetBalance(ctx, moduleAddr, params.DepositDenom)
depositDenom := k.GetDepositDenom(ctx)
balance := k.bk.GetBalance(ctx, moduleAddr, depositDenom)
borrowed := params.TotalValue.Sub(balance.Amount)
targetInterestRate := healthGainFactor.
Mul(sdk.NewDecFromInt(borrowed)).
Expand Down
10 changes: 2 additions & 8 deletions x/stablestake/keeper/msg_server_bond.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
assetprofiletypes "github.com/elys-network/elys/x/assetprofile/types"
Expand All @@ -19,12 +18,7 @@ func (k msgServer) Bond(goCtx context.Context, msg *types.MsgBond) (*types.MsgBo
params := k.GetParams(ctx)
sender := sdk.MustAccAddressFromBech32(msg.Creator)

entry, found := k.assetProfileKeeper.GetEntry(ctx, params.DepositDenom)
if !found {
return nil, errorsmod.Wrap(types.ErrInvalidDepositDenom, params.DepositDenom)
}

depositDenom := entry.Denom
depositDenom := k.GetDepositDenom(ctx)
depositCoin := sdk.NewCoin(depositDenom, msg.Amount)
err := k.bk.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, sdk.Coins{depositCoin})
if err != nil {
Expand All @@ -48,7 +42,7 @@ func (k msgServer) Bond(goCtx context.Context, msg *types.MsgBond) (*types.MsgBo
return nil, err
}

entry, found = k.assetProfileKeeper.GetEntry(ctx, shareDenom)
entry, found := k.assetProfileKeeper.GetEntry(ctx, shareDenom)
if !found {
// Set an entity to assetprofile
entry = assetprofiletypes.Entry{
Expand Down
7 changes: 1 addition & 6 deletions x/stablestake/keeper/msg_server_unbond.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
commitmentkeeper "github.com/elys-network/elys/x/commitment/keeper"
ctypes "github.com/elys-network/elys/x/commitment/types"
Expand Down Expand Up @@ -42,12 +41,8 @@ func (k msgServer) Unbond(goCtx context.Context, msg *types.MsgUnbond) (*types.M
}

redemptionAmount := sdk.NewDecFromInt(shareCoin.Amount).Mul(params.RedemptionRate).RoundInt()
entry, found := k.assetProfileKeeper.GetEntry(ctx, params.DepositDenom)
if !found {
return nil, errorsmod.Wrap(types.ErrInvalidDepositDenom, params.DepositDenom)
}

depositDenom := entry.Denom
depositDenom := k.GetDepositDenom(ctx)
redemptionCoin := sdk.NewCoin(depositDenom, redemptionAmount)
err = k.bk.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, sdk.Coins{redemptionCoin})
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions x/stablestake/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
}

func (k Keeper) GetDepositDenom(ctx sdk.Context) string {
params := k.GetParams(ctx)
depositDenom := params.DepositDenom
entry, found := k.assetProfileKeeper.GetEntry(ctx, params.DepositDenom)
if !found {
depositDenom = entry.Denom
}
return depositDenom
}
5 changes: 4 additions & 1 deletion x/stablestake/keeper/query_borrow_ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ func (k Keeper) BorrowRatio(goCtx context.Context, req *types.QueryBorrowRatioRe

params := k.GetParams(ctx)
moduleAddr := authtypes.NewModuleAddress(types.ModuleName)
balance := k.bk.GetBalance(ctx, moduleAddr, params.DepositDenom)

depositDenom := k.GetDepositDenom(ctx)

balance := k.bk.GetBalance(ctx, moduleAddr, depositDenom)
borrowed := params.TotalValue.Sub(balance.Amount)
borrowRatio := sdk.ZeroDec()
if params.TotalValue.GT(sdk.ZeroInt()) {
Expand Down
13 changes: 13 additions & 0 deletions x/stablestake/migrations/new_migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package migrations

import (
"github.com/elys-network/elys/x/stablestake/keeper"
)

type Migrator struct {
keeper keeper.Keeper
}

func NewMigrator(keeper keeper.Keeper) Migrator {
return Migrator{keeper: keeper}
}
14 changes: 14 additions & 0 deletions x/stablestake/migrations/v2_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package migrations

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/stablestake/types"
)

func (m Migrator) V2Migration(ctx sdk.Context) error {
oldParams := m.keeper.GetParams(ctx)
params := types.DefaultParams()
params.TotalValue = oldParams.TotalValue
m.keeper.SetParams(ctx, params)
return nil
}
9 changes: 8 additions & 1 deletion x/stablestake/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"

// this line is used by starport scaffolding # 1

"github.com/grpc-ecosystem/grpc-gateway/runtime"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/elys-network/elys/x/stablestake/client/cli"
"github.com/elys-network/elys/x/stablestake/keeper"
"github.com/elys-network/elys/x/stablestake/migrations"
"github.com/elys-network/elys/x/stablestake/types"
)

Expand Down Expand Up @@ -114,6 +116,11 @@ func NewAppModule(
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, 1, m.V2Migration)
if err != nil {
panic(err)
}
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
Expand All @@ -137,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 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down
10 changes: 1 addition & 9 deletions x/stablestake/types/query.pb.gw.go

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

0 comments on commit 4ab7cbe

Please sign in to comment.