Skip to content

Commit

Permalink
feat(bn): added min/max functions to bgn
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Nov 27, 2022
1 parent f43ef24 commit 1a1bce6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { avgUp, max, min, pow10, mulDivUp, parsePercent, parseRay, parseWad } fr

declare module "@ethersproject/bignumber/lib/bignumber" {
interface BigNumber {
min: (other: BigNumberish) => BigNumber;
max: (other: BigNumberish) => BigNumber;
min: (other: BigNumberish, ...others: BigNumberish[]) => BigNumber;
max: (other: BigNumberish, ...others: BigNumberish[]) => BigNumber;
sum: (others: BigNumberish[]) => BigNumber;
format: (decimals?: number, digits?: number) => string;
toFloat: (decimals?: number) => number;
Expand Down Expand Up @@ -62,18 +62,21 @@ declare module "@ethersproject/bignumber/lib/bignumber" {
let WAD: BigNumber;
let RAY: BigNumber;

let min: (other: BigNumberish, ...others: BigNumberish[]) => BigNumber;
let max: (other: BigNumberish, ...others: BigNumberish[]) => BigNumber;

let pow10: (power: BigNumberish) => BigNumber;
let parsePercent: (value: string) => BigNumber;
let parseWad: (value: string) => BigNumber;
let parseRay: (value: string) => BigNumber;
}
}

BigNumber.prototype.min = function (other: BigNumberish) {
return min(this, other);
BigNumber.prototype.min = function (y: BigNumberish, ...others: BigNumberish[]) {
return min(this, y, ...others);
};
BigNumber.prototype.max = function (other: BigNumberish) {
return max(this, other);
BigNumber.prototype.max = function (y: BigNumberish, ...others: BigNumberish[]) {
return max(this, y, ...others);
};
BigNumber.prototype.sum = function (others: BigNumberish[]) {
return others.reduce<BigNumber>((acc, val) => acc.add(val), this);
Expand Down Expand Up @@ -190,6 +193,9 @@ BigNumber.PERCENT = PERCENT;
BigNumber.WAD = WAD;
BigNumber.RAY = RAY;

BigNumber.min = min;
BigNumber.max = max;

BigNumber.pow10 = pow10;
BigNumber.parsePercent = parsePercent;
BigNumber.parseWad = parseWad;
Expand Down
18 changes: 12 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ import { parseUnits } from "@ethersproject/units";

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

export const min = (x: BigNumberish, y: BigNumberish) => {
export const min = (x: BigNumberish, ...others: BigNumberish[]): BigNumber => {
x = BigNumber.from(x);
y = BigNumber.from(y);

return x.gt(y) ? y : x;
if (others.length === 0) return x;

const y = BigNumber.from(others[0]);

return min(x.gt(y) ? y : x, ...others.slice(1));
};

export const max = (x: BigNumberish, y: BigNumberish) => {
export const max = (x: BigNumberish, ...others: BigNumberish[]): BigNumber => {
x = BigNumber.from(x);
y = BigNumber.from(y);

return x.gt(y) ? x : y;
if (others.length === 0) return x;

const y = BigNumber.from(others[0]);

return max(x.gt(y) ? x : y, ...others.slice(1));
};

export const mulDivUp = (x: BigNumberish, y: BigNumberish, scale: BigNumberish) => {
Expand Down
4 changes: 4 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ describe("ethers-maths", () => {
it("should return minimum", async () => {
expect(BigNumber.WAD.min(0).toString()).toEqual(ZERO);
expect(BigNumber.WAD.min(BigNumber.RAY).toString()).toEqual(WAD);
expect(BigNumber.WAD.min(BigNumber.WAD.add(1), 0, BigNumber.RAY).toString()).toEqual("0");
expect(BigNumber.min(BigNumber.WAD.add(1), 0, BigNumber.RAY).toString()).toEqual("0");
});

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

it("should average", async () => {
Expand Down

0 comments on commit 1a1bce6

Please sign in to comment.