You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Incorrect rounding in the ReputationMarket._calcCost() function.
Summary
When buying and selling, the cost is calculated using rounding based on isPositive. However, the rounding should be based on isBuy, not isPositive.
Root Cause
The _calcCost() function calculates cost by rounding based on isPositive.
If isPositive is false (indicating that DISTRUST votes are being traded), the calculation is rounded up.
Consider the following scenario:
A user buys 2 DISTRUST votes.
The user sells a DISTRUST vote.
The user sells another DISTRUST vote.
During the buying process, rounding up occurs once, but when selling, rounding up occurs twice—at steps 2 and 3. As a result, marketFunds will be decremented by a dust amount.
If marketFunds was originally 0 (with the market created by the admin at a 0 creation cost), then step 3 becomes impossible.
In fact, isPositive is never related to the rounding direction.
function _calcCost(
Market memorymarket,
boolisPositive,
boolisBuy,
uint256amount
) privatepurereturns (uint256cost) {
...
int256 costRatio = LMSR.getCost(
market.votes[TRUST],
market.votes[DISTRUST],
voteDelta[0],
voteDelta[1],
market.liquidityParameter
);
uint256 positiveCostRatio = costRatio >0?uint256(costRatio) : uint256(costRatio *-1);
// multiply cost ratio by base price to get cost; divide by 1e18 to apply ratio
cost = positiveCostRatio.mulDiv(
market.basePrice,
1e18,
1057 isPositive ? Math.Rounding.Floor : Math.Rounding.Ceil
);
}
Broad Khaki Wasp
Medium
Incorrect rounding in the
ReputationMarket._calcCost()
function.Summary
When buying and selling, the cost is calculated using rounding based on
isPositive
. However, the rounding should be based onisBuy
, notisPositive
.Root Cause
The _calcCost() function calculates
cost
by rounding based onisPositive
.If
isPositive
isfalse
(indicating thatDISTRUST
votes are being traded), the calculation is rounded up.Consider the following scenario:
DISTRUST
votes.DISTRUST
vote.DISTRUST
vote.During the buying process, rounding up occurs once, but when selling, rounding up occurs twice—at steps 2 and 3. As a result,
marketFunds
will be decremented by a dust amount.If
marketFunds
was originally 0 (with the market created by the admin at a 0 creation cost), then step 3 becomes impossible.In fact,
isPositive
is never related to the rounding direction.Internal pre-conditions
External pre-conditions
Attack Path
Impact
The last vote might not be sold.
PoC
Mitigation
Use
!isBuy
instead ofisPositive
.The text was updated successfully, but these errors were encountered: