Skip to content

Commit

Permalink
fix(constants): move to side file
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Oct 14, 2022
1 parent 4004202 commit 9a1b47a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 33 deletions.
15 changes: 15 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { pow10 } from "./utils";

export const PERCENT = pow10(4);
export const WAD = pow10(18);
export const RAY = pow10(27);
export const WAD_SQUARED = WAD.pow(2);

export const RAY_WAD_RATIO = RAY.div(WAD);
export const HALF_RAY_WAD_RATIO = RAY_WAD_RATIO.div(2);

export const WAD_PERCENT_RATIO = WAD.div(PERCENT);
export const HALF_WAD_PERCENT_RATIO = WAD_PERCENT_RATIO.div(2);

export const RAY_PERCENT_RATIO = RAY.div(PERCENT);
export const HALF_RAY_PERCENT_RATIO = RAY_PERCENT_RATIO.div(2);
55 changes: 24 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { formatUnits, parseUnits } from "@ethersproject/units";

import { avgUp, max, min, mulDivUp, pow10 } from "./utils";

export const PERCENT = pow10(4);
export const WAD = pow10(18);
export const RAY = pow10(27);

const RAY_WAD_RATIO = RAY.div(WAD);
const HALF_RAY_WAD_RATIO = RAY_WAD_RATIO.div(2);

const WAD_PERCENT_RATIO = WAD.div(PERCENT);
const HALF_WAD_PERCENT_RATIO = WAD_PERCENT_RATIO.div(2);

const RAY_PERCENT_RATIO = RAY.div(PERCENT);
const HALF_RAY_PERCENT_RATIO = RAY_PERCENT_RATIO.div(2);
import { formatUnits } from "@ethersproject/units";

import {
HALF_RAY_PERCENT_RATIO,
HALF_RAY_WAD_RATIO,
HALF_WAD_PERCENT_RATIO,
PERCENT,
RAY,
RAY_PERCENT_RATIO,
RAY_WAD_RATIO,
WAD,
WAD_PERCENT_RATIO,
WAD_SQUARED,
} from "./constants";
import { avgUp, max, min, pow10, mulDivUp, parsePercent, parseRay, parseWad } from "./utils";

