Commit f34b3d9d authored by Moody Salem's avatar Moody Salem

no more instanceof

parent a371e547
import JSBI from 'jsbi' import JSBI from 'jsbi'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
import { ChainId, WETH9 as _WETH9, TradeType, Rounding, Token, TokenAmount } from '@uniswap/sdk-core' import { ChainId, WETH9 as _WETH9, TradeType, Rounding, Token, CurrencyAmount } from '@uniswap/sdk-core'
import { Pair, Route, Trade } from '../index' import { Pair, Route, Trade } from '../index'
const ADDRESSES = [ const ADDRESSES = [
...@@ -37,16 +37,16 @@ describe('entities', () => { ...@@ -37,16 +37,16 @@ describe('entities', () => {
it('Pair', () => { it('Pair', () => {
pairs = [ pairs = [
new Pair( new Pair(
new TokenAmount(tokens[0], decimalize(1, tokens[0].decimals)), new CurrencyAmount(tokens[0], decimalize(1, tokens[0].decimals)),
new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)) new CurrencyAmount(tokens[1], decimalize(1, tokens[1].decimals))
), ),
new Pair( new Pair(
new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)), new CurrencyAmount(tokens[1], decimalize(1, tokens[1].decimals)),
new TokenAmount(tokens[2], decimalize(1, tokens[2].decimals)) new CurrencyAmount(tokens[2], decimalize(1, tokens[2].decimals))
), ),
new Pair( new Pair(
new TokenAmount(tokens[2], decimalize(1, tokens[2].decimals)), new CurrencyAmount(tokens[2], decimalize(1, tokens[2].decimals)),
new TokenAmount(WETH9, decimalize(1234, WETH9.decimals)) new CurrencyAmount(WETH9, decimalize(1234, WETH9.decimals))
) )
] ]
}) })
...@@ -61,14 +61,14 @@ describe('entities', () => { ...@@ -61,14 +61,14 @@ describe('entities', () => {
}) })
it('Price:Route.midPrice', () => { it('Price:Route.midPrice', () => {
invariant(route.input instanceof Token) invariant(route.input.isToken)
invariant(route.output instanceof Token) invariant(route.output.isToken)
expect(route.midPrice.quote(new TokenAmount(route.input, decimalize(1, route.input.decimals)))).toEqual( expect(route.midPrice.quote(new CurrencyAmount(route.input, decimalize(1, route.input.decimals)))).toEqual(
new TokenAmount(route.output, decimalize(1234, route.output.decimals)) new CurrencyAmount(route.output, decimalize(1234, route.output.decimals))
) )
expect( expect(
route.midPrice.invert().quote(new TokenAmount(route.output, decimalize(1234, route.output.decimals))) route.midPrice.invert().quote(new CurrencyAmount(route.output, decimalize(1234, route.output.decimals)))
).toEqual(new TokenAmount(route.input, decimalize(1, route.input.decimals))) ).toEqual(new CurrencyAmount(route.input, decimalize(1, route.input.decimals)))
expect(route.midPrice.toSignificant(1)).toEqual('1000') expect(route.midPrice.toSignificant(1)).toEqual('1000')
expect(route.midPrice.toSignificant(2)).toEqual('1200') expect(route.midPrice.toSignificant(2)).toEqual('1200')
...@@ -105,14 +105,14 @@ describe('entities', () => { ...@@ -105,14 +105,14 @@ describe('entities', () => {
route = new Route( route = new Route(
[ [
new Pair( new Pair(
new TokenAmount(tokens[1], decimalize(5, tokens[1].decimals)), new CurrencyAmount(tokens[1], decimalize(5, tokens[1].decimals)),
new TokenAmount(WETH9, decimalize(10, WETH9.decimals)) new CurrencyAmount(WETH9, decimalize(10, WETH9.decimals))
) )
], ],
tokens[1] tokens[1]
) )
const inputAmount = new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)) const inputAmount = new CurrencyAmount(tokens[1], decimalize(1, tokens[1].decimals))
const expectedOutputAmount = new TokenAmount(WETH9, '1662497915624478906') const expectedOutputAmount = new CurrencyAmount(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,8 +131,8 @@ describe('entities', () => { ...@@ -131,8 +131,8 @@ describe('entities', () => {
}) })
it('TradeType.EXACT_OUTPUT', () => { it('TradeType.EXACT_OUTPUT', () => {
const outputAmount = new TokenAmount(WETH9, '1662497915624478906') const outputAmount = new CurrencyAmount(WETH9, '1662497915624478906')
const expectedInputAmount = new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)) const expectedInputAmount = new CurrencyAmount(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)
expect(trade.tradeType).toEqual(TradeType.EXACT_OUTPUT) expect(trade.tradeType).toEqual(TradeType.EXACT_OUTPUT)
...@@ -155,8 +155,8 @@ describe('entities', () => { ...@@ -155,8 +155,8 @@ describe('entities', () => {
const route = new Route( const route = new Route(
[ [
new Pair( new Pair(
new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)), new CurrencyAmount(tokens[1], decimalize(1, tokens[1].decimals)),
new TokenAmount( new CurrencyAmount(
WETH9, WETH9,
JSBI.add( JSBI.add(
decimalize(10, WETH9.decimals), decimalize(10, WETH9.decimals),
...@@ -167,7 +167,7 @@ describe('entities', () => { ...@@ -167,7 +167,7 @@ describe('entities', () => {
], ],
tokens[1] tokens[1]
) )
const outputAmount = new TokenAmount(tokens[1], '1') const outputAmount = new CurrencyAmount(tokens[1], '1')
const trade = new Trade(route, outputAmount, TradeType.EXACT_INPUT) const trade = new Trade(route, outputAmount, TradeType.EXACT_INPUT)
expect(trade.priceImpact.toSignificant(18)).toEqual( expect(trade.priceImpact.toSignificant(18)).toEqual(
...@@ -177,8 +177,8 @@ describe('entities', () => { ...@@ -177,8 +177,8 @@ describe('entities', () => {
}) })
}) })
it('TokenAmount', () => { it('CurrencyAmount', () => {
const amount = new TokenAmount(WETH9, '1234567000000000000000') const amount = new CurrencyAmount(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')
}) })
......
This diff is collapsed.
import { BigintIsh, ChainId, Price, sqrt, Token, TokenAmount } from '@uniswap/sdk-core' import { BigintIsh, ChainId, Price, sqrt, Token, CurrencyAmount } 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'
...@@ -25,24 +25,26 @@ export const computePairAddress = ({ ...@@ -25,24 +25,26 @@ export const computePairAddress = ({
} }
export class Pair { export class Pair {
public readonly liquidityToken: Token public readonly liquidityToken: Token
private readonly tokenAmounts: [TokenAmount, TokenAmount] private readonly tokenAmounts: [CurrencyAmount, CurrencyAmount]
public static getAddress(tokenA: Token, tokenB: Token): string { public static getAddress(tokenA: Token, tokenB: Token): string {
return computePairAddress({ factoryAddress: FACTORY_ADDRESS, tokenA, tokenB }) return computePairAddress({ factoryAddress: FACTORY_ADDRESS, tokenA, tokenB })
} }
public constructor(tokenAmountA: TokenAmount, tokenAmountB: TokenAmount) { public constructor(currencyAmountA: CurrencyAmount, tokenAmountB: CurrencyAmount) {
const tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks invariant(currencyAmountA.currency.isToken && tokenAmountB.currency.isToken, 'TOKEN')
? [tokenAmountA, tokenAmountB] const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) // does safety checks
: [tokenAmountB, tokenAmountA] ? [currencyAmountA, tokenAmountB]
: [tokenAmountB, currencyAmountA]
invariant(tokenAmounts[0].currency.isToken && tokenAmounts[1].currency.isToken, 'TOKEN')
this.liquidityToken = new Token( this.liquidityToken = new Token(
tokenAmounts[0].token.chainId, tokenAmounts[0].currency.chainId,
Pair.getAddress(tokenAmounts[0].token, tokenAmounts[1].token), Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency),
18, 18,
'UNI-V2', 'UNI-V2',
'Uniswap V2' 'Uniswap V2'
) )
this.tokenAmounts = tokenAmounts as [TokenAmount, TokenAmount] this.tokenAmounts = tokenAmounts as [CurrencyAmount, CurrencyAmount]
} }
/** /**
...@@ -84,38 +86,40 @@ export class Pair { ...@@ -84,38 +86,40 @@ export class Pair {
} }
public get token0(): Token { public get token0(): Token {
return this.tokenAmounts[0].token invariant(this.tokenAmounts[0].currency.isToken)
return this.tokenAmounts[0].currency
} }
public get token1(): Token { public get token1(): Token {
return this.tokenAmounts[1].token invariant(this.tokenAmounts[1].currency.isToken)
return this.tokenAmounts[1].currency
} }
public get reserve0(): TokenAmount { public get reserve0(): CurrencyAmount {
return this.tokenAmounts[0] return this.tokenAmounts[0]
} }
public get reserve1(): TokenAmount { public get reserve1(): CurrencyAmount {
return this.tokenAmounts[1] return this.tokenAmounts[1]
} }
public reserveOf(token: Token): TokenAmount { public reserveOf(token: Token): CurrencyAmount {
invariant(this.involvesToken(token), 'TOKEN') invariant(this.involvesToken(token), 'TOKEN')
return token.equals(this.token0) ? this.reserve0 : this.reserve1 return token.equals(this.token0) ? this.reserve0 : this.reserve1
} }
public getOutputAmount(inputAmount: TokenAmount): [TokenAmount, Pair] { public getOutputAmount(inputAmount: CurrencyAmount): [CurrencyAmount, Pair] {
invariant(this.involvesToken(inputAmount.token), 'TOKEN') invariant(inputAmount.currency.isToken && this.involvesToken(inputAmount.currency), 'TOKEN')
if (JSBI.equal(this.reserve0.raw, ZERO) || JSBI.equal(this.reserve1.raw, ZERO)) { if (JSBI.equal(this.reserve0.raw, ZERO) || JSBI.equal(this.reserve1.raw, ZERO)) {
throw new InsufficientReservesError() throw new InsufficientReservesError()
} }
const inputReserve = this.reserveOf(inputAmount.token) const inputReserve = this.reserveOf(inputAmount.currency)
const outputReserve = this.reserveOf(inputAmount.token.equals(this.token0) ? this.token1 : this.token0) const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0)
const inputAmountWithFee = JSBI.multiply(inputAmount.raw, _997) const inputAmountWithFee = JSBI.multiply(inputAmount.raw, _997)
const numerator = JSBI.multiply(inputAmountWithFee, outputReserve.raw) const numerator = JSBI.multiply(inputAmountWithFee, outputReserve.raw)
const denominator = JSBI.add(JSBI.multiply(inputReserve.raw, _1000), inputAmountWithFee) const denominator = JSBI.add(JSBI.multiply(inputReserve.raw, _1000), inputAmountWithFee)
const outputAmount = new TokenAmount( const outputAmount = new CurrencyAmount(
inputAmount.token.equals(this.token0) ? this.token1 : this.token0, inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
JSBI.divide(numerator, denominator) JSBI.divide(numerator, denominator)
) )
if (JSBI.equal(outputAmount.raw, ZERO)) { if (JSBI.equal(outputAmount.raw, ZERO)) {
...@@ -124,37 +128,41 @@ export class Pair { ...@@ -124,37 +128,41 @@ export class Pair {
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))] return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))]
} }
public getInputAmount(outputAmount: TokenAmount): [TokenAmount, Pair] { public getInputAmount(outputAmount: CurrencyAmount): [CurrencyAmount, Pair] {
invariant(this.involvesToken(outputAmount.token), 'TOKEN') invariant(outputAmount.currency.isToken && this.involvesToken(outputAmount.currency), 'TOKEN')
if ( if (
JSBI.equal(this.reserve0.raw, ZERO) || JSBI.equal(this.reserve0.raw, ZERO) ||
JSBI.equal(this.reserve1.raw, ZERO) || JSBI.equal(this.reserve1.raw, ZERO) ||
JSBI.greaterThanOrEqual(outputAmount.raw, this.reserveOf(outputAmount.token).raw) JSBI.greaterThanOrEqual(outputAmount.raw, this.reserveOf(outputAmount.currency).raw)
) { ) {
throw new InsufficientReservesError() throw new InsufficientReservesError()
} }
const outputReserve = this.reserveOf(outputAmount.token) const outputReserve = this.reserveOf(outputAmount.currency)
const inputReserve = this.reserveOf(outputAmount.token.equals(this.token0) ? this.token1 : this.token0) const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0)
const numerator = JSBI.multiply(JSBI.multiply(inputReserve.raw, outputAmount.raw), _1000) const numerator = JSBI.multiply(JSBI.multiply(inputReserve.raw, outputAmount.raw), _1000)
const denominator = JSBI.multiply(JSBI.subtract(outputReserve.raw, outputAmount.raw), _997) const denominator = JSBI.multiply(JSBI.subtract(outputReserve.raw, outputAmount.raw), _997)
const inputAmount = new TokenAmount( const inputAmount = new CurrencyAmount(
outputAmount.token.equals(this.token0) ? this.token1 : this.token0, outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
JSBI.add(JSBI.divide(numerator, denominator), ONE) JSBI.add(JSBI.divide(numerator, denominator), ONE)
) )
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))] return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))]
} }
public getLiquidityMinted( public getLiquidityMinted(
totalSupply: TokenAmount, totalSupply: CurrencyAmount,
tokenAmountA: TokenAmount, tokenAmountA: CurrencyAmount,
tokenAmountB: TokenAmount tokenAmountB: CurrencyAmount
): TokenAmount { ): CurrencyAmount {
invariant(totalSupply.token.equals(this.liquidityToken), 'LIQUIDITY') invariant(totalSupply.currency.isToken && totalSupply.currency.equals(this.liquidityToken), 'LIQUIDITY')
const tokenAmounts = tokenAmountA.token.sortsBefore(tokenAmountB.token) // does safety checks const tokenAmounts =
tokenAmountA.currency.isToken &&
tokenAmountB.currency.isToken &&
tokenAmountA.currency.sortsBefore(tokenAmountB.currency) // does safety checks
? [tokenAmountA, tokenAmountB] ? [tokenAmountA, tokenAmountB]
: [tokenAmountB, tokenAmountA] : [tokenAmountB, tokenAmountA]
invariant(tokenAmounts[0].token.equals(this.token0) && tokenAmounts[1].token.equals(this.token1), 'TOKEN') invariant(tokenAmounts[0].currency.isToken && tokenAmounts[1].currency.isToken)
invariant(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), 'TOKEN')
let liquidity: JSBI let liquidity: JSBI
if (JSBI.equal(totalSupply.raw, ZERO)) { if (JSBI.equal(totalSupply.raw, ZERO)) {
...@@ -167,22 +175,22 @@ export class Pair { ...@@ -167,22 +175,22 @@ export class Pair {
if (!JSBI.greaterThan(liquidity, ZERO)) { if (!JSBI.greaterThan(liquidity, ZERO)) {
throw new InsufficientInputAmountError() throw new InsufficientInputAmountError()
} }
return new TokenAmount(this.liquidityToken, liquidity) return new CurrencyAmount(this.liquidityToken, liquidity)
} }
public getLiquidityValue( public getLiquidityValue(
token: Token, token: Token,
totalSupply: TokenAmount, totalSupply: CurrencyAmount,
liquidity: TokenAmount, liquidity: CurrencyAmount,
feeOn: boolean = false, feeOn: boolean = false,
kLast?: BigintIsh kLast?: BigintIsh
): TokenAmount { ): CurrencyAmount {
invariant(this.involvesToken(token), 'TOKEN') invariant(this.involvesToken(token), 'TOKEN')
invariant(totalSupply.token.equals(this.liquidityToken), 'TOTAL_SUPPLY') invariant(totalSupply.currency.isToken && totalSupply.currency.equals(this.liquidityToken), 'TOTAL_SUPPLY')
invariant(liquidity.token.equals(this.liquidityToken), 'LIQUIDITY') invariant(liquidity.currency.isToken && liquidity.currency.equals(this.liquidityToken), 'LIQUIDITY')
invariant(JSBI.lessThanOrEqual(liquidity.raw, totalSupply.raw), 'LIQUIDITY') invariant(JSBI.lessThanOrEqual(liquidity.raw, totalSupply.raw), 'LIQUIDITY')
let totalSupplyAdjusted: TokenAmount let totalSupplyAdjusted: CurrencyAmount
if (!feeOn) { if (!feeOn) {
totalSupplyAdjusted = totalSupply totalSupplyAdjusted = totalSupply
} else { } else {
...@@ -195,7 +203,7 @@ export class Pair { ...@@ -195,7 +203,7 @@ export class Pair {
const numerator = JSBI.multiply(totalSupply.raw, JSBI.subtract(rootK, rootKLast)) const numerator = JSBI.multiply(totalSupply.raw, JSBI.subtract(rootK, rootKLast))
const denominator = JSBI.add(JSBI.multiply(rootK, FIVE), rootKLast) const denominator = JSBI.add(JSBI.multiply(rootK, FIVE), rootKLast)
const feeLiquidity = JSBI.divide(numerator, denominator) const feeLiquidity = JSBI.divide(numerator, denominator)
totalSupplyAdjusted = totalSupply.add(new TokenAmount(this.liquidityToken, feeLiquidity)) totalSupplyAdjusted = totalSupply.add(new CurrencyAmount(this.liquidityToken, feeLiquidity))
} else { } else {
totalSupplyAdjusted = totalSupply totalSupplyAdjusted = totalSupply
} }
...@@ -204,7 +212,7 @@ export class Pair { ...@@ -204,7 +212,7 @@ export class Pair {
} }
} }
return new TokenAmount( return new CurrencyAmount(
token, token,
JSBI.divide(JSBI.multiply(liquidity.raw, this.reserveOf(token).raw), totalSupplyAdjusted.raw) JSBI.divide(JSBI.multiply(liquidity.raw, this.reserveOf(token).raw), totalSupplyAdjusted.raw)
) )
......
import { Token, WETH9, ChainId, TokenAmount, ETHER } from '@uniswap/sdk-core' import { Token, WETH9, ChainId, CurrencyAmount, 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 = WETH9[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 CurrencyAmount(token0, '100'), new CurrencyAmount(token1, '200'))
const pair_0_weth = new Pair(new TokenAmount(token0, '100'), new TokenAmount(weth, '100')) const pair_0_weth = new Pair(new CurrencyAmount(token0, '100'), new CurrencyAmount(weth, '100'))
const pair_1_weth = new Pair(new TokenAmount(token1, '175'), new TokenAmount(weth, '100')) const pair_1_weth = new Pair(new CurrencyAmount(token1, '175'), new CurrencyAmount(weth, '100'))
it('constructs a path from the tokens', () => { it('constructs a path from the tokens', () => {
const route = new Route([pair_0_1], token0) const route = new Route([pair_0_1], token0)
......
...@@ -32,18 +32,17 @@ export class Route { ...@@ -32,18 +32,17 @@ export class Route {
const weth: Token | undefined = WETH9[chainId as ChainId] const weth: Token | undefined = WETH9[chainId as ChainId]
invariant( invariant(
(input instanceof Token && pairs[0].involvesToken(input)) || (input.isToken && pairs[0].involvesToken(input)) || (input === ETHER && weth && pairs[0].involvesToken(weth)),
(input === ETHER && weth && pairs[0].involvesToken(weth)),
'INPUT' 'INPUT'
) )
invariant( invariant(
typeof output === 'undefined' || typeof output === 'undefined' ||
(output instanceof Token && pairs[pairs.length - 1].involvesToken(output)) || (output.isToken && pairs[pairs.length - 1].involvesToken(output)) ||
(output === ETHER && weth && pairs[pairs.length - 1].involvesToken(weth)), (output === ETHER && weth && pairs[pairs.length - 1].involvesToken(weth)),
'OUTPUT' 'OUTPUT'
) )
const path: Token[] = [input instanceof Token ? input : weth] const path: Token[] = [input.isToken ? input : weth]
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')
......
This diff is collapsed.
...@@ -9,7 +9,6 @@ import { ...@@ -9,7 +9,6 @@ import {
Price, Price,
sortedInsert, sortedInsert,
Token, Token,
TokenAmount,
TradeType, TradeType,
WETH9 WETH9
} from '@uniswap/sdk-core' } from '@uniswap/sdk-core'
...@@ -94,16 +93,16 @@ export interface BestTradeOptions { ...@@ -94,16 +93,16 @@ export interface BestTradeOptions {
* In other words, if the currency is ETHER, returns the WETH9 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): CurrencyAmount {
if (currencyAmount instanceof TokenAmount) return currencyAmount if (currencyAmount.currency.isToken) return currencyAmount
if (currencyAmount.currency === ETHER) return new TokenAmount(WETH9[chainId], currencyAmount.raw) if (currencyAmount.currency.isEther) return new CurrencyAmount(WETH9[chainId], currencyAmount.raw)
invariant(false, 'CURRENCY') throw new Error('CURRENCY')
} }
function wrappedCurrency(currency: Currency, chainId: ChainId): Token { function wrappedCurrency(currency: Currency, chainId: ChainId): Token {
if (currency instanceof Token) return currency if (currency.isToken) return currency
if (currency === ETHER) return WETH9[chainId] if (currency === ETHER) return WETH9[chainId]
invariant(false, 'CURRENCY') throw new Error('CURRENCY')
} }
/** /**
...@@ -159,7 +158,7 @@ export class Trade { ...@@ -159,7 +158,7 @@ export class Trade {
} }
public constructor(route: Route, amount: CurrencyAmount, tradeType: TradeType) { public constructor(route: Route, amount: CurrencyAmount, tradeType: TradeType) {
const amounts: TokenAmount[] = new Array(route.path.length) const amounts: CurrencyAmount[] = new Array(route.path.length)
const nextPairs: Pair[] = new Array(route.pairs.length) const nextPairs: Pair[] = new Array(route.pairs.length)
if (tradeType === TradeType.EXACT_INPUT) { if (tradeType === TradeType.EXACT_INPUT) {
invariant(currencyEquals(amount.currency, route.input), 'INPUT') invariant(currencyEquals(amount.currency, route.input), 'INPUT')
...@@ -218,9 +217,7 @@ export class Trade { ...@@ -218,9 +217,7 @@ export class Trade {
.add(slippageTolerance) .add(slippageTolerance)
.invert() .invert()
.multiply(this.outputAmount.raw).quotient .multiply(this.outputAmount.raw).quotient
return this.outputAmount instanceof TokenAmount return new CurrencyAmount(this.outputAmount.currency, slippageAdjustedAmountOut)
? new TokenAmount(this.outputAmount.token, slippageAdjustedAmountOut)
: CurrencyAmount.ether(slippageAdjustedAmountOut)
} }
} }
...@@ -234,9 +231,7 @@ export class Trade { ...@@ -234,9 +231,7 @@ export class Trade {
return this.inputAmount return this.inputAmount
} else { } else {
const slippageAdjustedAmountIn = new Fraction(ONE).add(slippageTolerance).multiply(this.inputAmount.raw).quotient const slippageAdjustedAmountIn = new Fraction(ONE).add(slippageTolerance).multiply(this.inputAmount.raw).quotient
return this.inputAmount instanceof TokenAmount return new CurrencyAmount(this.inputAmount.currency, slippageAdjustedAmountIn)
? new TokenAmount(this.inputAmount.token, slippageAdjustedAmountIn)
: CurrencyAmount.ether(slippageAdjustedAmountIn)
} }
} }
...@@ -267,10 +262,9 @@ export class Trade { ...@@ -267,10 +262,9 @@ export class Trade {
invariant(pairs.length > 0, 'PAIRS') invariant(pairs.length > 0, 'PAIRS')
invariant(maxHops > 0, 'MAX_HOPS') invariant(maxHops > 0, 'MAX_HOPS')
invariant(originalAmountIn === currencyAmountIn || currentPairs.length > 0, 'INVALID_RECURSION') invariant(originalAmountIn === currencyAmountIn || currentPairs.length > 0, 'INVALID_RECURSION')
const chainId: ChainId | undefined = const chainId: ChainId | undefined = currencyAmountIn.currency.isToken
currencyAmountIn instanceof TokenAmount ? currencyAmountIn.currency.chainId
? currencyAmountIn.token.chainId : currencyOut.isToken
: currencyOut instanceof Token
? currencyOut.chainId ? currencyOut.chainId
: undefined : undefined
invariant(chainId !== undefined, 'CHAIN_ID') invariant(chainId !== undefined, 'CHAIN_ID')
...@@ -280,10 +274,10 @@ export class Trade { ...@@ -280,10 +274,10 @@ export class Trade {
for (let i = 0; i < pairs.length; i++) { for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i] const pair = pairs[i]
// pair irrelevant // pair irrelevant
if (!pair.token0.equals(amountIn.token) && !pair.token1.equals(amountIn.token)) continue if (!currencyEquals(pair.token0, amountIn.currency) && !currencyEquals(pair.token1, amountIn.currency)) continue
if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO)) continue if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO)) continue
let amountOut: TokenAmount let amountOut: CurrencyAmount
try { try {
;[amountOut] = pair.getOutputAmount(amountIn) ;[amountOut] = pair.getOutputAmount(amountIn)
} catch (error) { } catch (error) {
...@@ -294,7 +288,7 @@ export class Trade { ...@@ -294,7 +288,7 @@ export class Trade {
throw error throw error
} }
// we have arrived at the output token, so this is the final trade of one of the paths // we have arrived at the output token, so this is the final trade of one of the paths
if (amountOut.token.equals(tokenOut)) { if (currencyEquals(amountOut.currency, tokenOut)) {
sortedInsert( sortedInsert(
bestTrades, bestTrades,
new Trade( new Trade(
...@@ -368,10 +362,9 @@ export class Trade { ...@@ -368,10 +362,9 @@ export class Trade {
invariant(pairs.length > 0, 'PAIRS') invariant(pairs.length > 0, 'PAIRS')
invariant(maxHops > 0, 'MAX_HOPS') invariant(maxHops > 0, 'MAX_HOPS')
invariant(originalAmountOut === currencyAmountOut || currentPairs.length > 0, 'INVALID_RECURSION') invariant(originalAmountOut === currencyAmountOut || currentPairs.length > 0, 'INVALID_RECURSION')
const chainId: ChainId | undefined = const chainId: ChainId | undefined = currencyAmountOut.currency.isToken
currencyAmountOut instanceof TokenAmount ? currencyAmountOut.currency.chainId
? currencyAmountOut.token.chainId : currencyIn.isToken
: currencyIn instanceof Token
? currencyIn.chainId ? currencyIn.chainId
: undefined : undefined
invariant(chainId !== undefined, 'CHAIN_ID') invariant(chainId !== undefined, 'CHAIN_ID')
...@@ -381,10 +374,10 @@ export class Trade { ...@@ -381,10 +374,10 @@ export class Trade {
for (let i = 0; i < pairs.length; i++) { for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i] const pair = pairs[i]
// pair irrelevant // pair irrelevant
if (!pair.token0.equals(amountOut.token) && !pair.token1.equals(amountOut.token)) continue if (!currencyEquals(pair.token0, amountOut.currency) && !currencyEquals(pair.token1, amountOut.currency)) continue
if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO)) continue if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO)) continue
let amountIn: TokenAmount let amountIn: CurrencyAmount
try { try {
;[amountIn] = pair.getInputAmount(amountOut) ;[amountIn] = pair.getInputAmount(amountOut)
} catch (error) { } catch (error) {
...@@ -395,7 +388,7 @@ export class Trade { ...@@ -395,7 +388,7 @@ export class Trade {
throw error throw error
} }
// we have arrived at the input token, so this is the first trade of one of the paths // we have arrived at the input token, so this is the first trade of one of the paths
if (amountIn.token.equals(tokenIn)) { if (currencyEquals(amountIn.currency, tokenIn)) {
sortedInsert( sortedInsert(
bestTrades, bestTrades,
new Trade( new Trade(
......
...@@ -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, WETH9 } from '@uniswap/sdk-core' import { ChainId, CurrencyAmount, ETHER, Percent, Token, 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')
...@@ -15,9 +15,12 @@ describe('Router', () => { ...@@ -15,9 +15,12 @@ describe('Router', () => {
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 pair_0_1 = new Pair(new TokenAmount(token0, JSBI.BigInt(1000)), new TokenAmount(token1, JSBI.BigInt(1000))) const pair_0_1 = new Pair(
new CurrencyAmount(token0, JSBI.BigInt(1000)),
new CurrencyAmount(token1, JSBI.BigInt(1000))
)
const pair_weth_0 = new Pair(new TokenAmount(WETH9[ChainId.MAINNET], '1000'), new TokenAmount(token0, '1000')) const pair_weth_0 = new Pair(new CurrencyAmount(WETH9[ChainId.MAINNET], '1000'), new CurrencyAmount(token0, '1000'))
describe('#swapCallParameters', () => { describe('#swapCallParameters', () => {
describe('exact in', () => { describe('exact in', () => {
...@@ -57,7 +60,10 @@ describe('Router', () => { ...@@ -57,7 +60,10 @@ describe('Router', () => {
it('token1 to ether', () => { it('token1 to ether', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactIn(new Route([pair_0_1, pair_weth_0], token1, ETHER), new TokenAmount(token1, JSBI.BigInt(100))), Trade.exactIn(
new Route([pair_0_1, pair_weth_0], token1, ETHER),
new CurrencyAmount(token1, JSBI.BigInt(100))
),
{ ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') } { ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') }
) )
expect(result.methodName).toEqual('swapExactTokensForETH') expect(result.methodName).toEqual('swapExactTokensForETH')
...@@ -72,7 +78,7 @@ describe('Router', () => { ...@@ -72,7 +78,7 @@ describe('Router', () => {
}) })
it('token0 to token1', () => { it('token0 to token1', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactIn(new Route([pair_0_1], token0, token1), new TokenAmount(token0, JSBI.BigInt(100))), Trade.exactIn(new Route([pair_0_1], token0, token1), new CurrencyAmount(token0, JSBI.BigInt(100))),
{ ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') } { ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') }
) )
expect(result.methodName).toEqual('swapExactTokensForTokens') expect(result.methodName).toEqual('swapExactTokensForTokens')
...@@ -89,7 +95,10 @@ describe('Router', () => { ...@@ -89,7 +95,10 @@ describe('Router', () => {
describe('exact out', () => { describe('exact out', () => {
it('ether to token1', () => { it('ether to token1', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactOut(new Route([pair_weth_0, pair_0_1], ETHER, token1), new TokenAmount(token1, JSBI.BigInt(100))), Trade.exactOut(
new Route([pair_weth_0, pair_0_1], ETHER, token1),
new CurrencyAmount(token1, JSBI.BigInt(100))
),
{ ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') } { ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') }
) )
expect(result.methodName).toEqual('swapETHForExactTokens') expect(result.methodName).toEqual('swapETHForExactTokens')
...@@ -118,7 +127,7 @@ describe('Router', () => { ...@@ -118,7 +127,7 @@ describe('Router', () => {
}) })
it('token0 to token1', () => { it('token0 to token1', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactOut(new Route([pair_0_1], token0, token1), new TokenAmount(token1, JSBI.BigInt(100))), Trade.exactOut(new Route([pair_0_1], token0, token1), new CurrencyAmount(token1, JSBI.BigInt(100))),
{ ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') } { ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') }
) )
expect(result.methodName).toEqual('swapTokensForExactTokens') expect(result.methodName).toEqual('swapTokensForExactTokens')
...@@ -155,7 +164,10 @@ describe('Router', () => { ...@@ -155,7 +164,10 @@ describe('Router', () => {
}) })
it('token1 to ether', () => { it('token1 to ether', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactIn(new Route([pair_0_1, pair_weth_0], token1, ETHER), new TokenAmount(token1, JSBI.BigInt(100))), Trade.exactIn(
new Route([pair_0_1, pair_weth_0], token1, ETHER),
new CurrencyAmount(token1, JSBI.BigInt(100))
),
{ {
ttl: 50, ttl: 50,
recipient: '0x0000000000000000000000000000000000000004', recipient: '0x0000000000000000000000000000000000000004',
...@@ -175,7 +187,7 @@ describe('Router', () => { ...@@ -175,7 +187,7 @@ describe('Router', () => {
}) })
it('token0 to token1', () => { it('token0 to token1', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactIn(new Route([pair_0_1], token0, token1), new TokenAmount(token0, JSBI.BigInt(100))), Trade.exactIn(new Route([pair_0_1], token0, token1), new CurrencyAmount(token0, JSBI.BigInt(100))),
{ {
ttl: 50, ttl: 50,
recipient: '0x0000000000000000000000000000000000000004', recipient: '0x0000000000000000000000000000000000000004',
...@@ -200,7 +212,7 @@ describe('Router', () => { ...@@ -200,7 +212,7 @@ describe('Router', () => {
Router.swapCallParameters( Router.swapCallParameters(
Trade.exactOut( Trade.exactOut(
new Route([pair_weth_0, pair_0_1], ETHER, token1), new Route([pair_weth_0, pair_0_1], ETHER, token1),
new TokenAmount(token1, JSBI.BigInt(100)) new CurrencyAmount(token1, JSBI.BigInt(100))
), ),
{ {
ttl: 50, ttl: 50,
...@@ -227,7 +239,7 @@ describe('Router', () => { ...@@ -227,7 +239,7 @@ describe('Router', () => {
it('token0 to token1', () => { it('token0 to token1', () => {
expect(() => expect(() =>
Router.swapCallParameters( Router.swapCallParameters(
Trade.exactOut(new Route([pair_0_1], token0, token1), new TokenAmount(token1, JSBI.BigInt(100))), Trade.exactOut(new Route([pair_0_1], token0, token1), new CurrencyAmount(token1, JSBI.BigInt(100))),
{ {
ttl: 50, ttl: 50,
recipient: '0x0000000000000000000000000000000000000004', recipient: '0x0000000000000000000000000000000000000004',
......
...@@ -1706,10 +1706,10 @@ ...@@ -1706,10 +1706,10 @@
semver "^7.3.2" semver "^7.3.2"
tsutils "^3.17.1" tsutils "^3.17.1"
"@uniswap/sdk-core@^1.0.11": "@uniswap/sdk-core@^2.0.2":
version "1.0.11" version "2.0.2"
resolved "https://registry.yarnpkg.com/@uniswap/sdk-core/-/sdk-core-1.0.11.tgz#f135a06696dc7cef0886e4f2edb1ce484a2cc4b7" resolved "https://registry.yarnpkg.com/@uniswap/sdk-core/-/sdk-core-2.0.2.tgz#748d1d189503d20d3027ef69927ad13cbf2d224a"
integrity sha512-fFfFBaum4AKYyaGbDc9b8hyW/2NEK1tH+bqewyJaY6QTAc4Q2snwcYFvreD+cgo1kGXj4L4XPrP6MdZr4uuBUQ== integrity sha512-Cx6epJgXE/b9ZP8GAes3LiYFXuxfd7UDZtn8Wvxr6xEMh8T21HHbsoEs9sB8iCBYfYuw2PT+Pza4GJqQNvnddg==
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