Swaps are not split when trade crosses target price
The swap function does not split trades that cross the target price, instead using the new amplifier for the entire trade. This can lead to a worse average execution price and potentially allow a trader to profit by exploiting the two amplifiers.
Submitted by cmichel, also found by gzeon
The protocol uses two amplifier values A1 and A2 for the swap, depending on the target price, see SwapUtils.determineA. The swap curve is therefore a join of two different curves at the target price. When doing a trade that crosses the target price, it should first perform the trade partially with A1 up to the target price, and then the rest of the trade order with A2.
However, the SwapUtils.swap / _calculateSwap function does not do this, it only uses the “new A”, see getYC step 5.
// 5. Check if we switched A's during the swap
if (aNew == a){ // We have used the correct A
return y;
} else { // We have switched A's, do it again with the new A
return getY(self, tokenIndexFrom, tokenIndexTo, x, xp, aNew, d);
}
Impact
Trades that cross the target price and would lead to a new amplifier being used are not split up and use the new amplifier for the entire trade. This can lead to a worse (better) average execution price than manually splitting the trade into two transactions, first up to but below the target price, and a second one with the rest of the trader order size, using both A1 and A2 values.
In the worst case, it could even be possible to make the entire trade with one amplifier and then sell the swap result again using the other amplifier making a profit.
Recommended Mitigation Steps
Trades that lead to a change in amplifier value need to be split up into two trades using both amplifiers to correctly calculate the swap result.
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
The swap calculation does not split the trade at the target price; it recalculates the entire trade with the new amplifier value.
Impact
Trades crossing the target price may get a worse average execution price, and in the worst case a trader could profit by using one amplifier and then selling with the other.
What the attacker needed
Trade crosses target price
The fix
The swap function should be modified to split trades that cross the target price into two parts, using both amplifier values.
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.
This report is about a decentralised exchange that uses two different mathematical curves for pricing depending on the trade size. When a trade is large enough to cross a threshold, the code should split it into two parts, each using a different curve. Instead, it applies the new curve to the whole trade, which can give a worse price and might let someone game the system for profit.
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.