import { ChainId, Token, Pair, TokenAmount, WETH, Price } from '../src' describe('Pair', () => { const USDC = new Token(ChainId.MAINNET, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 18, 'USDC', 'USD Coin') const DAI = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18, 'DAI', 'DAI Stablecoin') const HARMONY_MAINNET_WONE = new Token(ChainId.HARMONY_MAINNET, '0xcF664087a5bB0237a0BAd6742852ec6c8d69A27a', 18, 'WONE', 'Wrapped ONE') const HARMONY_MAINNET_BUSD = new Token(ChainId.HARMONY_MAINNET, '0xE176EBE47d621b984a73036B9DA5d834411ef734', 18, 'BUSD', 'Binance USD') describe('constructor', () => { it('cannot be used for tokens on different chains', () => { expect(() => new Pair(new TokenAmount(USDC, '100'), new TokenAmount(WETH[ChainId.RINKEBY], '100'))).toThrow( 'CHAIN_IDS' ) }) }) describe('#getAddress', () => { it('returns the correct address for Ethereum Mainnet', () => { expect(Pair.getAddress(USDC, DAI)).toEqual('0x2055f109930A174Ec3F34CE20F42C7337e743250') }) it('returns the correct address for Harmony Mainnet', () => { expect(Pair.getAddress(HARMONY_MAINNET_WONE, HARMONY_MAINNET_BUSD)).toEqual('0x3a0C4D87BdE442150779d63c1c695d003184dF52') }) }) 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) }) }) 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) }) }) 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 TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserve0).toEqual( new TokenAmount(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 TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserve1).toEqual( new TokenAmount(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( new Price(DAI, USDC, '100', '101') ) expect(new Pair(new TokenAmount(DAI, '100'), new TokenAmount(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( new Price(USDC, DAI, '101', '100') ) expect(new Pair(new TokenAmount(DAI, '100'), new TokenAmount(USDC, '101')).token1Price).toEqual( new Price(USDC, DAI, '101', '100') ) }) }) describe('#priceOf', () => { const pair = new Pair(new TokenAmount(USDC, '101'), new TokenAmount(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) }) it('throws if invalid token', () => { expect(() => pair.priceOf(WETH[ChainId.MAINNET])).toThrow('TOKEN') }) }) 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 TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserveOf(USDC)).toEqual( new TokenAmount(USDC, '100') ) }) it('throws if not in the pair', () => { expect(() => new Pair(new TokenAmount(DAI, '101'), new TokenAmount(USDC, '100')).reserveOf(WETH[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) }) }) 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 TokenAmount(USDC, '100'), new TokenAmount(DAI, '100')).involvesToken(WETH[ChainId.MAINNET]) ).toEqual(false) }) })