In the case of Single Asset Entry, new liquidity providers will suffer fund loss due to wrong formula of ΔRo
The formula for calculating the shares (ΔRo) a new liquidity provider receives when adding liquidity to a single-asset pool is incorrect, causing the provider to receive fewer shares than they should. An attacker could exploit this to cause fund loss to new liquidity providers, or new providers would simply lose funds.
Submitted by WatchPug
Current Implementation
When baseToken rebase up
Per the document: https://github.com/ElasticSwap/elasticswap/blob/a90bb67e2817d892b517da6c1ba6fae5303e9867/ElasticSwapMath.md#:~:text=When%20there%20is%20alphaDecay
and related code: https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L227-L283
Gamma is the ratio of shares received by the new liquidity provider when addLiquidity() (ΔRo) to the new totalSupply (total shares = Ro’ = Ro + ΔRo).
ΔRo = (Ro/(1 - γ)) * γ
Ro * Gamma
= --------------
1 - Gamma
⟺
ΔRo * ( 1 - Gamma ) = Gamma * Ro
ΔRo - Gamma * ΔRo = Gamma * Ro
ΔRo = Gamma * Ro + Gamma * ΔRo
ΔRo
Gamma = ---------
Ro + ΔRo
In the current implementation:
γ = ΔY / Y' / 2 * ( ΔX / α^ )
ΔY is the quoteToken added by the new liquidity provider. See:
Y’ is the new Y after addLiquidity(), Y' = Y + ΔY. See:
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L272
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L278
ΔX is ΔY * Omega. See:
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L259-L263
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L279
α^ is Alpha - X. See:
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L234-L235
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L280
For instance:
Given:
- Original State: X = Alpha = 1, Y = Beta = 1, Omega = X/Y = 1
- When
baseTokenrebase up: Alpha becomes 10 - Current State: Alpha = 10, X = 1, Y = Beta = 1, Omega = 1
When: new liquidity provider addLiquidity() with 4 quoteToken:
4 4 * Omega 16
Gamma = ------------ * ------------ = ----
(1+4) * 2 10 - 1 90
After addLiquidity():
- baseToken belongs to the newLP: 10 * 16 / 90 = 160 / 90 = 1.7777777777777777
- quoteToken belongs to the newLP: (1+4) * 16 / 90 = 80 / 90 = 0.8888888888888888
- In the terms of
quoteToken, the total value is: 160 / 90 / Omega + 80 / 90 = 240 / 90 = 2.6666666666666665
As a result, the new liquidity provider suffers a fund loss of 4 - 240 / 90 = 1.3333333333333333 in the terms of quoteToken
The case above can be reproduced by changing the numbers in this test unit.
When baseToken rebase down
Per the document: https://github.com/ElasticSwap/elasticswap/blob/a90bb67e2817d892b517da6c1ba6fae5303e9867/ElasticSwapMath.md#:~:text=When%20there%20is%20betaDecay
and related code: https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L297-L363
Gamma is the ratio of shares received by the new liquidity provider when addLiquidity() (ΔRo) to the new totalSupply (total shares = Ro’ = Ro + ΔRo).
ΔRo = (Ro/(1 - γ)) * γ
Ro * Gamma
= --------------
1 - Gamma
⟺
ΔRo * ( 1 - Gamma ) = Gamma * Ro
ΔRo - Gamma * ΔRo = Gamma * Ro
ΔRo = Gamma * Ro + Gamma * ΔRo
ΔRo
Gamma = ---------
Ro + ΔRo
In the current implementation:
γ = ΔX / X / 2 * ( ΔXByQuoteTokenAmount / β^ )
ΔX is the amount of baseToken added by the new liquidity provider. See:
X is the balanceOf baseToken. See:
ΔXByQuoteTokenAmount is ΔX / Omega, the value of ΔX in the terms of quoteToken. See:
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L318-L322
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L329-L333
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L359
β^ is maxΔX / Omega, the value of maxΔX in the terms of quoteToken. maxΔX = X - Alpha. See:
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L304-L305
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L318-L322
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L341-L342
- https://github.com/code-423n4/2022-01-elasticswap/blob/d107a198c0d10fbe254d69ffe5be3e40894ff078/elasticswap/src/libraries/MathLib.sol#L360
For instance:
Given:
- Original State: X = Alpha = 10, Y = Beta = 10, Omega = X/Y = 1
- When
baseTokenrebase down, Alpha becomes 1 - Current State: Alpha = 1, X = 10, Y = Beta = 10, Omega = 1
When: new liquidity provider addLiquidity() with 4 baseToken
4 4 / Omega 8
Gamma = -------- * ---------------- = ----
10 * 2 (10-1) / Omega 90
After addLiquidity():
- baseToken belongs to the newLP: (1 + 4) * 8 / 90 = 40 / 90 = 0.4444444444444444
- quoteToken belongs to the newLP: 10 * 8 / 90 = 80 / 90 = 0.8888888888888888
- In the terms of quoteToken, the total value is: 40 / 90 + 80 / 90 * Omega = 120 / 90 = 1.3333333333333333 < 4
As a result, the new liquidity provider suffers a fund loss of 4 - 120 / 90 = 2.6666666666666665 in the terms of quoteToken
The case above can be reproduced by changing the numbers in this test unit.
The correct formula for ΔRo
Recommendation
Update code and document using the correct formula for ΔRo.
0xean (ElasticSwap) confirmed and commented:
Finding is valid - solution seems to be partially correct and we are working on the fully correct version.
It seems that the suggested formula doesn’t cover a rebase down correctly and this is where our efforts are focused now.
Alex the Entreprenerd (judge) commented:
The warden has identified an issue with the math that reliably will provide a less-than-expected value to single-sided liquidity providers. The warden showed a consistent way for this to occur and while the recommended fix may not be completely correct, I believe the finding to be valid.
Because the warden found a set of cases that reliably make the protocol return less value than expected when compared to the goals of the protocol, I believe High Severity to be appropriate.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the formula for ΔRo (shares received) uses an incorrect expression for Gamma, the ratio of shares to total shares, leading to under-allocation of shares to new liquidity providers.
Impact
New liquidity providers could suffer fund loss when adding liquidity after a rebase, as the incorrect formula gives them fewer shares than they should receive.
What the attacker needed
A rebase event occurs; new liquidity provider adds liquidity
The fix
The code and document should be updated using the correct formula for ΔRo, though the exact correct formula is not fully provided in the text.
How it was found
- Set up a pool with initial state where baseToken and quoteToken have certain balances and Alpha equals X.
- Trigger a rebase up of baseToken so that Alpha becomes larger than X.
- Have a new liquidity provider call addLiquidity() with a certain amount of quoteToken.
- Observe that the new provider receives fewer shares than expected, resulting in a fund loss.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
In a liquidity pool, people deposit tokens and get shares representing their portion. When someone adds tokens, the pool calculates how many new shares to give them. This report shows that the calculation is wrong, so the new person gets too few shares and loses money.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.