-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArbitPyMasterContract.sol
More file actions
650 lines (543 loc) · 20.1 KB
/
Copy pathArbitPyMasterContract.sol
File metadata and controls
650 lines (543 loc) · 20.1 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
/**
* @title ArbitPyMaster - Advanced DeFi Arbitrage & Yield Optimization Contract
* @dev A comprehensive smart contract for the ArbitPy playground featuring:
* - Multi-DEX arbitrage execution
* - Yield farming optimization
* - Flash loan integration
* - Liquidity management
* - Fee distribution system
* - Emergency controls
*/
contract ArbitPyMaster is ReentrancyGuard, Ownable, Pausable {
using SafeERC20 for IERC20;
using SafeMath for uint256;
// ======================== STRUCTS ========================
struct ArbitrageParams {
address tokenA;
address tokenB;
address dexA;
address dexB;
uint256 amountIn;
uint256 minAmountOut;
bytes routerCallDataA;
bytes routerCallDataB;
}
struct UserPosition {
uint256 totalDeposited;
uint256 totalWithdrawn;
uint256 pendingRewards;
uint256 lastInteractionBlock;
mapping(address => uint256) tokenBalances;
}
struct PoolInfo {
address token;
uint256 totalSupply;
uint256 rewardRate;
uint256 lastRewardBlock;
uint256 accRewardPerShare;
bool active;
}
// ======================== EVENTS ========================
event ArbitrageExecuted(
address indexed user,
address tokenA,
address tokenB,
uint256 amountIn,
uint256 profit,
uint256 timestamp
);
event LiquidityAdded(
address indexed user,
address indexed token,
uint256 amount,
uint256 timestamp
);
event LiquidityRemoved(
address indexed user,
address indexed token,
uint256 amount,
uint256 timestamp
);
event RewardsClaimed(
address indexed user,
uint256 amount,
uint256 timestamp
);
event FlashLoanExecuted(
address indexed user,
address token,
uint256 amount,
uint256 fee
);
event StrategyExecuted(
address indexed user,
string strategyType,
uint256 inputAmount,
uint256 outputAmount
);
// ======================== STATE VARIABLES ========================
mapping(address => UserPosition) public userPositions;
mapping(uint256 => PoolInfo) public pools;
mapping(address => bool) public authorizedRouters;
mapping(address => uint256) public tokenPriceOracles;
uint256 public constant PRECISION = 1e18;
uint256 public constant MAX_FEE = 1000; // 10%
uint256 public constant FLASH_LOAN_FEE = 9; // 0.09%
uint256 public platformFee = 30; // 0.3%
uint256 public totalPoolCount;
uint256 public totalTVL;
uint256 public totalVolume;
uint256 public totalArbitrageProfit;
address public feeRecipient;
address public emergencyWithdrawer;
bool public flashLoansEnabled = true;
bool public arbitrageEnabled = true;
// Supported DEX routers
mapping(address => string) public dexNames;
address[] public supportedDEXs;
// ======================== MODIFIERS ========================
modifier onlyAuthorizedRouter(address router) {
require(authorizedRouters[router], "ArbitPy: Router not authorized");
_;
}
modifier validPool(uint256 poolId) {
require(poolId < totalPoolCount && pools[poolId].active, "ArbitPy: Invalid pool");
_;
}
modifier flashLoansAllowed() {
require(flashLoansEnabled, "ArbitPy: Flash loans disabled");
_;
}
modifier arbitrageAllowed() {
require(arbitrageEnabled, "ArbitPy: Arbitrage disabled");
_;
}
// ======================== CONSTRUCTOR ========================
constructor(
address _feeRecipient,
address _emergencyWithdrawer
) Ownable(msg.sender) {
require(_feeRecipient != address(0), "ArbitPy: Invalid fee recipient");
require(_emergencyWithdrawer != address(0), "ArbitPy: Invalid emergency withdrawer");
feeRecipient = _feeRecipient;
emergencyWithdrawer = _emergencyWithdrawer;
// Initialize first pool
_createPool(address(0), 0, true); // ETH pool
}
// ======================== ARBITRAGE FUNCTIONS ========================
/**
* @dev Execute arbitrage between two DEXs
* @param params Arbitrage parameters containing DEX addresses, tokens, amounts
*/
function executeArbitrage(
ArbitrageParams calldata params
) external payable nonReentrant whenNotPaused arbitrageAllowed {
require(params.amountIn > 0, "ArbitPy: Invalid amount");
require(params.tokenA != params.tokenB, "ArbitPy: Same tokens");
require(authorizedRouters[params.dexA] && authorizedRouters[params.dexB], "ArbitPy: Unauthorized DEX");
uint256 initialBalance;
if (params.tokenA == address(0)) {
require(msg.value == params.amountIn, "ArbitPy: Incorrect ETH amount");
initialBalance = address(this).balance - msg.value;
} else {
IERC20(params.tokenA).safeTransferFrom(msg.sender, address(this), params.amountIn);
initialBalance = IERC20(params.tokenA).balanceOf(address(this)) - params.amountIn;
}
// Execute trade on DEX A
uint256 intermediateAmount = _executeDEXTrade(params.dexA, params.routerCallDataA);
require(intermediateAmount > 0, "ArbitPy: DEX A trade failed");
// Execute trade on DEX B
uint256 finalAmount = _executeDEXTrade(params.dexB, params.routerCallDataB);
require(finalAmount >= params.minAmountOut, "ArbitPy: Slippage exceeded");
uint256 profit;
if (params.tokenA == address(0)) {
uint256 finalBalance = address(this).balance;
profit = finalBalance > initialBalance ? finalBalance - initialBalance : 0;
} else {
uint256 finalBalance = IERC20(params.tokenA).balanceOf(address(this));
profit = finalBalance > initialBalance ? finalBalance - initialBalance : 0;
}
// Charge platform fee
uint256 fee = profit.mul(platformFee).div(10000);
uint256 userProfit = profit.sub(fee);
if (fee > 0) {
if (params.tokenA == address(0)) {
payable(feeRecipient).transfer(fee);
} else {
IERC20(params.tokenA).safeTransfer(feeRecipient, fee);
}
}
// Transfer profit to user
if (userProfit > 0) {
if (params.tokenA == address(0)) {
payable(msg.sender).transfer(userProfit);
} else {
IERC20(params.tokenA).safeTransfer(msg.sender, userProfit);
}
}
// Update stats
totalArbitrageProfit = totalArbitrageProfit.add(profit);
totalVolume = totalVolume.add(params.amountIn);
emit ArbitrageExecuted(
msg.sender,
params.tokenA,
params.tokenB,
params.amountIn,
userProfit,
block.timestamp
);
}
// ======================== LIQUIDITY FUNCTIONS ========================
/**
* @dev Add liquidity to a specific pool
* @param poolId The pool to add liquidity to
* @param amount Amount of tokens to add
*/
function addLiquidity(
uint256 poolId,
uint256 amount
) external payable nonReentrant whenNotPaused validPool(poolId) {
require(amount > 0, "ArbitPy: Invalid amount");
PoolInfo storage pool = pools[poolId];
UserPosition storage user = userPositions[msg.sender];
// Update rewards before changing user position
_updateRewards(poolId, msg.sender);
if (pool.token == address(0)) {
require(msg.value == amount, "ArbitPy: Incorrect ETH amount");
} else {
IERC20(pool.token).safeTransferFrom(msg.sender, address(this), amount);
}
user.totalDeposited = user.totalDeposited.add(amount);
user.tokenBalances[pool.token] = user.tokenBalances[pool.token].add(amount);
user.lastInteractionBlock = block.number;
pool.totalSupply = pool.totalSupply.add(amount);
totalTVL = totalTVL.add(amount);
emit LiquidityAdded(msg.sender, pool.token, amount, block.timestamp);
}
/**
* @dev Remove liquidity from a specific pool
* @param poolId The pool to remove liquidity from
* @param amount Amount of tokens to remove
*/
function removeLiquidity(
uint256 poolId,
uint256 amount
) external nonReentrant whenNotPaused validPool(poolId) {
require(amount > 0, "ArbitPy: Invalid amount");
PoolInfo storage pool = pools[poolId];
UserPosition storage user = userPositions[msg.sender];
require(user.tokenBalances[pool.token] >= amount, "ArbitPy: Insufficient balance");
// Update rewards before changing user position
_updateRewards(poolId, msg.sender);
user.totalWithdrawn = user.totalWithdrawn.add(amount);
user.tokenBalances[pool.token] = user.tokenBalances[pool.token].sub(amount);
user.lastInteractionBlock = block.number;
pool.totalSupply = pool.totalSupply.sub(amount);
totalTVL = totalTVL.sub(amount);
// Transfer tokens back to user
if (pool.token == address(0)) {
payable(msg.sender).transfer(amount);
} else {
IERC20(pool.token).safeTransfer(msg.sender, amount);
}
emit LiquidityRemoved(msg.sender, pool.token, amount, block.timestamp);
}
// ======================== YIELD FARMING FUNCTIONS ========================
/**
* @dev Claim pending rewards for a user
*/
function claimRewards() external nonReentrant whenNotPaused {
UserPosition storage user = userPositions[msg.sender];
// Update all pool rewards for user
for (uint256 i = 0; i < totalPoolCount; i++) {
if (pools[i].active) {
_updateRewards(i, msg.sender);
}
}
uint256 rewards = user.pendingRewards;
require(rewards > 0, "ArbitPy: No rewards available");
user.pendingRewards = 0;
// Mint or transfer reward tokens (assuming ETH rewards for simplicity)
payable(msg.sender).transfer(rewards);
emit RewardsClaimed(msg.sender, rewards, block.timestamp);
}
// ======================== FLASH LOAN FUNCTIONS ========================
/**
* @dev Execute flash loan
* @param token Token to borrow
* @param amount Amount to borrow
* @param data Callback data for flash loan execution
*/
function flashLoan(
address token,
uint256 amount,
bytes calldata data
) external nonReentrant whenNotPaused flashLoansAllowed {
require(amount > 0, "ArbitPy: Invalid amount");
uint256 balanceBefore;
if (token == address(0)) {
balanceBefore = address(this).balance;
require(balanceBefore >= amount, "ArbitPy: Insufficient ETH liquidity");
} else {
balanceBefore = IERC20(token).balanceOf(address(this));
require(balanceBefore >= amount, "ArbitPy: Insufficient token liquidity");
}
uint256 fee = amount.mul(FLASH_LOAN_FEE).div(10000);
// Transfer tokens to borrower
if (token == address(0)) {
payable(msg.sender).transfer(amount);
} else {
IERC20(token).safeTransfer(msg.sender, amount);
}
// Execute callback
IFlashLoanReceiver(msg.sender).executeOperation(token, amount, fee, data);
// Verify repayment
uint256 balanceAfter;
if (token == address(0)) {
balanceAfter = address(this).balance;
} else {
balanceAfter = IERC20(token).balanceOf(address(this));
}
require(
balanceAfter >= balanceBefore.add(fee),
"ArbitPy: Flash loan not repaid"
);
emit FlashLoanExecuted(msg.sender, token, amount, fee);
}
// ======================== STRATEGY FUNCTIONS ========================
/**
* @dev Execute yield optimization strategy
* @param strategyType Type of strategy to execute
* @param inputToken Input token address
* @param inputAmount Amount of input tokens
* @param minOutputAmount Minimum output amount expected
*/
function executeStrategy(
string memory strategyType,
address inputToken,
uint256 inputAmount,
uint256 minOutputAmount
) external payable nonReentrant whenNotPaused {
require(inputAmount > 0, "ArbitPy: Invalid input amount");
uint256 outputAmount;
if (keccak256(bytes(strategyType)) == keccak256(bytes("COMPOUND"))) {
outputAmount = _executeCompoundStrategy(inputToken, inputAmount);
} else if (keccak256(bytes(strategyType)) == keccak256(bytes("YIELD_FARM"))) {
outputAmount = _executeYieldFarmStrategy(inputToken, inputAmount);
} else if (keccak256(bytes(strategyType)) == keccak256(bytes("LIQUIDITY_MINING"))) {
outputAmount = _executeLiquidityMiningStrategy(inputToken, inputAmount);
} else {
revert("ArbitPy: Unknown strategy");
}
require(outputAmount >= minOutputAmount, "ArbitPy: Strategy slippage exceeded");
emit StrategyExecuted(msg.sender, strategyType, inputAmount, outputAmount);
}
// ======================== VIEW FUNCTIONS ========================
/**
* @dev Get user position information
*/
function getUserPosition(address user) external view returns (
uint256 totalDeposited,
uint256 totalWithdrawn,
uint256 pendingRewards,
uint256 lastInteractionBlock
) {
UserPosition storage position = userPositions[user];
return (
position.totalDeposited,
position.totalWithdrawn,
position.pendingRewards,
position.lastInteractionBlock
);
}
/**
* @dev Get user token balance in a specific pool
*/
function getUserTokenBalance(address user, address token) external view returns (uint256) {
return userPositions[user].tokenBalances[token];
}
/**
* @dev Get pool information
*/
function getPoolInfo(uint256 poolId) external view returns (
address token,
uint256 totalSupply,
uint256 rewardRate,
uint256 lastRewardBlock,
uint256 accRewardPerShare,
bool active
) {
PoolInfo storage pool = pools[poolId];
return (
pool.token,
pool.totalSupply,
pool.rewardRate,
pool.lastRewardBlock,
pool.accRewardPerShare,
pool.active
);
}
/**
* @dev Get platform statistics
*/
function getPlatformStats() external view returns (
uint256 _totalTVL,
uint256 _totalVolume,
uint256 _totalArbitrageProfit,
uint256 _totalPoolCount
) {
return (totalTVL, totalVolume, totalArbitrageProfit, totalPoolCount);
}
// ======================== ADMIN FUNCTIONS ========================
/**
* @dev Create a new pool
*/
function createPool(
address token,
uint256 rewardRate
) external onlyOwner {
_createPool(token, rewardRate, true);
}
/**
* @dev Add authorized router
*/
function addAuthorizedRouter(
address router,
string memory name
) external onlyOwner {
require(router != address(0), "ArbitPy: Invalid router");
authorizedRouters[router] = true;
dexNames[router] = name;
supportedDEXs.push(router);
}
/**
* @dev Update platform fee
*/
function updatePlatformFee(uint256 newFee) external onlyOwner {
require(newFee <= MAX_FEE, "ArbitPy: Fee too high");
platformFee = newFee;
}
/**
* @dev Toggle flash loans
*/
function toggleFlashLoans() external onlyOwner {
flashLoansEnabled = !flashLoansEnabled;
}
/**
* @dev Toggle arbitrage
*/
function toggleArbitrage() external onlyOwner {
arbitrageEnabled = !arbitrageEnabled;
}
/**
* @dev Emergency pause
*/
function pause() external onlyOwner {
_pause();
}
/**
* @dev Emergency unpause
*/
function unpause() external onlyOwner {
_unpause();
}
/**
* @dev Emergency withdrawal by authorized withdrawer
*/
function emergencyWithdraw(
address token,
uint256 amount
) external {
require(msg.sender == emergencyWithdrawer, "ArbitPy: Unauthorized");
if (token == address(0)) {
payable(emergencyWithdrawer).transfer(amount);
} else {
IERC20(token).safeTransfer(emergencyWithdrawer, amount);
}
}
// ======================== INTERNAL FUNCTIONS ========================
function _createPool(
address token,
uint256 rewardRate,
bool active
) internal {
pools[totalPoolCount] = PoolInfo({
token: token,
totalSupply: 0,
rewardRate: rewardRate,
lastRewardBlock: block.number,
accRewardPerShare: 0,
active: active
});
totalPoolCount++;
}
function _executeDEXTrade(
address router,
bytes memory callData
) internal returns (uint256) {
(bool success, bytes memory result) = router.call(callData);
require(success, "ArbitPy: DEX trade failed");
// Decode return value (assuming it returns amount)
return abi.decode(result, (uint256));
}
function _updateRewards(uint256 poolId, address user) internal {
PoolInfo storage pool = pools[poolId];
UserPosition storage userPos = userPositions[user];
if (pool.totalSupply > 0) {
uint256 blocksPassed = block.number.sub(pool.lastRewardBlock);
uint256 reward = blocksPassed.mul(pool.rewardRate);
pool.accRewardPerShare = pool.accRewardPerShare.add(
reward.mul(PRECISION).div(pool.totalSupply)
);
pool.lastRewardBlock = block.number;
}
uint256 userBalance = userPos.tokenBalances[pool.token];
if (userBalance > 0) {
uint256 pending = userBalance.mul(pool.accRewardPerShare).div(PRECISION);
userPos.pendingRewards = userPos.pendingRewards.add(pending);
}
}
function _executeCompoundStrategy(
address /* inputToken */,
uint256 inputAmount
) internal pure returns (uint256) {
// Implementation for compound strategy
// This would integrate with compound protocol
return inputAmount.mul(105).div(100); // 5% yield simulation
}
function _executeYieldFarmStrategy(
address /* inputToken */,
uint256 inputAmount
) internal pure returns (uint256) {
// Implementation for yield farming strategy
return inputAmount.mul(108).div(100); // 8% yield simulation
}
function _executeLiquidityMiningStrategy(
address /* inputToken */,
uint256 inputAmount
) internal pure returns (uint256) {
// Implementation for liquidity mining strategy
return inputAmount.mul(112).div(100); // 12% yield simulation
}
// ======================== RECEIVE FUNCTION ========================
receive() external payable {
// Allow contract to receive ETH
}
}
// ======================== INTERFACES ========================
interface IFlashLoanReceiver {
function executeOperation(
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external;
}