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
Show file tree
Hide file tree
Changes from 11 commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
dist
node_modules

package-lock.json
koraykoska marked this conversation as resolved.
Show resolved Hide resolved
yarn.lock
koraykoska marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 27 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
// preset: 'ts-jest',
// testEnvironment: 'node',
koraykoska marked this conversation as resolved.
Show resolved Hide resolved
koraykoska marked this conversation as resolved.
Show resolved Hide resolved
preset: 'ts-jest',
testEnvironment: 'jsdom',
transform: {
// transform files with ts-jest
'^.+\\.(js|ts)$': [
'ts-jest',
{
tsconfig: {
// allow js in typescript
allowJs: true,
},
},
],
},
transformIgnorePatterns: [
// allow lit-html transformation
'node_modules/(?!lit-html)',
],
// for absolute imports
moduleNameMapper: {
'src/(.*)': '<rootDir>/src/$1',
},
}
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@uniswap/sdk-core",
"license": "MIT",
"version": "3.2.6",
"version": "4.0.0",
"description": "⚒️ An SDK for building applications on top of Uniswap V3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand All @@ -13,12 +13,10 @@
"uniswap",
"ethereum"
],
"module": "dist/sdk-core.esm.js",
"scripts": {
"build": "tsdx build",
"start": "tsdx watch",
"test": "tsdx test",
"prepublishOnly": "tsdx build"
"build": "tsc",
"test": "jest",
"prepublishOnly": "tsc"
},
"dependencies": {
"@ethersproject/address": "^5.0.2",
Expand All @@ -31,7 +29,11 @@
"devDependencies": {
"@types/big.js": "^4.0.5",
"@types/jest": "^24.0.25",
"tsdx": "^0.14.1"
"tslib": "^2.5.1",
koraykoska marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "^5.0.4",
"jest": "^29.5.0",
"ts-jest": "^29.1.0",
"jest-environment-jsdom": "^29.5.0"
},
"engines": {
"node": ">=10"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ether, Token } from './index'
import { Ether, Token } from 'src/entities/index'
koraykoska marked this conversation as resolved.
Show resolved Hide resolved

describe('Currency', () => {
const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ether } from './ether'
import { Ether } from 'src/entities/ether'

describe('Ether', () => {
it('static constructor uses cache', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import JSBI from 'jsbi'
import { MaxUint256 } from '../../constants'
import { Ether } from '../ether'
import { Token } from '../token'
import { CurrencyAmount } from './currencyAmount'
import { Percent } from './percent'
import { MaxUint256 } from 'src/constants'
import { Ether } from 'src/entities/ether'
import { Token } from 'src/entities/token'
import { CurrencyAmount } from 'src/entities/fractions/currencyAmount'
import { Percent } from 'src/entities/fractions/percent'

describe('CurrencyAmount', () => {
const ADDRESS_ONE = '0x0000000000000000000000000000000000000001'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import JSBI from 'jsbi'
import { Fraction } from './fraction'
import { Fraction } from 'src/entities/fractions/fraction'

describe('Fraction', () => {
describe('#quotient', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Percent } from './percent'
import { Percent } from 'src/entities/fractions/percent'

describe('Percent', () => {
describe('constructor', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Token } from '../token'
import { CurrencyAmount } from './currencyAmount'
import { Price } from './price'
import { Token } from 'src/entities/token'
import { CurrencyAmount } from 'src/entities/fractions/currencyAmount'
import { Price } from 'src/entities/fractions/price'

describe('Price', () => {
const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000'
Expand All @@ -20,7 +20,7 @@ describe('Price', () => {
it('object format works', () => {
const price = new Price({
baseAmount: CurrencyAmount.fromRawAmount(t0, 1),
quoteAmount: CurrencyAmount.fromRawAmount(t1, 54321)
quoteAmount: CurrencyAmount.fromRawAmount(t1, 54321),
})
expect(price.toSignificant(5)).toEqual('54321')
expect(price.baseCurrency.equals(t0))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Token } from './token'
import { Token } from 'src/entities/token'

describe('Token', () => {
const ADDRESS_ONE = '0x0000000000000000000000000000000000000001'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CurrencyAmount, Ether, Percent, Price, Token } from '../entities'
import { computePriceImpact } from './computePriceImpact'
import { CurrencyAmount, Ether, Percent, Price, Token } from 'src/entities'
import { computePriceImpact } from 'src/utils/computePriceImpact'

describe('#computePriceImpact', () => {
const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sortedInsert } from './sortedInsert'
import { sortedInsert } from 'src/utils/sortedInsert'

describe('#sortedInsert', () => {
const comp = (a: number, b: number) => a - b
Expand Down
4 changes: 2 additions & 2 deletions src/utils/sqrt.test.ts → src/__tests__/utils/sqrt.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSBI from 'jsbi'
import { MaxUint256 } from '../constants'
import { sqrt } from './sqrt'
import { MaxUint256 } from 'src/constants'
import { sqrt } from 'src/utils/sqrt'

describe('#sqrt', () => {
it('correct for 0-1000', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkValidAddress, validateAndParseAddress } from './validateAndParseAddress'
import { checkValidAddress, validateAndParseAddress } from 'src/utils/validateAndParseAddress'

describe('#validateAndParseAddress', () => {
it('returns same address if already checksummed', () => {
Expand Down
7 changes: 4 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import JSBI from 'jsbi'

// exports for external consumption
export type BigintIsh = JSBI | string | number
export type BigintIsh = bigint | JSBI | string | number

export enum TradeType {
EXACT_INPUT,
EXACT_OUTPUT
EXACT_OUTPUT,
}

export enum Rounding {
ROUND_DOWN,
ROUND_HALF_UP,
ROUND_UP
ROUND_UP,
}

export const MaxUint256 = JSBI.BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
export const MaxUint256BigInt = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
27 changes: 15 additions & 12 deletions src/entities/fractions/currencyAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import { Fraction } from './fraction'
import _Big from 'big.js'

import toFormat from 'toformat'
import { BigintIsh, Rounding, MaxUint256 } from '../../constants'
import { BigintIsh, Rounding, MaxUint256BigInt } from '../../constants'

const Big = toFormat(_Big)

export class CurrencyAmount<T extends Currency> extends Fraction {
public readonly currency: T
public readonly decimalScale: JSBI
public get decimalScale(): JSBI {
return JSBI.BigInt(this._decimalScale.toString(10))
}
public readonly _decimalScale: bigint

/**
* Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
Expand All @@ -39,39 +42,39 @@ export class CurrencyAmount<T extends Currency> extends Fraction {

protected constructor(currency: T, numerator: BigintIsh, denominator?: BigintIsh) {
super(numerator, denominator)
invariant(JSBI.lessThanOrEqual(this.quotient, MaxUint256), 'AMOUNT')
invariant(this.quotientBigInt <= MaxUint256BigInt, 'AMOUNT')
this.currency = currency
this.decimalScale = JSBI.exponentiate(JSBI.BigInt(10), JSBI.BigInt(currency.decimals))
this._decimalScale = 10n ** BigInt(currency.decimals)
}

public add(other: CurrencyAmount<T>): CurrencyAmount<T> {
invariant(this.currency.equals(other.currency), 'CURRENCY')
const added = super.add(other)
return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator)
return CurrencyAmount.fromFractionalAmount(this.currency, added._numerator, added._denominator)
}

public subtract(other: CurrencyAmount<T>): CurrencyAmount<T> {
invariant(this.currency.equals(other.currency), 'CURRENCY')
const subtracted = super.subtract(other)
return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator)
return CurrencyAmount.fromFractionalAmount(this.currency, subtracted._numerator, subtracted._denominator)
}

public multiply(other: Fraction | BigintIsh): CurrencyAmount<T> {
const multiplied = super.multiply(other)
return CurrencyAmount.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator)
return CurrencyAmount.fromFractionalAmount(this.currency, multiplied._numerator, multiplied._denominator)
}

public divide(other: Fraction | BigintIsh): CurrencyAmount<T> {
const divided = super.divide(other)
return CurrencyAmount.fromFractionalAmount(this.currency, divided.numerator, divided.denominator)
return CurrencyAmount.fromFractionalAmount(this.currency, divided._numerator, divided._denominator)
}

public toSignificant(
significantDigits: number = 6,
format?: object,
rounding: Rounding = Rounding.ROUND_DOWN
): string {
return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding)
return super.divide(this._decimalScale).toSignificant(significantDigits, format, rounding)
}

public toFixed(
Expand All @@ -80,16 +83,16 @@ export class CurrencyAmount<T extends Currency> extends Fraction {
rounding: Rounding = Rounding.ROUND_DOWN
): string {
invariant(decimalPlaces <= this.currency.decimals, 'DECIMALS')
return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding)
return super.divide(this._decimalScale).toFixed(decimalPlaces, format, rounding)
}

public toExact(format: object = { groupSeparator: '' }): string {
Big.DP = this.currency.decimals
return new Big(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format)
return new Big(this.quotientBigInt.toString()).div(this._decimalScale.toString()).toFormat(format)
}

public get wrapped(): CurrencyAmount<Token> {
if (this.currency.isToken) return this as CurrencyAmount<Token>
return CurrencyAmount.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator)
return CurrencyAmount.fromFractionalAmount(this.currency.wrapped, this._numerator, this._denominator)
}
}
Loading