Wrong assignment of `cumulativeBid` for RangeOrder state in getRangeOrderState function
The getRangeOrderState function in D3Trading incorrectly assigns cumulativeAsk to cumulativeBid for the toToken, causing incorrect accounting state. This can lead to reverts in PMMRangeOrder when the wrongly assigned cumulativeBid exceeds B0.
Source: https://github.com/sherlock-audit/2023-06-dodo-judging/issues/178
Found by
bitsurfer
Summary
Wrong assignment of cumulativeBid for RangeOrder state
Vulnerability Detail
In D3Trading, the getRangeOrderState function is returning RangeOrder (get swap status for internal swap) which is assinging wrong toTokenMMInfo.cumulativeBid which suppose to be cumulativeBid not cumulativeAsk
The error lies in the assignment of roState.toTokenMMInfo.cumulativeBid. Instead of assigning tokenCumMap[toToken].cumulativeAsk, it should be assigning tokenCumMap[toToken].cumulativeBid.
File: D3Trading.sol
86: roState.toTokenMMInfo.cumulativeBid =
87: allFlag >> (toTokenIndex) & 1 == 0 ? 0 : tokenCumMap[toToken].cumulativeAsk;
This wrong assignment value definitely will mess up accounting balance, resulting unknown state will occure, which is not expected by the protocol
For one case, this getRangeOrderState is being used in querySellTokens & queryBuyTokens which may later called from sellToken and buyToken. The issue is when calling _contructTokenState which can be reverted from PMMRangeOrder when buy or sell token
File: PMMRangeOrder.sol
100: // B
101: tokenState.B = askOrNot ? tokenState.B0 - tokenMMInfo.cumulativeAsk : tokenState.B0 - tokenMMInfo.cumulativeBid;
When the tokenMMInfo.cumulativeBid (which was wrongly assign from cumulativeAsk) is bigger than tokenState.B0, this will revert
Impact
This wrong assignment value definitely will mess up accounting balance, resulting unknown state will occure, which is not expected by the protocol. For example reverting state showing a case above.
Code Snippet
Tool used
Manual Review
Recommendation
Fix the error to
File: D3Trading.sol
86: roState.toTokenMMInfo.cumulativeBid =
--: allFlag >> (toTokenIndex) & 1 == 0 ? 0 : tokenCumMap[toToken].cumulativeAsk;
++: allFlag >> (toTokenIndex) & 1 == 0 ? 0 : tokenCumMap[toToken].cumulativeBid;
Discussion
Attens1423
fix pr:https://github.com/DODOEX/new-dodo-v3/pull/40
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 a copy-paste or typo error where cumulativeAsk was used instead of cumulativeBid in the assignment.
Impact
The wrong assignment could mess up accounting balance and cause reverts in PMMRangeOrder when cumulativeBid exceeds B0, leading to unexpected state.
What the attacker needed
None
The fix
The error should be fixed by assigning cumulativeBid instead of cumulativeAsk.
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 is a smart contract bug where a function copies the wrong number into a variable. The variable is supposed to hold the total amount bid, but it gets the total amount asked instead. That mix-up can make later calculations fail and cause the transaction to be rejected.
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.