-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswaps.sol
724 lines (591 loc) · 26.5 KB
/
swaps.sol
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
// SPDX-License-Identifier: MIT
// The source code of this contract uses the following contracts
// UniswapV2SwapExamples: https://solidity-by-example.org/defi/uniswap-v2/
// For the Router: https://github.com/Uniswap/v2-periphery/blob/master/contracts/UniswapV2Router02.sol
// For the Pairs: https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol
pragma solidity ^0.8.24;
interface IERC20 {
function balanceOf(address owner) external returns (uint);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
}
contract UniswapV2Swap {
IERC20 public weth;
IERC20 public dai;
IERC20 public usdc;
UniswapV2Router02 public router;
constructor(address _weth, address _dai, address _usdc){
weth = IERC20(_weth);
dai = IERC20(_dai);
usdc = IERC20(_usdc);
router = new UniswapV2Router02();
router.set_local_pair(_weth, _dai);
router.set_local_pair(_weth, _usdc);
router.set_local_pair(_usdc, _dai);
}
function swapSingleHopExactAmountIn(uint256 amountIn, uint256 amountOutMin)
external
returns (uint256 amountOut)
{
weth.transferFrom(msg.sender, address(this), amountIn);
weth.approve(address(router), amountIn);
address[] memory path = new address[](2);
path[0] = address(weth);
path[1] = address(dai);
uint256[] memory amounts = router.swapExactTokensForTokens(amountIn, amountOutMin, path, msg.sender);
return amounts[1];
}
function swapMultiHopExactAmountIn(uint256 amountIn, uint256 amountOutMin)
external
returns (uint256 amountOut)
{
dai.transferFrom(msg.sender, address(this), amountIn);
dai.approve(address(router), amountIn);
address[] memory path = new address[](3);
path[0] = address(dai);
path[1] = address(weth);
path[2] = address(usdc);
uint256[] memory amounts = router.swapExactTokensForTokens(amountIn, amountOutMin, path, msg.sender);
return amounts[2];
}
function swapSingleHopExactAmountOut(
uint256 amountOutDesired,
uint256 amountInMax
) external returns (uint256 amountOut) {
weth.transferFrom(msg.sender, address(this), amountInMax);
weth.approve(address(router), amountInMax);
address[] memory path = new address[](2);
path[0] = address(weth);
path[1] = address(dai);
uint256[] memory amounts = router.swapTokensForExactTokens(amountOutDesired, amountInMax, path, msg.sender);
if (amounts[0] < amountInMax) {
weth.transfer(msg.sender, amountInMax - amounts[0]);
}
return amounts[1];
}
function swapMultiHopExactAmountOut(
uint256 amountOutDesired,
uint256 amountInMax
) external returns (uint256 amountOut) {
dai.transferFrom(msg.sender, address(this), amountInMax);
dai.approve(address(router), amountInMax);
address[] memory path = new address[](3);
path[0] = address(dai);
path[1] = address(weth);
path[2] = address(usdc);
uint256[] memory amounts = router.swapTokensForExactTokens(amountOutDesired, amountInMax, path, msg.sender);
if (amounts[0] < amountInMax) {
dai.transfer(msg.sender, amountInMax - amounts[0]);
}
return amounts[2];
}
}
contract UniswapV2Router02 {
mapping (address => mapping (address => address)) public local_pairs;
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to
) external returns (uint[] memory amounts) {
amounts = uniswapV2Library_getAmountsOut(amountIn, path);
require(amounts[amounts.length - 1] >= amountOutMin, "UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT");
IERC20(path[0]).transferFrom(msg.sender, uniswapV2Library_pairFor(path[0], path[1]), amounts[0]);
_swap(amounts, path, to);
}
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to
) external returns (uint[] memory amounts) {
amounts = uniswapV2Library_getAmountsIn(amountOut, path);
require(amounts[0] <= amountInMax, "UniswapV2Router: EXCESSIVE_INPUT_AMOUNT");
IERC20(path[0]).transferFrom(msg.sender, uniswapV2Library_pairFor(path[0], path[1]), amounts[0]);
_swap(amounts, path, to);
}
function addLiquidity(
address tokenA,
address tokenB,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin,
address to
) external returns (uint[] memory amounts_liq) {
amounts_liq = new uint[](3);
uint[] memory amounts = _addLiquidity(
tokenA,
tokenB,
amountADesired,
amountBDesired,
amountAMin,
amountBMin
);
amounts_liq[0] = amounts[0];
amounts_liq[1] = amounts[1];
address pair = uniswapV2Library_pairFor(tokenA, tokenB);
IERC20(tokenA).transferFrom(msg.sender, pair, amounts[0]);
IERC20(tokenB).transferFrom(msg.sender, pair, amounts[1]);
amounts_liq[2] = UniswapV2Pair(pair).mint(to);
}
function _addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin
) internal returns (uint[] memory amounts) {
amounts = new uint[](2);
require(get_local_pair(tokenA, tokenB) != address(0));
uint[] memory reserves = uniswapV2Library_getReserves(tokenA, tokenB);
if (reserves[0] == 0 && reserves[1] == 0) {
amounts[0] = amountADesired;
amounts[1] = amountBDesired;
} else {
uint amountBOptimal = uniswapV2Library_quote(
amountADesired,
reserves[0],
reserves[1]
);
if (amountBOptimal <= amountBDesired) {
require(
amountBOptimal >= amountBMin,
"UniswapV2Router: INSUFFICIENT_B_AMOUNT"
);
amounts[0] = amountADesired;
amounts[1] = amountBOptimal;
} else {
uint amountAOptimal = uniswapV2Library_quote(
amountBDesired,
reserves[1],
reserves[0]
);
assert(amountAOptimal <= amountADesired);
require(
amountAOptimal >= amountAMin,
"UniswapV2Router: INSUFFICIENT_A_AMOUNT"
);
amounts[0] = amountAOptimal;
amounts[1] = amountBDesired;
}
}
}
function set_local_pair(address tokenA, address tokenB) public{
address[] memory tokens = uniswapV2Library_sortTokens(tokenA, tokenB);
local_pairs[tokens[0]][tokens[1]] = address(new UniswapV2Pair(address(tokens[0]), address(tokens[1])));
}
function get_local_pair(address tokenA, address tokenB) public returns(address pair){
address[] memory tokens = uniswapV2Library_sortTokens(tokenA, tokenB);
pair = local_pairs[tokens[0]][tokens[1]];
}
function sync_local_pair(address tokenA, address tokenB) public{
address[] memory tokens = uniswapV2Library_sortTokens(tokenA, tokenB);
UniswapV2Pair(local_pairs[tokens[0]][tokens[1]]).sync();
}
function _swap(uint[] memory amounts, address[] memory path, address _to) private {
for (uint i; i < path.length - 1; i++) {
address input = path[i];
address output = path[i + 1];
address[] memory tokens = uniswapV2Library_sortTokens(input, output);
uint amountOut = amounts[i + 1];
uint amount0Out = input == tokens[0] ? uint(0) : amountOut;
uint amount1Out = input == tokens[0] ? amountOut : uint(0);
address to = i < path.length - 2 ? uniswapV2Library_pairFor(output, path[i + 2]) : _to;
UniswapV2Pair(uniswapV2Library_pairFor(input, output)).swap(
amount0Out, amount1Out, to);
}
}
function uniswapV2Library_pairFor(address tokenA, address tokenB) private returns (address pair) {
address[] memory tokens = uniswapV2Library_sortTokens(tokenA, tokenB);
pair = local_pairs[tokens[0]][tokens[1]];
}
function uniswapV2Library_sortTokens(address tokenA, address tokenB) private returns (address[] memory tokens) {
tokens = new address[](2);
require(tokenA != tokenB, "UniswapV2Library: IDENTICAL_ADDRESSES");
tokens[0] = tokenA < tokenB ? tokenA : tokenB;
tokens[1] = tokenA < tokenB ? tokenB : tokenA;
require(tokens[0] != address(0), "UniswapV2Library: ZERO_ADDRESS");
}
function uniswapV2Library_getAmountsOut(uint amountIn, address[] memory path) private returns (uint[] memory amounts) {
require(path.length >= 2, "UniswapV2Library: INVALID_PATH");
amounts = new uint[](path.length);
amounts[0] = amountIn;
for (uint i; i < path.length - 1; i++) {
uint[] memory reserves = uniswapV2Library_getReserves(path[i], path[i + 1]);
amounts[i + 1] = uniswapV2Library_getAmountOut(amounts[i], reserves[0], reserves[1]);
}
}
function uniswapV2Library_getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) private returns (uint amountOut) {
require(amountIn > 0, "UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT");
require(reserveIn > 0 && reserveOut > 0, "UniswapV2Library: INSUFFICIENT_LIQUIDITY");
uint amountInWithFee = amountIn*997;
uint numerator = amountInWithFee*reserveOut;
uint denominator = reserveIn*1000 + amountInWithFee;
amountOut = numerator / denominator;
}
function uniswapV2Library_getReserves(address tokenA, address tokenB) private returns (uint[] memory reserves) {
reserves = new uint[](2);
address[] memory tokens = uniswapV2Library_sortTokens(tokenA, tokenB);
uint112[] memory pair_reserves = UniswapV2Pair(uniswapV2Library_pairFor(tokenA, tokenB)).getReserves();
reserves[0] = tokenA == tokens[0] ? pair_reserves[0] : pair_reserves[1];
reserves[1] = tokenA == tokens[0] ? pair_reserves[1] : pair_reserves[0];
}
function uniswapV2Library_getAmountsIn(uint amountOut, address[] memory path) private returns (uint[] memory amounts) {
require(path.length >= 2, "UniswapV2Library: INVALID_PATH");
amounts = new uint[](path.length);
amounts[amounts.length - 1] = amountOut;
for (uint i = path.length - 1; i > 0; i--) {
uint[] memory reserves = uniswapV2Library_getReserves(path[i - 1], path[i]);
amounts[i - 1] = uniswapV2Library_getAmountIn(amounts[i], reserves[0], reserves[1]);
}
}
function uniswapV2Library_getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) private returns (uint amountIn) {
require(amountOut > 0, "UniswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT");
require(reserveIn > 0 && reserveOut > 0, "UniswapV2Library: INSUFFICIENT_LIQUIDITY");
uint numerator = reserveIn*amountOut*1000;
uint denominator = (reserveOut-amountOut)*997;
amountIn = denominator != 0 ? (numerator / denominator) + 1 : 1;
}
function uniswapV2Library_quote(
uint amountA,
uint reserveA,
uint reserveB
) internal returns (uint amountB) {
require(amountA > 0, "UniswapV2Library: INSUFFICIENT_AMOUNT");
require(
reserveA > 0 && reserveB > 0,
"UniswapV2Library: INSUFFICIENT_LIQUIDITY"
);
amountB = (amountA * reserveB) / reserveA;
}
}
contract UniswapV2Pair{
uint256 private UINT112_MAX = 0xffffffffffffffffffffffffffff;
address public token0;
address public token1;
uint112 private reserve0;
uint112 private reserve1;
uint32 private blockTimestampLast;
uint public MINIMUM_LIQUIDITY = 10**3;
uint public price0CumulativeLast;
uint public price1CumulativeLast;
uint public totalSupply;
uint public kLast;
mapping(address => uint) public balanceOf;
event Sync(uint112 reserve0, uint112 reserve1);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Mint(address indexed sender, uint amount0, uint amount1);
constructor(address _token0, address _token1) {
token0 = _token0;
token1 = _token1;
}
function swap(uint amount0Out, uint amount1Out, address to) external {
require(amount0Out > 0 || amount1Out > 0, "UniswapV2: INSUFFICIENT_OUTPUT_AMOUNT");
uint112[] memory reserves = getReserves(); // gas savings
require(amount0Out < reserves[0] && amount1Out < reserves[1], "UniswapV2: INSUFFICIENT_LIQUIDITY");
uint balance0;
uint balance1;
{ // scope for _token{0,1}, avoids stack too deep errors
address _token0 = token0;
address _token1 = token1;
require(to != _token0 && to != _token1, "UniswapV2: INVALID_TO");
if (amount0Out > 0) IERC20(_token0).transfer(to, amount0Out); // optimistically transfer tokens
if (amount1Out > 0) IERC20(_token1).transfer(to, amount1Out); // optimistically transfer tokens
balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));
}
uint amount0In = balance0 > reserves[0] - amount0Out ? balance0 - (reserves[0] - amount0Out) : 0;
uint amount1In = balance1 > reserves[1] - amount1Out ? balance1 - (reserves[1] - amount1Out) : 0;
require(amount0In > 0 || amount1In > 0, "UniswapV2: INSUFFICIENT_INPUT_AMOUNT");
{ // scope for reserve{0,1}Adjusted, avoids stack too deep errors
uint balance0Adjusted = (balance0*1000)-(amount0In*3);
uint balance1Adjusted = (balance1*1000)-(amount1In*3);
require(balance0Adjusted*balance1Adjusted >= uint(reserves[0])*reserves[1]*(1000**2), "UniswapV2: K");
}
_update(balance0, balance1, reserves[0], reserves[1]);
emit Swap(msg.sender, amount0In, amount1In, amount0Out, amount1Out, to);
}
function mint(address to) external returns (uint liquidity) {
uint112[] memory pair_reserves = getReserves();
uint balance0 = IERC20(token0).balanceOf(address(this));
uint balance1 = IERC20(token1).balanceOf(address(this));
uint amount0 = balance0 - pair_reserves[0];
uint amount1 = balance1 - pair_reserves[1];
//bool feeOn = _mintFee(pair_reserves[0], pair_reserves[1]);
uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
if (_totalSupply == 0) {
liquidity = math_sqrt(amount0 * amount1) - MINIMUM_LIQUIDITY;
totalSupply = totalSupply + MINIMUM_LIQUIDITY;
balanceOf[address(0)] = balanceOf[address(0)] + MINIMUM_LIQUIDITY;
} else {
liquidity = math_min(
(amount0 * _totalSupply) / pair_reserves[0],
(amount1 * _totalSupply) / pair_reserves[1]
);
}
require(liquidity > 0, "UniswapV2: INSUFFICIENT_LIQUIDITY_MINTED");
totalSupply = totalSupply + liquidity;
balanceOf[to] = balanceOf[to] + liquidity;
_update(balance0, balance1, pair_reserves[0], pair_reserves[1]);
//if (feeOn) kLast = uint(reserve0) * reserve1; // reserve0 and reserve1 are up-to-date
emit Mint(msg.sender, amount0, amount1);
}
function sync() external {
_update(IERC20(token0).balanceOf(address(this)), IERC20(token1).balanceOf(address(this)), reserve0, reserve1);
}
function getReserves() public returns (uint112[] memory reserves) {
reserves = new uint112[](3);
reserves[0] = reserve0;
reserves[1] = reserve1;
reserves[2] = blockTimestampLast;
}
function math_min(uint x, uint y) internal returns (uint z) {
z = x < y ? x : y;
}
function math_sqrt(uint y) internal returns (uint z) {
if (y > 3) {
z = y;
uint x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
} else if (y != 0) {
z = 1;
}
}
function _update(uint balance0, uint balance1, uint112 _reserve0, uint112 _reserve1) private {
require(balance0 <= UINT112_MAX && balance1 <= UINT112_MAX, "UniswapV2: OVERFLOW");
uint32 blockTimestamp = uint32(block.timestamp % 2**32);
uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
price0CumulativeLast = price0CumulativeLast + (_reserve1/_reserve0) * timeElapsed;
price1CumulativeLast = price1CumulativeLast + (_reserve0/_reserve1) * timeElapsed;
}
reserve0 = uint112(balance0);
reserve1 = uint112(balance1);
blockTimestampLast = blockTimestamp;
emit Sync(reserve0, reserve1);
}
}
contract WETHMock {
uint256 private UINT256_MAX = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
event Approval(address indexed owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
function decimals() external returns (uint8) {
return 18;
}
function deposit() external payable {
balanceOf[msg.sender] = balanceOf[msg.sender] + msg.value;
emit Transfer(address(0), msg.sender, msg.value);
}
function approve(address spender, uint256 value) external returns (bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
function transfer(address to, uint256 value) external returns (bool) {
if (to != address(0) && to != address(this)) { // Transfer
uint256 balance = balanceOf[msg.sender];
require(balance >= value, "WETH: transfer amount exceeds balance");
balanceOf[msg.sender] = balance - value;
balanceOf[to] = balanceOf[to] + value;
emit Transfer(msg.sender, to, value);
} else { // Withdraw
uint256 balance = balanceOf[msg.sender];
require(balance >= value, "WETH: burn amount exceeds balance");
balanceOf[msg.sender] = balance - value;
emit Transfer(msg.sender, address(0), value);
(bool success, ) = msg.sender.call{value: value}("");
require(success, "WETH: ETH transfer failed");
}
return true;
}
function transferFrom(address from, address to, uint256 value) external returns (bool) {
if (from != msg.sender) {
uint256 allowed = allowance[from][msg.sender];
if (allowed != UINT256_MAX) {
require(allowed >= value, "WETH: request exceeds allowance");
uint256 reduced = allowed - value;
allowance[from][msg.sender] = reduced;
emit Approval(from, msg.sender, reduced);
}
}
if (to != address(0) && to != address(this)) {
uint256 balance = balanceOf[from];
require(balance >= value, "WETH: transfer amount exceeds balance");
balanceOf[from] = balance - value;
balanceOf[to] = balanceOf[to] + value;
emit Transfer(from, to, value);
} else {
uint256 balance = balanceOf[from];
require(balance >= value, "WETH: burn amount exceeds balance");
balanceOf[from] = balance - value;
emit Transfer(from, address(0), value);
(bool success, ) = msg.sender.call{value: value}("");
require(success, "WETH: ETH transfer failed");
}
return true;
}
}
contract DAIMock {
uint private UINT_MAX = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
uint256 public totalSupply;
mapping (address => uint) public balanceOf;
mapping (address => mapping (address => uint)) public allowance;
event Approval(address indexed src, address indexed guy, uint wad);
event Transfer(address indexed src, address indexed dst, uint wad);
function decimals() external returns (uint8) {
return 18;
}
function mint(address usr, uint wad) public {
balanceOf[usr] = balanceOf[usr] + wad;
totalSupply = totalSupply + wad;
emit Transfer(address(0), usr, wad);
}
function mintOnDeposit(address usr, uint wad) public {
mint(usr, wad);
}
function burn(address usr, uint wad) public {
if(balanceOf[usr] >= wad){
balanceOf[usr] = balanceOf[usr] - wad;
totalSupply = totalSupply - wad;
}
}
function approve(address usr, uint wad) external returns (bool) {
allowance[msg.sender][usr] = wad;
emit Approval(msg.sender, usr, wad);
return true;
}
function transfer(address dst, uint wad) public returns (bool) {
return transferFrom(msg.sender, dst, wad);
}
function transferFrom(address src, address dst, uint wad)
public returns (bool)
{
require(balanceOf[src] >= wad, "Dai/insufficient-balance");
if (src != msg.sender && allowance[src][msg.sender] != UINT_MAX) {
require(allowance[src][msg.sender] >= wad, "Dai/insufficient-allowance");
allowance[src][msg.sender] = allowance[src][msg.sender] - wad;
}
balanceOf[src] = balanceOf[src] - wad;
balanceOf[dst] = balanceOf[dst] + wad;
emit Transfer(src, dst, wad);
return true;
}
function safeTransferFrom(address from, address to, uint256 value) external{
transferFrom(from, to, value);
}
}
contract USDCMock {
uint256 private UINT256_MAX = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
uint256 private _totalSupply;
mapping(address account => uint256) private _balances;
mapping(address account => mapping(address spender => uint256)) private _allowances;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function decimals() external returns (uint8) {
return 18;
}
function mint(address account, uint256 value) public {
require(account != address(0), "USDC: invalid receiver");
_update(address(0), account, value);
}
function balanceOf(address account) public returns (uint256) {
return _balances[account];
}
function transfer(address to, uint256 value) public returns (bool) {
address owner = msg.sender;
_transfer(owner, to, value);
return true;
}
function allowance(address owner, address spender) public returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 value) public returns (bool) {
address owner = msg.sender;
_approve(owner, spender, value, true);
return true;
}
function transferFrom(address from, address to, uint256 value) public returns (bool) {
address spender = msg.sender;
_spendAllowance(from, spender, value);
_transfer(from, to, value);
return true;
}
function _transfer(address from, address to, uint256 value) private {
require(from != address(0), "USDC: invalid sender");
require(to != address(0), "USDC: invalid receiver");
_update(from, to, value);
}
function _update(address from, address to, uint256 value) private {
if (from == address(0)) {
_totalSupply = _totalSupply + value;
} else {
uint256 fromBalance = _balances[from];
require(fromBalance >= value, "USDC: insufficient balance");
_balances[from] = fromBalance - value;
}
if (to == address(0)) {
_totalSupply = _totalSupply - value;
} else {
_balances[to] = _balances[to] + value;
}
emit Transfer(from, to, value);
}
function _approve(address owner, address spender, uint256 value, bool emitEvent) private {
require(owner != address(0), "USDC: invalid approver");
require(spender != address(0), "USDC: invalid spender");
_allowances[owner][spender] = value;
if (emitEvent) {
emit Approval(owner, spender, value);
}
}
function _spendAllowance(address owner, address spender, uint256 value) private {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != UINT256_MAX) {
require(currentAllowance >= value, "USDC: insufficient allowance");
_approve(owner, spender, currentAllowance - value, false);
}
}
}
contract UniswapV2SwapTest {
UniswapV2Swap private _uni;
WETHMock private _weth;
DAIMock private _dai;
USDCMock private _usdc;
function testSwapLoop() public {
_weth = new WETHMock();
_dai = new DAIMock();
_usdc = new USDCMock();
_uni = new UniswapV2Swap(address(_weth), address(_dai), address(_usdc));
for (uint i = 0; i < 1000; i++) {
testSwapSingleHopExactAmountIn();
}
}
function testSwapSingleHopExactAmountIn() public {
uint256 wethAmount = 1e18;
_weth.deposit{value: 2*wethAmount}();
_weth.approve(address(_uni), 2*wethAmount);
_dai.mint(address(this), wethAmount);
_dai.approve(address(_uni), wethAmount);
_weth.transfer(_uni.router().get_local_pair(address(_weth), address(_dai)), wethAmount);
_dai.transfer(_uni.router().get_local_pair(address(_weth), address(_dai)), wethAmount);
_uni.router().sync_local_pair(address(_weth), address(_dai));
uint256 daiAmountMin = 1;
uint256 daiAmountOut = _uni.swapSingleHopExactAmountIn(wethAmount, daiAmountMin);
assert(daiAmountOut >= daiAmountMin);
}
}