π: Pancake new router is incompatible with Pancake LP implementation.
Users may get __revert__ txs upon __swaps__ due to the bug.
Vulnerability was reported to @PancakeSwap team asap.
Thread below π
2/ After @PancakeSwap V2 announcement, we have been working on supporting the new V2 pools.
β As with our security standard, we always perform smart contract review and testing, to ensure our usersβ safety.
βοΈ We have found and informed the @PancakeSwap team on the issue.
3/ π So, what is the actual bug?
The question comes down to: what changes from Pancake V1 to V2?
1οΈβ£ Swap fees: 0.2% β‘οΈ 0.25%
2οΈβ£ Fee distribution to LPs, to dev address
Below are the router __getAmountOut__ implementations (V1 & V2). The difference lies in the numbers.
4/ On the LP pair side, __swap__ function also encodes the swap fee for constant product calculation. Here are the V1 and V2 implementation:
5/ π At first glance, the implementation seems correct, as the parameters reflect 0.2% and 0.25% in the old and new versions, respectively.
However, in Solidity, there is no floating-point.
All divisions will be rounded down β¬οΈ.
6/ So, __balance0Adjusted__ and __balance1Adjusted__ can have their actual values truncated, reverting the next __require__ statement when checked for the constant product monovariant.
7/ Instead of performing divisions, itβs mostly always better to compare the cross-multiplication. This should be the correct implementation:
9/ So, the next question is: How does this affect #AlphaHomora?
#AlphaHomora performs multiple complex txs into one, including swaps. This means that users who open new positions, refill existing positions, and also liquidations may get revert txs.
10/ π¦ As liquidation is one of the core security features of #AlphaHomora to keep the protocol solvent, this bug can cause security loopholes in our system.
Even if the chances of tx reversions are not high, security is one of @AlphaFinanceLabβs top priorities.
11/ After weβve found the bug and made sure itβs reproducible, we immediately contacted @PancakeSwap team to inform them re:vulnerability.
They immediately responded and confirmed the vulnerability within minutes.
12/ To fix the issue, a new factory, router, and LP pairs need to be deployed.
Weβll be working towards supporting these new pools and again performing checks and tests on the new implementation.
3/ The most interesting feature is that users can supply liquidity to any desired price range, which then enables the __Concentrated Liquidity__ feature, as advertised.
π€ This is a challenge in terms of technical implementation, since gas limits may explode if done naively.
2/ Flash loan lets anyone borrow a very large amount of assets (as long as the amount + fee are paid back in the same transaction), which can be used to temporarily skew asset prices or balances.
3/ A typical flash loan attack pattern is a form of sandwich attack: (1) Skew price/balance (2) Main protocol uses the skewed price/balance (e.g. to open a position, deposit into AMM all at bad prices) (3) Un-skew price/balance