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: update quoter #32

Merged
merged 2 commits into from
Oct 15, 2024
Merged

refactor: update quoter #32

merged 2 commits into from
Oct 15, 2024

Conversation

chefburger
Copy link
Contributor

@chefburger chefburger commented Oct 13, 2024

This PR mainly focuses on updating the interface for quoters of both pool types including mixQuoter. Key points:

  1. instead of return BalanceDelta now quoter only returns the output for exactIn* and input for exactOut*
  2. no more pool state variable returned by quoter, for example activeBinAfterSwap for BinPool, tickCrossed for CLPool
  3. a rough gas estimation is returned (warning: this could still be a bit lower than actual cost since only the gas for swap calculation is included but the gas cost for token transfer is not taken into account

// int128 amountSpecifiedActual = (zeroForOne == (amountSpecified < 0)) ? deltas.amount0() : deltas.amount1();
// if (amountSpecifiedActual != amountSpecified) {
// revert NotEnoughLiquidity(poolKey.toId());
// }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove comments once confirmed

Copy link
Collaborator

@ChefMist ChefMist Oct 14, 2024

Choose a reason for hiding this comment

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

is this no longer applicable? https://github.com/pancakeswap/pancake-v4-periphery/blob/main/src/pool-bin/lens/BinQuoter.sol#L223-L226

or is it because BinPool will fire BinPool__OutOfLiquidity, so we no longer need to check here?

Copy link
Contributor Author

@chefburger chefburger Oct 14, 2024

Choose a reason for hiding this comment

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

  1. is this no longer applicable? https://github.com/pancakeswap/pancake-v4-periphery/blob/main/src/pool-bin/lens/BinQuoter.sol#L223-L226

Yes, i suspect in previous version can this even be triggered

  1. or is it because BinPool will fire BinPool__OutOfLiquidity, so we no longer need to check here?

Yes, can imagine if amountOut is less than expected then it must because of the illiquidity of the pool. Then it should revert with BinPool__OutOfLiquidity before the check

Copy link
Contributor Author

@chefburger chefburger Oct 14, 2024

Choose a reason for hiding this comment

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

Oh, it just hit me hook might have influence on this. For example, imagine we have stable swap implemented in hook contract. Let add it anyway, wdyt ? 😂

Copy link
Contributor

Choose a reason for hiding this comment

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

agree

uint160 sqrtPriceLimitX96;
bytes hookData;
}
error NotEnoughLiquidity(PoolId poolId);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is put here i.e. ICLQuoter instead of IQuoter because for BinPool this is gonna be error BinPool__OutOfLiquidity();

(uint256 reserveIn, uint256 reserveOut) =
V3SmartRouterHelper.getReserves(factoryV2, params.tokenIn, params.tokenOut);
amountOut = V3SmartRouterHelper.getAmountOut(params.amountIn, reserveIn, reserveOut);
gasEstimate = gasBefore - gasleft();
}
Copy link
Contributor Author

@chefburger chefburger Oct 13, 2024

Choose a reason for hiding this comment

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

Head up: this could be inaccurate since actual swap is not performed. Feel free to add comment if you guys have better solution

Copy link
Collaborator

Choose a reason for hiding this comment

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

lets create GH issue on this and take it from there.

  1. Fixing it in this PR will expand the scope
  2. its used by FE so we can eventually do a MixedQuoterV2 with proper gas estimate for v2 and stableswap

try vault.lock(abi.encodeCall(this._quoteExactInputSingle, (params))) {}
catch (bytes memory reason) {
return _handleRevertSingle(reason);
gasEstimate = gasBefore - gasleft();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The gas estimation is accurate than v2/stableSwap because of try-revert manner but still not 100% accurate because in real trade there would be some periphery logic & token settement

ChefMist
ChefMist previously approved these changes Oct 14, 2024
@chefburger
Copy link
Contributor Author

@ChefMist @ChefSnoopy Can u guys recheck and approve again.. Added back illiquidity check for binPool

/// @dev Check that the pool was not illiquid
/// even BinPool will emit BinPool__OutOfLiquidity when the pool is illiquid
/// We still need to apply the check in case hook contract manipulates the delta
int128 amountSpecifiedActual = (zeroForOne == (amountSpecified < 0)) ? deltas.amount0() : deltas.amount1();
Copy link
Collaborator

Choose a reason for hiding this comment

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

im curious, would this means basically if the pool has hook which charge a fee or take tokenIn for swap, quoter will fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not exactly, this will fail only when the hook does not respect the amountSpecified. For example, for exactInput the pool with hook require more/less input token or for exactOutput the pool with hook return more/less token.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Revisited this: it seems amountSpecified can never be manipulated through hook i.e. delta.tokenOnSpecifiedSide() will always equal to amountSpecified for binPool. I start to thinking the check here is unnecessary again 😂

CleanShot 2024-10-15 at 11 21 54@2x

For CLPool, the reason why it's needed is because CLPool#swap (step2 in the screenshot) support partial fulfilled which could be BinPool__OutOfLiquidity for binPool.

Copy link
Collaborator

@ChefMist ChefMist Oct 15, 2024

Choose a reason for hiding this comment

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

got it, thx. feel free to remove that or keep that (no harm i gues) since the previous implementation included that check

@chefburger chefburger merged commit 6e2ac42 into main Oct 15, 2024
2 checks passed
@chefburger chefburger deleted the refactor/quoter branch October 15, 2024 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants