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

price range condition check removed #361

Merged
merged 2 commits into from
Jul 22, 2022
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
33 changes: 18 additions & 15 deletions x/liquidity/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ func (k Keeper) ValidateMsgLimitOrder(ctx sdk.Context, msg *types.MsgLimitOrder)
return sdk.Coin{}, sdk.Coin{}, sdk.Dec{}, sdkerrors.Wrapf(sdkerrors.ErrNotFound, "pair %d not found", msg.PairId)
}

var upperPriceLimit, lowerPriceLimit sdk.Dec
if pair.LastPrice != nil {
lastPrice := *pair.LastPrice
upperPriceLimit = lastPrice.Mul(sdk.OneDec().Add(params.MaxPriceLimitRatio))
lowerPriceLimit = lastPrice.Mul(sdk.OneDec().Sub(params.MaxPriceLimitRatio))
} else {
upperPriceLimit = amm.HighestTick(int(params.TickPrecision))
lowerPriceLimit = amm.LowestTick(int(params.TickPrecision))
}
switch {
case msg.Price.GT(upperPriceLimit):
return sdk.Coin{}, sdk.Coin{}, sdk.Dec{}, sdkerrors.Wrapf(types.ErrPriceOutOfRange, "%s is higher than %s", msg.Price, upperPriceLimit)
case msg.Price.LT(lowerPriceLimit):
return sdk.Coin{}, sdk.Coin{}, sdk.Dec{}, sdkerrors.Wrapf(types.ErrPriceOutOfRange, "%s is lower than %s", msg.Price, lowerPriceLimit)
}
// uncomment the below code if price range limitation needs to enabled, doing so can cause partial-mismatch order issue -
// for the normal swap in frontend.

// var upperPriceLimit, lowerPriceLimit sdk.Dec
// if pair.LastPrice != nil {
// lastPrice := *pair.LastPrice
// upperPriceLimit = lastPrice.Mul(sdk.OneDec().Add(params.MaxPriceLimitRatio))
// lowerPriceLimit = lastPrice.Mul(sdk.OneDec().Sub(params.MaxPriceLimitRatio))
// } else {
// upperPriceLimit = amm.HighestTick(int(params.TickPrecision))
// lowerPriceLimit = amm.LowestTick(int(params.TickPrecision))
// }
// switch {
// case msg.Price.GT(upperPriceLimit):
// return sdk.Coin{}, sdk.Coin{}, sdk.Dec{}, sdkerrors.Wrapf(types.ErrPriceOutOfRange, "%s is higher than %s", msg.Price, upperPriceLimit)
// case msg.Price.LT(lowerPriceLimit):
// return sdk.Coin{}, sdk.Coin{}, sdk.Dec{}, sdkerrors.Wrapf(types.ErrPriceOutOfRange, "%s is lower than %s", msg.Price, lowerPriceLimit)
// }

switch msg.Direction {
case types.OrderDirectionBuy:
Expand Down
65 changes: 32 additions & 33 deletions x/liquidity/keeper/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

utils "github.com/comdex-official/comdex/types"
"github.com/comdex-official/comdex/x/liquidity"
"github.com/comdex-official/comdex/x/liquidity/amm"
"github.com/comdex-official/comdex/x/liquidity/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -79,38 +78,38 @@ func (s *KeeperTestSuite) TestLimitOrder() {
ExpErr: sdkerrors.Wrapf(sdkerrors.ErrNotFound, "pair %d not found", 69),
ExpResp: &types.Order{},
},
{
Name: "error price higher than upper limit",
Msg: *types.NewMsgLimitOrder(
appID1,
addr1,
pair.Id,
types.OrderDirectionBuy,
utils.ParseCoin("10030000uasset2"),
asset1.Denom,
amm.HighestTick(int(params.TickPrecision+1)),
newInt(10000000),
time.Second*10,
),
ExpErr: sdkerrors.Wrapf(types.ErrPriceOutOfRange, "%s is higher than %s", amm.HighestTick(int(params.TickPrecision+1)), amm.HighestTick(int(params.TickPrecision))),
ExpResp: &types.Order{},
},
{
Name: "error price lower than lower limit",
Msg: *types.NewMsgLimitOrder(
appID1,
addr1,
pair.Id,
types.OrderDirectionBuy,
utils.ParseCoin("10030000uasset2"),
asset1.Denom,
amm.LowestTick(int(params.TickPrecision-1)),
newInt(10000000),
time.Second*10,
),
ExpErr: sdkerrors.Wrapf(types.ErrPriceOutOfRange, "%s is lower than %s", amm.LowestTick(int(params.TickPrecision-1)), amm.LowestTick(int(params.TickPrecision))),
ExpResp: &types.Order{},
},
// {
// Name: "error price higher than upper limit",
// Msg: *types.NewMsgLimitOrder(
// appID1,
// addr1,
// pair.Id,
// types.OrderDirectionBuy,
// utils.ParseCoin("10030000uasset2"),
// asset1.Denom,
// amm.HighestTick(int(params.TickPrecision+1)),
// newInt(10000000),
// time.Second*10,
// ),
// ExpErr: sdkerrors.Wrapf(types.ErrPriceOutOfRange, "%s is higher than %s", amm.HighestTick(int(params.TickPrecision+1)), amm.HighestTick(int(params.TickPrecision))),
// ExpResp: &types.Order{},
// },
// {
// Name: "error price lower than lower limit",
// Msg: *types.NewMsgLimitOrder(
// appID1,
// addr1,
// pair.Id,
// types.OrderDirectionBuy,
// utils.ParseCoin("10030000uasset2"),
// asset1.Denom,
// amm.LowestTick(int(params.TickPrecision-1)),
// newInt(10000000),
// time.Second*10,
// ),
// ExpErr: sdkerrors.Wrapf(types.ErrPriceOutOfRange, "%s is lower than %s", amm.LowestTick(int(params.TickPrecision-1)), amm.LowestTick(int(params.TickPrecision))),
// ExpResp: &types.Order{},
// },
{
Name: "error invalid denom pair buy direction",
Msg: *types.NewMsgLimitOrder(
Expand Down