Skip to content

Commit

Permalink
Revert "test(CL): rewards logic to work on the extended price range (#…
Browse files Browse the repository at this point in the history
…6328)"

This reverts commit 3b5d993.
  • Loading branch information
czarcas7ic committed Oct 12, 2023
1 parent c04ceaf commit b757353
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 169 deletions.
28 changes: 0 additions & 28 deletions x/concentrated-liquidity/incentives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/osmoutils/accum"
"github.com/osmosis-labs/osmosis/osmoutils/osmoassert"
cl "github.com/osmosis-labs/osmosis/v19/x/concentrated-liquidity"
"github.com/osmosis-labs/osmosis/v19/x/concentrated-liquidity/model"
"github.com/osmosis-labs/osmosis/v19/x/concentrated-liquidity/types"
Expand Down Expand Up @@ -3550,30 +3549,3 @@ func (s *KeeperTestSuite) TestGetIncentiveRecordSerialized() {
})
}
}

// This test validates that incentive rewards are collected without issues
// when positions are created over the new extended range.
func (s *KeeperTestSuite) TestCollectIncentives_MinSpotPriceMigration() {
s.SetupTest()

incentiveAmount := osmomath.NewInt(1000)
incentiveCoin := sdk.NewCoin(OSMO, incentiveAmount)
expectedTotalIncentiveRewards := sdk.NewCoins(incentiveCoin)
_, positions, _ := s.swapToMinTickAndBack(osmomath.ZeroDec(), expectedTotalIncentiveRewards)

actualCollected := sdk.NewCoins()

// Collect incentive rewards
for _, position := range positions {
collected, _, err := s.App.ConcentratedLiquidityKeeper.CollectIncentives(s.Ctx, s.TestAccs[0], position.ID)
s.Require().NoError(err)

actualCollected = actualCollected.Add(collected...)
}

// Validate that the total incentive rewards collected are equal to the expected total incentive rewards
s.Require().Equal(len(expectedTotalIncentiveRewards), len(actualCollected))
for _, coin := range expectedTotalIncentiveRewards {
osmoassert.Equal(s.T(), oneAdditiveToleranceRoundDown, coin.Amount, actualCollected.AmountOf(coin.Denom))
}
}
98 changes: 9 additions & 89 deletions x/concentrated-liquidity/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/osmoutils"
"github.com/osmosis-labs/osmosis/osmoutils/accum"
"github.com/osmosis-labs/osmosis/osmoutils/osmoassert"
concentrated_liquidity "github.com/osmosis-labs/osmosis/v19/x/concentrated-liquidity"
"github.com/osmosis-labs/osmosis/v19/x/concentrated-liquidity/clmocks"
"github.com/osmosis-labs/osmosis/v19/x/concentrated-liquidity/math"
Expand All @@ -26,11 +25,6 @@ import (
"github.com/osmosis-labs/osmosis/v19/app/apptesting"
)

const (
OSMO = "uosmo"
migrationTestTimeBetweenSwapsSecs = 10
)

