Skip to content

Commit

Permalink
fix(power): add percent-based power
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Jan 29, 2023
1 parent b92b4f0 commit 7522d84
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};
Expand Down
2 changes: 2 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 7522d84

Please sign in to comment.