Commit 6e86d3c7 authored by Moody Salem's avatar Moody Salem

use the new sdk-core version

parent 6440270c
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -5,12 +5,12 @@ describe('Route', () => { ...@@ -5,12 +5,12 @@ 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 CurrencyAmount(token0, '100'), new CurrencyAmount(token1, '200')) const pair_0_1 = new Pair(CurrencyAmount.fromRawAmount(token0, '100'), CurrencyAmount.fromRawAmount(token1, '200'))
const pair_0_weth = new Pair(new CurrencyAmount(token0, '100'), new CurrencyAmount(weth, '100')) const pair_0_weth = new Pair(CurrencyAmount.fromRawAmount(token0, '100'), CurrencyAmount.fromRawAmount(weth, '100'))
const pair_1_weth = new Pair(new CurrencyAmount(token1, '175'), new CurrencyAmount(weth, '100')) const pair_1_weth = new Pair(CurrencyAmount.fromRawAmount(token1, '175'), CurrencyAmount.fromRawAmount(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, token1)
expect(route.pairs).toEqual([pair_0_1]) expect(route.pairs).toEqual([pair_0_1])
expect(route.path).toEqual([token0, token1]) expect(route.path).toEqual([token0, token1])
expect(route.input).toEqual(token0) expect(route.input).toEqual(token0)
...@@ -19,14 +19,14 @@ describe('Route', () => { ...@@ -19,14 +19,14 @@ describe('Route', () => {
}) })
it('can have a token as both input and output', () => { it('can have a token as both input and output', () => {
const route = new Route([pair_0_weth, pair_0_1, pair_1_weth], weth) const route = new Route([pair_0_weth, pair_0_1, pair_1_weth], weth, weth)
expect(route.pairs).toEqual([pair_0_weth, pair_0_1, pair_1_weth]) expect(route.pairs).toEqual([pair_0_weth, pair_0_1, pair_1_weth])
expect(route.input).toEqual(weth) expect(route.input).toEqual(weth)
expect(route.output).toEqual(weth) expect(route.output).toEqual(weth)
}) })
it('supports ether input', () => { it('supports ether input', () => {
const route = new Route([pair_0_weth], ETHER) const route = new Route([pair_0_weth], ETHER, token0)
expect(route.pairs).toEqual([pair_0_weth]) expect(route.pairs).toEqual([pair_0_weth])
expect(route.input).toEqual(ETHER) expect(route.input).toEqual(ETHER)
expect(route.output).toEqual(token0) expect(route.output).toEqual(token0)
......
import { ChainId, Currency, ETHER, Price, Token, WETH9 } from '@uniswap/sdk-core'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
import { ChainId, Currency, Price, Token, wrappedCurrency } from '@uniswap/sdk-core'
import { Pair } from './pair' import { Pair } from './pair'
export class Route { export class Route<TInput extends Currency, TOutput extends Currency> {
public readonly pairs: Pair[] public readonly pairs: Pair[]
public readonly path: Token[] public readonly path: Token[]
public readonly input: Currency public readonly input: TInput
public readonly output: Currency public readonly output: TOutput
public get midPrice(): Price { public constructor(pairs: Pair[], input: TInput, output: TOutput) {
const prices: Price[] = []
for (const [i, pair] of this.pairs.entries()) {
prices.push(
this.path[i].equals(pair.token0)
? new Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.raw, pair.reserve1.raw)
: new Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.raw, pair.reserve0.raw)
)
}
return prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0])
}
public constructor(pairs: Pair[], input: Currency, output?: Currency) {
invariant(pairs.length > 0, 'PAIRS') invariant(pairs.length > 0, 'PAIRS')
const chainId: ChainId | number = pairs[0].chainId const chainId: ChainId | number = pairs[0].chainId
invariant( invariant(
...@@ -29,20 +17,14 @@ export class Route { ...@@ -29,20 +17,14 @@ export class Route {
'CHAIN_IDS' 'CHAIN_IDS'
) )
const weth: Token | undefined = WETH9[chainId as ChainId] const wrappedInput = wrappedCurrency(input, chainId)
invariant(pairs[0].involvesToken(wrappedInput), 'INPUT')
invariant(
(input.isToken && pairs[0].involvesToken(input)) || (input === ETHER && weth && pairs[0].involvesToken(weth)),
'INPUT'
)
invariant( invariant(
typeof output === 'undefined' || typeof output === 'undefined' || pairs[pairs.length - 1].involvesToken(wrappedCurrency(output, chainId)),
(output.isToken && pairs[pairs.length - 1].involvesToken(output)) ||
(output === ETHER && weth && pairs[pairs.length - 1].involvesToken(weth)),
'OUTPUT' 'OUTPUT'
) )
const path: Token[] = [input.isToken ? input : weth] const path: Token[] = [wrappedInput]
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')
...@@ -53,7 +35,23 @@ export class Route { ...@@ -53,7 +35,23 @@ export class Route {
this.pairs = pairs this.pairs = pairs
this.path = path this.path = path
this.input = input this.input = input
this.output = output ?? path[path.length - 1] this.output = output
}
private _midPrice: Price<TInput, TOutput> | null = null
public get midPrice(): Price<TInput, TOutput> {
if (this._midPrice !== null) return this._midPrice
const prices: Price<Currency, Currency>[] = []
for (const [i, pair] of this.pairs.entries()) {
prices.push(
this.path[i].equals(pair.token0)
? new Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient)
: new Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient)
)
}
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0])
return (this._midPrice = new Price(this.input, this.output, reduced.denominator, reduced.numerator))
} }
public get chainId(): ChainId | number { public get chainId(): ChainId | number {
......
This diff is collapsed.
This diff is collapsed.
import JSBI from 'jsbi'
export { JSBI }
export { FACTORY_ADDRESS, INIT_CODE_HASH, MINIMUM_LIQUIDITY } from './constants' export { FACTORY_ADDRESS, INIT_CODE_HASH, MINIMUM_LIQUIDITY } from './constants'
export * from './errors' export * from './errors'
......
...@@ -16,15 +16,18 @@ describe('Router', () => { ...@@ -16,15 +16,18 @@ describe('Router', () => {
const token1 = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000002', 18, 't1') const token1 = new Token(ChainId.MAINNET, '0x0000000000000000000000000000000000000002', 18, 't1')
const pair_0_1 = new Pair( const pair_0_1 = new Pair(
new CurrencyAmount(token0, JSBI.BigInt(1000)), CurrencyAmount.fromRawAmount(token0, JSBI.BigInt(1000)),
new CurrencyAmount(token1, JSBI.BigInt(1000)) CurrencyAmount.fromRawAmount(token1, JSBI.BigInt(1000))
) )
const pair_weth_0 = new Pair(new CurrencyAmount(WETH9[ChainId.MAINNET], '1000'), new CurrencyAmount(token0, '1000')) const pair_weth_0 = new Pair(
CurrencyAmount.fromRawAmount(WETH9[ChainId.MAINNET], '1000'),
CurrencyAmount.fromRawAmount(token0, '1000')
)
describe('#swapCallParameters', () => { describe('#swapCallParameters', () => {
describe('exact in', () => { describe('exact in', () => {
it('ether to token1', () => { it.only('ether to token1', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactIn(new Route([pair_weth_0, pair_0_1], ETHER, token1), CurrencyAmount.ether(JSBI.BigInt(100))), Trade.exactIn(new Route([pair_weth_0, pair_0_1], ETHER, token1), CurrencyAmount.ether(JSBI.BigInt(100))),
{ ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') } { ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') }
...@@ -62,7 +65,7 @@ describe('Router', () => { ...@@ -62,7 +65,7 @@ describe('Router', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactIn( Trade.exactIn(
new Route([pair_0_1, pair_weth_0], token1, ETHER), new Route([pair_0_1, pair_weth_0], token1, ETHER),
new CurrencyAmount(token1, JSBI.BigInt(100)) CurrencyAmount.fromRawAmount(token1, JSBI.BigInt(100))
), ),
{ ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') } { ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') }
) )
...@@ -78,7 +81,7 @@ describe('Router', () => { ...@@ -78,7 +81,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 CurrencyAmount(token0, JSBI.BigInt(100))), Trade.exactIn(new Route([pair_0_1], token0, token1), CurrencyAmount.fromRawAmount(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')
...@@ -97,7 +100,7 @@ describe('Router', () => { ...@@ -97,7 +100,7 @@ describe('Router', () => {
const result = Router.swapCallParameters( const result = 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 CurrencyAmount(token1, JSBI.BigInt(100)) CurrencyAmount.fromRawAmount(token1, JSBI.BigInt(100))
), ),
{ ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') } { ttl: 50, recipient: '0x0000000000000000000000000000000000000004', allowedSlippage: new Percent('1', '100') }
) )
...@@ -127,7 +130,7 @@ describe('Router', () => { ...@@ -127,7 +130,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 CurrencyAmount(token1, JSBI.BigInt(100))), Trade.exactOut(new Route([pair_0_1], token0, token1), CurrencyAmount.fromRawAmount(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')
...@@ -166,7 +169,7 @@ describe('Router', () => { ...@@ -166,7 +169,7 @@ describe('Router', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactIn( Trade.exactIn(
new Route([pair_0_1, pair_weth_0], token1, ETHER), new Route([pair_0_1, pair_weth_0], token1, ETHER),
new CurrencyAmount(token1, JSBI.BigInt(100)) CurrencyAmount.fromRawAmount(token1, JSBI.BigInt(100))
), ),
{ {
ttl: 50, ttl: 50,
...@@ -187,7 +190,10 @@ describe('Router', () => { ...@@ -187,7 +190,10 @@ 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 CurrencyAmount(token0, JSBI.BigInt(100))), Trade.exactIn(
new Route([pair_0_1], token0, token1),
CurrencyAmount.fromRawAmount(token0, JSBI.BigInt(100))
),
{ {
ttl: 50, ttl: 50,
recipient: '0x0000000000000000000000000000000000000004', recipient: '0x0000000000000000000000000000000000000004',
...@@ -212,7 +218,7 @@ describe('Router', () => { ...@@ -212,7 +218,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 CurrencyAmount(token1, JSBI.BigInt(100)) CurrencyAmount.fromRawAmount(token1, JSBI.BigInt(100))
), ),
{ {
ttl: 50, ttl: 50,
...@@ -239,7 +245,10 @@ describe('Router', () => { ...@@ -239,7 +245,10 @@ 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 CurrencyAmount(token1, JSBI.BigInt(100))), Trade.exactOut(
new Route([pair_0_1], token0, token1),
CurrencyAmount.fromRawAmount(token1, JSBI.BigInt(100))
),
{ {
ttl: 50, ttl: 50,
recipient: '0x0000000000000000000000000000000000000004', recipient: '0x0000000000000000000000000000000000000004',
......
import { CurrencyAmount, ETHER, Percent, TradeType, validateAndParseAddress } from '@uniswap/sdk-core' import { Currency, CurrencyAmount, Percent, TradeType, validateAndParseAddress } from '@uniswap/sdk-core'
import { Trade } from 'entities' import { Trade } from 'entities'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
import { Token } from '../../../sdk-core'
/** /**
* Options for producing the arguments to send call to the router. * Options for producing the arguments to send call to the router.
...@@ -53,8 +54,8 @@ export interface SwapParameters { ...@@ -53,8 +54,8 @@ export interface SwapParameters {
value: string value: string
} }
function toHex(currencyAmount: CurrencyAmount) { function toHex(currencyAmount: CurrencyAmount<Currency>) {
return `0x${currencyAmount.raw.toString(16)}` return `0x${currencyAmount.quotient.toString(16)}`
} }
const ZERO_HEX = '0x0' const ZERO_HEX = '0x0'
...@@ -72,9 +73,12 @@ export abstract class Router { ...@@ -72,9 +73,12 @@ export abstract class Router {
* @param trade to produce call parameters for * @param trade to produce call parameters for
* @param options options for the call parameters * @param options options for the call parameters
*/ */
public static swapCallParameters(trade: Trade, options: TradeOptions | TradeOptionsDeadline): SwapParameters { public static swapCallParameters(
const etherIn = trade.inputAmount.currency === ETHER trade: Trade<Currency, Currency, TradeType>,
const etherOut = trade.outputAmount.currency === ETHER options: TradeOptions | TradeOptionsDeadline
): SwapParameters {
const etherIn = trade.inputAmount.currency.isEther
const etherOut = trade.outputAmount.currency.isEther
// the router does not support both ether in and out // the router does not support both ether in and out
invariant(!(etherIn && etherOut), 'ETHER_IN_OUT') invariant(!(etherIn && etherOut), 'ETHER_IN_OUT')
invariant(!('ttl' in options) || options.ttl > 0, 'TTL') invariant(!('ttl' in options) || options.ttl > 0, 'TTL')
...@@ -82,7 +86,7 @@ export abstract class Router { ...@@ -82,7 +86,7 @@ export abstract class Router {
const to: string = validateAndParseAddress(options.recipient) const to: string = validateAndParseAddress(options.recipient)
const amountIn: string = toHex(trade.maximumAmountIn(options.allowedSlippage)) const amountIn: string = toHex(trade.maximumAmountIn(options.allowedSlippage))
const amountOut: string = toHex(trade.minimumAmountOut(options.allowedSlippage)) const amountOut: string = toHex(trade.minimumAmountOut(options.allowedSlippage))
const path: string[] = trade.route.path.map(token => token.address) const path: string[] = trade.route.path.map((token: Token) => token.address)
const deadline = const deadline =
'ttl' in options 'ttl' in options
? `0x${(Math.floor(new Date().getTime() / 1000) + options.ttl).toString(16)}` ? `0x${(Math.floor(new Date().getTime() / 1000) + options.ttl).toString(16)}`
......
...@@ -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@^2.0.2": "@uniswap/sdk-core@^3.0.0-alpha.0":
version "2.0.2" version "3.0.0-alpha.0"
resolved "https://registry.yarnpkg.com/@uniswap/sdk-core/-/sdk-core-2.0.2.tgz#748d1d189503d20d3027ef69927ad13cbf2d224a" resolved "https://registry.yarnpkg.com/@uniswap/sdk-core/-/sdk-core-3.0.0-alpha.0.tgz#03f3515bc4b735c4d04b4a49e9518b2ea023a707"
integrity sha512-Cx6epJgXE/b9ZP8GAes3LiYFXuxfd7UDZtn8Wvxr6xEMh8T21HHbsoEs9sB8iCBYfYuw2PT+Pza4GJqQNvnddg== integrity sha512-42K3bYBYVdf45E5ek4GcaIkbW3eTkcpGYAufN7rw+LRsvnb5HlbF3FF0NxUP2WwLcyTe1DImHupIROUmCrC61A==
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