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

refactor: more discriptive error when no liquidity in swaps #5197

Merged
merged 1 commit into from
May 17, 2023
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
18 changes: 18 additions & 0 deletions x/concentrated-liquidity/swaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()

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