Skip to content

Commit

Permalink
add missing import
Browse files Browse the repository at this point in the history
  • Loading branch information
soilking committed May 23, 2024
1 parent 419dca7 commit 7a6944e
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions projects/subgraph-core/utils/Decimals.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { BigDecimal, BigInt } from "@graphprotocol/graph-ts";
import { BigDecimal, BigInt, Bytes } from "@graphprotocol/graph-ts";

export const DEFAULT_DECIMALS = 6;

export const ZERO_BI = BigInt.fromI32(0);
export const ONE_BI = BigInt.fromI32(1);
export const BI_6 = BigInt.fromI32(6);
export const BI_10 = BigInt.fromI32(10);
export const BI_MAX = BigInt.fromUnsignedBytes(Bytes.fromHexString("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
export const BI_MAX = BigInt.fromUnsignedBytes(
Bytes.fromHexString("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
);
export const ZERO_BD = BigDecimal.fromString("0");
export const ONE_BD = BigDecimal.fromString("1");
export const BD_10 = BigDecimal.fromString("10");
Expand All @@ -25,7 +27,10 @@ export function pow(base: BigDecimal, exponent: number): BigDecimal {
return result;
}

export function sqrt(value: BigDecimal, tolerance: BigDecimal = BigDecimal.fromString("0.0000001")): BigDecimal {
export function sqrt(
value: BigDecimal,
tolerance: BigDecimal = BigDecimal.fromString("0.0000001")
): BigDecimal {
if (value.equals(ZERO_BD)) {
return ZERO_BD;
}
Expand All @@ -41,8 +46,10 @@ export function sqrt(value: BigDecimal, tolerance: BigDecimal = BigDecimal.fromS
// Check if the difference is within the tolerance level
if (
lastX.minus(x).equals(ZERO_BD) ||
(lastX.minus(x).toString().startsWith("-") && lastX.minus(x).toString().substring(1) < tolerance.toString()) ||
(!lastX.minus(x).toString().startsWith("-") && lastX.minus(x).toString() < tolerance.toString())
(lastX.minus(x).toString().startsWith("-") &&
lastX.minus(x).toString().substring(1) < tolerance.toString()) ||
(!lastX.minus(x).toString().startsWith("-") &&
lastX.minus(x).toString() < tolerance.toString())
) {
break;
}
Expand All @@ -61,7 +68,9 @@ export function toDecimal(value: BigInt, decimals: number = DEFAULT_DECIMALS): B

export function toBigInt(value: BigDecimal, decimals: number = DEFAULT_DECIMALS): BigInt {
let precision = 10 ** decimals;
return BigInt.fromString(value.times(BigDecimal.fromString(precision.toString())).truncate(0).toString());
return BigInt.fromString(
value.times(BigDecimal.fromString(precision.toString())).truncate(0).toString()
);
}

export function emptyBigIntArray(length: i32): BigInt[] {
Expand Down Expand Up @@ -98,6 +107,10 @@ export function getBigDecimalArrayTotal(detail: BigDecimal[]): BigDecimal {
return total;
}

export function BigDecimal_isClose(value: BigDecimal, target: BigDecimal, window: BigDecimal): boolean {
export function BigDecimal_isClose(
value: BigDecimal,
target: BigDecimal,
window: BigDecimal
): boolean {
return target.minus(window) < value && value < target.plus(window);
}

0 comments on commit 7a6944e

Please sign in to comment.