diff --git a/x/concentrated-liquidity/swaps.go b/x/concentrated-liquidity/swaps.go index 3358c41f28e..d88a9b4a98c 100644 --- a/x/concentrated-liquidity/swaps.go +++ b/x/concentrated-liquidity/swaps.go @@ -259,6 +259,15 @@ func (k Keeper) computeOutAmtGivenIn( if err != nil { return sdk.Coin{}, sdk.Coin{}, sdk.Int{}, sdk.Dec{}, sdk.Dec{}, err } + + hasPositionInPool, err := k.HasAnyPositionForPool(ctx, poolId) + if err != nil { + return sdk.Coin{}, sdk.Coin{}, sdk.Int{}, sdk.Dec{}, sdk.Dec{}, err + } + if !hasPositionInPool { + return sdk.Coin{}, sdk.Coin{}, sdk.Int{}, sdk.Dec{}, sdk.Dec{}, types.NoSpotPriceWhenNoLiquidityError{PoolId: poolId} + } + asset0 := p.GetToken0() asset1 := p.GetToken1() tokenAmountInSpecified := tokenInMin.Amount.ToDec() @@ -429,6 +438,15 @@ func (k Keeper) calcInAmtGivenOut( if err != nil { return writeCtx, sdk.Coin{}, sdk.Coin{}, sdk.Int{}, sdk.Dec{}, sdk.Dec{}, err } + + hasPositionInPool, err := k.HasAnyPositionForPool(ctx, poolId) + if err != nil { + return writeCtx, sdk.Coin{}, sdk.Coin{}, sdk.Int{}, sdk.Dec{}, sdk.Dec{}, err + } + if !hasPositionInPool { + return writeCtx, sdk.Coin{}, sdk.Coin{}, sdk.Int{}, sdk.Dec{}, sdk.Dec{}, types.NoSpotPriceWhenNoLiquidityError{PoolId: poolId} + } + asset0 := p.GetToken0() asset1 := p.GetToken1() diff --git a/x/concentrated-liquidity/swaps_test.go b/x/concentrated-liquidity/swaps_test.go index f59e3049730..3204af9ec58 100644 --- a/x/concentrated-liquidity/swaps_test.go +++ b/x/concentrated-liquidity/swaps_test.go @@ -1602,6 +1602,38 @@ func (s *KeeperTestSuite) TestComputeAndSwapOutAmtGivenIn() { } } +func (s *KeeperTestSuite) TestSwapOutAmtGivenIn_NoPositions() { + s.SetupTest() + + pool := s.PrepareConcentratedPool() + + // perform swap + _, _, _, _, _, err := s.App.ConcentratedLiquidityKeeper.SwapOutAmtGivenIn( + s.Ctx, s.TestAccs[0], pool, + DefaultCoin0, DefaultCoin1.Denom, + sdk.ZeroDec(), sdk.ZeroDec(), + ) + + s.Require().Error(err) + s.Require().ErrorIs(err, types.NoSpotPriceWhenNoLiquidityError{PoolId: pool.GetId()}) +} + +func (s *KeeperTestSuite) TestSwapInAmtGivenOut_NoPositions() { + s.SetupTest() + + pool := s.PrepareConcentratedPool() + + // perform swap + _, _, _, _, _, err := s.App.ConcentratedLiquidityKeeper.SwapInAmtGivenOut( + s.Ctx, s.TestAccs[0], pool, + DefaultCoin0, DefaultCoin1.Denom, + sdk.ZeroDec(), sdk.ZeroDec(), + ) + + s.Require().Error(err) + s.Require().ErrorIs(err, types.NoSpotPriceWhenNoLiquidityError{PoolId: pool.GetId()}) +} + func (s *KeeperTestSuite) TestSwapOutAmtGivenIn_TickUpdates() { tests := make(map[string]SwapTest) for name, test := range swapOutGivenInCases {