Skip to content

Commit

Permalink
feat(scale): add generic scaling utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Feb 8, 2023
1 parent 66b0fd7 commit d808cc6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
9 changes: 0 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,3 @@ export const WAD_SQUARED = WAD.pow(2);
export const HALF_PERCENT = PERCENT.div(2);
export const HALF_WAD = WAD.div(2);
export const HALF_RAY = RAY.div(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);
58 changes: 37 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { formatUnits } from "@ethersproject/units";

import {
HALF_RAY_PERCENT_RATIO,
HALF_RAY_WAD_RATIO,
HALF_WAD_PERCENT_RATIO,
PERCENT,
RAY,
HALF_PERCENT,
HALF_RAY,
HALF_WAD,
RAY_PERCENT_RATIO,
RAY_WAD_RATIO,
WAD,
WAD_PERCENT_RATIO,
WAD_SQUARED,
} from "./constants";
import { PERCENT, RAY, HALF_PERCENT, HALF_RAY, HALF_WAD, WAD, WAD_SQUARED } from "./constants";
import {
avgHalfUp,
max,
Expand Down Expand Up @@ -52,6 +38,7 @@ declare module "@ethersproject/bignumber/lib/bignumber" {
percentDivDown: (other: BigNumberish) => BigNumber;
percentAvg: (other: BigNumberish, pct: BigNumberish) => BigNumber;
percentPow: (exponent: BigNumberish) => BigNumber;
percentToDecimals: (decimals: number) => BigNumber;
percentToWad: () => BigNumber;
percentToRay: () => BigNumber;
formatPercent: (digits?: number) => string;
Expand All @@ -67,6 +54,7 @@ declare module "@ethersproject/bignumber/lib/bignumber" {
wadDivDown: (other: BigNumberish) => BigNumber;
wadAvg: (other: BigNumberish, wad: BigNumberish) => BigNumber;
wadPow: (exponent: BigNumberish) => BigNumber;
wadToDecimals: (decimals: number) => BigNumber;
wadToPercent: () => BigNumber;
wadToRay: () => BigNumber;
formatWad: (digits?: number) => string;
Expand All @@ -82,6 +70,7 @@ declare module "@ethersproject/bignumber/lib/bignumber" {
rayDivDown: (other: BigNumberish) => BigNumber;
rayAvg: (other: BigNumberish, ray: BigNumberish) => BigNumber;
rayPow: (exponent: BigNumberish) => BigNumber;
rayToDecimals: (decimals: number) => BigNumber;
rayToPercent: () => BigNumber;
rayToWad: () => BigNumber;
formatRay: (digits?: number) => string;
Expand Down Expand Up @@ -170,11 +159,20 @@ BigNumber.prototype.percentAvg = function (other: BigNumberish, pct: BigNumberis
BigNumber.prototype.percentPow = function (exponent: BigNumberish) {
return powHalfUp(this, exponent, PERCENT);
};
BigNumber.prototype.percentToDecimals = function (decimals: number) {
if (decimals <= 4) {
const ratio = pow10(4 - decimals);

return this.add(ratio.div(2)).div(ratio);
}

return this.mul(pow10(decimals - 4));
};
BigNumber.prototype.percentToWad = function () {
return this.mul(WAD_PERCENT_RATIO);
return this.percentToDecimals(18);
};
BigNumber.prototype.percentToRay = function () {
return this.mul(RAY_PERCENT_RATIO);
return this.percentToDecimals(27);
};
BigNumber.prototype.formatPercent = function (digits?: number) {
return this.format(4, digits);
Expand Down Expand Up @@ -213,11 +211,20 @@ BigNumber.prototype.wadAvg = function (other: BigNumberish, wad: BigNumberish) {
BigNumber.prototype.wadPow = function (exponent: BigNumberish) {
return powHalfUp(this, exponent, WAD);
};
BigNumber.prototype.wadToDecimals = function (decimals: number) {
if (decimals <= 18) {
const ratio = pow10(18 - decimals);

return this.add(ratio.div(2)).div(ratio);
}

return this.mul(pow10(decimals - 18));
};
BigNumber.prototype.wadToPercent = function () {
return this.add(HALF_WAD_PERCENT_RATIO).div(WAD_PERCENT_RATIO);
return this.wadToDecimals(4);
};
BigNumber.prototype.wadToRay = function () {
return this.mul(RAY_WAD_RATIO);
return this.wadToDecimals(27);
};
BigNumber.prototype.formatWad = function (digits?: number) {
return this.format(18, digits);
Expand Down Expand Up @@ -256,11 +263,20 @@ BigNumber.prototype.rayAvg = function (other: BigNumberish, ray: BigNumberish) {
BigNumber.prototype.rayPow = function (exponent: BigNumberish) {
return powHalfUp(this, exponent, RAY);
};
BigNumber.prototype.rayToDecimals = function (decimals: number) {
if (decimals <= 27) {
const ratio = pow10(27 - decimals);

return this.add(ratio.div(2)).div(ratio);
}

return this.mul(pow10(decimals - 27));
};
BigNumber.prototype.rayToPercent = function () {
return this.add(HALF_RAY_PERCENT_RATIO).div(RAY_PERCENT_RATIO);
return this.rayToDecimals(4);
};
BigNumber.prototype.rayToWad = function () {
return this.add(HALF_RAY_WAD_RATIO).div(RAY_WAD_RATIO);
return this.rayToDecimals(18);
};
BigNumber.prototype.formatRay = function (digits?: number) {
return this.format(27, digits);
Expand Down
9 changes: 9 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,13 @@ describe("ethers-maths", () => {
expect(BigNumber.RAY.add(BigNumber.RAY).rayPow(2).formatRay(1)).toEqual("4.0");
expect(BigNumber.RAY.add(BigNumber.RAY).rayPow(3).formatRay(1)).toEqual("8.0");
});

it("should scale decimals", async () => {
expect(BigNumber.PERCENT.percentToWad().toString()).toEqual(BigNumber.WAD.toString());
expect(BigNumber.PERCENT.percentToRay().toString()).toEqual(BigNumber.RAY.toString());
expect(BigNumber.WAD.wadToPercent().toString()).toEqual(BigNumber.PERCENT.toString());
expect(BigNumber.WAD.wadToRay().toString()).toEqual(BigNumber.RAY.toString());
expect(BigNumber.RAY.rayToPercent().toString()).toEqual(BigNumber.PERCENT.toString());
expect(BigNumber.RAY.rayToWad().toString()).toEqual(BigNumber.WAD.toString());
});
});

0 comments on commit d808cc6

Please sign in to comment.