-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deps): remove ethers dependency
remove dependency on ethers for formatting BREAKING CHANGE: API
- Loading branch information
Showing
13 changed files
with
543 additions
and
755 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
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 compMul = (x: bigint, other: bigint) => { | ||
return (x * other) / WAD; | ||
}; | ||
|
||
export const compDiv = (x: BigNumberish, other: BigNumberish) => { | ||
return (toBigInt(x) * WAD_SQUARED) / toBigInt(other) / WAD; | ||
export const compDiv = (x: bigint, other: bigint) => { | ||
return (x * WAD_SQUARED) / other / WAD; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,38 @@ | ||
import { BigNumberish, formatUnits, toBigInt } from "ethers"; | ||
import { pow10 } from "./utils"; | ||
|
||
export const format = (x: BigNumberish, decimals?: number, digits?: number) => { | ||
const formatted = formatUnits(x, decimals); | ||
export const format = (x: bigint, decimals: number = 18, digits?: number) => { | ||
decimals = Math.floor(decimals); | ||
digits = Math.floor(digits ?? decimals); | ||
|
||
let dotIndex = formatted.indexOf("."); | ||
if (dotIndex < 0) dotIndex = formatted.length; | ||
if (decimals === 0) return x.toString(); | ||
|
||
decimals = formatted.length - 1 - dotIndex; | ||
digits ??= decimals; | ||
let negative = ""; | ||
if (x < 0n) { | ||
negative = "-"; | ||
x *= -1n; | ||
} | ||
|
||
const abs = x.toString().padStart(decimals + 1, "0"); | ||
|
||
const length = abs.length; | ||
const dotIndex = length - decimals; | ||
|
||
return digits < decimals | ||
? formatted.slice(0, dotIndex + (digits > 0 ? digits + 1 : 0)) | ||
: formatted + "0".repeat(digits - decimals); | ||
let full = negative + abs.substring(0, dotIndex); | ||
if (digits > 0) full += "." + abs.substring(dotIndex, dotIndex + digits).padEnd(digits, "0"); | ||
|
||
return full; | ||
}; | ||
|
||
export const toFloat = (x: BigNumberish, decimals?: number) => { | ||
export const toFloat = (x: bigint, decimals?: number) => { | ||
return parseFloat(format(x, decimals)); | ||
}; | ||
|
||
export const toDecimals = (x: BigNumberish, decimals: number, scaleDecimals: number) => { | ||
x = toBigInt(x); | ||
|
||
export const toDecimals = (x: bigint, decimals: number, scaleDecimals: number) => { | ||
if (decimals <= scaleDecimals) { | ||
const ratio = pow10(scaleDecimals - decimals); | ||
const ratio = pow10(BigInt(Math.floor(scaleDecimals - decimals))); | ||
|
||
return (x + ratio / 2n) / ratio; | ||
} | ||
|
||
return x * pow10(decimals - scaleDecimals); | ||
return x * pow10(BigInt(Math.floor(decimals - scaleDecimals))); | ||
}; |
Oops, something went wrong.