forked from hashflownetwork/paraswap-dex-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__DexName__-integration.test.ts.template
250 lines (221 loc) · 7.05 KB
/
__DexName__-integration.test.ts.template
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import dotenv from 'dotenv';
dotenv.config();
import { Interface, Result } from '@ethersproject/abi';
import { DummyDexHelper } from '../../dex-helper/index';
import { Network, SwapSide } from '../../constants';
import { BI_POWS } from '../../bigint-constants';
import { __DexName__ } from './__DexNameParam__';
import {
checkPoolPrices,
checkPoolsLiquidity,
checkConstantPoolPrices,
} from '../../../tests/utils';
import { Tokens } from '../../../tests/constants-e2e';
/*
README
======
This test script adds tests for __DexName__ general integration
with the DEX interface. The test cases below are example tests.
It is recommended to add tests which cover __DexName__ specific
logic.
You can run this individual test script by running:
`npx jest src/dex/<dex-name>/<dex-name>-integration.test.ts`
(This comment should be removed from the final implementation)
*/
function getReaderCalldata(
exchangeAddress: string,
readerIface: Interface,
amounts: bigint[],
funcName: string,
// TODO: Put here additional arguments you need
) {
return amounts.map(amount => ({
target: exchangeAddress,
callData: readerIface.encodeFunctionData(funcName, [
// TODO: Put here additional arguments to encode them
amount,
]),
}));
}
function decodeReaderResult(
results: Result,
readerIface: Interface,
funcName: string,
) {
// TODO: Adapt this function for your needs
return results.map(result => {
const parsed = readerIface.decodeFunctionResult(funcName, result);
return BigInt(parsed[0]._hex);
});
}
async function checkOnChainPricing(
__DexNameCamel__: __DexName__,
funcName: string,
blockNumber: number,
prices: bigint[],
amounts: bigint[],
) {
const exchangeAddress = ''; // TODO: Put here the real exchange address
// TODO: Replace dummy interface with the real one
// Normally you can get it from __DexNameCamel__.Iface or from eventPool.
// It depends on your implementation
const readerIface = new Interface('');
const readerCallData = getReaderCalldata(
exchangeAddress,
readerIface,
amounts.slice(1),
funcName,
);
const readerResult = (
await __DexNameCamel__.dexHelper.multiContract.methods
.aggregate(readerCallData)
.call({}, blockNumber)
).returnData;
const expectedPrices = [0n].concat(
decodeReaderResult(readerResult, readerIface, funcName),
);
expect(prices).toEqual(expectedPrices);
}
async function testPricingOnNetwork(
__DexNameCamel__: __DexName__,
network: Network,
dexKey: string,
blockNumber: number,
srcTokenSymbol: string,
destTokenSymbol: string,
side: SwapSide,
amounts: bigint[],
funcNameToCheck: string,
) {
const networkTokens = Tokens[network];
const pools = await __DexNameCamel__.getPoolIdentifiers(
networkTokens[srcTokenSymbol],
networkTokens[destTokenSymbol],
side,
blockNumber,
);
console.log(
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Identifiers: `,
pools,
);
expect(pools.length).toBeGreaterThan(0);
const poolPrices = await __DexNameCamel__.getPricesVolume(
networkTokens[srcTokenSymbol],
networkTokens[destTokenSymbol],
amounts,
side,
blockNumber,
pools,
);
console.log(
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Prices: `,
poolPrices,
);
expect(poolPrices).not.toBeNull();
if (__DexNameCamel__.hasConstantPriceLargeAmounts) {
checkConstantPoolPrices(poolPrices!, amounts, dexKey);
} else {
checkPoolPrices(poolPrices!, amounts, side, dexKey);
}
// Check if onchain pricing equals to calculated ones
await checkOnChainPricing(
__DexNameCamel__,
funcNameToCheck,
blockNumber,
poolPrices![0].prices,
amounts,
);
}
describe('__DexName__', function () {
const dexKey = '__DexName__';
let blockNumber: number;
let __DexNameCamel__: __DexName__;
describe('Mainnet', () => {
const network = Network.MAINNET;
const dexHelper = new DummyDexHelper(network);
const tokens = Tokens[network];
// TODO: Put here token Symbol to check against
// Don't forget to update relevant tokens in constant-e2e.ts
const srcTokenSymbol = 'srcTokenSymbol';
const destTokenSymbol = 'destTokenSymbol';
const amountsForSell = [
0n,
1n * BI_POWS[tokens[srcTokenSymbol].decimals],
2n * BI_POWS[tokens[srcTokenSymbol].decimals],
3n * BI_POWS[tokens[srcTokenSymbol].decimals],
4n * BI_POWS[tokens[srcTokenSymbol].decimals],
5n * BI_POWS[tokens[srcTokenSymbol].decimals],
6n * BI_POWS[tokens[srcTokenSymbol].decimals],
7n * BI_POWS[tokens[srcTokenSymbol].decimals],
8n * BI_POWS[tokens[srcTokenSymbol].decimals],
9n * BI_POWS[tokens[srcTokenSymbol].decimals],
10n * BI_POWS[tokens[srcTokenSymbol].decimals],
];
const amountsForBuy = [
0n,
1n * BI_POWS[tokens[destTokenSymbol].decimals],
2n * BI_POWS[tokens[destTokenSymbol].decimals],
3n * BI_POWS[tokens[destTokenSymbol].decimals],
4n * BI_POWS[tokens[destTokenSymbol].decimals],
5n * BI_POWS[tokens[destTokenSymbol].decimals],
6n * BI_POWS[tokens[destTokenSymbol].decimals],
7n * BI_POWS[tokens[destTokenSymbol].decimals],
8n * BI_POWS[tokens[destTokenSymbol].decimals],
9n * BI_POWS[tokens[destTokenSymbol].decimals],
10n * BI_POWS[tokens[destTokenSymbol].decimals],
];
beforeAll(async () => {
blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();
__DexNameCamel__ = new __DexName__(network, dexKey, dexHelper);
if (__DexNameCamel__.initializePricing) {
await __DexNameCamel__.initializePricing(blockNumber);
}
});
it('getPoolIdentifiers and getPricesVolume SELL', async function () {
await testPricingOnNetwork(
__DexNameCamel__,
network,
dexKey,
blockNumber,
srcTokenSymbol,
destTokenSymbol,
SwapSide.SELL,
amountsForSell,
'', // TODO: Put here proper function name to check pricing
);
});
it('getPoolIdentifiers and getPricesVolume BUY', async function () {
await testPricingOnNetwork(
__DexNameCamel__,
network,
dexKey,
blockNumber,
srcTokenSymbol,
destTokenSymbol,
SwapSide.BUY,
amountsForBuy,
'', // TODO: Put here proper function name to check pricing
);
});
it('getTopPoolsForToken', async function () {
// We have to check without calling initializePricing, because
// pool-tracker is not calling that function
const new__DexName__ = new __DexName__(network, dexKey, dexHelper);
if (new__DexName__.updatePoolState) {
await new__DexName__.updatePoolState();
}
const poolLiquidity = await new__DexName__.getTopPoolsForToken(
tokens[srcTokenSymbol].address,
10,
);
console.log(`${srcTokenSymbol} Top Pools:`, poolLiquidity);
if (!new__DexName__.hasConstantPriceLargeAmounts) {
checkPoolsLiquidity(
poolLiquidity,
Tokens[network][srcTokenSymbol].address,
dexKey,
);
}
});
});
});