Skip to content
DoS Audit finding Advanced

Calculation B0 meets devision 0 error when a token has small decimal and high price with a small kBid

A price calculation in the DODO V3 range-order contract divides by a value that can round to zero when a token has few decimals, a high price and a small bid factor. This makes the swap model invalid and causes swaps to revert for that token.

Protocol Team Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-06-dodo-judging/issues/241

Found by

Protocol Team When a token has small decimal and high price, like wbtc, it will generate a low-precision bid price. In _calB0WithPriceLimit, https://github.com/sherlock-audit/2023-06-dodo/blob/a8d30e611acc9762029f8756d6a5b81825faf348/new-dodo-v3/contracts/DODOV3MM/lib/PMMRangeOrder.sol#L141C1-L148C10

Solidity
        if (k == 0) {
            baseTarget = amount;
        } else {
            uint256 temp1 = priceUp + DecimalMath.mul(i, k) - i;
            uint256 temp2 = DecimalMath.mul(i, k);
            uint256 temp3 = DecimalMath.div(temp1, temp2);
            uint256 temp5 = DecimalMath.sqrt(temp3) - ONE;

            baseTarget = amount + DecimalMath.div(amount, temp5);

small bid price and kBid will cause temp2 =0, so that temp3 couldn't be calculated rightly.

Vulnerability Detail

Here is poc

Solidity
   function testQueryFail() public {
        token1ChainLinkOracle.feedData(30647 * 1e18);
        token2ChainLinkOracle.feedData(1 * 1e18);
        vm.startPrank(maker);
        uint32[] memory tokenKs = new uint32[](2);
        tokenKs[0] = 0;
        tokenKs[1] = (1<< 16) +1;
        address[] memory tokens = new address[](2);
        tokens[0] = address(token2);
        tokens[1] = address(token1);
        address[] memory slotIndex = new address[](2);
        slotIndex[0] = address(token1);
        slotIndex[1] = address(token2);
        uint80[] memory priceSlot = new uint80[](2);
        priceSlot[0] = 2191925019632266903652;
        priceSlot[1] = 720435765840878108682;

        uint64[] memory amountslot = new uint64[](2);
        amountslot[0] = stickAmount(10,8, 400000, 18);
        amountslot[1] = stickAmount(400000, 18, 400000, 18);
        d3MakerWithPool.setTokensKs(tokens, tokenKs);
        d3MakerWithPool.setTokensPrice(slotIndex, priceSlot);
        d3MakerWithPool.setTokensAmounts(slotIndex, amountslot);
        vm.stopPrank();

        (uint256 askDownPrice, uint256 askUpPrice, uint256 bidDownPrice, uint256 bidUpPrice, uint256 swapFee) =
            d3MM.getTokenMMPriceInfoForRead(address(token1));
        assertEq(askDownPrice, 304555028000000000000000000000000);
        assertEq(askUpPrice, 307231900000000000000000000000000);
        assertEq(bidDownPrice, 3291);
        assertEq(bidUpPrice, 3320);
        assertEq(swapFee, 1200000000000000);

        //console.log(askDownPrice);
        //console.log(askUpPrice);
        //console.log(bidDownPrice);
        //console.log(bidUpPrice);
        //console.log(swapFee);

        (,,uint kask, uint kbid,,) = d3MM.getTokenMMOtherInfoForRead(address(token1));
        assertEq(kask, 1e14);
        assertEq(kbid, 1e14);

        (askDownPrice, askUpPrice, bidDownPrice, bidUpPrice, swapFee) =
            d3MM.getTokenMMPriceInfoForRead(address(token2));
        assertEq(askDownPrice, 999999960000000000);
        assertEq(askUpPrice, 1000799800000000000);
        assertEq(bidDownPrice, 1000400120032008002);
        assertEq(bidUpPrice, 1001201241249250852);
        assertEq(swapFee, 200000000000000);

        (,,kask, kbid,,) = d3MM.getTokenMMOtherInfoForRead(address(token2));
        assertEq(kask, 0);
        assertEq(kbid, 0);

        //console.log(askDownPrice);
        //console.log(askUpPrice);
        //console.log(bidDownPrice);
        //console.log(bidUpPrice);
        //console.log(swapFee);
        //console.log(kask);
        //console.log(kbid);

        SwapCallbackData memory swapData;
        swapData.data = "";
        swapData.payer = user1;

        //uint256 gasleft1 = gasleft();
        uint256 receiveToToken = d3Proxy.sellTokens(
            address(d3MM),
            user1,
            address(token1),
            address(token2),
            1000000,
            0,
            abi.encode(swapData),
            block.timestamp + 1000
        );

It will revert. In this example, wbtc price is 30445, and k = 0.0001, suppose maker contains rules, but model is invalid.

Impact

Maker sets right parameters but traders can't swap. It will make swap model invalid.

Tool Used

Manual Review

Recommendation

  1. Fix formula for this corner case, like making temp2 = 1
  2. Improve calculation accuracy by consistently using precision 18 for calculations and converting to real decimal when processing amounts.

Discussion

Attens1423

fix pr: https://github.com/DODOEX/new-dodo-v3/pull/32

hrishibhat

Please note: This issue is not part of the contest submissions and is not eligible for contest rewards.

The original report as published on Sherlock, by Protocol Team. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 integer rounding in the price formula makes the divisor temp2 become zero for small bid factors and low-precision prices, so the division reverts.

Impact

Traders cannot swap a token whose parameters trigger this case, even when the maker set valid parameters, so the swap model becomes invalid for that token.

What the attacker needed

A pool with a high-priced, low-decimal token; a small bid factor (kBid)

The fix

The formula should handle this corner case, for example by avoiding a zero divisor, and calculations should use consistent 18-decimal precision; a fix pull request is linked.

How it was found

  1. Set up a pool with a high-priced, low-decimal token and a small bid factor.
  2. Read the pool price information for the token.
  3. Attempt a swap through the proxy contract.
  4. Observe that the swap reverts.

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.

A smart contract works out prices using whole numbers, and it cannot divide by zero. When a token is very valuable and has few decimal places, a small intermediate number gets rounded down to zero, so the division fails and the swap stops with an error.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by Protocol Team

View all

8 more disclosed reports, best first.

More from DODO V3 update

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.