-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_add_liquidity.js
559 lines (496 loc) · 14.8 KB
/
test_add_liquidity.js
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
const Factory = require("./node_modules/@uniswap/v2-core/build/UniswapV2Factory.json");
const Router = require("./node_modules/@uniswap/v2-periphery/build/UniswapV2Router02.json");
const ERC20 = require("./node_modules/@openzeppelin/contracts/build/contracts/ERC20PresetFixedSupply.json");
const Pair = require("./node_modules/@uniswap/v2-core/build/UniswapV2Pair.json");
const WETH = require("./node_modules/canonical-weth/build/contracts/WETH9.json");
//-------------------------------------------------------------------
function getCliArg(argName) {
Arg = [process.argv[Number(process.argv.indexOf(`--${argName}`) + 1)]];
return String(Arg);
} // expects arg to exist
console.log("----------------------");
const node_url = getCliArg("node_url");
const Web3 = require("web3");
const web3 = new Web3(node_url);
// Variables
const prvkey = getCliArg("prvkey");
const router_address = getCliArg("router_address");
const tokenA_address = getCliArg("tokenA_address");
const tokenB_address = getCliArg("tokenB_address");
const weth_address = getCliArg("weth_address");
const factory_address = getCliArg("factory_address");
const fund_pair = getCliArg("fund_pair");
const defund_pair = getCliArg("defund_pair");
// liquidity
const amountADesired = web3.utils.toWei(getCliArg("amountADesired"), "ether");
const amountBDesired = web3.utils.toWei(getCliArg("amountBDesired"), "ether");
const amountAUTDesired = web3.utils.toWei(
getCliArg("amountAUTDesired"),
"ether"
);
const amountAMin = web3.utils.toWei(getCliArg("amountAMin"), "ether");
const amountBMin = web3.utils.toWei(getCliArg("amountBMin"), "ether");
const amountAUTMin = web3.utils.toWei(getCliArg("amountAUTMin"), "ether");
const liquidity = web3.utils.toWei(getCliArg("liquidity"), "ether");
const factory = new web3.eth.Contract(Factory.abi, factory_address);
async function approve(tokenContract, spender, amount, sender) {
try {
await tokenContract.methods
.approve(spender, amount)
.send({ from: sender, gas: 10000000 })
.on("transactionHash", function (hash) {
console.log("transaction hash", hash);
// })
// .on('receipt', function (receipt) {
// console.log('receipt', receipt);
});
} catch (err) {
console.log("the approve transaction reverted! Lets see why...");
await tokenContract.methods
.approve(spender, amount)
.call({ from: sender, gas: 10000000 });
}
}
async function addLiquidity(
router,
tokenA_address,
tokenB_address,
amountADesired,
amountBDesired,
amountAMin,
amountBMin,
myAddress,
deadline
) {
const transfer = await router.methods
.addLiquidity(
tokenA_address,
tokenB_address,
amountADesired,
amountBDesired,
amountAMin,
amountBMin,
myAddress,
deadline
)
.call({
from: myAddress,
gas: 10000000,
});
console.log("----------------------");
console.log("tokenA in: ", web3.utils.fromWei(transfer[0]));
console.log("tokenB in: ", web3.utils.fromWei(transfer[1]));
console.log("liquidity tokens out: ", web3.utils.fromWei(transfer[2]));
try {
await router.methods
.addLiquidity(
tokenA_address,
tokenB_address,
amountADesired,
amountBDesired,
amountAMin,
amountBMin,
myAddress,
deadline
)
.send({
from: myAddress,
gas: 10000000,
})
.on("transactionHash", function (hash) {
console.log("transaction hash", hash);
});
} catch (err) {
console.log("the addLiquidity transaction reverted! Lets see why...");
await router.methods
.addLiquidity(
tokenA_address,
tokenB_address,
amountADesired,
amountBDesired,
amountAMin,
amountBMin,
myAddress,
deadline
)
.call({
from: myAddress,
gas: 10000000,
});
}
const pairAddress = await factory.methods
.getPair(tokenA_address, tokenB_address)
.call({ from: myAddress, gas: 10000000 });
console.log("----------------------");
console.log("tokenA Address", tokenA_address);
console.log("tokenB Address", tokenB_address);
console.log("pairAddress", pairAddress);
const pair = new web3.eth.Contract(Pair.abi, pairAddress);
const reserves = await pair.methods
.getReserves()
.call({ from: myAddress, gas: 10000000 });
console.log("reserves for token A", web3.utils.fromWei(reserves._reserve0));
console.log("reserves for token B", web3.utils.fromWei(reserves._reserve1));
console.log("----------------------");
}
async function addLiquidityETH(
router,
token_address,
amountTDesired,
amountAUTDesired,
amountTMin,
amountAUTMin,
myAddress,
deadline
) {
const transfer = await router.methods
.addLiquidityETH(
token_address,
amountTDesired,
amountTMin,
amountAUTMin,
myAddress,
deadline
)
.call({
from: myAddress,
gas: 10000000,
value: amountAUTDesired,
});
console.log("----------------------");
console.log("token in: ", web3.utils.fromWei(transfer[0]));
console.log("AUT in: ", web3.utils.fromWei(transfer[1]));
console.log("liquidity tokens out: ", web3.utils.fromWei(transfer[2]));
try {
await router.methods
.addLiquidityETH(
token_address,
amountTDesired,
amountTMin,
amountAUTMin,
myAddress,
deadline
)
.send({
from: myAddress,
gas: 10000000,
value: amountAUTDesired,
})
.on("transactionHash", function (hash) {
console.log("transaction hash", hash);
});
} catch (err) {
console.log("the addLiquidityETH transaction reverted! Lets see why...");
await router.methods
.addLiquidityETH(
token_address,
amountTDesired,
amountTMin,
amountAUTMin,
myAddress,
deadline
)
.call({
from: myAddress,
gas: 10000000,
value: amountAUTDesired,
});
}
const pairAddress = await factory.methods
.getPair(token_address, weth_address)
.call({ from: myAddress, gas: 10000000 });
console.log("----------------------");
console.log("token Address", token_address);
console.log("weth_address", weth_address);
console.log("pairAddress", pairAddress);
const pair = new web3.eth.Contract(Pair.abi, pairAddress);
const reserves = await pair.methods
.getReserves()
.call({ from: myAddress, gas: 10000000 });
console.log("reserves for token", web3.utils.fromWei(reserves._reserve0));
console.log("reserves for AUT", web3.utils.fromWei(reserves._reserve1));
console.log("----------------------");
}
async function removeLiquidity(
router,
tokenA_address,
tokenB_address,
liquidity,
amountAMin,
amountBMin,
myAddress,
deadline
) {
const transfer = await router.methods
.removeLiquidity(
tokenA_address,
tokenB_address,
liquidity,
amountAMin,
amountBMin,
myAddress,
deadline
)
.call({
from: myAddress,
gas: 10000000,
});
console.log("----------------------");
console.log("liquidity tokens in: ", web3.utils.fromWei(liquidity));
console.log("tokenA out: ", web3.utils.fromWei(transfer[0]));
console.log("tokenB out: ", web3.utils.fromWei(transfer[1]));
try {
await router.methods
.removeLiquidity(
tokenA_address,
tokenB_address,
liquidity,
amountAMin,
amountBMin,
myAddress,
deadline
)
.send({
from: myAddress,
gas: 10000000,
})
.on("transactionHash", function (hash) {
console.log("transaction hash", hash);
});
} catch (err) {
console.log("the removeLiquidity transaction reverted! Lets see why...");
await router.methods
.removeLiquidity(
tokenA_address,
tokenB_address,
liquidity,
amountAMin,
amountBMin,
myAddress,
deadline
)
.call({
from: myAddress,
gas: 10000000,
});
}
const pairAddress = await factory.methods
.getPair(tokenA_address, tokenB_address)
.call({ from: myAddress, gas: 10000000 });
console.log("----------------------");
console.log("tokenA Address", tokenA_address);
console.log("tokenB Address", tokenB_address);
console.log("pairAddress", pairAddress);
const pair = new web3.eth.Contract(Pair.abi, pairAddress);
const reserves = await pair.methods
.getReserves()
.call({ from: myAddress, gas: 10000000 });
console.log("reserves for token A", web3.utils.fromWei(reserves._reserve0));
console.log("reserves for token B", web3.utils.fromWei(reserves._reserve1));
console.log("----------------------");
}
async function removeLiquidityETH(
router,
token_address,
liquidity,
amountTMin,
amountAUTMin,
myAddress,
deadline
) {
const transfer = await router.methods
.removeLiquidityETH(
token_address,
liquidity,
amountTMin,
amountAUTMin,
myAddress,
deadline
)
.call({
from: myAddress,
gas: 10000000,
});
console.log("----------------------");
console.log("liquidity tokens in: ", web3.utils.fromWei(liquidity));
console.log("token out: ", web3.utils.fromWei(transfer[0]));
console.log("AUT out: ", web3.utils.fromWei(transfer[1]));
try {
await router.methods
.removeLiquidityETH(
token_address,
liquidity,
amountTMin,
amountAUTMin,
myAddress,
deadline
)
.send({
from: myAddress,
gas: 10000000,
})
.on("transactionHash", function (hash) {
console.log("transaction hash", hash);
});
} catch (err) {
console.log("the removeLiquidityETH transaction reverted! Lets see why...");
await router.methods
.removeLiquidityETH(
token_address,
liquidity,
amountTMin,
amountAUTMin,
myAddress,
deadline
)
.call({
from: myAddress,
gas: 10000000,
});
}
const pairAddress = await factory.methods
.getPair(token_address, weth_address)
.call({ from: myAddress, gas: 10000000 });
console.log("----------------------");
console.log("token Address", token_address);
console.log("weth_address", weth_address);
console.log("pairAddress", pairAddress);
const pair = new web3.eth.Contract(Pair.abi, pairAddress);
const reserves = await pair.methods
.getReserves()
.call({ from: myAddress, gas: 10000000 });
console.log("reserves for token", web3.utils.fromWei(reserves._reserve0));
console.log("reserves for AUT", web3.utils.fromWei(reserves._reserve1));
console.log("----------------------");
}
// the most amazing function on earth
async function main() {
const id = await web3.eth.net.getId();
const account = web3.eth.accounts.wallet.add(prvkey);
const myAddress = web3.utils.toChecksumAddress(account.address);
const router = new web3.eth.Contract(Router.abi, router_address);
const tokenA = new web3.eth.Contract(ERC20.abi, tokenA_address);
const tokenB = new web3.eth.Contract(ERC20.abi, tokenB_address);
const weth = new web3.eth.Contract(WETH.abi, weth_address);
// Pairs
const pairAB_address = await factory.methods
.getPair(tokenA_address, tokenB_address)
.call({ from: myAddress, gas: 10000000 });
const pairAB = new web3.eth.Contract(Pair.abi, pairAB_address);
const pairAAUT_address = await factory.methods
.getPair(tokenA_address, weth_address)
.call({ from: myAddress, gas: 10000000 });
const pairAAUT = new web3.eth.Contract(Pair.abi, pairAAUT_address);
const pairBAUT_address = await factory.methods
.getPair(tokenB_address, weth_address)
.call({ from: myAddress, gas: 10000000 });
const pairBAUT = new web3.eth.Contract(Pair.abi, pairBAUT_address);
console.log("tokenA", tokenA.options.address);
console.log("tokenB", tokenB.options.address);
// deadline
var BN = web3.utils.BN;
const time = Math.floor(Date.now() / 1000) + 200000;
const deadline = new BN(time);
// before calling addLiquidity we need to approve the router
// we need to approve atleast amountADesired and amountBDesired
const amountA = amountADesired;
const amountB = amountBDesired;
// approve the tokens
console.log("----------------------");
console.log("Start Approvals");
console.log("----------------------");
if (fund_pair == "0" || fund_pair == "1" || fund_pair == "2") {
// Only do approvals that are needed to save time
await approve(tokenA, router_address, amountA, myAddress);
await approve(tokenB, router_address, amountA, myAddress);
await approve(weth, router_address, amountA, myAddress);
}
if (defund_pair == "0" || defund_pair == "1" || defund_pair == "2") {
// Only do approvals that are needed to save time
await approve(pairAB, router_address, liquidity, myAddress);
await approve(pairAAUT, router_address, liquidity, myAddress);
await approve(pairBAUT, router_address, liquidity, myAddress);
}
console.log("----------------------");
// Add liquidity to token-token or AUT-token pair
if (fund_pair == "0") {
console.log("Add liquidity to tokenA-tokenB pair");
await addLiquidity(
router,
tokenA_address,
tokenB_address,
amountADesired,
amountBDesired,
amountAMin,
amountBMin,
myAddress,
deadline
);
} else if (fund_pair == "1") {
console.log("Add liquidity to tokenA-AUT pair");
await addLiquidityETH(
router,
tokenA_address,
amountADesired,
amountAUTDesired,
amountAMin,
amountAUTMin,
myAddress,
deadline
);
} else if (fund_pair == "2") {
console.log("Add liquidity to tokenB-AUT pair");
await addLiquidityETH(
router,
tokenB_address,
amountBDesired,
amountAUTDesired,
amountBMin,
amountAUTMin,
myAddress,
deadline
);
} else {
console.log("You have not chosen a pair to fund. Select 0, 1 or 2");
console.log("----------------------");
}
// remove liquidity from a token-token or AUT-token pair
if (defund_pair == "0") {
console.log("Remove liquidity from tokenA-tokenB pair");
await removeLiquidity(
router,
tokenA_address,
tokenB_address,
liquidity,
amountAMin,
amountBMin,
myAddress,
deadline
);
} else if (defund_pair == "1") {
console.log("Remove liquidity from tokenA-AUT pair");
removeLiquidityETH(
router,
tokenA_address,
liquidity,
amountAMin,
amountAUTMin,
myAddress,
deadline
);
} else if (defund_pair == "2") {
console.log("Remove liquidity from tokenB-AUT pair");
removeLiquidityETH(
router,
tokenB_address,
liquidity,
amountBMin,
amountAUTMin,
myAddress,
deadline
);
} else {
console.log("You have not chosen a pair to defund. Select 0, 1 or 2");
}
}
main();