Mean Bug Disclosure: Rounding on Swaps

2 min readAug 16, 2021


Today at 1 PM UTC, we’ve discovered a bug on our protocol related to a rounding error on *some* swaps of the protocol. This bug causes some users to be unable to withdraw funds from their already swapped positions if they are the last to withdraw from the pool.

Please take into consideration that this is just a bug disclosure, no funds are at risk and the protocol continues to work as intended. If you are a user affected by this bug (can’t withdraw due to the small difference, please contact us through our Bug Reports and we will have it fixed ASAP).

The bug

The bug is on line 129/130 of the DCAPairSwapHandler.sol contract.

While a market-maker is executing a swap, they receive the minimum amount they have to provide in order to have a “valid swap”, these amounts are transferred on _getNextSwapInfo (_finalNeededTokenA and _finalNeededTokenB).

While designing it, we didn’t take into consideration that Solidity rounds down, so from time to time (not every swap), the swaps executed on the protocol will be executed with a precision error due to this rounding mechanism.

This precision error becomes an issue whenever you are the “last one” to withdraw on that pair, example:

  • Your position says you have 0.02194200 WBTC to withdraw
  • Pool owns 0.02194198 WBTC
  • Due to a 2e-8 WBTC (~ 0.0009244222 USD) difference on the pool balance, you won’t be able to withdraw from the pair (the withdraw will always revert as the pool doesn’t have enough funds for the payout)

TL;DR = Solidity rounds down, we should be enforcing a round upwards.


  • 5AM (UTC) user reports bug
  • 1PM (UTC) team became aware of the bug report
  • 2PM (UTC) team became aware of the bug (swap rate precision)
  • 6.45PM (UTC) bug discovered — contingency plan created

What’s next?

Our contracts are immutable (non-upgradeable) so we can’t perform a quick-fix of this bug, so we created a contingency plan for users to rest assured that they can keep relying on V1 until a new version is deployed:

  • For users that can be affected by this bug, we will monitor all pairs and match missing funds by creating our own positions, therefore allowing every user to withdraw their swapped funds. In case you are still not allowed to withdraw them please communicate with us through Bug Reports.
  • We will not urge to deploy a new version just for the sake of fixing this bug. Our plan is to add a few features that we had already planned for V2 and release a new version with the bug fix + features within the next 4 weeks.




Balmy is your home for accessing the world of decentralized finance as it was meant to be: safe, open, and intuitive.