-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDexSwapArbitrage.sol
76 lines (65 loc) · 2.12 KB
/
DexSwapArbitrage.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
pragma solidity ^0.6.6 ;
import './libraries/DexSwapLibrary.sol';
import './interfaces/IDexSwapRouter.sol';
import './interfaces/IDexSwapPair.sol';
import './interfaces/IDexSwapFactory.sol';
import './interfaces/IERC20.sol';
contract DexSwapArbitrage {
address public factory;
uint constant deadline = 10 days;
IDexSwapRouter public dexSwapRoute;
constructor(address _factory, address _dexSwapRoute) public {
factory = _factory;
dexSwapRoute = IDexSwapRouter(_dexSwapRoute);
}
function startDexSwapArbitrage(
address token0,
address token1,
uint amount0,
uint amount1
) external {
address pairAddress = IDexSwapFactory(factory).getPair(token0, token1);
require(pairAddress != address(0), 'This pool does not exist');
IDexSwapPair(pairAddress).swap(
amount0,
amount1,
address(this),
bytes('not empty')
);
}
function uniswapV2Call(
address _sender,
uint _amount0,
uint _amount1,
bytes calldata _data
) external {
address[] memory path = new address[](2);
uint amountToken = _amount0 == 0 ? _amount1 : _amount0;
address token0 = IDexSwapPair(msg.sender).token0();
address token1 = IDexSwapPair(msg.sender).token1();
require(
msg.sender == DexSwapLibrary.pairFor(factory, token0, token1),
'Unauthorized'
);
require(_amount0 == 0 || _amount1 == 0);
path[0] = _amount0 == 0 ? token1 : token0;
path[1] = _amount0 == 0 ? token0 : token1;
IERC20 token = IERC20(_amount0 == 0 ? token1 : token0);
token.approve(address(dexSwapRoute), amountToken);
uint amountRequired = DexSwapLibrary.getAmountsIn(
factory,
amountToken,
path
)[0];
uint amountReceived = dexSwapRoute.swapExactTokensForTokens(
amountToken,
amountRequired,
path,
msg.sender,
deadline
)[1];
IERC20 otherToken = IERC20(_amount0 == 0 ? token0 : token1);
otherToken.transfer(msg.sender, amountRequired);
otherToken.transfer(tx.origin, amountReceived - amountRequired);
}
}