Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

feat: native bigint support, modernization of sdk #69

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: remove dependency to instanceof for JSBI
  • Loading branch information
koraykoska committed Jul 1, 2023
commit 4d560ca5068403c592f1fff6ecea39fd6fab54ef
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
"uniswap",
"ethereum"
],
"module": "dist/sdk-core.esm.js",
"scripts": {
"build": "tsc",
"test": "jest",
14 changes: 3 additions & 11 deletions src/entities/fractions/fraction.ts
Original file line number Diff line number Diff line change
@@ -32,22 +32,14 @@ export class Fraction {
public readonly _denominator: bigint

public constructor(numerator: BigintIsh, denominator: BigintIsh = 1n) {
if (numerator instanceof JSBI) {
this._numerator = BigInt(numerator.toString(10))
} else {
this._numerator = BigInt(numerator as BigintIshNonJSBI)
}
this._numerator = BigInt(numerator.toString(10))

if (denominator instanceof JSBI) {
this._denominator = BigInt(denominator.toString(10))
} else {
this._denominator = BigInt(denominator as BigintIshNonJSBI)
}
this._denominator = BigInt(denominator.toString(10))
}

private static tryParseFraction(fractionish: BigintIsh | Fraction): Fraction {
if (
fractionish instanceof JSBI ||
(typeof fractionish === 'object' && fractionish.constructor === JSBI) ||
typeof fractionish === 'bigint' ||
typeof fractionish === 'number' ||
typeof fractionish === 'string'
18 changes: 9 additions & 9 deletions src/utils/sqrt.ts
Original file line number Diff line number Diff line change
@@ -10,20 +10,20 @@ export const MAX_SAFE_INTEGER_BIGINT = BigInt(Number.MAX_SAFE_INTEGER)
*/
export function sqrt<T extends JSBI | bigint>(value: T): T {
let bigIntValue: bigint
if (value instanceof JSBI) {
bigIntValue = BigInt(value.toString(10))
} else {
if (typeof value === 'bigint') {
bigIntValue = value
} else {
bigIntValue = BigInt(value.toString(10))
}

invariant(bigIntValue >= 0n, 'NEGATIVE')

// rely on built in sqrt if possible
if (bigIntValue < MAX_SAFE_INTEGER_BIGINT) {
if (value instanceof JSBI) {
return JSBI.BigInt(Math.floor(Math.sqrt(Number(bigIntValue)))) as T
} else {
if (typeof value === 'bigint') {
return BigInt(Math.floor(Math.sqrt(Number(bigIntValue)))) as T
} else {
return JSBI.BigInt(Math.floor(Math.sqrt(Number(bigIntValue)))) as T
}
}

@@ -36,9 +36,9 @@ export function sqrt<T extends JSBI | bigint>(value: T): T {
x = (bigIntValue / x + x) / 2n
}

if (value instanceof JSBI) {
return JSBI.BigInt(z.toString(10)) as T
} else {
if (typeof value === 'bigint') {
return z as T
} else {
return JSBI.BigInt(z.toString(10)) as T
}
}