declare module "@ethersproject/bignumber/lib/bignumber" {
interface BigNumber {
Expand Down Expand Up @@ -76,10 +75,10 @@ BigNumber.prototype.sum = function (others: BigNumberish[]) {
};

BigNumber.prototype.compMul = function (other: BigNumberish) {
return BigNumber.from(this).mul(other).div(WAD);
return this.mul(other).div(WAD);
};
BigNumber.prototype.compDiv = function (other: BigNumberish) {
return WAD.mul(this).mul(WAD).div(other).div(WAD);
return this.mul(WAD_SQUARED).div(other).div(WAD);
};

BigNumber.prototype.percentAdd = function (pct: BigNumberish) {
Expand Down Expand Up @@ -108,10 +107,10 @@ BigNumber.prototype.formatPercent = function () {
};

BigNumber.prototype.wadAdd = function (wad: BigNumberish) {
return this.wadMul(PERCENT.add(wad));
return this.wadMul(WAD.add(wad));
};
BigNumber.prototype.wadSub = function (wad: BigNumberish) {
return this.wadMul(PERCENT.sub(wad));
return this.wadMul(WAD.sub(wad));
};
BigNumber.prototype.wadMul = function (other: BigNumberish) {
return mulDivUp(this, other, WAD);
Expand All @@ -133,10 +132,10 @@ BigNumber.prototype.formatWad = function () {
};

BigNumber.prototype.rayAdd = function (ray: BigNumberish) {
return this.rayMul(PERCENT.add(ray));
return this.rayMul(RAY.add(ray));
};
BigNumber.prototype.raySub = function (ray: BigNumberish) {
return this.rayMul(PERCENT.sub(ray));
return this.rayMul(RAY.sub(ray));
};
BigNumber.prototype.rayMul = function (other: BigNumberish) {
return mulDivUp(this, other, RAY);
Expand All @@ -162,12 +161,6 @@ BigNumber.WAD = WAD;
BigNumber.RAY = RAY;

BigNumber.pow10 = pow10;
BigNumber.parsePercent = function (value: string) {
return parseUnits(value, 2);
};
BigNumber.parseWad = function (value: string) {
return parseUnits(value, 18);
};
BigNumber.parseRay = function (value: string) {
return parseUnits(value, 27);
};
BigNumber.parsePercent = parsePercent;
BigNumber.parseWad = parseWad;
BigNumber.parseRay = parseRay;
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { parseUnits } from "@ethersproject/units";

export const pow10 = (power: BigNumberish) => BigNumber.from(10).pow(power);

Expand Down Expand Up @@ -30,3 +31,7 @@ export const avgUp = (x: BigNumberish, y: BigNumberish, pct: BigNumberish, scale

return max(0, scale.sub(pct)).mul(x).add(min(scale, pct).mul(y)).add(scale.div(2)).div(scale);
};

export const parsePercent = (value: string) => parseUnits(value, 2);
export const parseWad = (value: string) => parseUnits(value, 18);
export const parseRay = (value: string) => parseUnits(value, 27);
54 changes: 52 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,58 @@ import { BigNumber } from "@ethersproject/bignumber";

import "../src";

const ZERO = "0";
const WAD = BigNumber.WAD.toString();
const RAY = BigNumber.RAY.toString();

const TWO_WAD = BigNumber.WAD.mul(2).toString();

describe("ethers-maths", () => {
it("should overload", async () => {
BigNumber.from(0).wadMul(1);
it("should return minimum", async () => {
expect(BigNumber.WAD.min(0).toString()).toEqual(ZERO);
expect(BigNumber.WAD.min(BigNumber.RAY).toString()).toEqual(WAD);
});

it("should return maximum", async () => {
expect(BigNumber.WAD.max(0).toString()).toEqual(WAD);
expect(BigNumber.WAD.max(BigNumber.RAY).toString()).toEqual(RAY);
});

it("should average", async () => {
expect(
BigNumber.WAD.percentAvg(BigNumber.WAD.mul(3), BigNumber.PERCENT.div(2)).toString()
).toEqual(TWO_WAD);
expect(BigNumber.WAD.wadAvg(BigNumber.WAD.mul(3), BigNumber.WAD.div(2)).toString()).toEqual(
TWO_WAD
);
expect(BigNumber.WAD.rayAvg(BigNumber.WAD.mul(3), BigNumber.RAY.div(2)).toString()).toEqual(
TWO_WAD
);
});

it("should double on addition", async () => {
expect(BigNumber.WAD.percentAdd(BigNumber.PERCENT).toString()).toEqual(TWO_WAD);
expect(BigNumber.WAD.wadAdd(BigNumber.WAD).toString()).toEqual(TWO_WAD);
expect(BigNumber.WAD.rayAdd(BigNumber.RAY).toString()).toEqual(TWO_WAD);
});

it("should zero on subtraction", async () => {
expect(BigNumber.WAD.percentSub(BigNumber.PERCENT).toString()).toEqual(ZERO);
expect(BigNumber.WAD.wadSub(BigNumber.WAD).toString()).toEqual(ZERO);
expect(BigNumber.WAD.raySub(BigNumber.RAY).toString()).toEqual(ZERO);
});

it("should preserve units on multiplication", async () => {
expect(BigNumber.WAD.percentMul(BigNumber.PERCENT).toString()).toEqual(WAD);
expect(BigNumber.WAD.compMul(BigNumber.WAD).toString()).toEqual(WAD);
expect(BigNumber.WAD.wadMul(BigNumber.WAD).toString()).toEqual(WAD);
expect(BigNumber.WAD.rayMul(BigNumber.RAY).toString()).toEqual(WAD);
});

it("should preserve units on division", async () => {
expect(BigNumber.WAD.percentDiv(BigNumber.PERCENT).toString()).toEqual(WAD);
expect(BigNumber.WAD.compDiv(BigNumber.WAD).toString()).toEqual(WAD);
expect(BigNumber.WAD.wadDiv(BigNumber.WAD).toString()).toEqual(WAD);
expect(BigNumber.WAD.rayDiv(BigNumber.RAY).toString()).toEqual(WAD);
});
});

0 comments on commit 9a1b47a

Please sign in to comment.