Commit 52266e31 authored by Moody Salem's avatar Moody Salem

more cleanup

parent d71be6c7
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
"prepublishOnly": "tsdx build" "prepublishOnly": "tsdx build"
}, },
"dependencies": { "dependencies": {
"@uniswap/sdk-core": "^1.0.6", "@uniswap/sdk-core": "^1.0.7",
"tiny-invariant": "^1.1.0", "tiny-invariant": "^1.1.0",
"tiny-warning": "^1.0.3" "tiny-warning": "^1.0.3"
}, },
......
...@@ -7,7 +7,8 @@ export const INIT_CODE_HASH = '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbe ...@@ -7,7 +7,8 @@ export const INIT_CODE_HASH = '0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbe
export const MINIMUM_LIQUIDITY = JSBI.BigInt(1000) export const MINIMUM_LIQUIDITY = JSBI.BigInt(1000)
// exports for internal consumption // exports for internal consumption
export const THREE = JSBI.BigInt(3) export const ZERO = JSBI.BigInt(0)
export const ONE = JSBI.BigInt(1)
export const FIVE = JSBI.BigInt(5) export const FIVE = JSBI.BigInt(5)
export const _997 = JSBI.BigInt(997) export const _997 = JSBI.BigInt(997)
export const _1000 = JSBI.BigInt(1000) export const _1000 = JSBI.BigInt(1000)
import { ChainId, WETH, Token } from '@uniswap/sdk-core'
import { Fetcher } from './fetcher'
// TODO: replace the provider in these tests
describe.skip('data', () => {
it('Token', async () => {
const token = await Fetcher.fetchTokenData(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F') // DAI
expect(token.decimals).toEqual(18)
})
it('Token:CACHE', async () => {
const token = await Fetcher.fetchTokenData(ChainId.MAINNET, '0xE0B7927c4aF23765Cb51314A0E0521A9645F0E2A') // DGD
expect(token.decimals).toEqual(9)
})
it('Pair', async () => {
const token = new Token(ChainId.RINKEBY, '0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735', 18) // DAI
const pair = await Fetcher.fetchPairData(WETH[ChainId.RINKEBY], token)
expect(pair.liquidityToken.address).toEqual('0x8B22F85d0c844Cf793690F6D9DFE9F11Ddb35449')
})
})
import JSBI from 'jsbi' import JSBI from 'jsbi'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
import { ChainId, WETH as _WETH, TradeType, Rounding, Token, TokenAmount } from '@uniswap/sdk-core' import { ChainId, WETH9 as _WETH9, TradeType, Rounding, Token, TokenAmount } from '@uniswap/sdk-core'
import { Pair, Route, Trade } from '../index' import { Pair, Route, Trade } from '../index'
const ADDRESSES = [ const ADDRESSES = [
...@@ -9,7 +9,7 @@ const ADDRESSES = [ ...@@ -9,7 +9,7 @@ const ADDRESSES = [
'0x0000000000000000000000000000000000000003' '0x0000000000000000000000000000000000000003'
] ]
const CHAIN_ID = ChainId.RINKEBY const CHAIN_ID = ChainId.RINKEBY
const WETH = _WETH[ChainId.RINKEBY] const WETH9 = _WETH9[ChainId.RINKEBY]
const DECIMAL_PERMUTATIONS: [number, number, number][] = [ const DECIMAL_PERMUTATIONS: [number, number, number][] = [
[0, 0, 0], [0, 0, 0],
[0, 9, 18], [0, 9, 18],
...@@ -46,7 +46,7 @@ describe('entities', () => { ...@@ -46,7 +46,7 @@ describe('entities', () => {
), ),
new Pair( new Pair(
new TokenAmount(tokens[2], decimalize(1, tokens[2].decimals)), new TokenAmount(tokens[2], decimalize(1, tokens[2].decimals)),
new TokenAmount(WETH, decimalize(1234, WETH.decimals)) new TokenAmount(WETH9, decimalize(1234, WETH9.decimals))
) )
] ]
}) })
...@@ -55,9 +55,9 @@ describe('entities', () => { ...@@ -55,9 +55,9 @@ describe('entities', () => {
it('Route', () => { it('Route', () => {
route = new Route(pairs, tokens[0]) route = new Route(pairs, tokens[0])
expect(route.pairs).toEqual(pairs) expect(route.pairs).toEqual(pairs)
expect(route.path).toEqual(tokens.concat([WETH])) expect(route.path).toEqual(tokens.concat([WETH9]))
expect(route.input).toEqual(tokens[0]) expect(route.input).toEqual(tokens[0])
expect(route.output).toEqual(WETH) expect(route.output).toEqual(WETH9)
}) })
it('Price:Route.midPrice', () => { it('Price:Route.midPrice', () => {
...@@ -106,13 +106,13 @@ describe('entities', () => { ...@@ -106,13 +106,13 @@ describe('entities', () => {
[ [
new Pair( new Pair(
new TokenAmount(tokens[1], decimalize(5, tokens[1].decimals)), new TokenAmount(tokens[1], decimalize(5, tokens[1].decimals)),
new TokenAmount(WETH, decimalize(10, WETH.decimals)) new TokenAmount(WETH9, decimalize(10, WETH9.decimals))
) )
], ],
tokens[1] tokens[1]
) )
const inputAmount = new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)) const inputAmount = new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals))
const expectedOutputAmount = new TokenAmount(WETH, '1662497915624478906') const expectedOutputAmount = new TokenAmount(WETH9, '1662497915624478906')
const trade = new Trade(route, inputAmount, TradeType.EXACT_INPUT) const trade = new Trade(route, inputAmount, TradeType.EXACT_INPUT)
expect(trade.route).toEqual(route) expect(trade.route).toEqual(route)
expect(trade.tradeType).toEqual(TradeType.EXACT_INPUT) expect(trade.tradeType).toEqual(TradeType.EXACT_INPUT)
...@@ -131,7 +131,7 @@ describe('entities', () => { ...@@ -131,7 +131,7 @@ describe('entities', () => {
}) })
it('TradeType.EXACT_OUTPUT', () => { it('TradeType.EXACT_OUTPUT', () => {
const outputAmount = new TokenAmount(WETH, '1662497915624478906') const outputAmount = new TokenAmount(WETH9, '1662497915624478906')
const expectedInputAmount = new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)) const expectedInputAmount = new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals))
const trade = new Trade(route, outputAmount, TradeType.EXACT_OUTPUT) const trade = new Trade(route, outputAmount, TradeType.EXACT_OUTPUT)
expect(trade.route).toEqual(route) expect(trade.route).toEqual(route)
...@@ -157,9 +157,9 @@ describe('entities', () => { ...@@ -157,9 +157,9 @@ describe('entities', () => {
new Pair( new Pair(
new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)), new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)),
new TokenAmount( new TokenAmount(
WETH, WETH9,
JSBI.add( JSBI.add(
decimalize(10, WETH.decimals), decimalize(10, WETH9.decimals),
tokens[1].decimals === 9 ? JSBI.BigInt('30090280812437312') : JSBI.BigInt('30090270812437322') tokens[1].decimals === 9 ? JSBI.BigInt('30090280812437312') : JSBI.BigInt('30090270812437322')
) )
) )
...@@ -178,7 +178,7 @@ describe('entities', () => { ...@@ -178,7 +178,7 @@ describe('entities', () => {
}) })
it('TokenAmount', () => { it('TokenAmount', () => {
const amount = new TokenAmount(WETH, '1234567000000000000000') const amount = new TokenAmount(WETH9, '1234567000000000000000')
expect(amount.toExact()).toEqual('1234.567') expect(amount.toExact()).toEqual('1234.567')
expect(amount.toExact({ groupSeparator: ',' })).toEqual('1,234.567') expect(amount.toExact({ groupSeparator: ',' })).toEqual('1,234.567')
}) })
......
import { ChainId, Token, TokenAmount, WETH, Price } from '@uniswap/sdk-core' import { ChainId, Token, TokenAmount, WETH9, Price } from '@uniswap/sdk-core'
import { InsufficientInputAmountError } from '../errors' import { InsufficientInputAmountError } from '../errors'
import { Pair } from './pair' import { Pair } from './pair'
...@@ -8,7 +8,7 @@ describe('Pair', () => { ...@@ -8,7 +8,7 @@ describe('Pair', () => {
describe('constructor', () => { describe('constructor', () => {
it('cannot be used for tokens on different chains', () => { it('cannot be used for tokens on different chains', () => {
expect(() => new Pair(new TokenAmount(USDC, '100'), new TokenAmount(WETH[ChainId.RINKEBY], '100'))).toThrow( expect(() => new Pair(new TokenAmount(USDC, '100'), new TokenAmount(WETH9[ChainId.RINKEBY], '100'))).toThrow(
'CHAIN_IDS' 'CHAIN_IDS'
) )
}) })
...@@ -83,7 +83,7 @@ describe('Pair', () => { ...@@ -83,7 +83,7 @@ describe('Pair', () => {
}) })
it('throws if invalid token', () => { it('throws if invalid token', () => {
expect(() => pair.priceOf(WETH[ChainId.MAINNET])).toThrow('TOKEN') expect(() => pair.priceOf(WETH9[ChainId.MAINNET])).toThrow('TOKEN')
}) })
}) })
...@@ -99,7 +99,7 @@ describe('Pair', () => { ...@@ -99,7 +99,7 @@ describe('Pair', () => {
it('throws if not in the pair', () => { it('throws if not in the pair', () => {
expect(() => expect(() =>
new Pair(new TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserveOf(WETH[ChainId.MAINNET]) new Pair(new TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserveOf(WETH9[ChainId.MAINNET])
).toThrow('TOKEN') ).toThrow('TOKEN')
}) })
}) })
...@@ -114,7 +114,7 @@ describe('Pair', () => { ...@@ -114,7 +114,7 @@ describe('Pair', () => {
expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).involvesToken(USDC)).toEqual(true) expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).involvesToken(USDC)).toEqual(true)
expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).involvesToken(DAI)).toEqual(true) expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).involvesToken(DAI)).toEqual(true)
expect( expect(
new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).involvesToken(WETH[ChainId.MAINNET]) new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).involvesToken(WETH9[ChainId.MAINNET])
).toEqual(false) ).toEqual(false)
}) })
describe('miscellaneous', () => { describe('miscellaneous', () => {
......
import { babylonianSqrt, BigintIsh, ChainId, ONE, Price, Token, TokenAmount, ZERO } from '@uniswap/sdk-core' import { babylonianSqrt, BigintIsh, ChainId, Price, Token, TokenAmount } from '@uniswap/sdk-core'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
import JSBI from 'jsbi' import JSBI from 'jsbi'
import { pack, keccak256 } from '@ethersproject/solidity' import { pack, keccak256 } from '@ethersproject/solidity'
import { getCreate2Address } from '@ethersproject/address' import { getCreate2Address } from '@ethersproject/address'
import { FACTORY_ADDRESS, INIT_CODE_HASH, MINIMUM_LIQUIDITY, FIVE, _997, _1000 } from '../constants' import { FACTORY_ADDRESS, INIT_CODE_HASH, MINIMUM_LIQUIDITY, FIVE, _997, _1000, ONE, ZERO } from '../constants'
import { parseBigintIsh } from '../utils' import { parseBigintIsh } from '../utils'
import { InsufficientReservesError, InsufficientInputAmountError } from '../errors' import { InsufficientReservesError, InsufficientInputAmountError } from '../errors'
let PAIR_ADDRESS_CACHE: { [token0Address: string]: { [token1Address: string]: string } } = {}
export class Pair { export class Pair {
public readonly liquidityToken: Token public readonly liquidityToken: Token
private readonly tokenAmounts: [TokenAmount, TokenAmount] private readonly tokenAmounts: [TokenAmount, TokenAmount]
...@@ -17,22 +15,12 @@ export class Pair { ...@@ -17,22 +15,12 @@ export class Pair {
public static getAddress(tokenA: Token, tokenB: Token): string { public static getAddress(tokenA: Token, tokenB: Token): string {
const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks const tokens = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] // does safety checks
if (PAIR_ADDRESS_CACHE?.[tokens[0].address]?.[tokens[1].address] === undefined) { return getCreate2Address(
PAIR_ADDRESS_CACHE = {
...PAIR_ADDRESS_CACHE,
[tokens[0].address]: {
...PAIR_ADDRESS_CACHE?.[tokens[0].address],
[tokens[1].address]: getCreate2Address(
FACTORY_ADDRESS, FACTORY_ADDRESS,
keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]), keccak256(['bytes'], [pack(['address', 'address'], [tokens[0].address, tokens[1].address])]),
INIT_CODE_HASH INIT_CODE_HASH
) )
} }
}
}
return PAIR_ADDRESS_CACHE[tokens[0].address][tokens[1].address]
}
public constructor(tokenAmountA: TokenAmount, tokenAmountB: TokenAmount) { public constructor(tokenAmountA: TokenAmount, tokenAmountB: TokenAmount) {
const tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks const tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks
......
import { Token, WETH, ChainId, TokenAmount, ETHER } from '@uniswap/sdk-core' import { Token, WETH9, ChainId, TokenAmount, ETHER } from '@uniswap/sdk-core'
import { Pair, Route } from './index' import { Pair, Route } from './index'
describe('Route', () => { describe('Route', () => {
const token0 = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000001', 18, 't0') const token0 = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000001', 18, 't0')
const token1 = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000002', 18, 't1') const token1 = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000002', 18, 't1')
const weth = WETH[ChainId.MAINNET] const weth = WETH9[ChainId.MAINNET]
const pair_0_1 = new Pair(new TokenAmount(token0, '100'), new TokenAmount(token1, '200')) const pair_0_1 = new Pair(new TokenAmount(token0, '100'), new TokenAmount(token1, '200'))
const pair_0_weth = new Pair(new TokenAmount(token0, '100'), new TokenAmount(weth, '100')) const pair_0_weth = new Pair(new TokenAmount(token0, '100'), new TokenAmount(weth, '100'))
const pair_1_weth = new Pair(new TokenAmount(token1, '175'), new TokenAmount(weth, '100')) const pair_1_weth = new Pair(new TokenAmount(token1, '175'), new TokenAmount(weth, '100'))
......
import { ChainId, Currency, ETHER, Price, Token, WETH } from '@uniswap/sdk-core' import { ChainId, Currency, ETHER, Price, Token, WETH9 } from '@uniswap/sdk-core'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
import { Pair } from './pair' import { Pair } from './pair'
...@@ -29,17 +29,17 @@ export class Route { ...@@ -29,17 +29,17 @@ export class Route {
) )
invariant( invariant(
(input instanceof Token && pairs[0].involvesToken(input)) || (input instanceof Token && pairs[0].involvesToken(input)) ||
(input === ETHER && pairs[0].involvesToken(WETH[pairs[0].chainId])), (input === ETHER && pairs[0].involvesToken(WETH9[pairs[0].chainId])),
'INPUT' 'INPUT'
) )
invariant( invariant(
typeof output === 'undefined' || typeof output === 'undefined' ||
(output instanceof Token && pairs[pairs.length - 1].involvesToken(output)) || (output instanceof Token && pairs[pairs.length - 1].involvesToken(output)) ||
(output === ETHER && pairs[pairs.length - 1].involvesToken(WETH[pairs[0].chainId])), (output === ETHER && pairs[pairs.length - 1].involvesToken(WETH9[pairs[0].chainId])),
'OUTPUT' 'OUTPUT'
) )
const path: Token[] = [input instanceof Token ? input : WETH[pairs[0].chainId]] const path: Token[] = [input instanceof Token ? input : WETH9[pairs[0].chainId]]
for (const [i, pair] of pairs.entries()) { for (const [i, pair] of pairs.entries()) {
const currentInput = path[i] const currentInput = path[i]
invariant(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), 'PATH') invariant(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), 'PATH')
......
...@@ -2,7 +2,7 @@ import { Pair } from './pair' ...@@ -2,7 +2,7 @@ import { Pair } from './pair'
import { Route } from './route' import { Route } from './route'
import { Trade } from './trade' import { Trade } from './trade'
import JSBI from 'jsbi' import JSBI from 'jsbi'
import { ChainId, ETHER, CurrencyAmount, Percent, Token, TokenAmount, TradeType, WETH } from '@uniswap/sdk-core' import { ChainId, ETHER, CurrencyAmount, Percent, Token, TokenAmount, TradeType, WETH9 } from '@uniswap/sdk-core'
describe('Trade', () => { describe('Trade', () => {
const token0 = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000001', 18, 't0') const token0 = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000001', 18, 't0')
...@@ -17,7 +17,7 @@ describe('Trade', () => { ...@@ -17,7 +17,7 @@ describe('Trade', () => {
const pair_1_3 = new Pair(new TokenAmount(token1, JSBI.BigInt(1200)), new TokenAmount(token3, JSBI.BigInt(1300))) const pair_1_3 = new Pair(new TokenAmount(token1, JSBI.BigInt(1200)), new TokenAmount(token3, JSBI.BigInt(1300)))
const pair_weth_0 = new Pair( const pair_weth_0 = new Pair(
new TokenAmount(WETH[ChainId.MAINNET], JSBI.BigInt(1000)), new TokenAmount(WETH9[ChainId.MAINNET], JSBI.BigInt(1000)),
new TokenAmount(token0, JSBI.BigInt(1000)) new TokenAmount(token0, JSBI.BigInt(1000))
) )
...@@ -146,10 +146,10 @@ describe('Trade', () => { ...@@ -146,10 +146,10 @@ describe('Trade', () => {
) )
expect(result).toHaveLength(2) expect(result).toHaveLength(2)
expect(result[0].inputAmount.currency).toEqual(ETHER) expect(result[0].inputAmount.currency).toEqual(ETHER)
expect(result[0].route.path).toEqual([WETH[ChainId.MAINNET], token0, token1, token3]) expect(result[0].route.path).toEqual([WETH9[ChainId.MAINNET], token0, token1, token3])
expect(result[0].outputAmount.currency).toEqual(token3) expect(result[0].outputAmount.currency).toEqual(token3)
expect(result[1].inputAmount.currency).toEqual(ETHER) expect(result[1].inputAmount.currency).toEqual(ETHER)
expect(result[1].route.path).toEqual([WETH[ChainId.MAINNET], token0, token3]) expect(result[1].route.path).toEqual([WETH9[ChainId.MAINNET], token0, token3])
expect(result[1].outputAmount.currency).toEqual(token3) expect(result[1].outputAmount.currency).toEqual(token3)
}) })
it('works for ETHER currency output', () => { it('works for ETHER currency output', () => {
...@@ -160,10 +160,10 @@ describe('Trade', () => { ...@@ -160,10 +160,10 @@ describe('Trade', () => {
) )
expect(result).toHaveLength(2) expect(result).toHaveLength(2)
expect(result[0].inputAmount.currency).toEqual(token3) expect(result[0].inputAmount.currency).toEqual(token3)
expect(result[0].route.path).toEqual([token3, token0, WETH[ChainId.MAINNET]]) expect(result[0].route.path).toEqual([token3, token0, WETH9[ChainId.MAINNET]])
expect(result[0].outputAmount.currency).toEqual(ETHER) expect(result[0].outputAmount.currency).toEqual(ETHER)
expect(result[1].inputAmount.currency).toEqual(token3) expect(result[1].inputAmount.currency).toEqual(token3)
expect(result[1].route.path).toEqual([token3, token1, token0, WETH[ChainId.MAINNET]]) expect(result[1].route.path).toEqual([token3, token1, token0, WETH9[ChainId.MAINNET]])
expect(result[1].outputAmount.currency).toEqual(ETHER) expect(result[1].outputAmount.currency).toEqual(ETHER)
}) })
}) })
...@@ -371,10 +371,10 @@ describe('Trade', () => { ...@@ -371,10 +371,10 @@ describe('Trade', () => {
) )
expect(result).toHaveLength(2) expect(result).toHaveLength(2)
expect(result[0].inputAmount.currency).toEqual(ETHER) expect(result[0].inputAmount.currency).toEqual(ETHER)
expect(result[0].route.path).toEqual([WETH[ChainId.MAINNET], token0, token1, token3]) expect(result[0].route.path).toEqual([WETH9[ChainId.MAINNET], token0, token1, token3])
expect(result[0].outputAmount.currency).toEqual(token3) expect(result[0].outputAmount.currency).toEqual(token3)
expect(result[1].inputAmount.currency).toEqual(ETHER) expect(result[1].inputAmount.currency).toEqual(ETHER)
expect(result[1].route.path).toEqual([WETH[ChainId.MAINNET], token0, token3]) expect(result[1].route.path).toEqual([WETH9[ChainId.MAINNET], token0, token3])
expect(result[1].outputAmount.currency).toEqual(token3) expect(result[1].outputAmount.currency).toEqual(token3)
}) })
it('works for ETHER currency output', () => { it('works for ETHER currency output', () => {
...@@ -385,10 +385,10 @@ describe('Trade', () => { ...@@ -385,10 +385,10 @@ describe('Trade', () => {
) )
expect(result).toHaveLength(2) expect(result).toHaveLength(2)
expect(result[0].inputAmount.currency).toEqual(token3) expect(result[0].inputAmount.currency).toEqual(token3)
expect(result[0].route.path).toEqual([token3, token0, WETH[ChainId.MAINNET]]) expect(result[0].route.path).toEqual([token3, token0, WETH9[ChainId.MAINNET]])
expect(result[0].outputAmount.currency).toEqual(ETHER) expect(result[0].outputAmount.currency).toEqual(ETHER)
expect(result[1].inputAmount.currency).toEqual(token3) expect(result[1].inputAmount.currency).toEqual(token3)
expect(result[1].route.path).toEqual([token3, token1, token0, WETH[ChainId.MAINNET]]) expect(result[1].route.path).toEqual([token3, token1, token0, WETH9[ChainId.MAINNET]])
expect(result[1].outputAmount.currency).toEqual(ETHER) expect(result[1].outputAmount.currency).toEqual(ETHER)
}) })
}) })
......
...@@ -5,15 +5,14 @@ import { ...@@ -5,15 +5,14 @@ import {
currencyEquals, currencyEquals,
ETHER, ETHER,
Fraction, Fraction,
ONE,
Percent, Percent,
Price, Price,
Token, Token,
TokenAmount, TokenAmount,
TradeType, TradeType,
WETH, WETH9
ZERO
} from '@uniswap/sdk-core' } from '@uniswap/sdk-core'
import { ONE, ZERO } from '../constants'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
import { sortedInsert } from '../utils' import { sortedInsert } from '../utils'
...@@ -92,18 +91,18 @@ export interface BestTradeOptions { ...@@ -92,18 +91,18 @@ export interface BestTradeOptions {
/** /**
* Given a currency amount and a chain ID, returns the equivalent representation as the token amount. * Given a currency amount and a chain ID, returns the equivalent representation as the token amount.
* In other words, if the currency is ETHER, returns the WETH token amount for the given chain. Otherwise, returns * In other words, if the currency is ETHER, returns the WETH9 token amount for the given chain. Otherwise, returns
* the input currency amount. * the input currency amount.
*/ */
function wrappedAmount(currencyAmount: CurrencyAmount, chainId: ChainId): TokenAmount { function wrappedAmount(currencyAmount: CurrencyAmount, chainId: ChainId): TokenAmount {
if (currencyAmount instanceof TokenAmount) return currencyAmount if (currencyAmount instanceof TokenAmount) return currencyAmount
if (currencyAmount.currency === ETHER) return new TokenAmount(WETH[chainId], currencyAmount.raw) if (currencyAmount.currency === ETHER) return new TokenAmount(WETH9[chainId], currencyAmount.raw)
invariant(false, 'CURRENCY') invariant(false, 'CURRENCY')
} }
function wrappedCurrency(currency: Currency, chainId: ChainId): Token { function wrappedCurrency(currency: Currency, chainId: ChainId): Token {
if (currency instanceof Token) return currency if (currency instanceof Token) return currency
if (currency === ETHER) return WETH[chainId] if (currency === ETHER) return WETH9[chainId]
invariant(false, 'CURRENCY') invariant(false, 'CURRENCY')
} }
......
import { Contract } from '@ethersproject/contracts'
import { getNetwork } from '@ethersproject/networks'
import { getDefaultProvider } from '@ethersproject/providers'
import { ChainId, Token, TokenAmount } from '@uniswap/sdk-core'
import { Pair } from './entities/pair'
import IUniswapV2Pair from '@uniswap/v2-core/build/IUniswapV2Pair.json'
import invariant from 'tiny-invariant'
import ERC20 from './abis/ERC20.json'
let TOKEN_DECIMALS_CACHE: { [chainId: number]: { [address: string]: number } } = {
[ChainId.MAINNET]: {
'0xE0B7927c4aF23765Cb51314A0E0521A9645F0E2A': 9 // DGD
}
}
/**
* Contains methods for constructing instances of pairs and tokens from on-chain data.
*/
export abstract class Fetcher {
/**
* Cannot be constructed.
*/
private constructor() {}
/**
* Fetch information for a given token on the given chain, using the given ethers provider.
* @param chainId chain of the token
* @param address address of the token on the chain
* @param provider provider used to fetch the token
* @param symbol optional symbol of the token
* @param name optional name of the token
*/
public static async fetchTokenData(
chainId: ChainId,
address: string,
provider = getDefaultProvider(getNetwork(chainId)),
symbol?: string,
name?: string
): Promise<Token> {
const parsedDecimals =
typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === 'number'
? TOKEN_DECIMALS_CACHE[chainId][address]
: await new Contract(address, ERC20, provider).decimals().then((decimals: number): number => {
TOKEN_DECIMALS_CACHE = {
...TOKEN_DECIMALS_CACHE,
[chainId]: {
...TOKEN_DECIMALS_CACHE?.[chainId],
[address]: decimals
}
}
return decimals
})
return new Token(chainId, address, parsedDecimals, symbol, name)
}
/**
* Fetches information about a pair and constructs a pair from the given two tokens.
* @param tokenA first token
* @param tokenB second token
* @param provider the provider to use to fetch the data
*/
public static async fetchPairData(
tokenA: Token,
tokenB: Token,
provider = getDefaultProvider(getNetwork(tokenA.chainId))
): Promise<Pair> {
invariant(tokenA.chainId === tokenB.chainId, 'CHAIN_ID')
const address = Pair.getAddress(tokenA, tokenB)
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]))
}
}
...@@ -6,4 +6,3 @@ export { FACTORY_ADDRESS, INIT_CODE_HASH, MINIMUM_LIQUIDITY } from './constants' ...@@ -6,4 +6,3 @@ export { FACTORY_ADDRESS, INIT_CODE_HASH, MINIMUM_LIQUIDITY } from './constants'
export * from './errors' export * from './errors'
export * from './entities' export * from './entities'
export * from './router' export * from './router'
export * from './fetcher'
...@@ -2,7 +2,7 @@ import JSBI from 'jsbi' ...@@ -2,7 +2,7 @@ import JSBI from 'jsbi'
import { Pair, Route, Trade } from './entities' import { Pair, Route, Trade } from './entities'
import { Router } from './router' import { Router } from './router'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
import { ChainId, CurrencyAmount, ETHER, Percent, Token, TokenAmount, WETH } from '@uniswap/sdk-core' import { ChainId, CurrencyAmount, ETHER, Percent, Token, TokenAmount, WETH9 } from '@uniswap/sdk-core'
function checkDeadline(deadline: string[] | string): void { function checkDeadline(deadline: string[] | string): void {
expect(typeof deadline).toBe('string') expect(typeof deadline).toBe('string')
...@@ -17,7 +17,7 @@ describe('Router', () => { ...@@ -17,7 +17,7 @@ describe('Router', () => {
const pair_0_1 = new Pair(new TokenAmount(token0, JSBI.BigInt(1000)), new TokenAmount(token1, JSBI.BigInt(1000))) const pair_0_1 = new Pair(new TokenAmount(token0, JSBI.BigInt(1000)), new TokenAmount(token1, JSBI.BigInt(1000)))
const pair_weth_0 = new Pair(new TokenAmount(WETH[ChainId.MAINNET], '1000'), new TokenAmount(token0, '1000')) const pair_weth_0 = new Pair(new TokenAmount(WETH9[ChainId.MAINNET], '1000'), new TokenAmount(token0, '1000'))
describe('#swapCallParameters', () => { describe('#swapCallParameters', () => {
describe('exact in', () => { describe('exact in', () => {
...@@ -29,7 +29,7 @@ describe('Router', () => { ...@@ -29,7 +29,7 @@ describe('Router', () => {
expect(result.methodName).toEqual('swapExactETHForTokens') expect(result.methodName).toEqual('swapExactETHForTokens')
expect(result.args.slice(0, -1)).toEqual([ expect(result.args.slice(0, -1)).toEqual([
'0x51', '0x51',
[WETH[ChainId.MAINNET].address, token0.address, token1.address], [WETH9[ChainId.MAINNET].address, token0.address, token1.address],
'0x0000000000000000000000000000000000000004' '0x0000000000000000000000000000000000000004'
]) ])
expect(result.value).toEqual('0x64') expect(result.value).toEqual('0x64')
...@@ -48,7 +48,7 @@ describe('Router', () => { ...@@ -48,7 +48,7 @@ describe('Router', () => {
expect(result.methodName).toEqual('swapExactETHForTokens') expect(result.methodName).toEqual('swapExactETHForTokens')
expect(result.args).toEqual([ expect(result.args).toEqual([
'0x51', '0x51',
[WETH[ChainId.MAINNET].address, token0.address, token1.address], [WETH9[ChainId.MAINNET].address, token0.address, token1.address],
'0x0000000000000000000000000000000000000004', '0x0000000000000000000000000000000000000004',
'0x32' '0x32'
]) ])
...@@ -64,7 +64,7 @@ describe('Router', () => { ...@@ -64,7 +64,7 @@ describe('Router', () => {
expect(result.args.slice(0, -1)).toEqual([ expect(result.args.slice(0, -1)).toEqual([
'0x64', '0x64',
'0x51', '0x51',
[token1.address, token0.address, WETH[ChainId.MAINNET].address], [token1.address, token0.address, WETH9[ChainId.MAINNET].address],
'0x0000000000000000000000000000000000000004' '0x0000000000000000000000000000000000000004'
]) ])
expect(result.value).toEqual('0x0') expect(result.value).toEqual('0x0')
...@@ -95,7 +95,7 @@ describe('Router', () => { ...@@ -95,7 +95,7 @@ describe('Router', () => {
expect(result.methodName).toEqual('swapETHForExactTokens') expect(result.methodName).toEqual('swapETHForExactTokens')
expect(result.args.slice(0, -1)).toEqual([ expect(result.args.slice(0, -1)).toEqual([
'0x64', '0x64',
[WETH[ChainId.MAINNET].address, token0.address, token1.address], [WETH9[ChainId.MAINNET].address, token0.address, token1.address],
'0x0000000000000000000000000000000000000004' '0x0000000000000000000000000000000000000004'
]) ])
expect(result.value).toEqual('0x80') expect(result.value).toEqual('0x80')
...@@ -110,7 +110,7 @@ describe('Router', () => { ...@@ -110,7 +110,7 @@ describe('Router', () => {
expect(result.args.slice(0, -1)).toEqual([ expect(result.args.slice(0, -1)).toEqual([
'0x64', '0x64',
'0x80', '0x80',
[token1.address, token0.address, WETH[ChainId.MAINNET].address], [token1.address, token0.address, WETH9[ChainId.MAINNET].address],
'0x0000000000000000000000000000000000000004' '0x0000000000000000000000000000000000000004'
]) ])
expect(result.value).toEqual('0x0') expect(result.value).toEqual('0x0')
...@@ -147,7 +147,7 @@ describe('Router', () => { ...@@ -147,7 +147,7 @@ describe('Router', () => {
expect(result.methodName).toEqual('swapExactETHForTokensSupportingFeeOnTransferTokens') expect(result.methodName).toEqual('swapExactETHForTokensSupportingFeeOnTransferTokens')
expect(result.args.slice(0, -1)).toEqual([ expect(result.args.slice(0, -1)).toEqual([
'0x51', '0x51',
[WETH[ChainId.MAINNET].address, token0.address, token1.address], [WETH9[ChainId.MAINNET].address, token0.address, token1.address],
'0x0000000000000000000000000000000000000004' '0x0000000000000000000000000000000000000004'
]) ])
expect(result.value).toEqual('0x64') expect(result.value).toEqual('0x64')
...@@ -167,7 +167,7 @@ describe('Router', () => { ...@@ -167,7 +167,7 @@ describe('Router', () => {
expect(result.args.slice(0, -1)).toEqual([ expect(result.args.slice(0, -1)).toEqual([
'0x64', '0x64',
'0x51', '0x51',
[token1.address, token0.address, WETH[ChainId.MAINNET].address], [token1.address, token0.address, WETH9[ChainId.MAINNET].address],
'0x0000000000000000000000000000000000000004' '0x0000000000000000000000000000000000000004'
]) ])
expect(result.value).toEqual('0x0') expect(result.value).toEqual('0x0')
......
...@@ -2006,10 +2006,10 @@ ...@@ -2006,10 +2006,10 @@
semver "^7.3.2" semver "^7.3.2"
tsutils "^3.17.1" tsutils "^3.17.1"
"@uniswap/sdk-core@^1.0.6": "@uniswap/sdk-core@^1.0.7":
version "1.0.6" version "1.0.7"
resolved "https://registry.yarnpkg.com/@uniswap/sdk-core/-/sdk-core-1.0.6.tgz#46ca5df49661600d484480f871d6f38ffffd7e53" resolved "https://registry.yarnpkg.com/@uniswap/sdk-core/-/sdk-core-1.0.7.tgz#f75a9dc2e0ef9b701ba49214cefd6e5c39c74cdc"
integrity sha512-SvdrOL5mIAIsp47ODDcqlo47p6iQsVZQnn4eem9pK8TO29owSGAz5CqzJB5uxRFPcPvIVYgujMPH4RB9ieF8QA== integrity sha512-/q+SUnVnJBdp09a75q+DxsnuRdnSIqB8Uqz43tFu0xfRII/R40ZXVZXoKe9dJZNzUZnkMuNXm0DS6jf37i9oaw==
dependencies: dependencies:
"@ethersproject/address" "^5.0.2" "@ethersproject/address" "^5.0.2"
big.js "^5.2.2" big.js "^5.2.2"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment