Skip to content

Commit

Permalink
fix(imports): make everything flat
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Feb 22, 2023
1 parent 10b6499 commit 8d0a0d5
Show file tree
Hide file tree
Showing 9 changed files with 1,190 additions and 1,489 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml → .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release
name: Push

on:
push:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
jobs:
jest:
runs-on: ubuntu-latest
if: github.head_ref != 'ethers-v5' && github.head_ref != 'ethers-v6' # already triggered by push

strategy:
matrix:
Expand Down
5 changes: 5 additions & 0 deletions .husky/post-checkout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

git submodule update --init --recursive
yarn
5 changes: 5 additions & 0 deletions .husky/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

git submodule update --init --recursive
yarn
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@
"ethers": "^5.0.0"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^3.3.0",
"@trivago/prettier-plugin-sort-imports": "4.0.0",
"@types/jest": "^27.5.2",
"@typescript-eslint/eslint-plugin": "^5.34.0",
"@typescript-eslint/parser": "^5.34.0",
"commitizen": "^4.2.5",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"commitizen": "^4.3.0",
"conventional-changelog-conventionalcommits": "^5.0.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.22.0",
"eslint-config-prettier": "^8.5.0",
"eslint": "^8.34.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.1",
"jest": "^27.2.0",
"husky": "^8.0.3",
"jest": "^29.4.3",
"lint-staged": "^10.5.4",
"prettier": "^2.7.1",
"ts-jest": "^27.0.5",
"typescript": "^4.7.4"
"prettier": "^2.8.4",
"ts-jest": "^29.0.5",
"typescript": "^4.9.5"
},
"peerDependencies": {
"ethers": "^5.0.0"
Expand Down Expand Up @@ -141,4 +141,4 @@
}
}
}
}
}
223 changes: 221 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { BigNumberish } from "ethers";
import { BigNumberish, BigNumber, utils } from "ethers";

import "./prototype";
import { PERCENT, RAY, HALF_PERCENT, HALF_RAY, HALF_WAD, WAD, WAD_SQUARED } from "./constants";
import {
avgHalfUp,
max,
min,
sum,
pow10,
mulDivHalfUp,
parsePercent,
parseRay,
parseWad,
powHalfUp,
mulDivUp,
mulDivDown,
} from "./utils";

declare module "ethers" {
interface BigNumber {
Expand Down Expand Up @@ -80,3 +94,208 @@ declare module "ethers" {
let parseRay: (value: string) => BigNumber;
}
}

BigNumber.prototype.min = function (y: BigNumberish, ...others: BigNumberish[]) {
return min(this, y, ...others);
};
BigNumber.prototype.max = function (y: BigNumberish, ...others: BigNumberish[]) {
return max(this, y, ...others);
};
BigNumber.prototype.sum = function (others: BigNumberish[]) {
return sum(this, others);
};
BigNumber.prototype.format = function (decimals?: number, digits?: number) {
const formatted = utils.formatUnits(this, decimals);

let dotIndex = formatted.indexOf(".");
if (dotIndex < 0) dotIndex = formatted.length;

decimals = formatted.length - 1 - dotIndex;
digits ??= decimals;

return digits < decimals
? formatted.slice(0, dotIndex + 1 + digits)
: formatted + "0".repeat(digits - decimals);
};
BigNumber.prototype.toFloat = function (decimals?: number) {
return parseFloat(this.format(decimals));
};

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

BigNumber.prototype.percentAdd = function (pct: BigNumberish) {
return this.percentMul(PERCENT.add(pct));
};
BigNumber.prototype.percentSub = function (pct: BigNumberish) {
return this.percentMul(PERCENT.sub(pct));
};
BigNumber.prototype.percentMul = function (other: BigNumberish) {
return mulDivHalfUp(this, other, PERCENT);
};
BigNumber.prototype.percentMulUp = function (other: BigNumberish) {
return mulDivUp(this, other, PERCENT);
};
BigNumber.prototype.percentMulDown = function (other: BigNumberish) {
return mulDivDown(this, other, PERCENT);
};
BigNumber.prototype.percentDiv = function (other: BigNumberish) {
return mulDivHalfUp(this, PERCENT, other);
};
BigNumber.prototype.percentDivUp = function (other: BigNumberish) {
return mulDivUp(this, PERCENT, other);
};
BigNumber.prototype.percentDivDown = function (other: BigNumberish) {
return mulDivDown(this, PERCENT, other);
};
BigNumber.prototype.percentAvg = function (other: BigNumberish, pct: BigNumberish) {
return avgHalfUp(this, other, pct, PERCENT);
};
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.percentToDecimals(18);
};
BigNumber.prototype.percentToRay = function () {
return this.percentToDecimals(27);
};
BigNumber.prototype.formatPercent = function (digits?: number) {
return this.format(4, digits);
};
BigNumber.prototype.toPercentFloat = function () {
return this.toFloat(4);
};

BigNumber.prototype.wadAdd = function (wad: BigNumberish) {
return this.wadMul(WAD.add(wad));
};
BigNumber.prototype.wadSub = function (wad: BigNumberish) {
return this.wadMul(WAD.sub(wad));
};
BigNumber.prototype.wadMul = function (other: BigNumberish) {
return mulDivHalfUp(this, other, WAD);
};
BigNumber.prototype.wadMulUp = function (other: BigNumberish) {
return mulDivUp(this, other, WAD);
};
BigNumber.prototype.wadMulDown = function (other: BigNumberish) {
return mulDivDown(this, other, WAD);
};
BigNumber.prototype.wadDiv = function (other: BigNumberish) {
return mulDivHalfUp(this, WAD, other);
};
BigNumber.prototype.wadDivUp = function (other: BigNumberish) {
return mulDivUp(this, WAD, other);
};
BigNumber.prototype.wadDivDown = function (other: BigNumberish) {
return mulDivDown(this, WAD, other);
};
BigNumber.prototype.wadAvg = function (other: BigNumberish, wad: BigNumberish) {
return avgHalfUp(this, other, wad, WAD);
};
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.wadToDecimals(4);
};
BigNumber.prototype.wadToRay = function () {
return this.wadToDecimals(27);
};
BigNumber.prototype.formatWad = function (digits?: number) {
return this.format(18, digits);
};
BigNumber.prototype.toWadFloat = function () {
return this.toFloat(18);
};

BigNumber.prototype.rayAdd = function (ray: BigNumberish) {
return this.rayMul(RAY.add(ray));
};
BigNumber.prototype.raySub = function (ray: BigNumberish) {
return this.rayMul(RAY.sub(ray));
};
BigNumber.prototype.rayMul = function (other: BigNumberish) {
return mulDivHalfUp(this, other, RAY);
};
BigNumber.prototype.rayMulUp = function (other: BigNumberish) {
return mulDivUp(this, other, RAY);
};
BigNumber.prototype.rayMulDown = function (other: BigNumberish) {
return mulDivDown(this, other, RAY);
};
BigNumber.prototype.rayDiv = function (other: BigNumberish) {
return mulDivHalfUp(this, RAY, other);
};
BigNumber.prototype.rayDivUp = function (other: BigNumberish) {
return mulDivUp(this, RAY, other);
};
BigNumber.prototype.rayDivDown = function (other: BigNumberish) {
return mulDivDown(this, RAY, other);
};
BigNumber.prototype.rayAvg = function (other: BigNumberish, ray: BigNumberish) {
return avgHalfUp(this, other, ray, RAY);
};
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.rayToDecimals(4);
};
BigNumber.prototype.rayToWad = function () {
return this.rayToDecimals(18);
};
BigNumber.prototype.formatRay = function (digits?: number) {
return this.format(27, digits);
};
BigNumber.prototype.toRayFloat = function () {
return this.toFloat(27);
};

BigNumber.PERCENT = PERCENT;
BigNumber.HALF_PERCENT = HALF_PERCENT;
BigNumber.WAD = WAD;
BigNumber.HALF_WAD = HALF_WAD;
BigNumber.RAY = RAY;
BigNumber.HALF_RAY = HALF_RAY;

BigNumber.min = min;
BigNumber.max = max;
BigNumber.sum = (others: BigNumberish[]) => sum(0, others);

BigNumber.pow10 = pow10;
BigNumber.parsePercent = parsePercent;
BigNumber.parseWad = parseWad;
BigNumber.parseRay = parseRay;
Loading

0 comments on commit 8d0a0d5

Please sign in to comment.