-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlindBackrunLogic.sol
executable file
·273 lines (235 loc) · 10.7 KB
/
BlindBackrunLogic.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
pragma solidity ^0.8.19;
import "openzeppelin/access/Ownable.sol";
import "forge-std/console.sol";
import "./IWETH.sol";
interface IUniswapV2Pair {
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function token0() external view returns (address);
function token1() external view returns (address);
}
interface IPairReserves{
struct PairReserves {
uint256 reserve0;
uint256 reserve1;
uint256 price;
bool isWETHZero;
}
}
// Do not simply open up _executeArbitrage to any caller, this code should either be protected by onlyOwner or should not maintain a WETH balance
abstract contract BlindBackrunLogic is Ownable {
uint256 constant uniswappyFee = 997;
IWETH public immutable WETH;
constructor(IWETH _wethAddress) {
WETH = _wethAddress;
}
/// @notice Executes an arbitrage transaction between two Uniswap V2 pairs.
/// @notice Pair addresses need to be computed off-chain.
/// @dev Only the contract owner can call this function.
/// @param firstPairAddress Address of the first Uniswap V2 pair.
/// @param secondPairAddress Address of the second Uniswap V2 pair.
function _executeArbitrage(
address firstPairAddress,
address secondPairAddress,
uint percentageToPayToCoinbase
) internal {
uint256 balanceBefore = WETH.balanceOf(address(this));
console.log("Starting balance : %s", balanceBefore);
IUniswapV2Pair firstPair = IUniswapV2Pair(firstPairAddress);
IUniswapV2Pair secondPair = IUniswapV2Pair(secondPairAddress);
console.log("\n--------- PAIR DATA ---------");
IPairReserves.PairReserves memory firstPairData = getPairData(firstPair);
IPairReserves.PairReserves memory secondPairData = getPairData(secondPair);
console.log("\n--------- PRICES ---------");
console.log("firstPair price : %s", firstPairData.price);
console.log("secondPair price : %s", secondPairData.price);
uint256 amountIn = getAmountIn(firstPairData, secondPairData);
console.log("\n--------- TRADE AMOUNTS ---------");
console.log("amountIn : %s", amountIn);
WETH.transfer(firstPairAddress, amountIn);
uint256 firstPairAmountOut;
uint256 finalAmountOut;
if (firstPairData.isWETHZero == true){
firstPairAmountOut = getAmountOut(amountIn, firstPairData.reserve0, firstPairData.reserve1);
finalAmountOut = getAmountOut(firstPairAmountOut, secondPairData.reserve1, secondPairData.reserve0);
console.log("firstPairAmountOut: %s", firstPairAmountOut);
console.log("finalAmountOut : %s", finalAmountOut);
console.log("arb profit : %s", finalAmountOut - amountIn);
console.log("\n--------- TRADING ---------");
firstPair.swap(0, firstPairAmountOut, secondPairAddress, "");
console.log("firstPair swap done");
secondPair.swap(finalAmountOut, 0, address(this), "");
console.log("secondPair swap done");
} else {
firstPairAmountOut = getAmountOut(amountIn, firstPairData.reserve1, firstPairData.reserve0);
finalAmountOut = getAmountOut(firstPairAmountOut, secondPairData.reserve0, secondPairData.reserve1);
console.log("\n--------- TRADE AMOUNTS ---------");
console.log("amountIn : %s", amountIn);
console.log("firstPairAmountOut: %s", firstPairAmountOut);
console.log("finalAmountOut : %s", finalAmountOut);
console.log("arb profit : %s", finalAmountOut - amountIn);
console.log("\n--------- TRADING ---------");
firstPair.swap(firstPairAmountOut, 0, secondPairAddress, "");
console.log("firstPair swap done");
secondPair.swap(0, finalAmountOut, address(this), "");
console.log("secondPair swap done");
}
uint256 balanceAfter = WETH.balanceOf(address(this));
require(balanceAfter > balanceBefore, "Arbitrage failed");
console.log("\n--------- SUCCESS ---------");
console.log("Ending balance : %s", balanceAfter);
uint profit = balanceAfter - balanceBefore;
console.log("Profit : %s", profit);
WETH.withdraw(profit);
uint profitToCoinbase = profit * percentageToPayToCoinbase / 100;
uint profitToCaller = profit - profitToCoinbase;
block.coinbase.transfer(profitToCoinbase);
payable(tx.origin).transfer(profitToCaller);
}
/// @notice Calculates the required input amount for the arbitrage transaction.
/// @param firstPairData Struct containing data about the first Uniswap V2 pair.
/// @param secondPairData Struct containing data about the second Uniswap V2 pair.
/// @return amountIn, the optimal amount to trade to arbitrage two v2 pairs.
function getAmountIn(
IPairReserves.PairReserves memory firstPairData,
IPairReserves.PairReserves memory secondPairData
) public view returns (uint256) {
uint256 numerator = getNumerator(firstPairData, secondPairData);
console.log("numerator: %s", numerator);
uint256 denominator = getDenominator(firstPairData, secondPairData);
console.log("denominator: %s", denominator);
uint256 amountIn =
numerator
* 1000
/ denominator;
return amountIn;
}
function getNumerator(
IPairReserves.PairReserves memory firstPairData,
IPairReserves.PairReserves memory secondPairData
) public view returns (uint256) {
if (firstPairData.isWETHZero == true) {
uint presqrt = uniswappyFee
* uniswappyFee
* firstPairData.reserve1
* secondPairData.reserve0
/ secondPairData.reserve1
/ firstPairData.reserve0;
console.log("presqrt: %s", presqrt);
console.log("sqrt(presqrt): %s", sqrt(presqrt));
uint256 numerator =
(
sqrt(presqrt)
- 1e3
)
* secondPairData.reserve1
* firstPairData.reserve0;
return numerator;
} else {
uint presqrt = uniswappyFee
* uniswappyFee
* firstPairData.reserve0
* secondPairData.reserve1
/ secondPairData.reserve0
/ firstPairData.reserve1;
uint256 numerator =
(
sqrt(presqrt)
- 1e3
)
* secondPairData.reserve0
* firstPairData.reserve1;
return numerator;
}
}
function getDenominator(
IPairReserves.PairReserves memory firstPairData,
IPairReserves.PairReserves memory secondPairData
) public pure returns (uint256){
if (firstPairData.isWETHZero == true) {
uint256 denominator =
(
uniswappyFee
* secondPairData.reserve1
* 1000
)
+ (
uniswappyFee
* uniswappyFee
* firstPairData.reserve1
);
return denominator;
} else {
uint256 denominator =
(
uniswappyFee
* secondPairData.reserve0
* 1000
)
+ (
uniswappyFee
* uniswappyFee
* firstPairData.reserve0
);
return denominator;
}
}
/// @notice Retrieves price and reserve data for a given Uniswap V2 pair. Also checks which token is WETH.
/// @param pair The Uniswap V2 pair to retrieve data for.
/// @return A IPairReserves.PairReserves struct containing price and reserve data for the given pair.
function getPairData(IUniswapV2Pair pair) private view returns (IPairReserves.PairReserves memory) {
(uint256 reserve0, uint256 reserve1, ) = pair.getReserves();
console.log("pair:", address(pair));
console.log("reserve0:",reserve0);
console.log("reserve1:", reserve1);
uint256 price;
bool isWETHZero = false;
if (pair.token0() == address(WETH)) {
price = reserve1 * 1e18 / reserve0;
isWETHZero = true;
} else {
price = reserve0 * 1e18 / reserve1;
}
return IPairReserves.PairReserves(reserve0, reserve1, price, isWETHZero);
}
/// @notice Calculates the square root of a given number.
/// @param x The number to calculate the square root of.
/// @return y, The square root of the given number.
function sqrt(uint256 x) private pure returns (uint256) {
if (x == 0) return 0;
uint256 z = (x + 1) / 2;
uint256 y = x;
while (z < y) {
y = z;
z = ((x / z) + z) / 2;
}
return y;
}
/// @notice Calculates the output amount for a given input amount and reserves.
/// @param amountIn The input amount.
/// @param reserveIn The reserve of the input token.
/// @param reserveOut The reserve of the output token.
/// @return amountOut The output amount.
function getAmountOut(uint amountIn,
uint reserveIn,
uint reserveOut
) internal pure returns (uint amountOut) {
uint amountInWithFee = amountIn * 997;
uint numerator = amountInWithFee * reserveOut;
uint denominator = reserveIn * 1000 + amountInWithFee;
amountOut = numerator / denominator;
return amountOut;
}
/// @notice Executes a call to another contract with the provided data and value. The owner of this contract can help rescue assets accidentally left on contract, but in normal operation, this contract does not hold assets and is not centrally controlled.
/// @dev Only the contract owner can call this function.
/// @dev Reverted calls will result in a revert.
/// @param _to The address of the contract to call.
/// @param _value The amount of Ether to send with the call.
/// @param _data The calldata to send with the call.
function call(address payable _to, uint256 _value, bytes memory _data) external onlyOwner {
(bool success, ) = _to.call{value: _value}(_data);
require(success, "External call failed");
}
/// @notice Fallback function that allows the contract to receive Ether.
receive() external payable {}
}