From 7522d84f44f6a759bf696605c6e120c8fd9c105d Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Sun, 29 Jan 2023 20:08:10 +0100 Subject: [PATCH] fix(power): add percent-based power --- src/index.ts | 4 ++++ test/index.spec.ts | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/index.ts b/src/index.ts index d64d923..6f00c7c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -47,6 +47,7 @@ declare module "@ethersproject/bignumber/lib/bignumber" { percentDivUp: (other: BigNumberish) => BigNumber; percentDivDown: (other: BigNumberish) => BigNumber; percentAvg: (other: BigNumberish, pct: BigNumberish) => BigNumber; + percentPow: (exponent: BigNumberish) => BigNumber; percentToWad: () => BigNumber; percentToRay: () => BigNumber; formatPercent: (digits?: number) => string; @@ -158,6 +159,9 @@ BigNumber.prototype.percentDivDown = function (other: BigNumberish) { BigNumber.prototype.percentAvg = function (other: BigNumberish, pct: BigNumberish) { return avgUp(this, other, pct, PERCENT); }; +BigNumber.prototype.percentPow = function (exponent: BigNumberish) { + return pow(this, exponent, PERCENT); +}; BigNumber.prototype.percentToWad = function () { return this.mul(WAD_PERCENT_RATIO); }; diff --git a/test/index.spec.ts b/test/index.spec.ts index db6b0f4..8b987ad 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -103,6 +103,8 @@ describe("ethers-maths", () => { }); it("should raise to the power of n", async () => { + expect(BigNumber.PERCENT.add(BigNumber.PERCENT).percentPow(2).formatPercent(1)).toEqual("4.0"); + expect(BigNumber.PERCENT.add(BigNumber.PERCENT).percentPow(3).formatPercent(1)).toEqual("8.0"); expect(BigNumber.WAD.add(BigNumber.WAD).wadPow(2).formatWad(1)).toEqual("4.0"); expect(BigNumber.WAD.add(BigNumber.WAD).wadPow(3).formatWad(1)).toEqual("8.0"); expect(BigNumber.RAY.add(BigNumber.RAY).rayPow(2).formatRay(1)).toEqual("4.0");