Skip to content

Commit

Permalink
Avoid tx when market price is zero, remove price from external liquid…
Browse files Browse the repository at this point in the history
…ity (#988)

* handle zero price

* remove price

* remove asset price test
  • Loading branch information
amityadav0 authored Nov 25, 2024
1 parent 3eb97d9 commit b9eb1ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 42 deletions.
42 changes: 19 additions & 23 deletions x/amm/keeper/msg_server_feed_multiple_external_liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package keeper

import (
"context"

sdkmath "cosmossdk.io/math"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
Expand All @@ -22,28 +22,24 @@ func (k Keeper) GetExternalLiquidityRatio(ctx sdk.Context, pool types.Pool, amou
return nil, assetprofiletypes.ErrAssetProfileNotFound
}
if entry.DisplayName == el.Asset {
price, found := k.oracleKeeper.GetAssetPrice(ctx, el.Asset)
if !found {
return nil, fmt.Errorf("asset price not set: %s", el.Asset)
} else {
O_Tvl := price.Price.Mul(el.Amount)
P_Tvl := asset.Token.Amount.ToLegacyDec().Mul(price.Price)

// Ensure tvl is not zero to avoid division by zero
if P_Tvl.IsZero() {
return nil, types.ErrAmountTooLow
}

liquidityRatio := LiquidityRatioFromPriceDepth(el.Depth)
// Ensure tvl is not zero to avoid division by zero
if liquidityRatio.IsZero() {
return nil, types.ErrAmountTooLow
}
asset.ExternalLiquidityRatio = (O_Tvl.Quo(P_Tvl)).Quo(liquidityRatio)

if asset.ExternalLiquidityRatio.LT(sdkmath.LegacyOneDec()) {
asset.ExternalLiquidityRatio = sdkmath.LegacyOneDec()
}

O_Tvl := el.Amount
P_Tvl := asset.Token.Amount.ToLegacyDec()

// Ensure tvl is not zero to avoid division by zero
if P_Tvl.IsZero() {
return nil, types.ErrAmountTooLow
}

liquidityRatio := LiquidityRatioFromPriceDepth(el.Depth)
// Ensure tvl is not zero to avoid division by zero
if liquidityRatio.IsZero() {
return nil, types.ErrAmountTooLow
}
asset.ExternalLiquidityRatio = (O_Tvl.Quo(P_Tvl)).Quo(liquidityRatio)

if asset.ExternalLiquidityRatio.LT(sdkmath.LegacyOneDec()) {
asset.ExternalLiquidityRatio = sdkmath.LegacyOneDec()
}
}
}
Expand Down
19 changes: 0 additions & 19 deletions x/amm/keeper/msg_server_feed_multiple_external_liquidity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,6 @@ func (suite *AmmKeeperTestSuite) TestGetExternalLiquidityRatio() {
expectedResult: nil,
expectedError: fmt.Errorf("asset profile not found for denom"),
},
{
name: "missing asset price",
pool: types.Pool{
PoolAssets: []types.PoolAsset{
{
Token: sdk.NewCoin("asset2denom", sdkmath.NewInt(1000)),
},
},
},
amountDepthInfo: []types.AssetAmountDepth{
{
Asset: "asset2",
Amount: sdkmath.LegacyNewDec(500),
Depth: sdkmath.LegacyMustNewDecFromStr("0.5"),
},
},
expectedResult: nil,
expectedError: fmt.Errorf("asset price not set: asset2"),
},
{
name: "division by zero",
pool: types.Pool{
Expand Down
10 changes: 10 additions & 0 deletions x/tradeshield/keeper/pending_spot_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"math"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
Expand Down Expand Up @@ -187,6 +188,9 @@ func (k Keeper) ExecuteStopLossOrder(ctx sdk.Context, order types.SpotOrder) err
if err != nil {
return err
}
if marketPrice.IsZero() {
return errorsmod.Wrapf(types.ErrZeroMarketPrice, "Base denom: %s, Quote denom: %s", order.OrderPrice.BaseDenom, order.OrderPrice.QuoteDenom)
}

if marketPrice.GT(order.OrderPrice.Rate) {
// skip the order
Expand Down Expand Up @@ -225,6 +229,9 @@ func (k Keeper) ExecuteLimitSellOrder(ctx sdk.Context, order types.SpotOrder) er
if err != nil {
return err
}
if marketPrice.IsZero() {
return errorsmod.Wrapf(types.ErrZeroMarketPrice, "Base denom: %s, Quote denom: %s", order.OrderPrice.BaseDenom, order.OrderPrice.QuoteDenom)
}

if marketPrice.LT(order.OrderPrice.Rate) {
// skip the order
Expand Down Expand Up @@ -263,6 +270,9 @@ func (k Keeper) ExecuteLimitBuyOrder(ctx sdk.Context, order types.SpotOrder) err
if err != nil {
return err
}
if marketPrice.IsZero() {
return errorsmod.Wrapf(types.ErrZeroMarketPrice, "Base denom: %s, Quote denom: %s", order.OrderPrice.BaseDenom, order.OrderPrice.QuoteDenom)
}

if marketPrice.GT(order.OrderPrice.Rate) {
// skip the order
Expand Down
1 change: 1 addition & 0 deletions x/tradeshield/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ var (
ErrPriceNotFound = errors.Register(ModuleName, 1103, "price not found")
ErrSizeZero = errors.Register(ModuleName, 1104, "zero order ids ")
ErrInvalidStatus = errors.Register(ModuleName, 1105, "invalid status")
ErrZeroMarketPrice = errors.Register(ModuleName, 1106, "market price is zero")
)

0 comments on commit b9eb1ac

Please sign in to comment.