From e93947e205c7cc7ad6bf33522bfcf59063a614e1 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 16 May 2023 22:11:44 -0400 Subject: [PATCH] refactor: more discriptive error when no liquidity in swaps (#5197) --- x/concentrated-liquidity/swaps.go | 18 +++++++++++++++ x/concentrated-liquidity/swaps_test.go | 32 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/x/concentrated-liquidity/swaps.go b/x/concentrated-liquidity/swaps.go index 3bb2a15a121..2de067af80e 100644 --- a/x/concentrated-liquidity/swaps.go +++ b/x/concentrated-liquidity/swaps.go @@ -254,6 +254,15 @@ func (k Keeper) computeOutAmtGivenIn( if err != nil { return sdk.Coin{}, sdk.Coin{}, 0, 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() @@ -423,6 +432,15 @@ func (k Keeper) computeInAmtGivenOut( if err != nil { return sdk.Coin{}, sdk.Coin{}, 0, 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 5ee4403e2fa..591b516bb6c 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 {