sherlock-audit / 2023-04-hubble-exchange-judging

7 stars 6 forks source link

kutugu - getRequiredMargin rounding direction errors undercalculated the requiredMargin #182

Closed sherlock-admin closed 1 year ago

sherlock-admin commented 1 year ago

kutugu

medium

getRequiredMargin rounding direction errors undercalculated the requiredMargin

Summary

For a specified amount of order, getRequiredMargin should be rounded up. Otherwise, due to the precision error of division, the calculated margin is insufficient.

Vulnerability Detail

    function getRequiredMargin(int256 baseAssetQuantity, uint256 price) public view returns(uint256 requiredMargin) {
        uint quoteAsset = abs(baseAssetQuantity).toUint256() * price / 1e18;
        requiredMargin = quoteAsset * minAllowableMargin / 1e6;
        requiredMargin += quoteAsset * takerFee / 1e6;
    }

            reserveAmount = getRequiredMargin(order.baseAssetQuantity, order.price);
        }

        // add orderInfo for the corresponding orderHash
        orderInfo[orderHash] = OrderInfo(block.number, 0, reserveAmount, OrderStatus.Placed);
        emit OrderPlaced(order.trader, orderHash, order, block.timestamp);

The margin should be rounded up

Impact

Round direction errors will undercalculate the requiredMargin resulting lock margin is insufficient.

Code Snippet

Tool used

Manual Review

Recommendation

Round up instead of round down