Skip to content

Commit

Permalink
feat(constants): add halves of common units
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Feb 4, 2023
1 parent 630431c commit 95464c1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
61 changes: 51 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ BigNumber.RAY;

#### `PERCENT`

Returns the common PERCENT unit, which is also known as 100% in basis points
Returns the common PERCENT unit, which is also known as `100%` in basis points

_Most commonly used as Aave's `PERCENTAGE_FACTOR`_

Expand All @@ -172,6 +172,47 @@ PERCENT;
BigNumber.PERCENT;
```

#### `HALF_WAD`

Returns half of the common WAD unit, which is also known as `0.5 ether` in Solidity

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

// Returns a 1 followed by 18 zeros: 1000000000000000000
HALF_WAD;
BigNumber.HALF_WAD;
```

#### `HALF_RAY`

Returns half of the common RAY unit, which is also known as `0.5e9 ether` in Solidity

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

// Returns a 1 followed by 27 zeros: 1000000000000000000000000000
HALF_RAY;
BigNumber.HALF_RAY;
```

#### `HALF_PERCENT`

Returns the common PERCENT unit, which is also known as `50%` in basis points

_Most commonly used as Aave's `HALF_PERCENTAGE_FACTOR`_

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

// Returns a 1 followed by 4 zeros: 10000
HALF_PERCENT;
BigNumber.HALF_PERCENT;
```

### Wad-based utilities

#### `wadMul`
Expand All @@ -196,7 +237,7 @@ Returns the result of the addition of a BigNumberish and a wad-based percentage

```typescript
BigNumber.WAD.wadAdd(
BigNumber.WAD.div(2) // 50% in wad
BigNumber.HALF_WAD // 50% in wad
); // 1.0 * (1.0 + 0.5) = 1.5 (in wad)
```

Expand All @@ -206,7 +247,7 @@ Returns the result of the subtraction of a BigNumberish and a wad-based percenta

```typescript
BigNumber.WAD.wadSub(
BigNumber.WAD.div(2) // 50% in wad
BigNumber.HALF_WAD // 50% in wad
); // 1.0 * (1.0 - 0.5) = 0.5 (in wad)
```

Expand All @@ -217,7 +258,7 @@ Returns the weighted average of 2 BigNumberishs, using a wad-based weight (18 de
```typescript
BigNumber.WAD.wadAvg(
BigNumber.WAD.mul(2), // 2 WAD
BigNumber.WAD.div(2) // 50% in WAD
BigNumber.HALF_WAD // 50% in WAD
); // 1.0 * (1.0 - 0.5) + 2.0 * 0.5 = 1.5 (in wad)
```

Expand Down Expand Up @@ -320,7 +361,7 @@ Returns the result of the addition of a BigNumberish and a ray-based percentage

```typescript
BigNumber.RAY.rayAdd(
BigNumber.RAY.div(2) // 50% in ray
BigNumber.HALF_RAY // 50% in ray
); // 1.0 * (1.0 + 0.5) = 1.5 (in ray)
```

Expand All @@ -330,7 +371,7 @@ Returns the result of the subtraction of a BigNumberish and a ray-based percenta

```typescript
BigNumber.RAY.raySub(
BigNumber.RAY.div(2) // 50% in ray
BigNumber.HALF_RAY // 50% in ray
); // 1.0 * (1.0 - 0.5) = 0.5 (in ray)
```

Expand All @@ -341,7 +382,7 @@ Returns the weighted average of 2 BigNumberishs, using a ray-based weight (27 de
```typescript
BigNumber.RAY.rayAvg(
BigNumber.RAY.mul(2), // 2 RAY
BigNumber.RAY.div(2) // 50% in RAY
BigNumber.HALF_RAY // 50% in RAY
); // 1.0 * (1.0 - 0.5) + 2.0 * 0.5 = 1.5 (in ray)
```

Expand Down Expand Up @@ -444,7 +485,7 @@ Returns the result of the addition of a BigNumberish and a percent-based percent

```typescript
BigNumber.PERCENT.percentAdd(
BigNumber.PERCENT.div(2) // 50% in percent
BigNumber.HALF_PERCENT // 50% in percent
); // 1.0 * (1.0 + 0.5) = 1.5 (in percent)
```

Expand All @@ -454,7 +495,7 @@ Returns the result of the subtraction of a BigNumberish and a percent-based perc

```typescript
BigNumber.PERCENT.percentSub(
BigNumber.PERCENT.div(2) // 50% in percent
BigNumber.HALF_PERCENT // 50% in percent
); // 1.0 * (1.0 - 0.5) = 0.5 (in percent)
```

Expand All @@ -465,7 +506,7 @@ Returns the weighted average of 2 BigNumberishs, using a percent-based weight (4
```typescript
BigNumber.PERCENT.percentAvg(
BigNumber.PERCENT.mul(2), // 2 PERCENT
BigNumber.PERCENT.div(2) // 50% in PERCENT
BigNumber.HALF_PERCENT // 50% in PERCENT
); // 1.0 * (1.0 - 0.5) + 2.0 * 0.5 = 1.5 (in percent)
```

Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const WAD = pow10(18);
export const RAY = pow10(27);
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);

Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
HALF_WAD_PERCENT_RATIO,
PERCENT,
RAY,
HALF_PERCENT,
HALF_RAY,
HALF_WAD,
RAY_PERCENT_RATIO,
RAY_WAD_RATIO,
WAD,
Expand Down Expand Up @@ -87,8 +90,11 @@ declare module "@ethersproject/bignumber/lib/bignumber" {

namespace BigNumber {
let PERCENT: BigNumber;
let HALF_PERCENT: BigNumber;
let WAD: BigNumber;
let HALF_WAD: BigNumber;
let RAY: BigNumber;
let HALF_RAY: BigNumber;

let min: (other: BigNumberish, ...others: BigNumberish[]) => BigNumber;
let max: (other: BigNumberish, ...others: BigNumberish[]) => BigNumber;
Expand Down Expand Up @@ -264,8 +270,11 @@ BigNumber.prototype.toRayFloat = function () {
};

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;
Expand Down

0 comments on commit 95464c1

Please sign in to comment.