forked from BlockNG-Foundation/blockng-swap-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpair.test.ts
118 lines (107 loc) · 4.68 KB
/
pair.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { ChainId, Token, Pair, TokenAmount, WETH, Price } from '../src'
describe('Pair', () => {
const USDC = new Token(ChainId.MAINNET, '0x4ee06d0486ced674E75Ed9e521725580e8ffDA21', 18, 'USDC', 'USD Coin') // lawETP
const DAI = new Token(ChainId.MAINNET, '0x0b00366fBF7037E9d75E4A569ab27dAB84759302', 18, 'DAI', 'DAI Stablecoin') // LAW
describe('constructor', () => {
it('cannot be used for tokens on different chains', () => {
expect(() => new Pair(new TokenAmount(USDC, '100'), new TokenAmount(WETH[ChainId.TESTNET], '100'))).toThrow(
'CHAIN_IDS'
)
})
})
describe('#getAddress', () => {
it('returns the correct address', () => {
expect(Pair.getAddress(USDC, DAI)).toEqual('0x8735628e1E5442B49A37F9751B0793C11014d1b6')
})
})
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)
})
})