Skip to content

Commit

Permalink
feat(v2-sdk): separate esm / cjs builds
Browse files Browse the repository at this point in the history
  • Loading branch information
just-toby committed Dec 16, 2024
1 parent c6c87a9 commit 401913b
Show file tree
Hide file tree
Showing 20 changed files with 2,134 additions and 683 deletions.
14 changes: 14 additions & 0 deletions sdks/v2-sdk/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
}
}
60 changes: 50 additions & 10 deletions sdks/v2-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,48 @@
"ethereum"
],
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"module": "dist/v2-sdk.esm.js",
"main": "./dist/cjs/src/index.js",
"module": "./dist/esm/src/index.js",
"types": "./dist/types/src/index.d.ts",
"files": [
"dist"
],
"engines": {
"node": ">=10"
},
"scripts": {
"build": "tsdx build",
"lint": "tsdx lint src",
"release": "semantic-release",
"start": "tsdx watch",
"test": "tsdx test"
"clean": "rm -rf dist",
"build": "yarn clean && yarn build:cjs && yarn build:esm && yarn build:types",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:esm": "tsc -p tsconfig.esm.json",
"build:types": "tsc -p tsconfig.types.json",
"lint": "eslint src --ext .ts",
"lint:fix": "eslint src --ext .ts --fix",
"test": "jest"
},
"dependencies": {
"@ethersproject/address": "^5.0.2",
"@ethersproject/solidity": "^5.0.9",
"@typescript-eslint/eslint-plugin": "8.18.1",
"@uniswap/sdk-core": "^6.0.0",
"eslint-config-prettier": "9.1.0",
"prettier": "3.4.2",
"tiny-invariant": "^1.1.0",
"tiny-warning": "^1.0.3"
},
"devDependencies": {
"@types/big.js": "^4.0.5",
"@types/jest": "^24.0.25",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"@uniswap/v2-core": "^1.0.1",
"eslint-config-react-app": "7.0.1",
"tsdx": "^0.14.1"
"eslint": "^8.0.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.0.0",
"prettier": "^3.0.0",
"ts-jest": "^29.0.0",
"typescript": "^4.3.3"
},
"prettier": {
"printWidth": 120,
Expand Down Expand Up @@ -74,5 +87,32 @@
}
]
]
},
"exports": {
".": {
"types": "./dist/types/src/index.d.ts",
"import": "./dist/esm/src/index.js",
"require": "./dist/cjs/src/index.js"
}
},
"sideEffects": false,
"jest": {
"transform": {
"^.+\\.(t|j)sx?$": [
"ts-jest",
{
"tsconfig": "tsconfig.test.json"
}
]
},
"testEnvironment": "node",
"extensionsToTreatAsEsm": [
".ts"
],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"testRegex": ".*\\.test\\.ts$",
"rootDir": "src"
}
}
6 changes: 3 additions & 3 deletions sdks/v2-sdk/src/constants.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { INIT_CODE_HASH } from './constants'

import { bytecode } from '@uniswap/v2-core/build/UniswapV2Pair.json'
import { keccak256 } from '@ethersproject/solidity'
import { bytecode } from '@uniswap/v2-core/build/UniswapV2Pair.json'

import { INIT_CODE_HASH } from './constants'

// this _could_ go in constants, except that it would cost every consumer of the sdk the CPU to compute the hash
// and load the JSON.
Expand Down
29 changes: 16 additions & 13 deletions sdks/v2-sdk/src/entities/entities.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WETH9 as _WETH9, CurrencyAmount, Token, TradeType } from '@uniswap/sdk-core'
import JSBI from 'jsbi'
import invariant from 'tiny-invariant'
import { WETH9 as _WETH9, TradeType, Token, CurrencyAmount } from '@uniswap/sdk-core'

import { Pair, Route, Trade } from '../index'

const ADDRESSES = [
Expand Down Expand Up @@ -33,15 +34,15 @@ describe('entities', () => {
pairs = [
new Pair(
CurrencyAmount.fromRawAmount(tokens[0], decimalize(1, tokens[0].decimals)),
CurrencyAmount.fromRawAmount(tokens[1], decimalize(1, tokens[1].decimals))
CurrencyAmount.fromRawAmount(tokens[1], decimalize(1, tokens[1].decimals)),
),
new Pair(
CurrencyAmount.fromRawAmount(tokens[1], decimalize(1, tokens[1].decimals)),
CurrencyAmount.fromRawAmount(tokens[2], decimalize(1, tokens[2].decimals))
CurrencyAmount.fromRawAmount(tokens[2], decimalize(1, tokens[2].decimals)),
),
new Pair(
CurrencyAmount.fromRawAmount(tokens[2], decimalize(1, tokens[2].decimals)),
CurrencyAmount.fromRawAmount(WETH9, decimalize(1234, WETH9.decimals))
CurrencyAmount.fromRawAmount(WETH9, decimalize(1234, WETH9.decimals)),
),
]
})
Expand All @@ -59,13 +60,15 @@ describe('entities', () => {
invariant(route.input.isToken)
invariant(route.output.isToken)
expect(
route.midPrice.quote(CurrencyAmount.fromRawAmount(route.input, decimalize(1, route.input.decimals))).toExact()
route.midPrice
.quote(CurrencyAmount.fromRawAmount(route.input, decimalize(1, route.input.decimals)))
.toExact(),
).toEqual(CurrencyAmount.fromRawAmount(route.output, decimalize(1234, route.output.decimals)).toExact())
expect(
route.midPrice
.invert()
.quote(CurrencyAmount.fromRawAmount(route.output, decimalize(1234, route.output.decimals)))
.toExact()
.toExact(),
).toEqual(CurrencyAmount.fromRawAmount(route.input, decimalize(1, route.input.decimals)).toExact())

expect(route.midPrice.invert().toSignificant(5)).toEqual('0.00081037')
Expand All @@ -80,11 +83,11 @@ describe('entities', () => {
[
new Pair(
CurrencyAmount.fromRawAmount(tokens[1], decimalize(5, tokens[1].decimals)),
CurrencyAmount.fromRawAmount(WETH9, decimalize(10, WETH9.decimals))
CurrencyAmount.fromRawAmount(WETH9, decimalize(10, WETH9.decimals)),
),
],
tokens[1],
WETH9
WETH9,
)
const inputAmount = CurrencyAmount.fromRawAmount(tokens[1], decimalize(1, tokens[1].decimals))
const expectedOutputAmount = CurrencyAmount.fromRawAmount(WETH9, '1662497915624478906')
Expand Down Expand Up @@ -129,19 +132,19 @@ describe('entities', () => {
WETH9,
JSBI.add(
decimalize(10, WETH9.decimals),
tokens[1].decimals === 9 ? JSBI.BigInt('30090280812437312') : JSBI.BigInt('30090270812437322')
)
)
tokens[1].decimals === 9 ? JSBI.BigInt('30090280812437312') : JSBI.BigInt('30090270812437322'),
),
),
),
],
tokens[1],
WETH9
WETH9,
)
const outputAmount = CurrencyAmount.fromRawAmount(tokens[1], '1')
const trade = new Trade(route, outputAmount, TradeType.EXACT_INPUT)

expect(trade.priceImpact.toSignificant(18)).toEqual(
tokens[1].decimals === 9 ? '0.300000099400899902' : '0.3000000000000001'
tokens[1].decimals === 9 ? '0.300000099400899902' : '0.3000000000000001',
)
}
})
Expand Down
Loading

0 comments on commit 401913b

Please sign in to comment.