ethers-maths

➗ Useful ethers-based math libraries to ease your journey through off-chain fixed-point arithmetics
Install
npm install ethers-maths
yarn add ethers-maths
Usage
Just import the module and you'll benefit from an augmented, and typed, BigInt
class!
import "ethers-maths";
const WAD = BigInt.pow10(18);
BigInt.from(1).wadMul(WAD);
BigInt.from(WAD * 2n).rayMul(0.5e27);
If you choose to avoid prototype pollution, you can always import specific utilities:
import * as WadMath from "ethers-maths/lib/wad";
import * as RayMath from "ethers-maths/lib/ray";
import * as PercentMath from "ethers-maths/lib/percent";
Book
Scale-agnostic utilities
approxEqAbs
Returns whether the BigNumber is approximately close to the given BigNumber, within the given tolerance
import { approxEqAbs } from "ethers-maths/lib/utils";
approxEqAbs(0, 1, "1");
BigNumber.approxEqAbs(0, 1, "1");
BigNumber.from(0).approxEqAbs(1, "1");
min
Returns the minimum between input BigNumberish, as a BigInt
import { min } from "ethers-maths/lib/utils";
min(0, 1, "2", ...);
BigInt.min(0, 1, "2", ...);
BigInt.from(0).min(1, "2", ...);
max
Returns the maximum between input BigNumberish, as a BigInt
import { max } from "ethers-maths/lib/utils";
max(0, 1, "2", ...);
BigInt.max(0, 1, "2", ...);
BigInt.from(0).max(1, "2", ...);
sum
Returns the sum of input BigNumberish array, as a BigInt
import { sum } from "ethers-maths/lib/utils";
sum([0, 1, "2"]);
BigInt.sum([0, 1, "2"]);
BigInt.from(0).sum([1, "2"]);
format
Returns a string representation of the BigInt's value, formatted according to:
- the input number of decimals the BigInt value holds (default: 0)
- the input number of digits expected after the unit, truncating the trailing digits if any (default: keep all digits after the decimal point)
BigInt.from(19).format(3, 2);
toFloat
Returns a float representation of the BigInt's value, parsed according to the input number of decimals the BigInt value holds (default: 0)
Note: parsing a too large value may result in parsing NaN
(because the BigInt's value may still be too large to fit in a JS floating-point number)
BigInt.from(19).toFloat(3, 2);
pow10
Returns a 1 followed by the input number of zeros (10 raised to the power of the input)
import { pow10 } from "ethers-maths/lib/utils";
pow10(2);
BigInt.pow10(2);
mulDiv
Performs a multiplication followed by a division, rounded half up
import { mulDivHalfUp } from "ethers-maths/lib/utils";
mulDivHalfUp(BigInt.WAD, 1, 1);
BigInt.WAD.mulDiv(1, 1);
mulDivUp
Performs a multiplication followed by a division, rounded up
import { mulDivUp } from "ethers-maths/lib/utils";
mulDivUp(BigInt.WAD - 1n, 1, BigInt.WAD);
(BigInt.WAD - 1n).mulDivUp(1, BigInt.WAD);
mulDivDown
Performs a multiplication followed by a division, rounded down
import { mulDivDown } from "ethers-maths/lib/utils";
mulDivDown(BigInt.WAD + 1n, 1, BigInt.WAD);
(BigInt.WAD + 1n).mulDivDown(1, BigInt.WAD);
Scale constants
WAD
Returns the common WAD unit, which is also known as ether
in Solidity
Most commonly used as the ERC20 token unit
import { WAD } from "ethers-maths/lib/constants";
WAD;
BigInt.WAD;
RAY
Returns the common RAY unit, which is also known as 1e9 ether
in Solidity
Most commonly used as Aave's index unit
import { RAY } from "ethers-maths/lib/constants";
RAY;
BigInt.RAY;
PERCENT
Returns the common PERCENT unit, which is also known as 100%
in basis points
Most commonly used as Aave's PERCENTAGE_FACTOR
import { PERCENT } from "ethers-maths/lib/constants";
PERCENT;
BigInt.PERCENT;
HALF_WAD
Returns half of the common WAD unit, which is also known as 0.5 ether
in Solidity
import { HALF_WAD } from "ethers-maths/lib/constants";
HALF_WAD;
BigInt.HALF_WAD;
HALF_RAY
Returns half of the common RAY unit, which is also known as 0.5e9 ether
in Solidity
import { HALF_RAY } from "ethers-maths/lib/constants";
HALF_RAY;
BigInt.HALF_RAY;
HALF_PERCENT
Returns the common PERCENT unit, which is also known as 50%
in basis points
Most commonly used as Aave's HALF_PERCENTAGE_FACTOR
import { HALF_PERCENT } from "ethers-maths/lib/constants";
HALF_PERCENT;
BigInt.HALF_PERCENT;
Wad-based utilities
wadMul
Returns the result of the wad-based multiplication (18 decimals precision), rounded half up
BigInt.WAD.wadMul(BigInt.WAD);
wadDiv
Returns the result of the wad-based division (18 decimals precision), rounded half up
BigInt.WAD.wadDiv(BigInt.WAD);
wadAdd
Returns the result of the addition of a BigNumberish and a wad-based percentage of it (18 decimals), rounded half up
BigInt.WAD.wadAdd(
BigInt.HALF_WAD,
);
wadSub
Returns the result of the subtraction of a BigNumberish and a wad-based percentage of it (18 decimals), rounded half up
BigInt.WAD.wadSub(
BigInt.HALF_WAD,
);
wadAvg
Returns the weighted average of 2 BigNumberishs, using a wad-based weight (18 decimals), rounded half up
BigInt.WAD.wadAvg(
BigInt.WAD * 2n,
BigInt.HALF_WAD,
);
wadPow
Returns the integer power of a BigInt, calculated using wad-based multiplications (18 decimals precision), rounded half up
BigInt.WAD *
2n
.wadPow(2);
wadPowUp
Returns the integer power of a BigInt, calculated using wad-based multiplications (4 decimals precision), rounded up
BigInt.PERCENT *
2n
.wadPowUp(2);
wadPowDown
Returns the integer power of a BigInt, calculated using wad-based multiplications (4 decimals precision), rounded down
BigInt.PERCENT *
2n
.wadPowDown(2);
wadExpN
Returns the N-th order Taylor polynomial approximation of the integer exp of a BigInt, calculated using wad-based multiplications (4 decimals precision), rounded down
BigInt.PERCENT.wadExpN(3);
wadMulUp
Returns the result of the wad-based multiplication (18 decimals precision), rounded up
(BigInt.WAD - 1n).wadMulUp(BigInt.WAD - 1n);
wadMulDown
Returns the result of the wad-based multiplication (18 decimals precision), rounded down
(BigInt.WAD - 1n).wadMulDown(BigInt.WAD - 1n);
wadDivUp
Returns the result of the wad-based division (18 decimals precision), rounded up
BigInt.WAD.wadDivUp(BigInt.WAD - 1n);
wadDivDown
Returns the result of the wad-based division (18 decimals precision), rounded down
BigInt.WAD.wadDivDown(BigInt.WAD - 1n);
formatWad
Returns a string representation of the BigInt's value, formatted to 18 decimals and with the input number of digits expected after the unit, truncating the trailing digits if any (default: keep all digits after the decimal point)
BigInt.WAD.formatWad(3);
toWadFloat
Returns a float representation of the BigInt's value, parsed as a wad-based number.
Note: parsing a too large value may result in parsing NaN
(because the BigInt's value may still be too large to fit in a JS floating-point number)
BigInt.WAD.toWadFloat();
wadToPercent
Scales the wad-based BigInt down to the percent scale (losing 14 decimals)
BigInt.WAD.wadToPercent();
wadToRay
Scales the wad-based BigInt up to the ray scale (adding 9 decimals)
BigInt.WAD.wadToRay();
wadToDecimals
Scales the wad-based BigInt up or down to the given scale defined by its number of decimals
BigInt.WAD.wadToDecimals(27);
Ray-based utilities
rayMul
Returns the result of the ray-based multiplication (27 decimals precision), rounded half up
BigInt.RAY.rayMul(BigInt.RAY);
rayDiv
Returns the result of the ray-based division (27 decimals precision), rounded half up
BigInt.RAY.rayDiv(BigInt.RAY);
rayAdd
Returns the result of the addition of a BigNumberish and a ray-based percentage of it (27 decimals), rounded half up
BigInt.RAY.rayAdd(
BigInt.HALF_RAY,
);
raySub
Returns the result of the subtraction of a BigNumberish and a ray-based percentage of it (27 decimals), rounded half up
BigInt.RAY.raySub(
BigInt.HALF_RAY,
);
rayAvg
Returns the weighted average of 2 BigNumberishs, using a ray-based weight (27 decimals), rounded half up
BigInt.RAY.rayAvg(
BigInt.RAY * 2n,
BigInt.HALF_RAY,
);
rayPow
Returns the integer power of a BigInt, calculated using ray-based multiplications (27 decimals precision), rounded half up
(BigInt.RAY * 2n)
.rayPow(2);
rayPowUp
Returns the integer power of a BigInt, calculated using ray-based multiplications (4 decimals precision), rounded up
BigInt.PERCENT *
2n
.rayPowUp(2);
rayPowDown
Returns the integer power of a BigInt, calculated using ray-based multiplications (4 decimals precision), rounded down
BigInt.PERCENT *
2n
.rayPowDown(2);
rayExpN
Returns the N-th order Taylor polynomial approximation of the integer exp of a BigInt, calculated using ray-based multiplications (4 decimals precision), rounded down
BigInt.PERCENT.rayExpN(3);
rayMulUp
Returns the result of the ray-based multiplication (27 decimals precision), rounded up
(BigInt.RAY - 1n).rayMulUp(BigInt.RAY - 1n);
rayMulDown
Returns the result of the ray-based multiplication (27 decimals precision), rounded down
(BigInt.RAY - 1n).rayMulDown(BigInt.RAY - 1n);
rayDivUp
Returns the result of the ray-based division (27 decimals precision), rounded up
BigInt.RAY.rayDivUp(BigInt.RAY - 1n);
rayDivDown
Returns the result of the ray-based division (27 decimals precision), rounded down
BigInt.RAY.rayDivDown(BigInt.RAY - 1n);
formatRay
Returns a string representation of the BigInt's value, formatted to 27 decimals and with the input number of digits expected after the unit, truncating the trailing digits if any (default: keep all digits after the decimal point)
BigInt.RAY.formatRay(3);
toRayFloat
Returns a float representation of the BigInt's value, parsed as a ray-based number.
Note: parsing a too large value may result in parsing NaN
(because the BigInt's value may still be too large to fit in a JS floating-point number)
BigInt.RAY.toRayFloat();
rayToPercent
Scales the ray-based BigInt down to the percent scale (losing 23 decimals)
BigInt.RAY.rayToPercent();
rayToWad
Scales the ray-based BigInt down to the wad scale (losing 9 decimals)
BigInt.RAY.rayToWad();
rayToDecimals
Scales the ray-based BigInt up or down to the given scale defined by its number of decimals
BigInt.RAY.rayToDecimals(18);
Percent-based utilities
percentMul
Returns the result of the percent-based multiplication (4 decimals precision), rounded half up
BigInt.PERCENT.percentMul(BigInt.PERCENT);
percentDiv
Returns the result of the percent-based division (4 decimals precision), rounded half up
BigInt.PERCENT.percentDiv(BigInt.PERCENT);
percentAdd
Returns the result of the addition of a BigNumberish and a percent-based percentage of it (4 decimals), rounded half up
BigInt.PERCENT.percentAdd(
BigInt.HALF_PERCENT,
);
percentSub
Returns the result of the subtraction of a BigNumberish and a percent-based percentage of it (4 decimals), rounded half up
BigInt.PERCENT.percentSub(
BigInt.HALF_PERCENT,
);
percentAvg
Returns the weighted average of 2 BigNumberishs, using a percent-based weight (4 decimals), rounded half up
BigInt.PERCENT.percentAvg(
BigInt.PERCENT * 2n,
BigInt.HALF_PERCENT,
);
percentPow
Returns the integer power of a BigInt, calculated using percent-based multiplications (4 decimals precision), rounded half up
BigInt.PERCENT *
2n
.percentPow(2);
percentPowUp
Returns the integer power of a BigInt, calculated using percent-based multiplications (4 decimals precision), rounded up
BigInt.PERCENT *
2n
.percentPowUp(2);
percentPowDown
Returns the integer power of a BigInt, calculated using percent-based multiplications (4 decimals precision), rounded down
BigInt.PERCENT *
2n
.percentPowDown(2);
percentExpN
Returns the N-th order Taylor polynomial approximation of the integer exp of a BigInt, calculated using percent-based multiplications (4 decimals precision), rounded down
BigInt.PERCENT.percentExpN(3);
percentMulUp
Returns the result of the percent-based multiplication (4 decimals precision), rounded up
(BigInt.PERCENT - 1n).percentMulUp(BigInt.PERCENT - 1n);
percentMulDown
Returns the result of the percent-based multiplication (4 decimals precision), rounded down
(BigInt.PERCENT - 1n).percentMulDown(BigInt.PERCENT - 1n);
percentDivUp
Returns the result of the percent-based division (4 decimals precision), rounded up
BigInt.PERCENT.percentDivUp(BigInt.PERCENT - 1n);
percentDivDown
Returns the result of the percent-based division (4 decimals precision), rounded down
BigInt.PERCENT.percentDivDown(BigInt.PERCENT - 1n);
formatPercent
Returns a string representation of the BigInt's value, formatted to 4 decimals and with the input number of digits expected after the unit, truncating the trailing digits if any (default: keep all digits after the decimal point)
BigInt.PERCENT.formatPercent(3);
toPercentFloat
Returns a float representation of the BigInt's value, parsed as a percent-based number.
Note: parsing a too large value may result in parsing NaN
(because the BigInt's value may still be too large to fit in a JS floating-point number)
BigInt.PERCENT.toPercentFloat();
percentToWad
Scales the percent-based BigInt up to the wad scale (adding 14 decimals)
BigInt.PERCENT.percentToWad();
percentToRay
Scales the percent-based BigInt up to the ray scale (adding 23 decimals)
BigInt.PERCENT.percentToRay();
percentToDecimals
Scales the percent-based BigInt up or down to the given scale defined by its number of decimals
BigInt.RAY.percentToDecimals(27);