Skip to content

Commit

Permalink
Resolve the usage of deposit denom
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Feb 14, 2024
1 parent 62efc70 commit d32ccbe
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
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
}
6 changes: 1 addition & 5 deletions x/stablestake/keeper/query_borrow_ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ func (k Keeper) BorrowRatio(goCtx context.Context, req *types.QueryBorrowRatioRe
params := k.GetParams(ctx)
moduleAddr := authtypes.NewModuleAddress(types.ModuleName)

depositDenom := params.DepositDenom
entry, found := k.assetProfileKeeper.GetEntry(ctx, params.DepositDenom)
if !found {
depositDenom = entry.Denom
}
depositDenom := k.GetDepositDenom(ctx)

balance := k.bk.GetBalance(ctx, moduleAddr, depositDenom)
borrowed := params.TotalValue.Sub(balance.Amount)
Expand Down

0 comments on commit d32ccbe

Please sign in to comment.