Skip to content

Commit

Permalink
no more instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed May 7, 2021
1 parent a371e54 commit f34b3d9
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 245 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {
"@ethersproject/address": "^5.0.0",
"@ethersproject/solidity": "^5.0.0",
"@uniswap/sdk-core": "^1.0.11",
"@uniswap/sdk-core": "^2.0.2",
"tiny-invariant": "^1.1.0",
"tiny-warning": "^1.0.3"
},
Expand Down
48 changes: 24 additions & 24 deletions src/entities/entities.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JSBI from 'jsbi'
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'

const ADDRESSES = [
Expand Down Expand Up @@ -37,16 +37,16 @@ describe('entities', () => {
it('Pair', () => {
pairs = [
new Pair(
new TokenAmount(tokens[0], decimalize(1, tokens[0].decimals)),
new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals))
new CurrencyAmount(tokens[0], decimalize(1, tokens[0].decimals)),
new CurrencyAmount(tokens[1], decimalize(1, tokens[1].decimals))
),
new Pair(
new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)),
new TokenAmount(tokens[2], decimalize(1, tokens[2].decimals))
new CurrencyAmount(tokens[1], decimalize(1, tokens[1].decimals)),
new CurrencyAmount(tokens[2], decimalize(1, tokens[2].decimals))
),
new Pair(
new TokenAmount(tokens[2], decimalize(1, tokens[2].decimals)),
new TokenAmount(WETH9, decimalize(1234, WETH9.decimals))
new CurrencyAmount(tokens[2], decimalize(1, tokens[2].decimals)),
new CurrencyAmount(WETH9, decimalize(1234, WETH9.decimals))
)
]
})
Expand All @@ -61,14 +61,14 @@ describe('entities', () => {
})

