Skip to content

Commit

Permalink
Update the init code hash, other small tweaks (Uniswap#19)
Browse files Browse the repository at this point in the history
* Update the init code hash, other small tweaks

* Lint

* Use pair ABI instead of token ABI to get reserves

* Update package description
Set the prototype of our custom errors

* codepoints -> emoji

* skip the data tests

* Factory address, fix checksum warning

Co-authored-by: Noah Zinsmeister <noahwz@gmail.com>
  • Loading branch information
moodysalem and NoahZinsmeister committed May 5, 2020
1 parent c08d9a2 commit 5826a6d
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
dist
node_modules
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![Actions Status](https://github.com/Uniswap/uniswap-sdk/workflows/CI/badge.svg)](https://github.com/Uniswap/uniswap-sdk)
[![npm version](https://img.shields.io/npm/v/@uniswap/sdk/next.svg)](https://www.npmjs.com/package/@uniswap/sdk/v/next)
[![npm bundle size (scoped version)](https://img.shields.io/bundlephobia/minzip/@uniswap/sdk/2.0.0-beta.23)](https://bundlephobia.com/result?p=@uniswap/sdk@2.0.0-beta.23)

In-depth documentation on this SDK is available at [uniswap.org](https://uniswap.org/docs/v2/SDK/getting-started/).
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@uniswap/sdk",
"license": "GPL-3.0-or-later",
"version": "2.0.0-beta.23",
"description": "🛠 An SDK for building applications on top of Uniswap.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -35,6 +36,7 @@
"devDependencies": {
"@types/big.js": "^4.0.5",
"@types/jest": "^24.0.25",
"@uniswap/v2-core": "1.0.0",
"babel-plugin-transform-jsbi-to-bigint": "^1.3.1",
"tsdx": "^0.12.3"
},
Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export enum Rounding {
ROUND_UP
}

export const FACTORY_ADDRESS = '0xe2f197885abe8ec7c866cFf76605FD06d4576218'
export const FACTORY_ADDRESS = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f'

export const INIT_CODE_HASH = '0x0b77fb54a078d9399fa29cac94f5b35b9f11611b456ab507c7f46754712b642b'
export const INIT_CODE_HASH = '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f'

export const MINIMUM_LIQUIDITY = JSBI.BigInt(1000)

Expand Down
9 changes: 4 additions & 5 deletions src/entities/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
_997,
_1000
} from '../constants'
import ERC20 from '../abis/ERC20.json'
import IUniswapV2Pair from '@uniswap/v2-core/build/IUniswapV2Pair.json'
import { sqrt, parseBigintIsh } from '../utils'
import { InsufficientReservesError, InsufficientInputAmountError } from '../errors'
import { Token } from './token'
Expand Down Expand Up @@ -54,11 +54,10 @@ export class Pair {
tokenB: Token,
provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair> {
invariant(tokenA.chainId === tokenB.chainId, 'CHAIN_ID')
const address = Pair.getAddress(tokenA, tokenB)
const balances = await Promise.all([
new Contract(tokenA.address, ERC20, provider).balanceOf(address),
new Contract(tokenB.address, ERC20, provider).balanceOf(address)
])
const [reserves0, reserves1] = await new Contract(address, IUniswapV2Pair.abi, provider).getReserves()
const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0]
return new Pair(new TokenAmount(tokenA, balances[0]), new TokenAmount(tokenB, balances[1]))
}

Expand Down
5 changes: 5 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// see https://stackoverflow.com/a/41102306
const CAN_SET_PROTOTYPE = 'setPrototypeOf' in Object

export class InsufficientReservesError extends Error {
public constructor() {
super()
this.name = this.constructor.name
if (CAN_SET_PROTOTYPE) Object.setPrototypeOf(this, new.target.prototype)
}
}

export class InsufficientInputAmountError extends Error {
public constructor() {
super()
this.name = this.constructor.name
if (CAN_SET_PROTOTYPE) Object.setPrototypeOf(this, new.target.prototype)
}
}
16 changes: 16 additions & 0 deletions test/constants.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { INIT_CODE_HASH } from '../src/constants'

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

// 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.
const COMPUTED_INIT_CODE_HASH = keccak256(['bytes'], [`0x${bytecode}`])

describe('constants', () => {
describe('INIT_CODE_HASH', () => {
it.only('matches computed bytecode hash', () => {
expect(COMPUTED_INIT_CODE_HASH).toEqual(INIT_CODE_HASH)
})
})
})
5 changes: 3 additions & 2 deletions test/data.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChainId, WETH, Token, Pair } from '../src'

describe('data', () => {
// TODO: replace the provider in these tests
describe.skip('data', () => {
it('Token', async () => {
const token = await Token.fetchData(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F') // DAI
expect(token.decimals).toEqual(18)
Expand All @@ -14,6 +15,6 @@ describe('data', () => {
it('Pair', async () => {
const token = new Token(ChainId.RINKEBY, '0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735', 18) // DAI
const pair = await Pair.fetchData(WETH[ChainId.RINKEBY], token)
expect(pair.liquidityToken.address).toEqual('0x3DDd4674C99979EAd4a3160f12567c90a07f0e94')
expect(pair.liquidityToken.address).toEqual('0x8507903035cFc0Bc7c8c62334dea16E2DA1EcecD')
})
})
12 changes: 12 additions & 0 deletions test/pair.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Token, Pair } from '../src/entities'
import { ChainId } from '../src/constants'

describe('Pair', () => {
describe('#getAddress', () => {
it('returns the correct address', () => {
const usdc = new Token(ChainId.MAINNET, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 18, 'USDC', 'USD Coin')
const dai = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18, 'DAI', 'DAI Stablecoin')
expect(Pair.getAddress(usdc, dai)).toEqual('0xAE461cA67B15dc8dc81CE7615e0320dA1A9aB8D5')
})
})
})
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,11 @@
semver "^6.3.0"
tsutils "^3.17.1"

"@uniswap/v2-core@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@uniswap/v2-core/-/v2-core-1.0.0.tgz#e0fab91a7d53e8cafb5326ae4ca18351116b0844"
integrity sha512-BJiXrBGnN8mti7saW49MXwxDBRFiWemGetE58q8zgfnPPzQKq55ADltEILqOt6VFZ22kVeVKbF8gVd8aY3l7pA==

abab@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.3.tgz#623e2075e02eb2d3f2475e49f99c91846467907a"
Expand Down

0 comments on commit 5826a6d

Please sign in to comment.