bancorprotocol / contracts-solidity

Bancor Protocol Contracts
Other
841 stars 391 forks source link

Add utility functions for computing `x * y / z` safely and accurately #637

Closed barakman closed 3 years ago

barakman commented 3 years ago
  1. Add utility functions mulDivF and mulDivC, which allow to safely compute x * y / z (revert only if the actual result is larger than 256 bits).
  2. In internal function LiquidityProtection.protectedAmountPlusFee, use function mulDivF, instead of function reducedRatio (which yields an approximated result).
  3. Add utility function productRatio, which allows to compute (x * y) / (z * z) with high precision; we can later consider using it in order to improve the accuracy in internal function LiquidityProtection.compensationAmount