Skip to content

Commit

Permalink
refactor: more discriptive error when no liquidity in swaps (#5197)
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn authored and pysel committed Jun 6, 2023
1 parent 201ffd4 commit e93947e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
18 changes: 18 additions & 0 deletions x/concentrated-liquidity/swaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down
32 changes: 32 additions & 0 deletions x/concentrated-liquidity/swaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e93947e

Please sign in to comment.