Open code423n4 opened 2 years ago
@0xSSDD - please review.
@0xSSDD @0xean Can I get confirmation on your review?
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.
On Feb 5, 2022, at 6:44 AM, Alex The Entreprenerd @.***> wrote:
@0xSSDD @0xean Can I get confirmation on your review?
— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you were mentioned.
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
closed mistakenly due to linked PR - will leave open for judging report.
Handle
WatchPug
Vulnerability details
Current Implementation
When
baseToken
rebase upPer 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 whenaddLiquidity()
(ΔRo) to the new totalSupply (total shares = Ro' = Ro + ΔRo).In the current implementation:
ΔY is the
quoteToken
added by the new liquidity provider. See:Y' is the new Y after
addLiquidity()
,Y' = Y + ΔY
. See:ΔX is
ΔY * Omega
. See:α^ is
Alpha - X
. See:For instance:
Given:
baseToken
rebase up: Alpha becomes 10When: new liquidity provider
addLiquidity()
with 4 quoteToken:After
addLiquidity()
:quoteToken
, the total value is: 160 / 90 / Omega + 80 / 90 = 240 / 90 = 2.6666666666666665As 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 downPer 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 whenaddLiquidity()
(ΔRo) to the new totalSupply (total shares = Ro' = Ro + ΔRo).In the current implementation:
Δ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:β^ is maxΔX / Omega, the value of maxΔX in the terms of
quoteToken
.maxΔX = X - Alpha
. See:For instance:
Given:
baseToken
rebase down, Alpha becomes 1When: new liquidity provider
addLiquidity()
with4 baseToken
After
addLiquidity()
: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
When baseToken rebase up
When baseToken rebase down
Recommendation
Update code and document using the correct formula for ΔRo.