Skip to content

Commit

Permalink
extract invariant and range test changes from bugfix PR (#5561)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlpinYukseloglu authored Jun 18, 2023
1 parent 2f30676 commit ea5d6a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 11 additions & 9 deletions x/concentrated-liquidity/invariant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ func (s *KeeperTestSuite) assertTotalRewardsInvariant() {
totalCollectedIncentives = totalCollectedIncentives.Add(collectedIncentives...)
}

// We allow for an additive tolerance of 1 per position in the pool, since this is the maximum that can be truncated
// when collecting spread rewards.
// For global invariant checks, we simply ensure that any rounding error was in the pool's favor.
// This is to allow for cases where we slightly overround, which would otherwise fail here.
// TODO: create ErrTolerance type that allows for additive OR multiplicative tolerance to allow for
// tightening this check further.
errTolerance := osmomath.ErrTolerance{
AdditiveTolerance: sdk.NewDec(int64(len(allPositions))),
RoundingDir: osmomath.RoundDown,
RoundingDir: osmomath.RoundDown,
}

// Assert total collected spread rewards and incentives equal to expected
Expand Down Expand Up @@ -144,15 +145,16 @@ func (s *KeeperTestSuite) assertWithdrawAllInvariant() {
totalWithdrawn = totalWithdrawn.Add(withdrawn...)
}

// We allow for an additive tolerance of 1 per position in the pool, since this is the maximum that can be truncated
// when collecting spread rewards.
// For global invariant checks, we simply ensure that any rounding error was in the pool's favor.
// This is to allow for cases where we slightly overround, which would otherwise fail here.
// TODO: create ErrTolerance type that allows for additive OR multiplicative tolerance to allow for
// tightening this check further.
errTolerance := osmomath.ErrTolerance{
AdditiveTolerance: sdk.NewDec(int64(len(allPositions))),
RoundingDir: osmomath.RoundDown,
RoundingDir: osmomath.RoundDown,
}

// Assert total withdrawn assets equal to expected
s.Require().True(errTolerance.EqualCoins(expectedTotalWithdrawn, totalWithdrawn))
s.Require().True(errTolerance.EqualCoins(expectedTotalWithdrawn, totalWithdrawn), "expected withdrawn vs. actual: %s vs. %s", expectedTotalWithdrawn, totalWithdrawn)

// Refetch total pool balances across all pools
remainingPositions, finalTotalPoolAssets, remainingTotalSpreadRewards, remainingTotalIncentives := s.getAllPositionsAndPoolBalances(cachedCtx)
Expand Down
12 changes: 6 additions & 6 deletions x/concentrated-liquidity/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type RangeTestParams struct {
var (
DefaultRangeTestParams = RangeTestParams{
// Base amounts
baseNumPositions: 1000,
baseNumPositions: 10,
baseAssets: sdk.NewCoins(sdk.NewCoin(ETH, sdk.NewInt(5000000000)), sdk.NewCoin(USDC, sdk.NewInt(5000000000))),
baseTimeBetweenJoins: time.Hour,
baseSwapAmount: sdk.NewInt(10000000),
Expand Down Expand Up @@ -109,7 +109,8 @@ func (s *KeeperTestSuite) setupRangesAndAssertInvariants(pool types.Concentrated

// Set up assets for new position
curAssets := getRandomizedAssets(testParams.baseAssets, testParams.fuzzAssets)
s.FundAcc(curAddr, curAssets)
roundingError := sdk.NewCoins(sdk.NewCoin(pool.GetToken0(), sdk.OneInt()), sdk.NewCoin(pool.GetToken1(), sdk.OneInt()))
s.FundAcc(curAddr, curAssets.Add(roundingError...))

// Set up position
curPositionId, actualAmt0, actualAmt1, curLiquidity, actualLowerTick, actualUpperTick, err := s.clk.CreatePosition(s.Ctx, pool.GetId(), curAddr, curAssets, sdk.ZeroInt(), sdk.ZeroInt(), ranges[curRange][0], ranges[curRange][1])
Expand All @@ -122,7 +123,6 @@ func (s *KeeperTestSuite) setupRangesAndAssertInvariants(pool types.Concentrated

// Let time elapse after join if applicable
timeElapsed := s.addRandomizedBlockTime(testParams.baseTimeBetweenJoins, testParams.fuzzTimeBetweenJoins)
s.assertGlobalInvariants()

// Execute swap against pool if applicable
swappedIn, swappedOut := s.executeRandomizedSwap(pool, swapAddresses, testParams.baseSwapAmount, testParams.fuzzSwapAmounts)
Expand All @@ -149,7 +149,8 @@ func (s *KeeperTestSuite) setupRangesAndAssertInvariants(pool types.Concentrated

// Ensure the pool balance is exactly equal to the assets added + amount swapped in - amount swapped out
poolAssets := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetAddress())
s.Require().Equal(totalAssets, poolAssets)
poolSpreadRewards := s.App.BankKeeper.GetAllBalances(s.Ctx, pool.GetSpreadRewardsAddress())
s.Require().Equal(totalAssets, poolAssets.Add(poolSpreadRewards...))
}

// numPositionSlice prepares a slice tracking the number of positions to create on each range, fuzzing the number at each step if applicable.
Expand Down Expand Up @@ -187,7 +188,7 @@ func (s *KeeperTestSuite) prepareNumPositionSlice(ranges [][]int64, baseNumPosit
// TODO: Make swaps that target getting to a tick boundary exactly
func (s *KeeperTestSuite) executeRandomizedSwap(pool types.ConcentratedPoolExtension, swapAddresses []sdk.AccAddress, baseSwapAmount sdk.Int, fuzzSwap bool) (sdk.Coin, sdk.Coin) {
// Quietly skip if no swap assets or swap addresses provided
if baseSwapAmount.Equal(sdk.Int{}) || len(swapAddresses) == 0 {
if (baseSwapAmount == sdk.Int{}) || len(swapAddresses) == 0 {
return sdk.Coin{}, sdk.Coin{}
}

Expand Down Expand Up @@ -250,7 +251,6 @@ func (s *KeeperTestSuite) addRandomizedBlockTime(baseTimeToAdd time.Duration, fu
}

s.AddBlockTime(timeToAdd)

}

return baseTimeToAdd
Expand Down

0 comments on commit ea5d6a9

Please sign in to comment.