Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Question: Slight difference in simulated swap out amounts #93

Open
arb000r opened this issue Jun 6, 2023 · 4 comments
Open

Question: Slight difference in simulated swap out amounts #93

arb000r opened this issue Jun 6, 2023 · 4 comments

Comments

@arb000r
Copy link

arb000r commented Jun 6, 2023

Hey.

Thanks for the amazing effort on this library!
When doing some comparisons to Swap events outputted for an arbitrary pair (for this example I am using 500 fee tier WETH/USDC on Optimism), I notice a very small margin of error from the output of the simulation vs what the swap log shows.

The negative amount 0 representing the output of the swap from logs.
'Amount out from internal swap' representing the result of the simulated swap.

I thought it could be due to a difference in pool state after the swap log, as the batch tick data call would hit the chain AFTER the swap log. However, I switched to a completely pool 'state' which is initialised with tick data in either direction and altered the simulation to use that data, there is still a small error. See DIFF. Is this due to just precision difference between EVM and Rust?

Am able to get 0 DIFF when the output is USDC sometimes...

2023-06-06T08:19:50.046Z INFO  [verdant] Found a swap event 0x68df429f0050417cac5ebb407bbe2adcbe6aab990d9d7f65d8bc2a93f08695fe. Running sims.
2023-06-06T08:19:50.046Z INFO  [cfmms::pool::uniswap_v3] SQRT PRICE FROM LOG 3374780273765172741931233
2023-06-06T08:19:50.046Z INFO  [cfmms::pool::uniswap_v3] AMOUNT 0 "-1472873957491611"
2023-06-06T08:19:50.046Z INFO  [cfmms::pool::uniswap_v3] AMOUNT 1 "2673712"
2023-06-06T08:19:50.047Z INFO  [cfmms::pool::uniswap_v3] Current tick -201286 false
2023-06-06T08:19:50.048Z INFO  [cfmms::pool::uniswap_v3] AMOUNT OUT FROM INTERNAL SWAP "1472873826317980"
2023-06-06T08:19:50.049Z INFO  [cfmms::pool::uniswap_v3] DIFF "0.00000008905964446774203"```
@0xKitsune
Copy link
Owner

Hey thanks for opening up an issue for this. There is an issue with the UniswapV3 swap sim that this PR should fix. There are still a few outstanding issues with the PR, but I am going to merge it into a staging branch and fix them quickly so we can get this patched up.

I will bolster the tests and make sure that this patch addresses the issue.

@arb000r
Copy link
Author

arb000r commented Jun 12, 2023

Thanks.

@arb000r
Copy link
Author

arb000r commented Jun 19, 2023

@0xKitsune Can I help in any way to get that fix pushed out?

@0xKitsune
Copy link
Owner

0xKitsune commented Jun 19, 2023

Thanks for following up on this. I just merged the changes into main that should fix UniswapV3 swap simulation. We will need to bolster tests and validate that these changes correct any issues.

If you would like, you can help by updating the swap simulation tests to be more robust, ensuring that swap simulation is accurate. Once we have validated that the changes fix everything, I can cut a new release on crates.io.

My workload has been high lately so sorry for the delay on this. I will be working to get the tests updated and the new version published, but any help would be greatly appreciated. Let me know if you have any questions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants