QuantAMMMathGuard._guardQuantammWeightsdoesn't clamp weights between min and max as intended due to incorrect clamping logic
Let's take a look at QuantAMMMathGuard._clampWeights implementation
Clamping logic was written very strangely. It works for two-token case but can fail for three or more token case.
For example, consider the following scenario:
absoluteMin= 0.2
weightLength= 3
weights = [0.189, 0.61, 0.201]
Let's calculate update weights step-by-step:
sumRemainerWeight = 0.8because we only have one absoluteMin violation (0.189)
sumOtherWeights = 0.6because we have just one absoluteMax violation (0.61)
propertionalRemainer = 0.8 / 0.6 = 1.3333
And updated weights will be
weights[0] = 0.2 (clamped to absoluteMin
weights[1] = 0.6 * 1.3333 = 0.8(previously clamped to absoluteMaxbut later multiplied by proportionalRemainder
weights[2] = 0.201 * 1.3333 = 0.268(multiplied by proportionalRemainder
So situation got worse by _clampWeights
Now we have to apply the following from _normalizeWeightUpdates:
And the situation gets even worse, because weights[0]will be a negative value:
So the final result will be [-0.68, 0.8, 0.268], which made situation even worse in despite of the guard's original intention
Note
The following comment is absolutely wrong because guard rail can be broken up to ONE, not up to 1e-18
// There might a very small (1e-18) rounding error, add this to the first element.
// In some edge cases, this might break a guard rail, but only by 1e-18, which is modelled to be acceptable.
Put the following code snippet to MathGuard.t.soland run forge test MathGuard.t.sol --match-test testWeightDistortion -vv
Console Output:
Incorrect weight calculation
Incorrect multiplier calculation
Foundry
Revisit clamping logic.
Likelihood: Medium/High, when a weight is above absoluteMax. Impact: Low/Medium, weights deviate much faster, and sum of weights also.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.