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

add swap to price tests #904

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

add swap to price tests #904

wants to merge 4 commits into from

Conversation

snreynolds
Copy link
Member

Related Issue

Which issue does this pull request resolve?

Description of changes

@snreynolds snreynolds requested a review from a team as a code owner October 15, 2024 16:52
saucepoint
saucepoint previously approved these changes Oct 15, 2024
Copy link
Collaborator

@saucepoint saucepoint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fuzz test where the starting pool price is fuzzed would be cool -- but this looks good enough to me haha

@@ -808,6 +808,47 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
assertEq(manager.protocolFeesAccrued(currency1), expectedProtocolFee);
}

function test_swap_toLiquidity_fromMinPrice() public {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth naming this one withLiquidity and the other one withoutLiquidity?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think toLiquidity is accurate because this test traverses the swap until reaching nonzero liquidity. The relevant limit is the output amount, whereas in the other test the relevant limit is the price.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(can still include withoutLiquidity in the other test name)

modifyLiquidityRouter.modifyLiquidity(_key, params, ZERO_BYTES);

// zeroForOne=false to swap higher
IPoolManager.SwapParams memory swapParams = IPoolManager.SwapParams(false, 1, TickMath.MAX_SQRT_PRICE - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you instead used -1 for amountSpecified, I think this test could be more precise about the final price:

assertEq(sqrtPriceX96, TickMath.getSqrtPriceAtTick(-10));

(sqrtPriceX96,,,) = manager.getSlot0(_key.toId());

// The swap pushes the price to the sqrtPriceLimit.
assertEq(TickMath.getTickAtSqrtPrice(SQRT_PRICE_1_4), TickMath.getTickAtSqrtPrice(sqrtPriceX96));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird that this is testing by tick rather than by price. Shouldn't this test instead verify that the limit price is equal to the final price?

assertEq(SQRT_PRICE_1_4, sqrtPriceX96);

} else {
// the price is between the position, so let's just swap down
zeroForOne = true;
sqrtPriceX96Limit = TickMath.MIN_SQRT_PRICE + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the case that's failing the fuzz test


modifyLiquidityRouter.modifyLiquidity(_key, params, ZERO_BYTES);

IPoolManager.SwapParams memory swapParams = IPoolManager.SwapParams(zeroForOne, 1, sqrtPriceX96Limit);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failure happens because the price does move slightly. It's not ideal for the test to fail probabilistically. It fails because the getTickAtSqrtPrice flips from 0 to -1.

I think that case would pass if this amountSpecified was -1, because that amount would become fee and not move the price.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I meant to change to -1 thanks!

@hensha256 hensha256 added the tests label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants