Skip to content

Commit

Permalink
feat(lib): extract utils to scale-specific domains
Browse files Browse the repository at this point in the history
BREAKING CHANGE: function names changes
  • Loading branch information
Rubilmax committed Sep 29, 2023
1 parent 7b50d72 commit ad89d52
Show file tree
Hide file tree
Showing 8 changed files with 424 additions and 113 deletions.
36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ BigInt.from(1).wadMul(WAD); // 1
BigInt.from(WAD * 2n).rayMul(0.5e27); // WAD
```

If you choose to avoid prototype pollution, you can always import specific utilities:

```typescript
import * as WadMath from "ethers-maths/lib/wad";
import * as RayMath from "ethers-maths/lib/ray";
import * as PercentMath from "ethers-maths/lib/percent";
```

---

## Book
Expand Down Expand Up @@ -125,7 +133,7 @@ Returns whether the BigNumber is approximately close to the given BigNumber, wit

```typescript
// only if you want to avoid BigNumber prototype pollution
import { approxEqAbs } from "ethers-maths/utils";
import { approxEqAbs } from "ethers-maths/lib/utils";

// Returns whether the BigNumber is approximately close to the given BigNumber, within the given tolerance: true
approxEqAbs(0, 1, "1");
Expand All @@ -139,7 +147,7 @@ Returns the minimum between input BigNumberish, as a BigInt

```typescript
// only if you want to avoid BigInt prototype pollution
import { min } from "ethers-maths/utils";
import { min } from "ethers-maths/lib/utils";

// Returns the minimum between input BigNumberish, as a BigInt: 0
min(0, 1, "2", ...);
Expand All @@ -153,7 +161,7 @@ Returns the maximum between input BigNumberish, as a BigInt

```typescript
// only if you want to avoid BigInt prototype pollution
import { max } from "ethers-maths/utils";
import { max } from "ethers-maths/lib/utils";

// Returns the maximum between input BigNumberish, as a BigInt: 2
max(0, 1, "2", ...);
Expand All @@ -167,7 +175,7 @@ Returns the sum of input BigNumberish array, as a BigInt

```typescript
// only if you want to avoid BigInt prototype pollution
import { sum } from "ethers-maths/utils";
import { sum } from "ethers-maths/lib/utils";

// Returns the sum of input BigNumberish array, as a BigInt: 3
sum([0, 1, "2"]);
Expand Down Expand Up @@ -204,7 +212,7 @@ Returns a 1 followed by the input number of zeros (10 raised to the power of the

```typescript
// only if you want to avoid BigInt prototype pollution
import { pow10 } from "ethers-maths/utils";
import { pow10 } from "ethers-maths/lib/utils";

// Returns a 1 followed by the input number of zeros: 100
pow10(2);
Expand All @@ -217,7 +225,7 @@ Performs a multiplication followed by a division, rounded half up

```typescript
// only if you want to avoid BigInt prototype pollution
import { mulDivHalfUp } from "ethers-maths/utils";
import { mulDivHalfUp } from "ethers-maths/lib/utils";

// 1.0 (in wad) * 1 / 1 = 1.0 (in wad)
mulDivHalfUp(BigInt.WAD, 1, 1);
Expand All @@ -230,7 +238,7 @@ Performs a multiplication followed by a division, rounded up

```typescript
// only if you want to avoid BigInt prototype pollution
import { mulDivUp } from "ethers-maths/utils";
import { mulDivUp } from "ethers-maths/lib/utils";

// 0.999999999999999999 * 1 / WAD = 1.0 (in wad, rounded up)
mulDivUp(BigInt.WAD - 1n, 1, BigInt.WAD);
Expand All @@ -243,7 +251,7 @@ Performs a multiplication followed by a division, rounded down

```typescript
// only if you want to avoid BigInt prototype pollution
import { mulDivDown } from "ethers-maths/utils";
import { mulDivDown } from "ethers-maths/lib/utils";

// 1.000000000000000001 * 1 / WAD = 1.0 (in wad, rounded down)
mulDivDown(BigInt.WAD + 1n, 1, BigInt.WAD);
Expand All @@ -262,7 +270,7 @@ _Most commonly used as the ERC20 token unit_

```typescript
// only if you want to avoid BigInt prototype pollution
import { WAD } from "ethers-maths/constants";
import { WAD } from "ethers-maths/lib/constants";

// Returns a 1 followed by 18 zeros: 1000000000000000000
WAD;
Expand All @@ -277,7 +285,7 @@ _Most commonly used as Aave's index unit_

```typescript
// only if you want to avoid BigInt prototype pollution
import { RAY } from "ethers-maths/constants";
import { RAY } from "ethers-maths/lib/constants";

// Returns a 1 followed by 27 zeros: 1000000000000000000000000000
RAY;
Expand All @@ -292,7 +300,7 @@ _Most commonly used as Aave's `PERCENTAGE_FACTOR`_

```typescript
// only if you want to avoid BigInt prototype pollution
import { PERCENT } from "ethers-maths/constants";
import { PERCENT } from "ethers-maths/lib/constants";

// Returns a 1 followed by 4 zeros: 10000
PERCENT;
Expand All @@ -305,7 +313,7 @@ Returns half of the common WAD unit, which is also known as `0.5 ether` in Solid

```typescript
// only if you want to avoid BigInt prototype pollution
import { HALF_WAD } from "ethers-maths/constants";
import { HALF_WAD } from "ethers-maths/lib/constants";

// Returns a 1 followed by 18 zeros: 1000000000000000000
HALF_WAD;
Expand All @@ -318,7 +326,7 @@ Returns half of the common RAY unit, which is also known as `0.5e9 ether` in Sol

```typescript
// only if you want to avoid BigInt prototype pollution
import { HALF_RAY } from "ethers-maths/constants";
import { HALF_RAY } from "ethers-maths/lib/constants";

// Returns a 1 followed by 27 zeros: 1000000000000000000000000000
HALF_RAY;
Expand All @@ -333,7 +341,7 @@ _Most commonly used as Aave's `HALF_PERCENTAGE_FACTOR`_

```typescript
// only if you want to avoid BigInt prototype pollution
import { HALF_PERCENT } from "ethers-maths/constants";
import { HALF_PERCENT } from "ethers-maths/lib/constants";

// Returns a 1 followed by 4 zeros: 10000
HALF_PERCENT;
Expand Down
10 changes: 10 additions & 0 deletions src/comp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BigNumberish, toBigInt } from "ethers";
import { WAD, WAD_SQUARED } from "./constants";

export const compMul = (x: BigNumberish, other: BigNumberish) => {
return (toBigInt(x) * toBigInt(other)) / WAD;
};

export const compDiv = (x: BigNumberish, other: BigNumberish) => {
return (toBigInt(x) * WAD_SQUARED) / toBigInt(other) / WAD;
};
32 changes: 32 additions & 0 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { BigNumberish, formatUnits, toBigInt } from "ethers";
import { pow10 } from "./utils";

export const format = (x: BigNumberish, decimals?: number, digits?: number) => {
const formatted = formatUnits(x, 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 + (digits > 0 ? digits + 1 : 0))
: formatted + "0".repeat(digits - decimals);
};

export const toFloat = (x: BigNumberish, decimals?: number) => {
return parseFloat(format(x, decimals));
};

export const toDecimals = (x: BigNumberish, decimals: number, scaleDecimals: number) => {
x = toBigInt(x);

if (decimals <= scaleDecimals) {
const ratio = pow10(scaleDecimals - decimals);

return (x + ratio / 2n) / ratio;
}

return x * pow10(decimals - scaleDecimals);
};
Loading

0 comments on commit ad89d52

Please sign in to comment.