var (
DefaultMinTick, DefaultMaxTick = types.MinInitializedTick, types.MaxTick
DefaultMinCurrentTick = types.MinCurrentTick
Expand Down Expand Up @@ -471,106 +465,32 @@ func (s *KeeperTestSuite) runMultiplePositionRanges(ranges [][]int64, rangeTestP
// creates a pososition from the first test account with default coins and no slippage protection
// for a given pool id and ticks.
// a convinience method for testing.
func (s *KeeperTestSuite) createDefaultPosition(poolId uint64, lowerTick, upperTick int64) cl.CreatePositionData {
func (s *KeeperTestSuite) createDefaultPosition(poolId uint64, lowerTick, upperTick int64) uint64 {
positionData, err := s.App.ConcentratedLiquidityKeeper.CreatePosition(s.Ctx, poolId, s.TestAccs[0], DefaultCoins, sdk.ZeroInt(), sdk.ZeroInt(), lowerTick, upperTick)
s.Require().NoError(err)
return positionData
return positionData.ID
}

// sets up positions for testing the migration of the min spot price from 10^-12 to 10^-30.
// Specifically, creates positions:
// - original full range
// - new extended full range
// - position between the new min spot price and the old min spot price
func (s *KeeperTestSuite) setupPositionsForMinSpotPriceMigration(spreadFactor osmomath.Dec) (poolId uint64, positions []cl.CreatePositionData) {
pool := s.PrepareCustomConcentratedPool(s.TestAccs[0], ETH, USDC, DefaultTickSpacing, spreadFactor)
poolId = pool.GetId()
func (s *KeeperTestSuite) setupPositionsForMinSpotPriceMigration() uint64 {
pool := s.PrepareConcentratedPool()
poolId := pool.GetId()

// Fund test account with tokens necessary for all positions
s.FundAcc(s.TestAccs[0], DefaultCoins.Add(DefaultCoins...).Add(DefaultCoins...))

// Setup an original full range position
position := s.createDefaultPosition(poolId, types.MinInitializedTick, types.MaxTick)
positions = append(positions, position)
s.createDefaultPosition(poolId, types.MinInitializedTick, types.MaxTick)

// Setup a full range position on the new min spot price
position = s.createDefaultPosition(poolId, types.MinInitializedTickV2, types.MaxTick)
positions = append(positions, position)
s.createDefaultPosition(poolId, types.MinInitializedTickV2, types.MaxTick)

// Setup a position between the new min spot price and the old min spot price
position = s.createDefaultPosition(poolId, types.MinInitializedTickV2+1000, types.MinInitializedTick-1000)
positions = append(positions, position)

return poolId, positions
}

// swaps to the lowered minimum tick of 10^-30 and back to the original tick which is in the positive range.
// There are 3 positions setup over the swapped range (see setupPositionsForMinSpotPriceMigration for details).
// Additionally, spread factor and incentive rewards are configurable as parameters.
// Returns pool id position data and actual tokens swapped in (in zero for one direction and back).
func (s *KeeperTestSuite) swapToMinTickAndBack(spreadFactor osmomath.Dec, incentiveRewards sdk.Coins) (poolId uint64, positions []cl.CreatePositionData, actualAmountsSwappedIn sdk.Coins) {
poolId, positions = s.setupPositionsForMinSpotPriceMigration(spreadFactor)

// Refetch pool
pool, err := s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, poolId)
s.Require().NoError(err)

incentiveCreator := s.TestAccs[2]
s.FundAcc(incentiveCreator, incentiveRewards)

// Create incentive rewards if desired
if !incentiveRewards.Empty() {
s.Require().Len(incentiveRewards, 1)
incentiveCoin := incentiveRewards[0]

_, err = s.App.ConcentratedLiquidityKeeper.CreateIncentive(
s.Ctx, poolId, s.TestAccs[2],
incentiveCoin, incentiveCoin.Amount.ToLegacyDec().Quo(osmomath.NewDec(migrationTestTimeBetweenSwapsSecs)), s.Ctx.BlockTime(), time.Nanosecond)
s.Require().NoError(err)
}

// esimate amount in to swap left all the way until the new min initialized tick
amountZeroIn, _, _ := s.computeSwapAmounts(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTickV2, true, false)

// Fund swapper
swapper := s.TestAccs[1]
coinZeroIn := sdk.NewCoin(pool.GetToken0(), amountZeroIn.TruncateInt())
s.FundAcc(swapper, sdk.NewCoins(coinZeroIn))

// perform the swap to the new min initialized tick.
actualSwappedInZeroForOne, tokenOut, _, err := s.App.ConcentratedLiquidityKeeper.SwapOutAmtGivenIn(
s.Ctx, swapper, pool,
coinZeroIn, pool.GetToken1(),
spreadFactor, osmomath.ZeroBigDec(),
)
s.Require().NoError(err)

// Increase time so that incentives are collected over the current position
// 10 seconds
s.AddBlockTime(time.Second * migrationTestTimeBetweenSwapsSecs)

// Refetch pool
pool, err = s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, poolId)
s.Require().NoError(err)

// Swap amount out to the end up in the original tick
actualSwappedInOneForZero, inverseTokenOut, _, err := s.App.ConcentratedLiquidityKeeper.SwapOutAmtGivenIn(
s.Ctx, swapper, pool,
tokenOut, pool.GetToken0(),
spreadFactor, osmomath.ZeroBigDec(),
)
s.Require().NoError(err)

tolerance := multiplicativeTolerance
// If spread factor is set, increase tolerance to account for it.
if !spreadFactor.IsZero() {
tolerance.MultiplicativeTolerance = osmomath.MustNewDecFromStr("0.001")
}

// Original amount in should roughly equal the amount out when performing the inverse swap
osmoassert.Equal(s.T(), tolerance, coinZeroIn.Amount, inverseTokenOut.Amount)

actualAmountsSwappedIn = append(actualAmountsSwappedIn, actualSwappedInZeroForOne, actualSwappedInOneForZero)
s.createDefaultPosition(poolId, types.MinInitializedTickV2+1000, types.MinInitializedTick-1000)

return poolId, positions, actualAmountsSwappedIn
return poolId
}
41 changes: 0 additions & 41 deletions x/concentrated-liquidity/spread_rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var (
oneEthCoins = sdk.NewDecCoins(oneEth)
onlyUSDC = [][]string{{USDC}, {USDC}, {USDC}, {USDC}}
onlyETH = [][]string{{ETH}, {ETH}, {ETH}, {ETH}}
emptyCoins = sdk.NewCoins()
)

func (s *KeeperTestSuite) TestCreateAndGetSpreadRewardAccumulator() {
Expand Down Expand Up @@ -1479,46 +1478,6 @@ func (s *KeeperTestSuite) TestFunctional_SpreadRewards_LP() {
s.Require().Equal(sdk.Coins{}, collectedThree)
}

// This test validates that spread rewards are collected without issues
// when positions are created over the new extended range.
func (s *KeeperTestSuite) TestCollectSpreadRewards_MinSpotPriceMigration() {
s.SetupTest()

spreadFactor := types.AuthorizedSpreadFactors[1]
s.Require().False(spreadFactor.IsZero())

poolId, positions, coinsSwappedIn := s.swapToMinTickAndBack(spreadFactor, emptyCoins)

s.Require().Len(coinsSwappedIn, 2)
tokenInZeroForOne := coinsSwappedIn[0]
tokenInOneForZero := coinsSwappedIn[1]

// fetch pool
pool, err := s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, poolId)
s.Require().NoError(err)

expectedTotalSpreadRewards := sdk.NewCoins(
sdk.NewCoin(pool.GetToken0(), tokenInZeroForOne.Amount.ToLegacyDec().Mul(spreadFactor).TruncateInt()),
sdk.NewCoin(pool.GetToken1(), tokenInOneForZero.Amount.ToLegacyDec().Mul(spreadFactor).TruncateInt()),
)

actualCollected := sdk.NewCoins()

// Collect spread rewards
for _, position := range positions {
collected, err := s.App.ConcentratedLiquidityKeeper.CollectSpreadRewards(s.Ctx, s.TestAccs[0], position.ID)
s.Require().NoError(err)

actualCollected = actualCollected.Add(collected...)
}

// Validate that the total spread rewards collected is equal to the expected total spread rewards
s.Require().Equal(len(expectedTotalSpreadRewards), len(actualCollected))
for _, coin := range expectedTotalSpreadRewards {
osmoassert.Equal(s.T(), oneAdditiveTolerance, coin.Amount, actualCollected.AmountOf(coin.Denom))
}
}

// CollectAndAssertSpreadRewards collects spread rewards from a given pool for all positions and verifies that the total spread rewards collected match the expected total spread rewards.
// The method also checks that if the ticks that were active during the swap lie within the range of a position, then the position's spread reward accumulators
// are not empty. The total spread rewards collected are compared to the expected total spread rewards within an additive tolerance defined by an error tolerance struct.
Expand Down
58 changes: 47 additions & 11 deletions x/concentrated-liquidity/swaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ var (
oneAdditiveTolerance = osmomath.ErrTolerance{
AdditiveTolerance: osmomath.OneDec(),
}

oneAdditiveToleranceRoundDown = osmomath.ErrTolerance{
AdditiveTolerance: osmomath.OneDec(),
RoundingDir: osmomath.RoundDown,
}

swapOutGivenInCases = map[string]SwapTest{
// One price range
//
Expand Down Expand Up @@ -3518,17 +3512,59 @@ func (s *KeeperTestSuite) TestComputeMaxInAmtGivenMaxTicksCrossed() {
// or panics when swapping to the new min tick and back.
// Additionally, it validates that the swap amounts are roughly equal to the inverse amounts of a given swap.
func (s *KeeperTestSuite) TestSwap_MinSpotPriceMigration() {

errTolerance := osmomath.ErrTolerance{
MultiplicativeTolerance: osmomath.MustNewDecFromStr("0.001"),
}

s.Run("out given in", func() {
s.SetupTest()
// Validated by the helper method.
// This helper is reused in other more complex tests.
s.swapToMinTickAndBack(osmomath.ZeroDec(), emptyCoins)

poolId := s.setupPositionsForMinSpotPriceMigration()

// Refetch pool
pool, err := s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, poolId)
s.Require().NoError(err)

// esimate amount in to swap left all the way until the new min initialized tick
amountZeroIn, _, _ := s.computeSwapAmounts(poolId, pool.GetCurrentSqrtPrice(), types.MinInitializedTickV2, true, false)

// Fund swapper
swapper := s.TestAccs[1]
coinZeroIn := sdk.NewCoin(pool.GetToken0(), amountZeroIn.TruncateInt())
s.FundAcc(swapper, sdk.NewCoins(coinZeroIn))

// perform the swap to the new min initialized tick.
_, tokenOut, _, err := s.App.ConcentratedLiquidityKeeper.SwapOutAmtGivenIn(
s.Ctx, swapper, pool,
coinZeroIn, pool.GetToken1(),
osmomath.ZeroDec(), osmomath.ZeroBigDec(),
)
s.Require().NoError(err)

// Refetch pool
pool, err = s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, poolId)
s.Require().NoError(err)

// Confirm all liquidity was consumed and `MinCurrentTick` set
s.Require().Equal(types.MinCurrentTick, pool.GetCurrentTick())

// Swap amount out to the end up in the original tick
_, inverseTokenOut, _, err := s.App.ConcentratedLiquidityKeeper.SwapOutAmtGivenIn(
s.Ctx, swapper, pool,
tokenOut, pool.GetToken0(),
osmomath.ZeroDec(), osmomath.ZeroBigDec(),
)
s.Require().NoError(err)

// Original amount in should roughly equal the amount out when performing the inverse swap
s.Require().Equal(0, errTolerance.Compare(coinZeroIn.Amount, inverseTokenOut.Amount), "expected: %s, got: %s", coinZeroIn.Amount, inverseTokenOut.Amount)
})

s.Run("in given out", func() {
s.SetupTest()

poolId, _ := s.setupPositionsForMinSpotPriceMigration(osmomath.ZeroDec())
poolId := s.setupPositionsForMinSpotPriceMigration()

// Refetch pool
pool, err := s.App.ConcentratedLiquidityKeeper.GetPoolById(s.Ctx, poolId)
Expand Down Expand Up @@ -3575,7 +3611,7 @@ func (s *KeeperTestSuite) TestSwap_MinSpotPriceMigration() {
s.Require().NoError(err)

// Original amount in should roughly equal the amount out when performing the inverse swap
osmoassert.Equal(s.T(), multiplicativeTolerance, coinOneOut.Amount, inverseTokenOut.Amount)
s.Require().Equal(0, errTolerance.Compare(coinOneOut.Amount, inverseTokenOut.Amount), "expected: %s, got: %s", coinOneOut.Amount, inverseTokenOut.Amount)
})
}

Expand Down

0 comments on commit b757353

Please sign in to comment.