-
Notifications
You must be signed in to change notification settings - Fork 35
/
testpoolswap.test.ts
192 lines (167 loc) · 7.01 KB
/
testpoolswap.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import BigNumber from 'bignumber.js'
import { addPoolLiquidity, createPoolPair, createToken, getNewAddress, mintTokens } from '@defichain/testing'
import { RpcApiError } from '../../../src'
import { poolpair } from '@defichain/jellyfish-api-core'
describe('Poolpair', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
})
afterAll(async () => {
await container.stop()
})
it('should testpoolswap', async () => {
const tokenAddress = await getNewAddress(container)
const dfiAddress = await getNewAddress(container)
const poolLiquidityAddress = await getNewAddress(container)
await createToken(container, 'CAT', { collateralAddress: tokenAddress })
await mintTokens(container, 'CAT', { address: dfiAddress })
await createPoolPair(container, 'CAT', 'DFI')
await addPoolLiquidity(container, {
tokenA: 'CAT',
amountA: 1000,
tokenB: 'DFI',
amountB: 500,
shareAddress: poolLiquidityAddress
})
const poolpairResult: poolpair.PoolPairsResult = await container.call('getpoolpair', ['CAT-DFI'])
expect(Object.keys(poolpairResult).length).toStrictEqual(1)
// Note(canonbrother): simulate poolswap calculation to find reserveB
// case: swap 666 CAT to DFI
// 1000 : 500 = sqrt(1000 * 500) = 707.10678118
// 1666 : ? = sqrt(1666 * ?) = 707.10678118
// ? = 707.10678118^2 / 1666 = 300.120048014
// 1666 : 300.120048014 = 707.10678118
// 500 - 300.120048014 = 199.879951986 <-- testpoolswap returns
const poolpair: poolpair.PoolPairInfo = Object.values(poolpairResult)[0]
const reserveAAfter: BigNumber = new BigNumber(poolpair.reserveA).plus(666)
const reserveBAfter: BigNumber = new BigNumber(poolpair.totalLiquidity).pow(2).div(reserveAAfter)
const result = await client.poolpair.testPoolSwap({
from: tokenAddress,
tokenFrom: 'CAT',
amountFrom: 666,
to: await getNewAddress(container),
tokenTo: 'DFI'
}) // 199.99999729@0
const testPoolSwapResultAmount = new BigNumber(result.split('@')[0]) // 199.99999729
const swapped = new BigNumber(poolpair.reserveB).minus(reserveBAfter) // 199.87995198635029880408
// Note(canonbrother): the result has slightly diff, use toFixed() to ignore the decimals
expect(swapped.toFixed(0)).toStrictEqual(testPoolSwapResultAmount.toFixed(0))
})
it('should testpoolswap with maxPrice', async () => {
const tokenAddress = await getNewAddress(container)
const dfiAddress = await getNewAddress(container)
const poolLiquidityAddress = await getNewAddress(container)
await createToken(container, 'ELF', { collateralAddress: tokenAddress })
await mintTokens(container, 'ELF', { address: dfiAddress })
await createPoolPair(container, 'ELF', 'DFI')
await addPoolLiquidity(container, {
tokenA: 'ELF',
amountA: 200,
tokenB: 'DFI',
amountB: 500,
shareAddress: poolLiquidityAddress
})
const receive = await client.poolpair.testPoolSwap({
from: tokenAddress,
tokenFrom: 'ELF',
amountFrom: 666,
to: await getNewAddress(container),
tokenTo: 'DFI',
// reserveA/reserveB: 0.4
// reserveB/reserveA: 2.5
// if set maxPrice lower than 2.5 will hit error
maxPrice: 2.5
})
const swappedAmount = new BigNumber(500).minus(new BigNumber(500 * 200).dividedBy(200 + 666)).multipliedBy(1e8).minus(1).dividedBy(1e8).decimalPlaces(8, BigNumber.ROUND_CEIL)
expect(typeof receive).toStrictEqual('string')
expect(receive).toStrictEqual(`${swappedAmount.toString()}@0`) // calculation refers to 'should testpoolswap' above
})
it('should be failed as maxPrice is set lower than reserveB/reserveA', async () => {
const tokenAddress = await getNewAddress(container)
const dfiAddress = await getNewAddress(container)
const poolLiquidityAddress = await getNewAddress(container)
await createToken(container, 'FOX', { collateralAddress: tokenAddress })
await mintTokens(container, 'FOX', { address: dfiAddress })
await createPoolPair(container, 'FOX', 'DFI')
await addPoolLiquidity(container, {
tokenA: 'FOX',
amountA: 1000,
tokenB: 'DFI',
amountB: 500,
shareAddress: poolLiquidityAddress
})
const promise = client.poolpair.testPoolSwap({
from: tokenAddress,
tokenFrom: 'FOX',
amountFrom: 666,
to: await getNewAddress(container),
tokenTo: 'DFI',
// reserveA/reserveB: 2
// reserveB/reserveA: 0.5
// set maxPrice lower than 0.5 will hit error
maxPrice: 0.4
})
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toMatchObject({
payload: {
code: -32600,
message: 'Cannot find usable pool pair. Details: Price is higher than indicated.',
method: 'testpoolswap'
}
})
})
it('testpoolswap should not affect the ori poolpair data', async () => {
const tokenAddress = await getNewAddress(container)
const dfiAddress = await getNewAddress(container)
const poolLiquidityAddress = await getNewAddress(container)
await createToken(container, 'DOG', { collateralAddress: tokenAddress })
await mintTokens(container, 'DOG', { address: dfiAddress })
await createPoolPair(container, 'DOG', 'DFI')
await addPoolLiquidity(container, {
tokenA: 'DOG',
amountA: 1000,
tokenB: 'DFI',
amountB: 500,
shareAddress: poolLiquidityAddress
})
const poolpairResultBefore: poolpair.PoolPairsResult = await container.call('getpoolpair', ['DOG-DFI'])
expect(Object.keys(poolpairResultBefore).length).toStrictEqual(1)
await client.poolpair.testPoolSwap({
from: tokenAddress,
tokenFrom: 'CAT',
amountFrom: 666,
to: await getNewAddress(container),
tokenTo: 'DFI'
})
const poolpairResultAfter: poolpair.PoolPairsResult = await container.call('getpoolpair', ['DOG-DFI'])
expect(Object.keys(poolpairResultAfter).length).toStrictEqual(1)
expect(poolpairResultBefore).toStrictEqual(poolpairResultAfter)
})
it('should be failed as lack of liquidity', async () => {
const tokenBatAddress = await getNewAddress(container)
await createToken(container, 'BAT')
await mintTokens(container, 'BAT')
await createPoolPair(container, 'BAT', 'DFI')
const promise = client.poolpair.testPoolSwap({
from: tokenBatAddress,
tokenFrom: 'BAT',
amountFrom: 13,
to: await getNewAddress(container),
tokenTo: 'DFI'
})
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toMatchObject({
payload: {
code: -32600,
message: 'Cannot find usable pool pair. Details: Lack of liquidity.',
method: 'testpoolswap'
}
})
})
})