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

UniswapV3Pool::simulate_swap_mut overdrafts pools #110

Open
fabianbergmark opened this issue Nov 27, 2023 · 6 comments
Open

UniswapV3Pool::simulate_swap_mut overdrafts pools #110

fabianbergmark opened this issue Nov 27, 2023 · 6 comments

Comments

@fabianbergmark
Copy link

amms UniswapV3Pool::simulate_swap_mut succeeds to swap from IC to Weth on a pool with very small weth balance (0x30C777EFb634759D235F3aAdBf562a4945ec7F6a), where output amount is larger than the current weth balance of the pool.

Is there a way I can verify that this is not a sync issue by inspection of the UniswapV3Pool struct?

@0xOsiris
Copy link
Member

0xOsiris commented Dec 5, 2023

Can you share the quantities and block you are simulating on. Maybe write an example test of the scenario?

@fabianbergmark
Copy link
Author

fabianbergmark commented Jan 26, 2024

Before I dig deeper, I suspect this might be caused by an issue with how I sync the pool state from logs. I'm running a geth node, which iiuc is sufficient for v2pools but maybe not v3?

Alternatively it could be due to #105. What would be needed to support syncing v3 from a checkpoint?

@0xOsiris
Copy link
Member

Before I dig deeper, I suspect this might be caused by an issue with how I sync the pool state from logs. I'm running a geth node, which iiuc is sufficient for v2pools but maybe not v3?

Alternatively it could be due to #105. What would be needed to support syncing v3 from a checkpoint?

IIRC we would need to synchronize the Tick bitmaps of the v3 pools by syncing the mint/burn logs from the last synced block on the checkpoint to the chain head. Which is not currently implemented. So, the liquidity positions might be outdated when pulling from a checkpoint, and therefore could lead to overflow/underflow when syncing the mint/burn logs from the chain head onwards.

@fabianbergmark
Copy link
Author

I've been doing some digging into a specific contract: 0xbbd2d2f1c729156f128bd2401477e59256d4da9b.
The reserves I log is from calculate_virtual_reserves. What I find strange is that the last burn event doesn't affect the reserves at all.

19136418 creation block
    
19136418 mint event MintFilter { sender: 0xc36442b4a4522e871399cd717abdd847ab11fe88, owner: 0xc36442b4a4522e871399cd717abdd847ab11fe88, tick_lower: 280320, tick_upper: 280380, amount: 6814308178012212284, amount_0: 0, amount_1: 24999999999999999998302 }
reserves before log Err(UniswapV3MathError(R))
reserves after log Err(UniswapV3MathError(R))

swap event SwapFilter { sender: 0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad, recipient: 0xab491a6cced1c277ae3f2ddba0995a1c7ca5ece3, amount_0: 8057809913, amount_1: -12034426644211325774520, sqrt_price_x96: 96899797195315228613501546353885045, liquidity: 6814308178012212284, tick: 280351 }
reserves before log Err(UniswapV3MathError(R))
reserves after log Ok((5571620438407, 8334163545102383462135234))

19137748 swap event SwapFilter { sender: 0x0b8a49d816cc709b6eadb09498030ae3416b66dc, recipient: 0xa88800cd213da5ae406ce248380802bd53b47647, amount_0: 1030384105, amount_1: -1536387654224908997417, sqrt_price_x96: 96881934021596932143520717412713476, liquidity: 6814308178012212284, tick: 280347 }
reserves before log Ok((5571620438407, 8334163545102383462135234))
reserves after log Ok((5572734818211, 8332496962384934696093510))

19137749 swap event SwapFilter { sender: 0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad, recipient: 0xa8961eb3bfb918edd20d1b81678a7855fae77f16, amount_0: 5751770283, amount_1: -8565967860927147218039, sqrt_price_x96: 96782339774308945015944204737030145, liquidity: 6814308178012212284, tick: 280326 }
reserves before log Ok((5572734818211, 8332496962384934696093510))
reserves after log Ok((5578588969959, 8323752869224038604935005))

19137775 swap event SwapFilter { sender: 0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad, recipient: 0x848650cd11e3f3e2d69ab421c3eae75ac7b80211, amount_0: 1838765148, amount_1: -2734713200654965485608, sqrt_price_x96: 96750543985766461035989159181923180, liquidity: 6814308178012212284, tick: 280320 }
reserves before log Ok((5578588969959, 8323752869224038604935005))
reserves after log Ok((5580262714013, 8321256242705219796492288))

19138212 swap event SwapFilter { sender: 0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad, recipient: 0x6b6cfdf92a4bf115b56b542bf59160b62e4a55ac, amount_0: 21000000, amount_1: -31221983305126378005, sqrt_price_x96: 96750180976007774456156643280560554, liquidity: 6814308178012212284, tick: 280320 }
reserves before log Ok((5580262714013, 8321256242705219796492288))
reserves after log Ok((5580262714013, 8321256242705219796492288))

19138858 swap event SwapFilter { sender: 0x01bd2da640345f1c29831b7cef9a434298408172, recipient: 0xdf71525e5c7435ef860909547d1589e53c004f03, amount_0: 65433618, amount_1: -97282655966123558516, sqrt_price_x96: 96749049896245110764756950763719153, liquidity: 6814308178012212284, tick: 280320 }
reserves before log Ok((5580262714013, 8321256242705219796492288))
reserves after log Ok((5580262714013, 8321256242705219796492288))

19140957 burn event BurnFilter { owner: 0xc36442b4a4522e871399cd717abdd847ab11fe88, tick_lower: 280320, tick_upper: 280380, amount: 6814308178012212284, amount_0: 16714867575, amount_1: 710402586194 }
reserves before log Ok((5580262714013, 8321256242705219796492288))
reserves after log Ok((5580262714013, 8321256242705219796492288))

@fabianbergmark
Copy link
Author

Could it be that this check should be using >= and <=?

if self.tick > tick_lower && self.tick < tick_upper {

@0xOsiris
Copy link
Member

hmm, can you share a repro?

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

No branches or pull requests

2 participants