Submitted by carrotsmuggler, also found by 0xAlix2, Abdessamed, DadeKuma, DadeKuma, DadeKuma, gegul, LonnyFlash, and Tigerfrake
/contracts/pool-manager/src/manager/commands.rs#L75
The protocol allows the creation of constant product pools and stableswap pools. Stable-swap pools, as established by curve, can have any number of tokens and so the protocol allows for the creation of pools with 2 or more tokens.
Constant product market-makers (CPMM), however, can have multiple tokens as well; however, the protocol here uses the uniswap formula, which only works for 2-token pools. For pools with more than 2 tokens, this model does not work anymore, and invariants need to be established with different formulas with products of all tokens quantities, like shown in the balancer protocol.
The issue is that the protocol here does not check if the constant product pool being created has more than 2 tokens. Surprisingly, it is perfectly possible to create a constant product pool with 3 tokens, add/remove liquidity and even do swaps in them, even though the protocol was never designed to handle this.
The POC below will show how we can set up a 3-token CPMM pool, add liquidity and even do swaps in it. The issue is that these pools are completely broken and should not be allowed.
The compute_swap function in the helpers.rs contract calculates the number of output tokens given the number of input tokens.
But these are only valid for 2-token uniswap-style pools. If there are more than 2 tokens involved, the invariant changes from being x * y = k to x * y * z = k, and the formula above does not work anymore. So for multi token pools, this formula should not be used, or x-y swaps can be arbitraged off of with y-z swaps and vice versa.
Furthermore, there is a check in the assert_slippage_tolerance function in the helpers contract:
This explicitly shows that constant product pools are only allowed to have 2 tokens. However, if no slippage tolerance is specified, this check can be completely bypassed.
By never sending a slippage tolerance, users can create, add/remove liquidity and even do swaps in pools with more than 2 tokens following constant product algorithm. But these pools are completely broken and should not be allowed since the invariants are not functioning correctly
The POC below creates a CPMM pool with 3 tokens - uwhale, uluna and uusd. It is shown that liquidity can be added and swaps can be performed.
First, some helper functions are needed to check and print out the token balances.
And heres the actual test:
The test runs fine and heres the output:
It shows:
While the swap is correctly functioning here, it doesnt maintain the correct pool invariant and can be arbitraged off of when the pools grow imbalanced.
Add an explicit check during pool creation to make sure constant product pools cannot have more than 2 tokens.
jvr0x (MANTRA) confirmed