it('Price:Route.midPrice', () => {
invariant(route.input instanceof Token)
invariant(route.output instanceof Token)
expect(route.midPrice.quote(new TokenAmount(route.input, decimalize(1, route.input.decimals)))).toEqual(
new TokenAmount(route.output, decimalize(1234, route.output.decimals))
invariant(route.input.isToken)
invariant(route.output.isToken)
expect(route.midPrice.quote(new CurrencyAmount(route.input, decimalize(1, route.input.decimals)))).toEqual(
new CurrencyAmount(route.output, decimalize(1234, route.output.decimals))
)
expect(
route.midPrice.invert().quote(new TokenAmount(route.output, decimalize(1234, route.output.decimals)))
).toEqual(new TokenAmount(route.input, decimalize(1, route.input.decimals)))
route.midPrice.invert().quote(new CurrencyAmount(route.output, decimalize(1234, route.output.decimals)))
).toEqual(new CurrencyAmount(route.input, decimalize(1, route.input.decimals)))

expect(route.midPrice.toSignificant(1)).toEqual('1000')
expect(route.midPrice.toSignificant(2)).toEqual('1200')
Expand Down Expand Up @@ -105,14 +105,14 @@ describe('entities', () => {
route = new Route(
[
new Pair(
new TokenAmount(tokens[1], decimalize(5, tokens[1].decimals)),
new TokenAmount(WETH9, decimalize(10, WETH9.decimals))
new CurrencyAmount(tokens[1], decimalize(5, tokens[1].decimals)),
new CurrencyAmount(WETH9, decimalize(10, WETH9.decimals))
)
],
tokens[1]
)
const inputAmount = new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals))
const expectedOutputAmount = new TokenAmount(WETH9, '1662497915624478906')
const inputAmount = new CurrencyAmount(tokens[1], decimalize(1, tokens[1].decimals))
const expectedOutputAmount = new CurrencyAmount(WETH9, '1662497915624478906')
const trade = new Trade(route, inputAmount, TradeType.EXACT_INPUT)
expect(trade.route).toEqual(route)
expect(trade.tradeType).toEqual(TradeType.EXACT_INPUT)
Expand All @@ -131,8 +131,8 @@ describe('entities', () => {
})

it('TradeType.EXACT_OUTPUT', () => {
const outputAmount = new TokenAmount(WETH9, '1662497915624478906')
const expectedInputAmount = new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals))
const outputAmount = new CurrencyAmount(WETH9, '1662497915624478906')
const expectedInputAmount = new CurrencyAmount(tokens[1], decimalize(1, tokens[1].decimals))
const trade = new Trade(route, outputAmount, TradeType.EXACT_OUTPUT)
expect(trade.route).toEqual(route)
expect(trade.tradeType).toEqual(TradeType.EXACT_OUTPUT)
Expand All @@ -155,8 +155,8 @@ describe('entities', () => {
const route = new Route(
[
new Pair(
new TokenAmount(tokens[1], decimalize(1, tokens[1].decimals)),
new TokenAmount(
new CurrencyAmount(tokens[1], decimalize(1, tokens[1].decimals)),
new CurrencyAmount(
WETH9,
JSBI.add(
decimalize(10, WETH9.decimals),
Expand All @@ -167,7 +167,7 @@ describe('entities', () => {
],
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)

expect(trade.priceImpact.toSignificant(18)).toEqual(
Expand All @@ -177,8 +177,8 @@ describe('entities', () => {
})
})

it('TokenAmount', () => {
const amount = new TokenAmount(WETH9, '1234567000000000000000')
it('CurrencyAmount', () => {
const amount = new CurrencyAmount(WETH9, '1234567000000000000000')
expect(amount.toExact()).toEqual('1234.567')
expect(amount.toExact({ groupSeparator: ',' })).toEqual('1,234.567')
})
Expand Down
118 changes: 59 additions & 59 deletions src/entities/pair.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainId, Token, TokenAmount, WETH9, Price } from '@uniswap/sdk-core'
import { ChainId, Token, WETH9, Price, CurrencyAmount, currencyEquals } from '@uniswap/sdk-core'
import { InsufficientInputAmountError } from '../errors'
import { computePairAddress, Pair } from './pair'

Expand Down Expand Up @@ -43,9 +43,9 @@ describe('Pair', () => {

describe('constructor', () => {
it('cannot be used for tokens on different chains', () => {
expect(() => new Pair(new TokenAmount(USDC, '100'), new TokenAmount(WETH9[ChainId.RINKEBY], '100'))).toThrow(
'CHAIN_IDS'
)
expect(
() => new Pair(new CurrencyAmount(USDC, '100'), new CurrencyAmount(WETH9[ChainId.RINKEBY], '100'))
).toThrow('CHAIN_IDS')
})
})

Expand All @@ -57,61 +57,61 @@ describe('Pair', () => {

describe('#token0', () => {
it('always is the token that sorts before', () => {
expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).token0).toEqual(DAI)
expect(new Pair(new TokenAmount(DAI, '100'), new TokenAmount(USDC, '100')).token0).toEqual(DAI)
expect(new Pair(new CurrencyAmount(USDC, '100'), new CurrencyAmount(DAI, '100')).token0).toEqual(DAI)
expect(new Pair(new CurrencyAmount(DAI, '100'), new CurrencyAmount(USDC, '100')).token0).toEqual(DAI)
})
})
describe('#token1', () => {
it('always is the token that sorts after', () => {
expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).token1).toEqual(USDC)
expect(new Pair(new TokenAmount(DAI, '100'), new TokenAmount(USDC, '100')).token1).toEqual(USDC)
expect(new Pair(new CurrencyAmount(USDC, '100'), new CurrencyAmount(DAI, '100')).token1).toEqual(USDC)
expect(new Pair(new CurrencyAmount(DAI, '100'), new CurrencyAmount(USDC, '100')).token1).toEqual(USDC)
})
})
describe('#reserve0', () => {
it('always comes from the token that sorts before', () => {
expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '101')).reserve0).toEqual(
new TokenAmount(DAI, '101')
expect(new Pair(new CurrencyAmount(USDC, '100'), new CurrencyAmount(DAI, '101')).reserve0).toEqual(
new CurrencyAmount(DAI, '101')
)
expect(new Pair(new TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserve0).toEqual(
new TokenAmount(DAI, '101')
expect(new Pair(new CurrencyAmount(DAI, '101'), new CurrencyAmount(USDC, '100')).reserve0).toEqual(
new CurrencyAmount(DAI, '101')
)
})
})
describe('#reserve1', () => {
it('always comes from the token that sorts after', () => {
expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '101')).reserve1).toEqual(
new TokenAmount(USDC, '100')
expect(new Pair(new CurrencyAmount(USDC, '100'), new CurrencyAmount(DAI, '101')).reserve1).toEqual(
new CurrencyAmount(USDC, '100')
)
expect(new Pair(new TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserve1).toEqual(
new TokenAmount(USDC, '100')
expect(new Pair(new CurrencyAmount(DAI, '101'), new CurrencyAmount(USDC, '100')).reserve1).toEqual(
new CurrencyAmount(USDC, '100')
)
})
})

describe('#token0Price', () => {
it('returns price of token0 in terms of token1', () => {
expect(new Pair(new TokenAmount(USDC, '101'), new TokenAmount(DAI, '100')).token0Price).toEqual(
expect(new Pair(new CurrencyAmount(USDC, '101'), new CurrencyAmount(DAI, '100')).token0Price).toEqual(
new Price(DAI, USDC, '100', '101')
)
expect(new Pair(new TokenAmount(DAI, '100'), new TokenAmount(USDC, '101')).token0Price).toEqual(
expect(new Pair(new CurrencyAmount(DAI, '100'), new CurrencyAmount(USDC, '101')).token0Price).toEqual(
new Price(DAI, USDC, '100', '101')
)
})
})

describe('#token1Price', () => {
it('returns price of token1 in terms of token0', () => {
expect(new Pair(new TokenAmount(USDC, '101'), new TokenAmount(DAI, '100')).token1Price).toEqual(
expect(new Pair(new CurrencyAmount(USDC, '101'), new CurrencyAmount(DAI, '100')).token1Price).toEqual(
new Price(USDC, DAI, '101', '100')
)
expect(new Pair(new TokenAmount(DAI, '100'), new TokenAmount(USDC, '101')).token1Price).toEqual(
expect(new Pair(new CurrencyAmount(DAI, '100'), new CurrencyAmount(USDC, '101')).token1Price).toEqual(
new Price(USDC, DAI, '101', '100')
)
})
})

describe('#priceOf', () => {
const pair = new Pair(new TokenAmount(USDC, '101'), new TokenAmount(DAI, '100'))
const pair = new Pair(new CurrencyAmount(USDC, '101'), new CurrencyAmount(DAI, '100'))
it('returns price of token in terms of other token', () => {
expect(pair.priceOf(DAI)).toEqual(pair.token0Price)
expect(pair.priceOf(USDC)).toEqual(pair.token1Price)
Expand All @@ -124,60 +124,60 @@ describe('Pair', () => {

describe('#reserveOf', () => {
it('returns reserves of the given token', () => {
expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '101')).reserveOf(USDC)).toEqual(
new TokenAmount(USDC, '100')
expect(new Pair(new CurrencyAmount(USDC, '100'), new CurrencyAmount(DAI, '101')).reserveOf(USDC)).toEqual(
new CurrencyAmount(USDC, '100')
)
expect(new Pair(new TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserveOf(USDC)).toEqual(
new TokenAmount(USDC, '100')
expect(new Pair(new CurrencyAmount(DAI, '101'), new CurrencyAmount(USDC, '100')).reserveOf(USDC)).toEqual(
new CurrencyAmount(USDC, '100')
)
})

it('throws if not in the pair', () => {
expect(() =>
new Pair(new TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserveOf(WETH9[ChainId.MAINNET])
new Pair(new CurrencyAmount(DAI, '101'), new CurrencyAmount(USDC, '100')).reserveOf(WETH9[ChainId.MAINNET])
).toThrow('TOKEN')
})
})

describe('#chainId', () => {
it('returns the token0 chainId', () => {
expect(new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).chainId).toEqual(ChainId.MAINNET)
expect(new Pair(new TokenAmount(DAI, '100'), new TokenAmount(USDC, '100')).chainId).toEqual(ChainId.MAINNET)
expect(new Pair(new CurrencyAmount(USDC, '100'), new CurrencyAmount(DAI, '100')).chainId).toEqual(ChainId.MAINNET)
expect(new Pair(new CurrencyAmount(DAI, '100'), new CurrencyAmount(USDC, '100')).chainId).toEqual(ChainId.MAINNET)
})
})
describe('#involvesToken', () => {
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 CurrencyAmount(USDC, '100'), new CurrencyAmount(DAI, '100')).involvesToken(USDC)).toEqual(true)
expect(new Pair(new CurrencyAmount(USDC, '100'), new CurrencyAmount(DAI, '100')).involvesToken(DAI)).toEqual(true)
expect(
new Pair(new TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).involvesToken(WETH9[ChainId.MAINNET])
new Pair(new CurrencyAmount(USDC, '100'), new CurrencyAmount(DAI, '100')).involvesToken(WETH9[ChainId.MAINNET])
).toEqual(false)
})
describe('miscellaneous', () => {
it('getLiquidityMinted:0', async () => {
const tokenA = new Token(ChainId.RINKEBY, '0x0000000000000000000000000000000000000001', 18)
const tokenB = new Token(ChainId.RINKEBY, '0x0000000000000000000000000000000000000002', 18)
const pair = new Pair(new TokenAmount(tokenA, '0'), new TokenAmount(tokenB, '0'))
const pair = new Pair(new CurrencyAmount(tokenA, '0'), new CurrencyAmount(tokenB, '0'))

expect(() => {
pair.getLiquidityMinted(
new TokenAmount(pair.liquidityToken, '0'),
new TokenAmount(tokenA, '1000'),
new TokenAmount(tokenB, '1000')
new CurrencyAmount(pair.liquidityToken, '0'),
new CurrencyAmount(tokenA, '1000'),
new CurrencyAmount(tokenB, '1000')
)
}).toThrow(InsufficientInputAmountError)

expect(() => {
pair.getLiquidityMinted(
new TokenAmount(pair.liquidityToken, '0'),
new TokenAmount(tokenA, '1000000'),
new TokenAmount(tokenB, '1')
new CurrencyAmount(pair.liquidityToken, '0'),
new CurrencyAmount(tokenA, '1000000'),
new CurrencyAmount(tokenB, '1')
)
}).toThrow(InsufficientInputAmountError)

const liquidity = pair.getLiquidityMinted(
new TokenAmount(pair.liquidityToken, '0'),
new TokenAmount(tokenA, '1001'),
new TokenAmount(tokenB, '1001')
new CurrencyAmount(pair.liquidityToken, '0'),
new CurrencyAmount(tokenA, '1001'),
new CurrencyAmount(tokenB, '1001')
)

expect(liquidity.raw.toString()).toEqual('1')
Expand All @@ -186,14 +186,14 @@ describe('Pair', () => {
it('getLiquidityMinted:!0', async () => {
const tokenA = new Token(ChainId.RINKEBY, '0x0000000000000000000000000000000000000001', 18)
const tokenB = new Token(ChainId.RINKEBY, '0x0000000000000000000000000000000000000002', 18)
const pair = new Pair(new TokenAmount(tokenA, '10000'), new TokenAmount(tokenB, '10000'))
const pair = new Pair(new CurrencyAmount(tokenA, '10000'), new CurrencyAmount(tokenB, '10000'))

expect(
pair
.getLiquidityMinted(
new TokenAmount(pair.liquidityToken, '10000'),
new TokenAmount(tokenA, '2000'),
new TokenAmount(tokenB, '2000')
new CurrencyAmount(pair.liquidityToken, '10000'),
new CurrencyAmount(tokenA, '2000'),
new CurrencyAmount(tokenB, '2000')
)
.raw.toString()
).toEqual('2000')
Expand All @@ -202,57 +202,57 @@ describe('Pair', () => {
it('getLiquidityValue:!feeOn', async () => {
const tokenA = new Token(ChainId.RINKEBY, '0x0000000000000000000000000000000000000001', 18)
const tokenB = new Token(ChainId.RINKEBY, '0x0000000000000000000000000000000000000002', 18)
const pair = new Pair(new TokenAmount(tokenA, '1000'), new TokenAmount(tokenB, '1000'))
const pair = new Pair(new CurrencyAmount(tokenA, '1000'), new CurrencyAmount(tokenB, '1000'))

{
const liquidityValue = pair.getLiquidityValue(
tokenA,
new TokenAmount(pair.liquidityToken, '1000'),
new TokenAmount(pair.liquidityToken, '1000'),
new CurrencyAmount(pair.liquidityToken, '1000'),
new CurrencyAmount(pair.liquidityToken, '1000'),
false
)
expect(liquidityValue.token.equals(tokenA)).toBe(true)
expect(currencyEquals(liquidityValue.currency, tokenA)).toBe(true)
expect(liquidityValue.raw.toString()).toBe('1000')
}

// 500
{
const liquidityValue = pair.getLiquidityValue(
tokenA,
new TokenAmount(pair.liquidityToken, '1000'),
new TokenAmount(pair.liquidityToken, '500'),
new CurrencyAmount(pair.liquidityToken, '1000'),
new CurrencyAmount(pair.liquidityToken, '500'),
false
)
expect(liquidityValue.token.equals(tokenA)).toBe(true)
expect(currencyEquals(liquidityValue.currency, tokenA)).toBe(true)
expect(liquidityValue.raw.toString()).toBe('500')
}

// tokenB
{
const liquidityValue = pair.getLiquidityValue(
tokenB,
new TokenAmount(pair.liquidityToken, '1000'),
new TokenAmount(pair.liquidityToken, '1000'),
new CurrencyAmount(pair.liquidityToken, '1000'),
new CurrencyAmount(pair.liquidityToken, '1000'),
false
)
expect(liquidityValue.token.equals(tokenB)).toBe(true)
expect(currencyEquals(liquidityValue.currency, tokenB)).toBe(true)
expect(liquidityValue.raw.toString()).toBe('1000')
}
})

it('getLiquidityValue:feeOn', async () => {
const tokenA = new Token(ChainId.RINKEBY, '0x0000000000000000000000000000000000000001', 18)
const tokenB = new Token(ChainId.RINKEBY, '0x0000000000000000000000000000000000000002', 18)
const pair = new Pair(new TokenAmount(tokenA, '1000'), new TokenAmount(tokenB, '1000'))
const pair = new Pair(new CurrencyAmount(tokenA, '1000'), new CurrencyAmount(tokenB, '1000'))

const liquidityValue = pair.getLiquidityValue(
tokenA,
new TokenAmount(pair.liquidityToken, '500'),
new TokenAmount(pair.liquidityToken, '500'),
new CurrencyAmount(pair.liquidityToken, '500'),
new CurrencyAmount(pair.liquidityToken, '500'),
true,
'250000' // 500 ** 2
)
expect(liquidityValue.token.equals(tokenA)).toBe(true)
expect(currencyEquals(liquidityValue.currency, tokenA)).toBe(true)
expect(liquidityValue.raw.toString()).toBe('917') // ceiling(1000 - (500 * (1 / 6)))
})
})
Expand Down
Loading

0 comments on commit f34b3d9

Please sign in to comment.