-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiscellaneous.test.ts
173 lines (150 loc) · 5.67 KB
/
miscellaneous.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { ChainId, Token, TokenAmount, Pair, InsufficientInputAmountError } from '../src'
import { sortedInsert } from '../src/utils'
describe('miscellaneous', () => {
it('getLiquidityMinted:0', async () => {
const tokenA = new Token(ChainId.BSCTESTNET, '0x0000000000000000000000000000000000000001', 18)
const tokenB = new Token(ChainId.BSCTESTNET, '0x0000000000000000000000000000000000000002', 18)
const pair = new Pair(new TokenAmount(tokenA, '0'), new TokenAmount(tokenB, '0'))
expect(() => {
pair.getLiquidityMinted(
new TokenAmount(pair.liquidityToken, '0'),
new TokenAmount(tokenA, '1000'),
new TokenAmount(tokenB, '1000')
)
}).toThrow(InsufficientInputAmountError)
expect(() => {
pair.getLiquidityMinted(
new TokenAmount(pair.liquidityToken, '0'),
new TokenAmount(tokenA, '1000000'),
new TokenAmount(tokenB, '1')
)
}).toThrow(InsufficientInputAmountError)
const liquidity = pair.getLiquidityMinted(
new TokenAmount(pair.liquidityToken, '0'),
new TokenAmount(tokenA, '1001'),
new TokenAmount(tokenB, '1001')
)
expect(liquidity.raw.toString()).toEqual('1')
})
it('getLiquidityMinted:!0', async () => {
const tokenA = new Token(ChainId.BSCTESTNET, '0x0000000000000000000000000000000000000001', 18)
const tokenB = new Token(ChainId.BSCTESTNET, '0x0000000000000000000000000000000000000002', 18)
const pair = new Pair(new TokenAmount(tokenA, '10000'), new TokenAmount(tokenB, '10000'))
expect(
pair
.getLiquidityMinted(
new TokenAmount(pair.liquidityToken, '10000'),
new TokenAmount(tokenA, '2000'),
new TokenAmount(tokenB, '2000')
)
.raw.toString()
).toEqual('2000')
})
it('getLiquidityValue:!feeOn', async () => {
const tokenA = new Token(ChainId.BSCTESTNET, '0x0000000000000000000000000000000000000001', 18)
const tokenB = new Token(ChainId.BSCTESTNET, '0x0000000000000000000000000000000000000002', 18)
const pair = new Pair(new TokenAmount(tokenA, '1000'), new TokenAmount(tokenB, '1000'))
{
const liquidityValue = pair.getLiquidityValue(
tokenA,
new TokenAmount(pair.liquidityToken, '1000'),
new TokenAmount(pair.liquidityToken, '1000'),
false
)
expect(liquidityValue.token.equals(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'),
false
)
expect(liquidityValue.token.equals(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'),
false
)
expect(liquidityValue.token.equals(tokenB)).toBe(true)
expect(liquidityValue.raw.toString()).toBe('1000')
}
})
it('getLiquidityValue:feeOn', async () => {
const tokenA = new Token(ChainId.BSCTESTNET, '0x0000000000000000000000000000000000000001', 18)
const tokenB = new Token(ChainId.BSCTESTNET, '0x0000000000000000000000000000000000000002', 18)
const pair = new Pair(new TokenAmount(tokenA, '1000'), new TokenAmount(tokenB, '1000'))
const liquidityValue = pair.getLiquidityValue(
tokenA,
new TokenAmount(pair.liquidityToken, '500'),
new TokenAmount(pair.liquidityToken, '500'),
true,
'250000' // 500 ** 2
)
expect(liquidityValue.token.equals(tokenA)).toBe(true)
expect(liquidityValue.raw.toString()).toBe('917') // ceiling(1000 - (500 * (1 / 6)))
})
describe('#sortedInsert', () => {
const comp = (a: number, b: number) => a - b
it('throws if maxSize is 0', () => {
expect(() => sortedInsert([], 1, 0, comp)).toThrow('MAX_SIZE_ZERO')
})
it('throws if items.length > maxSize', () => {
expect(() => sortedInsert([1, 2], 1, 1, comp)).toThrow('ITEMS_SIZE')
})
it('adds if empty', () => {
const arr: number[] = []
expect(sortedInsert(arr, 3, 2, comp)).toEqual(null)
expect(arr).toEqual([3])
})
it('adds if not full', () => {
const arr: number[] = [1, 5]
expect(sortedInsert(arr, 3, 3, comp)).toEqual(null)
expect(arr).toEqual([1, 3, 5])
})
it('adds if will not be full after', () => {
const arr: number[] = [1]
expect(sortedInsert(arr, 0, 3, comp)).toEqual(null)
expect(arr).toEqual([0, 1])
})
it('returns add if sorts after last', () => {
const arr = [1, 2, 3]
expect(sortedInsert(arr, 4, 3, comp)).toEqual(4)
expect(arr).toEqual([1, 2, 3])
})
it('removes from end if full', () => {
const arr = [1, 3, 4]
expect(sortedInsert(arr, 2, 3, comp)).toEqual(4)
expect(arr).toEqual([1, 2, 3])
})
it('uses comparator', () => {
const arr = [4, 2, 1]
expect(sortedInsert(arr, 3, 3, (a, b) => comp(a, b) * -1)).toEqual(1)
expect(arr).toEqual([4, 3, 2])
})
describe('maxSize of 1', () => {
it('empty add', () => {
const arr: number[] = []
expect(sortedInsert(arr, 3, 1, comp)).toEqual(null)
expect(arr).toEqual([3])
})
it('full add greater', () => {
const arr: number[] = [2]
expect(sortedInsert(arr, 3, 1, comp)).toEqual(3)
expect(arr).toEqual([2])
})
it('full add lesser', () => {
const arr: number[] = [4]
expect(sortedInsert(arr, 3, 1, comp)).toEqual(4)
expect(arr).toEqual([3])
})
})
})
})