balancer / balancer-core

Balancer on the EVM

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

errorDelta in math_with_fees.js does not always hold

EricR opened this issue · comments

Description

The BPool contract is tested to ensure that rounding errors do not introduce a delta into token calculations that exceeds 10**-8. This constant, errorDelta, does not hold when a pool's swap fee is set to its maximum allowed value, which is defined on line 27 of BConst.sol:

    uint256 constant MAX_FEE           = BONE / 10;

When the constant swapFee on line 6 of math_with_fees.js is changed to 0.1, the following test fails:

$ truffle test test/math_with_fees.js
Using network 'development'.


Compiling your contracts...
===========================
> Compiling ./contracts/BFactory.sol
> Compiling ./contracts/BMath.sol
> Compiling ./contracts/BPool.sol
> Compiling ./contracts/test/TMath.sol

[...]

  12 passing (2s)
  1 failing

  1) Contract: BPool
       With fees
         swap_ExactMarginalPrice:

      AssertionError: expected 0.0000491203806036733 to be at most 1e-8
      + expected - actual

      -0.0000491203806036733
      +1e-8

      at Context.it (test/math_with_fees.js:218:14)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)



error Command failed with exit code 1.

Exploit Scenario

A pool is created with the maximally allowed swap fee. As a result, rounding errors introduce unexpected variance into token calculations.

Recommendation

Update math_with_fees.js so that additional testing is performed using the maximally allowed swap fee. Then, either lower MAX_FEE or errorDelta to an acceptable number so that tests pass.

In addition, the amount of variance users should expect to see due to rounding errors should be clearly documented.

This was due to an approximation being done in swapExactMarginalPrice. This method has been removed and will be implemented in an offchain order router