generated from yuichiroaoki/typescript-hardhat
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathFlashloan.sol
195 lines (171 loc) · 5.85 KB
/
Flashloan.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "./uniswap/IUniswapV2Router.sol";
import "./uniswap/v3/ISwapRouter.sol";
import "./dodo/IDODO.sol";
import "./interfaces/IFlashloan.sol";
import "./base/DodoBase.sol";
import "./dodo/IDODOProxy.sol";
import "./base/FlashloanValidation.sol";
import "./base/Withdraw.sol";
import "./libraries/Part.sol";
import "./libraries/RouteUtils.sol";
contract Flashloan is IFlashloan, DodoBase, FlashloanValidation, Withdraw {
using SafeMath for uint256;
using SafeERC20 for IERC20;
event SentProfit(address recipient, uint256 profit);
event SwapFinished(address token, uint256 amount);
function dodoFlashLoan(FlashParams memory params)
external
checkParams(params)
{
bytes memory data = abi.encode(
FlashCallbackData({
me: msg.sender,
flashLoanPool: params.flashLoanPool,
loanAmount: params.loanAmount,
routes: params.routes
})
);
address loanToken = RouteUtils.getInitialToken(params.routes[0]);
IDODO(params.flashLoanPool).flashLoan(
IDODO(params.flashLoanPool)._BASE_TOKEN_() == loanToken
? params.loanAmount
: 0,
IDODO(params.flashLoanPool)._BASE_TOKEN_() == loanToken
? 0
: params.loanAmount,
address(this),
data
);
}
function _flashLoanCallBack(
address,
uint256,
uint256,
bytes calldata data
) internal override {
FlashCallbackData memory decoded = abi.decode(
data,
(FlashCallbackData)
);
address loanToken = RouteUtils.getInitialToken(decoded.routes[0]);
require(
IERC20(loanToken).balanceOf(address(this)) >= decoded.loanAmount,
"Failed to borrow loan token"
);
routeLoop(decoded.routes, decoded.loanAmount);
emit SwapFinished(
loanToken,
IERC20(loanToken).balanceOf(address(this))
);
require(
IERC20(loanToken).balanceOf(address(this)) >= decoded.loanAmount,
"Not enough amount to return loan"
);
//Return funds
IERC20(loanToken).transfer(decoded.flashLoanPool, decoded.loanAmount);
// send all loanToken to msg.sender
uint256 remained = IERC20(loanToken).balanceOf(address(this));
IERC20(loanToken).transfer(decoded.me, remained);
emit SentProfit(decoded.me, remained);
}
function routeLoop(Route[] memory routes, uint256 totalAmount)
internal
checkTotalRoutePart(routes)
{
for (uint256 i = 0; i < routes.length; i++) {
uint256 amountIn = Part.partToAmountIn(routes[i].part, totalAmount);
hopLoop(routes[i], amountIn);
}
}
function hopLoop(Route memory route, uint256 totalAmount) internal {
uint256 amountIn = totalAmount;
for (uint256 i = 0; i < route.hops.length; i++) {
amountIn = pickProtocol(route.hops[i], amountIn);
}
}
function pickProtocol(Hop memory hop, uint256 amountIn)
internal
returns (uint256 amountOut)
{
if (hop.protocol == 0) {
amountOut = uniswapV3(hop.data, amountIn, hop.path);
} else if (hop.protocol < 8) {
amountOut = uniswapV2(hop.data, amountIn, hop.path);
} else {
amountOut = dodoV2Swap(hop.data, amountIn, hop.path);
}
}
function uniswapV3(
bytes memory data,
uint256 amountIn,
address[] memory path
) internal returns (uint256 amountOut) {
(address router, uint24 fee) = abi.decode(data, (address, uint24));
ISwapRouter swapRouter = ISwapRouter(router);
approveToken(path[0], address(swapRouter), amountIn);
// single swaps
amountOut = swapRouter.exactInputSingle(
ISwapRouter.ExactInputSingleParams({
tokenIn: path[0],
tokenOut: path[1],
fee: fee,
recipient: address(this),
deadline: block.timestamp,
amountIn: amountIn,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
})
);
}
function uniswapV2(
bytes memory data,
uint256 amountIn,
address[] memory path
) internal returns (uint256 amountOut) {
address router = abi.decode(data, (address));
approveToken(path[0], router, amountIn);
return
IUniswapV2Router(router).swapExactTokensForTokens(
amountIn,
1,
path,
address(this),
block.timestamp
)[1];
}
function dodoV2Swap(
bytes memory data,
uint256 amountIn,
address[] memory path
) internal returns (uint256 amountOut) {
(address dodoV2Pool, address dodoApprove, address dodoProxy) = abi
.decode(data, (address, address, address));
address[] memory dodoPairs = new address[](1); //one-hop
dodoPairs[0] = dodoV2Pool;
uint256 directions = IDODO(dodoV2Pool)._BASE_TOKEN_() == path[0]
? 0
: 1;
approveToken(path[0], dodoApprove, amountIn);
amountOut = IDODOProxy(dodoProxy).dodoSwapV2TokenToToken(
path[0],
path[1],
amountIn,
1,
dodoPairs,
directions,
false,
block.timestamp
);
}
function approveToken(
address token,
address to,
uint256 amountIn
) internal {
require(IERC20(token).approve(to, amountIn), "approve failed");
}
}