-
Notifications
You must be signed in to change notification settings - Fork 21
/
arbitrage.sol
67 lines (59 loc) · 3.92 KB
/
arbitrage.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IUniswapV2Router {
function swapExactTokensForETH(uint amountIn,uint amountOutMin,address[] calldata path,address to,uint deadline) external returns (uint[] memory amounts);
function getAmountsOut(uint amountIn, address[] memory path) external view returns (uint[] memory amounts);
function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts);
function swapETHForExactTokens(uint amountOut,address[] calldata path,address to,uint deadline) external payable returns (uint[] memory amounts);
function swapExactETHForTokens(uint amountOutMin,address[] calldata path,address to,uint deadline) external payable returns (uint[] memory amounts);
}
interface ISushiSwapRouter {
function swapExactETHForTokens(uint amountOutMin,address[] calldata path,address to,uint deadline) external payable returns (uint[] memory amounts);
function getAmountsOut(uint amountIn, address[] memory path) external view returns (uint[] memory amounts);
function swapTokensForExactTokens(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts);
function swapExactTokensForETH(uint amountIn,uint amountOutMin,address[] calldata path,address to,uint deadline) external returns (uint[] memory amounts);
function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts);
}
interface IWETH9 {
function deposit() external payable;
function withdraw(uint256 wad) external;
function approve(address guy, uint256 wad) external returns (bool);
function transfer(address dst, uint256 wad) external returns (bool);
function transferFrom(address src, address dst, uint256 wad) external returns (bool);
function totalSupply() external view returns (uint256);
function balanceOf(address src) external view returns (uint256);
function allowance(address src, address guy) external view returns (uint256);
event Approval(address indexed src, address indexed guy, uint256 wad);
event Transfer(address indexed src, address indexed dst, uint256 wad);
event Deposit(address indexed dst, uint256 wad);
event Withdrawal(address indexed src, uint256 wad);
}
contract MyContract {
address constant WETH_ADDRESS = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6;
address constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address constant SUSHISWAP_ROUTER_ADDRESS = 0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506;
IUniswapV2Router private uniswapRouter;
ISushiSwapRouter private sushiswapRouter;
constructor() {
uniswapRouter = IUniswapV2Router(UNISWAP_ROUTER_ADDRESS);
sushiswapRouter = ISushiSwapRouter(SUSHISWAP_ROUTER_ADDRESS);
}
function swap(address _address,uint256 _amountIn) external {
IWETH9(WETH_ADDRESS).transferFrom(msg.sender, address(this), _amountIn);
IWETH9(WETH_ADDRESS).approve(address(UNISWAP_ROUTER_ADDRESS) ,_amountIn);
// Buy the token on Uniswap with ETH
address[] memory path = new address[](2);
path[0] = WETH_ADDRESS;
path[1] = _address;
// Get the amount of tokens received
uint256[] memory amounts = uniswapRouter.swapExactTokensForTokens(_amountIn, 0, path, address(this), block.timestamp);
uint256 amountOut = amounts[1];
// Sell the token on Sushiswap with _address
IWETH9(_address).approve(address(SUSHISWAP_ROUTER_ADDRESS) ,amountOut);
path[0] = _address;
path[1] = WETH_ADDRESS;
uint256[] memory amounts_1 = sushiswapRouter.swapExactTokensForTokens(amountOut,0, path, msg.sender, block.timestamp);
uint256 amountOut_1 = amounts_1[1];
require(amountOut_1 > _amountIn , "Arbitrage fail !");
}
}