-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.js
331 lines (301 loc) · 18.6 KB
/
swap.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
const { expect } = require('chai');
const { ethers } = require('hardhat');
describe('Uniswap Contract', async () => {
let tokenA;
let tokenB;
let weth;
let UniswapV2Pair;
let uniswapV2Pair;
let uniswapV2Factory;
let uniswapV2Router02;
let amountETHMin=ethers.parseEther("1");
let amountAMin=ethers.parseEther("1");
let amountBMin=ethers.parseEther("1");
let amountOut = ethers.parseEther("1");
let amountIn = ethers.parseEther("10");
let amountInMax = ethers.parseEther("100")
const ETH_AMOUNT = ethers.parseEther('100');
let TOKEN_A_AMOUNT = ethers.parseEther('100');
let TOKEN_B_AMOUNT = ethers.parseEther('200');
// let TOKEN_A_AMOUNTA = ethers.parseEther('200');
// let TOKEN_B_AMOUNTB = ethers.parseEther('100');
const deadline = Math.floor(Date.now() / 1000) + 3600;
async function _addLiquidity(){
await tokenA.connect(signer[0]).approve(uniswapV2Router02.target, TOKEN_A_AMOUNT);
await tokenB.connect(signer[0]).approve(uniswapV2Router02.target, TOKEN_B_AMOUNT);
console.log(`TOKEN_A_AMOUNT : ${TOKEN_A_AMOUNT} \n TOKEN_B_AMOUNT : ${TOKEN_B_AMOUNT}`);
// function addLiquidity(address tokenA,address tokenB, uint amountADesired,uint amountBDesired,uint amountAMin,uint amountBMin,address to,uint deadline)
await uniswapV2Router02.connect(signer[0]).addLiquidity(tokenA.target,tokenB.target,TOKEN_A_AMOUNT,TOKEN_B_AMOUNT,amountAMin,amountBMin,signer[0].address,deadline);
}
async function _addLiquidityETH(){
await tokenA.connect(signer[0]).approve(uniswapV2Router02.target, TOKEN_A_AMOUNT);
console.log(`TOKEN_A_AMOUNT : ${TOKEN_A_AMOUNT} \n ETH_AMOUNT : ${ETH_AMOUNT}`);
// function addLiquidityETH(address token,uint amountTokenDesired,uint amountTokenMin,uint amountETHMin,address to,uint deadline)
await uniswapV2Router02.connect(signer[0]).addLiquidityETH(tokenA.target,TOKEN_A_AMOUNT,amountETHMin,ETH_AMOUNT,signer[0].address,deadline,{ value: ETH_AMOUNT });
}
beforeEach(async () => {
signer = await ethers.getSigners();
const TokenA = await ethers.getContractFactory('TokenA');
tokenA = await TokenA.connect(signer[0]).deploy();
const TokenB = await ethers.getContractFactory('TokenB');
tokenB = await TokenB.connect(signer[0]).deploy();
const Weth = await ethers.getContractFactory('WETH9');
weth = await Weth.connect(signer[0]).deploy();
const UniswapV2Factory = await ethers.getContractFactory('UniswapV2Factory');
uniswapV2Factory = await UniswapV2Factory.connect(signer[0]).deploy(signer[0].address);
const UniswapV2Router02 = await ethers.getContractFactory('UniswapV2Router02');
uniswapV2Router02 = await UniswapV2Router02.connect(signer[0]).deploy(uniswapV2Factory.target,weth.target);
UniswapV2Pair = await ethers.getContractFactory('UniswapV2Pair');
uniswapV2Pair = await UniswapV2Pair.connect(signer[0]).deploy();
const GetInit = await ethers.getContractFactory('CalHash');
getInit = await GetInit.deploy();
initHash = await getInit.connect(signer[0]).getInitHash();
});
it(' *** Check The ethers Version & Contract address *** \n ', async () => {
const version = ethers.version;
console.log(`Ethers Version : v${version}
Token A Contract Address : ${await tokenA.target}
Token B Contract Address : ${await tokenB.target}
UniswapV2Factory Contract Address : ${await uniswapV2Factory.target}
UniswapV2Router02 Contract Address : ${await uniswapV2Router02.target}
WETH9 Contract Address : ${await weth.target}`);
});
it(' *** Token A : Check Name , Symbol and Totalsupply *** \n ', async () => {
console.log(`Name of the Token A : ${await tokenA.name()}`);
expect(await tokenA.name()).to.be.equal('TokenA');
console.log(`Symbol of the Token A : ${await tokenA.symbol()}`);
expect(await tokenA.symbol()).to.be.equal('TA');
console.log(`TotalSupply of the Token A : ${await tokenA.totalSupply()}`);
expect(await tokenA.totalSupply()).to.be.equal(ethers.parseEther('10000000'));
console.log(`Signer Balance of TokenA : ${await tokenA.balanceOf(signer[0].address)}`);
});
it(' *** Token B : Check Name , Symbol and Totalsupply *** \n ', async () => {
console.log(`Name of the Token B : ${await tokenB.name()}`);
expect(await tokenB.name()).to.be.equal('TokenB');
console.log(`Symbol of the Token B : ${await tokenB.symbol()}`);
expect(await tokenB.symbol()).to.be.equal('TB');
console.log(`TotalSupply of the Token B : ${await tokenB.totalSupply()}`);
expect(await tokenB.totalSupply()).to.be.equal(ethers.parseEther('10000000'));
console.log(`Signer Balance of TokenB : ${await tokenB.balanceOf(signer[0].address)}`);
});
it(' *** Check AddLiquidity *** ', async () => {
console.log(`Init Hash : ${initHash}`);
await _addLiquidity();
// function addLiquidity(address tokenA,address tokenB, uint amountADesired,uint amountBDesired,uint amountAMin,uint amountBMin,address to,uint deadline)
console.log(`Contract Address of Uniswap Pair: ${uniswapV2Pair.target}`);
let pair = await uniswapV2Factory.getPair(tokenA.target, tokenB.target);
console.log(`Pair Address : ${pair}`);
let uniswapV2PairAt = await uniswapV2Pair.connect(signer[0]).attach(pair);
console.log(`Balance Of Attached Uniswap Pair : ${await uniswapV2PairAt.balanceOf(signer[0].address)}`);
console.log(`reserve ${await uniswapV2PairAt.getReserves()}`);
// await tokenA.connect(signer[0]).approve(uniswapV2Router02.target, TOKEN_A_AMOUNTA);
// await tokenB.connect(signer[0]).approve(uniswapV2Router02.target, TOKEN_B_AMOUNTB);
// console.log(`TOKEN_A_AMOUNT : ${TOKEN_A_AMOUNTA} \n TOKEN_B_AMOUNT : ${TOKEN_B_AMOUNTB}`);
// await uniswapV2Router02.connect(signer[0]).addLiquidity(tokenA.target,tokenB.target,TOKEN_A_AMOUNTA,TOKEN_B_AMOUNTB,1,1,signer[0].address,deadline);
// console.log(`reserve ${await uniswapV2PairAt.getReserves()}`);
// console.log(`reserve ${await uniswapV2Pair.getReserves()}`)
});
it(' *** Check AddLiquidityETH *** ', async () => {
// console.log(`Init Hash : ${initHash}`);
await tokenA.connect(signer[0]).approve(uniswapV2Router02.target, TOKEN_A_AMOUNT);
await uniswapV2Router02.connect(signer[0]).addLiquidityETH(tokenA.target,TOKEN_A_AMOUNT,1,ETH_AMOUNT,signer[0].address,deadline,{ value: ETH_AMOUNT });
console.log(`Contract Address of Uniswap Pair Contract: ${uniswapV2Pair.target}`);
pair = await uniswapV2Factory.getPair(tokenA.target, weth.target);
console.log(`Pair Address Of Token/Weth via Factory: ${pair}`);
let uniswapV2PairAt = await uniswapV2Pair.connect(signer[0]).attach(pair);
let liquidity = await uniswapV2PairAt.balanceOf(signer[0].address);
console.log(`Liquidity After addLiquidityETH Function : ${liquidity}`);
console.log(`Reserve After addLiquidityETH: ${await uniswapV2PairAt.getReserves()}`);
});
it(' *** Check RemoveLiquidity *** ', async () => {
// console.log(`Init Hash : ${initHash}`);
await tokenA.connect(signer[0]).approve(uniswapV2Router02.target, TOKEN_A_AMOUNT);
await tokenB.connect(signer[0]).approve(uniswapV2Router02.target, TOKEN_B_AMOUNT);
await uniswapV2Router02.connect(signer[0]).addLiquidity(tokenA.target,tokenB.target,TOKEN_A_AMOUNT,TOKEN_B_AMOUNT,1,1,signer[0].address,deadline);
console.log(`Contract Address of Uniswap Pair Contract: ${uniswapV2Pair.target}`);
pair = await uniswapV2Factory.getPair(tokenA.target, tokenB.target);
console.log(`Pair Address Of TokenA/TokenB via Factory: ${pair}`);
let uniswapV2PairAt = await uniswapV2Pair.connect(signer[0]).attach(pair);
console.log(`Liquidity After Add Liquidity Function : ${await uniswapV2PairAt.balanceOf(signer[0].address)}`);
let liquidity = await uniswapV2PairAt.balanceOf(signer[0].address);
console.log(`Reserve After Add Liquidity: ${await uniswapV2PairAt.getReserves()}`);
await uniswapV2PairAt.connect(signer[0]).approve(uniswapV2Router02.target, liquidity);
await uniswapV2Router02.connect(signer[0]).removeLiquidity(tokenA.target,tokenB.target,liquidity,1,1,signer[0].address,deadline);
console.log(`Liquidity After Remove Liquidity Function : ${await uniswapV2PairAt.balanceOf(signer[0].address)}`);
console.log(`Reserve After Remove Liquidity: ${await uniswapV2PairAt.getReserves()}`);
});
it(' *** Check removeLiquidityETH *** ', async () => {
// console.log(`Init Hash : ${initHash}`);
await _addLiquidityETH();
console.log(`Contract Address of Uniswap Pair Contract: ${uniswapV2Pair.target}`);
pair = await uniswapV2Factory.getPair(tokenA.target, weth.target);
console.log(`Pair Address Of Token/Weth via Factory: ${pair}`);
let uniswapV2PairAt = await uniswapV2Pair.connect(signer[0]).attach(pair);
let liquidity = await uniswapV2PairAt.balanceOf(signer[0].address);
console.log(`Liquidity After addLiquidityETH Function : ${liquidity}`);
console.log(`Reserve After addLiquidityETH: ${await uniswapV2PairAt.getReserves()}`);
await uniswapV2PairAt.connect(signer[0]).approve(uniswapV2Router02.target, liquidity);
await uniswapV2Router02.connect(signer[0]).removeLiquidityETH(tokenA.target,liquidity,1,1,signer[1].address,deadline);
console.log(`Liquidity After removeLiquidityETH Function : ${await uniswapV2PairAt.balanceOf(signer[1].address)}`);
console.log(`Reserve After removeLiquidityETH: ${await uniswapV2PairAt.getReserves()}`);
});
it("swapExactTokensForTokens function", async function () {
await _addLiquidity();
console.log(`Contract Address of Uniswap Pair Contract: ${uniswapV2Pair.target}`);
pair = await uniswapV2Factory.getPair(tokenA.target, tokenB.target);
console.log(`Pair Address Of TokenA/TokenB via Factory: ${pair}`);
let uniswapV2PairAt =await uniswapV2Pair.connect(signer[0]).attach(pair);
// let liquidity = await uniswapV2PairAt.balanceOf(signer[0].address);
let liquidity =await uniswapV2PairAt.balanceOf(signer[0].address);
console.log(`Liquidity After addLiquidity Function : ${liquidity}`);
console.log(`Reserve After addLiquidity: ${(await uniswapV2PairAt.getReserves())}`);
await uniswapV2PairAt.connect(signer[0]).approve(uniswapV2Router02.target, liquidity);
await tokenA.connect(signer[0]).approve(uniswapV2Router02.target,TOKEN_A_AMOUNT);
let iniBalT1 = await tokenA.balanceOf(signer[0].address);
let iniBalT2 = await tokenB.balanceOf(signer[0].address);
// const amountIn = ethers.parseEther("100");
await uniswapV2Router02.connect(signer[0]).swapExactTokensForTokens(amountIn,1,[tokenA.target,tokenB.target],signer[0].address, deadline);
console.log(`Liquidity After swap Function : ${liquidity}`);
console.log(`Reserve After swap: ${(await uniswapV2PairAt.getReserves())}`);
let fnlBalT1 = await tokenA.balanceOf(signer[0].address);
let fnlBalT2 = await tokenB.balanceOf(signer[0].address);
// let fnlBalT3 = await tokenA.balanceOf(signer[4].address);
expect(iniBalT1).to.be.greaterThan(fnlBalT1);
expect(fnlBalT2).to.be.greaterThan(iniBalT2);
// expect(fnlBalT3).to.equal(0.3);
console.log(`
Initial Balance of Token A : ${iniBalT1}
Initial Balance of Token B : ${iniBalT2}
Final Balance of Token A : ${fnlBalT1}
Final Balance of Token B : ${fnlBalT2}
`);
// await _addLiquidity();
// console.log(`Contract Address of Uniswap Pair Contract: ${uniswapV2Pair.target}`);
// pair = await uniswapV2Factory.getPair(tokenA.target, tokenB.target);
// console.log(`Pair Address Of TokenA/TokenB via Factory: ${pair}`);
// uniswapV2PairAt =await uniswapV2Pair.connect(signer[0]).attach(pair);
// // let liquidity = await uniswapV2PairAt.balanceOf(signer[0].address);
// liquidity =await uniswapV2PairAt.balanceOf(signer[0].address);
// console.log(`Liquidity After addLiquidity Function : ${liquidity}`);
// console.log(`Reserve After addLiquidity: ${(await uniswapV2PairAt.getReserves())}`);
// iniBalT1 = await tokenA.balanceOf(signer[0].address);
// iniBalT2 = await tokenB.balanceOf(signer[0].address);
// console.log(`
// Initial Balance of Token A : ${iniBalT1}
// Initial Balance of Token B : ${iniBalT2}
// `);
});
it("swapTokensForExactTokens", async ()=>{
await _addLiquidity();
// console.log(`Contract Address of Uniswap Pair Contract: ${uniswapV2Pair.target}`);
// pair = await uniswapV2Factory.getPair(tokenA.target, tokenB.target);
// console.log(`Pair Address Of TokenA/TokenB via Factory: ${pair}`);
// let uniswapV2PairAt =await uniswapV2Pair.connect(signer[0]).attach(pair);
// // let liquidity = await uniswapV2PairAt.balanceOf(signer[0].address);
// let liquidity =await uniswapV2PairAt.balanceOf(signer[0].address);
// console.log(`Liquidity After addLiquidity Function : ${liquidity}`);
// console.log(`Reserve After addLiquidity: ${(await uniswapV2PairAt.getReserves())}`);
// await uniswapV2PairAt.connect(signer[0]).approve(uniswapV2Router02.target, liquidity);
await tokenA.connect(signer[0]).approve(uniswapV2Router02.target,TOKEN_A_AMOUNT);
let iniBalT1 = await tokenA.balanceOf(signer[0].address);
let iniBalT2 = await tokenB.balanceOf(signer[0].address);
amountOut=ethers.parseEther("10");
await uniswapV2Router02.connect(signer[0]).swapTokensForExactTokens(amountOut,TOKEN_A_AMOUNT,[tokenA.target,tokenB.target],signer[0].address, deadline);
// console.log(`Liquidity After swap Function : ${liquidity}`);
// console.log(`Reserve After swap: ${(await uniswapV2PairAt.getReserves())}`);
let fnlBalT1 = await tokenA.balanceOf(signer[0].address);
let fnlBalT2 = await tokenB.balanceOf(signer[0].address);
console.log(`
Initial Balance of Token A : ${iniBalT1}
Initial Balance of Token B : ${iniBalT2}
Final Balance of Token A : ${fnlBalT1}
Final Balance of Token B : ${fnlBalT2}
`);
})
it("swapExactETHForTokens",async ()=>{
await _addLiquidityETH();
pair = await uniswapV2Factory.getPair(tokenA.target, weth.target);
console.log(`Pair Address Of TokenA/weth via Factory: ${pair}`);
let uniswapV2PairAt =await uniswapV2Pair.connect(signer[0]).attach(pair);
let iniBalT1 = ((await tokenA.balanceOf(signer[0].address)));
console.log(`Reserve Before swap: ${(await uniswapV2PairAt.getReserves())}`);
await uniswapV2Router02.connect(signer[0]).swapExactETHForTokens(1,[weth.target,tokenA.target],signer[0].address, deadline,{value:ETH_AMOUNT});
let fnlBalT1 = ((await tokenA.balanceOf(signer[0].address)));
console.log(`
Initial Balance of Token A : ${iniBalT1}
Final Balance of Token A : ${fnlBalT1}
`);
console.log(`Reserve After swap: ${(await uniswapV2PairAt.getReserves())}`);
})
// it("swapTokensForExactETH",async ()=>{
// await _addLiquidityETH();
// pair = await uniswapV2Factory.getPair(tokenA.target, weth.target);
// console.log(`Pair Address Of TokenA/weth via Factory: ${pair}`);
// let uniswapV2PairAt =await uniswapV2Pair.connect(signer[0]).attach(pair);
// let iniBalT1 = ((await tokenA.balanceOf(signer[0].address)));
// console.log(`Reserve Before swap: ${(await uniswapV2PairAt.getReserves())}`);
// amountOut=ethers.parseEther("10");
// await tokenA.connect(signer[0]).approve(uniswapV2Router02.target,amountInMax);
// await uniswapV2Router02.connect(signer[0]).swapTokensForExactETH(
// amountOut, amountInMax, [tokenA.target,weth.target], signer[0].address, deadline
// )
// let fnlBalT1 = ((await tokenA.balanceOf(signer[0].address)));
// console.log(`
// Initial Balance of Token A : ${iniBalT1}
// Final Balance of Token A : ${fnlBalT1}
// `);
// console.log(`Reserve After swap: ${(await uniswapV2PairAt.getReserves())}`);
// })
it("swapTokensForExactETH function", async function () {
await _addLiquidityETH();
pair = await uniswapV2Factory.getPair(tokenA.target, weth.target);
console.log(`Pair Address Of TokenA/weth via Factory: ${pair}`);
let uniswapV2PairAt =await uniswapV2Pair.connect(signer[0]).attach(pair);
console.log(`Reserve Before swap: ${(await uniswapV2PairAt.getReserves())}`);
let iniBalToken =await tokenA.balanceOf(signer[0].address);
// amountOut=ethers.parseEther("10");
await tokenA.connect(signer[0]).approve(uniswapV2Router02.target,amountInMax);
await uniswapV2Router02.connect(signer[0]).swapTokensForExactETH(amountOut,amountInMax,[tokenA.target,weth.target],signer[0].address,deadline);
let fnlBalToken =await tokenA.balanceOf(signer[0].address);
console.log(`
Initial Balance of Token A : ${iniBalToken}
Final Balance of Token A : ${fnlBalToken}
`);
console.log(`Reserve After swap: ${(await uniswapV2PairAt.getReserves())}`);
});
it("swapExactTokensForETH function", async function () {
await _addLiquidityETH();
pair = await uniswapV2Factory.getPair(tokenA.target, weth.target);
console.log(`Pair Address Of TokenA/weth via Factory: ${pair}`);
let uniswapV2PairAt =await uniswapV2Pair.connect(signer[0]).attach(pair);
console.log(`Reserve Before swap: ${(await uniswapV2PairAt.getReserves())}`);
let iniBalToken = ((await tokenA.balanceOf(signer[0].address)));
await tokenA.connect(signer[0]).approve(uniswapV2Router02.target,amountInMax);
// (uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
await uniswapV2Router02.connect(signer[0]).swapExactTokensForETH(amountInMax,amountOut,[tokenA.target,weth.target],signer[0].address,deadline);
let fnlBalToken = ((await tokenA.balanceOf(signer[0].address)));
console.log(`
Initial Balance of Token A : ${iniBalToken}
Final Balance of Token A : ${fnlBalToken}
`);
console.log(`Reserve After swap: ${(await uniswapV2PairAt.getReserves())}`);
});
it("swapETHForExactTokens function", async function () {
await _addLiquidityETH();
pair = await uniswapV2Factory.getPair(tokenA.target, weth.target);
console.log(`Pair Address Of TokenA/weth via Factory: ${pair}`);
let uniswapV2PairAt =await uniswapV2Pair.connect(signer[0]).attach(pair);
console.log(`Reserve Before swap: ${(await uniswapV2PairAt.getReserves())}`);
let iniBalToken = await tokenA.balanceOf(signer[0].address);
await tokenA.connect(signer[0]).approve(uniswapV2Router02.target,TOKEN_A_AMOUNT);
// (uint amountOut, address[] calldata path, address to, uint deadline)
amountOut=ethers.parseEther("1");
await uniswapV2Router02.connect(signer[0]).swapETHForExactTokens(amountOut,[weth.target,tokenA.target],signer[0].address,deadline,{value:amountIn});
let fnlBalToken = await tokenA.balanceOf(signer[0].address);
console.log(`
Initial Balance of Token A : ${iniBalToken}
Final Balance of Token A : ${fnlBalToken}
`);
console.log(`Reserve After swap: ${(await uniswapV2PairAt.getReserves())}`);
});
});