Description:
Decentralized Finance (DeFi) protocol contract providing Swap, Factory functionality.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
{{
"language": "Solidity",
"sources": {
"FlashArbitrageHardcoded.sol": {
"content": "// SPDX-License-Identifier: MIT\r
pragma solidity ^0.8.10;\r
\r
/**\r
* @title FlashArbitrageHardcoded\r
* @notice 硬编码 Pool 地址版本 - 100% 能部署成功\r
* @dev 不通过 Provider.getPool() 获取地址,直接硬编码\r
* \r
* 优点:\r
* - 构造函数不做外部调用,Gas 估算必定成功\r
* - 部署更快,Gas 更便宜\r
* - 代码更简单\r
* \r
* 注意:\r
* - 如果 Aave 升级 Pool,需要重新部署合约\r
* - Pool 地址截至 2025年11月1日有效\r
*/\r
\r
interface IPool {\r
function flashLoanSimple(\r
address receiverAddress,\r
address asset,\r
uint256 amount,\r
bytes calldata params,\r
uint16 referralCode\r
) external;\r
}\r
\r
interface IERC20 {\r
function balanceOf(address account) external view returns (uint256);\r
function transfer(address recipient, uint256 amount) external returns (bool);\r
function approve(address spender, uint256 amount) external returns (bool);\r
}\r
\r
interface IUniswapV2Router02 {\r
function swapExactTokensForTokens(\r
uint amountIn,\r
uint amountOutMin,\r
address[] calldata path,\r
address to,\r
uint deadline\r
) external returns (uint[] memory amounts);\r
}\r
\r
contract FlashArbitrageHardcoded {\r
// ===== 状态变量 =====\r
address public owner;\r
\r
// 硬编码所有关键地址(Ethereum Mainnet)\r
address public constant POOL = 0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2; // Aave V3 Pool\r
address public constant UNISWAP = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;\r
address public constant SUSHISWAP = 0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506;\r
address public constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;\r
address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;\r
\r
// ===== 事件 =====\r
event ContractDeployed(address indexed owner, uint256 timestamp);\r
event FlashLoanRequested(address indexed initiator, uint256 amount, bool buyOnUni);\r
event ArbitrageExecuted(uint256 borrowAmount, uint256 profit);\r
event TokensWithdrawn(address indexed token, uint256 amount);\r
\r
// ===== 修饰符 =====\r
modifier onlyOwner() {\r
require(msg.sender == owner, "Only owner");\r
_;\r
}\r
\r
// ===== 构造函数 =====\r
/**\r
* @notice 构造函数 - 超级简单,无外部调用\r
* @dev 无参数,所有地址都是硬编码的\r
*/\r
constructor() {\r
owner = msg.sender;\r
emit ContractDeployed(owner, block.timestamp);\r
}\r
\r
// ===== 主要功能 =====\r
\r
/**\r
* @notice 请求闪电贷并执行套利\r
* @param amount 借贷金额(USDT,6 decimals)\r
* @param buyOnUni true = Uni买/Sushi卖, false = Sushi买/Uni卖\r
*/\r
function requestFlashLoan(uint256 amount, bool buyOnUni) external onlyOwner {\r
require(amount > 0, "Amount must be greater than zero");\r
\r
bytes memory params = abi.encode(buyOnUni);\r
emit FlashLoanRequested(msg.sender, amount, buyOnUni);\r
\r
IPool(POOL).flashLoanSimple(\r
address(this),\r
USDT,\r
amount,\r
params,\r
0\r
);\r
}\r
\r
/**\r
* @notice Aave 闪电贷回调函数\r
*/\r
function executeOperation(\r
address asset,\r
uint256 amount,\r
uint256 premium,\r
address initiator,\r
bytes calldata params\r
) external returns (bool) {\r
require(msg.sender == POOL, "Caller must be Pool");\r
require(initiator == address(this), "Invalid initiator");\r
require(asset == USDT, "Asset must be USDT");\r
\r
bool buyOnUni = abi.decode(params, (bool));\r
\r
// 执行套利\r
_performArbitrage(amount, buyOnUni);\r
\r
// 归还贷款\r
uint256 repayAmount = amount + premium;\r
IERC20(USDT).approve(POOL, repayAmount);\r
\r
// 提取利润\r
uint256 balance = IERC20(USDT).balanceOf(address(this));\r
if (balance > repayAmount) {\r
uint256 profit = balance - repayAmount;\r
IERC20(USDT).transfer(owner, profit);\r
emit ArbitrageExecuted(amount, profit);\r
} else {\r
emit ArbitrageExecuted(amount, 0);\r
}\r
\r
return true;\r
}\r
\r
/**\r
* @notice 内部函数:执行套利交易\r
*/\r
function _performArbitrage(uint256 amount, bool buyOnUni) internal {\r
address buyRouter = buyOnUni ? UNISWAP : SUSHISWAP;\r
address sellRouter = buyOnUni ? SUSHISWAP : UNISWAP;\r
\r
// 步骤1:USDT -> WETH\r
IERC20(USDT).approve(buyRouter, amount);\r
\r
address[] memory path = new address[](2);\r
path[0] = USDT;\r
path[1] = WETH;\r
\r
uint[] memory amounts = IUniswapV2Router02(buyRouter).swapExactTokensForTokens(\r
amount,\r
1,\r
path,\r
address(this),\r
block.timestamp + 300\r
);\r
\r
// 步骤2:WETH -> USDT\r
uint256 wethAmount = amounts[1];\r
IERC20(WETH).approve(sellRouter, wethAmount);\r
\r
path[0] = WETH;\r
path[1] = USDT;\r
\r
IUniswapV2Router02(sellRouter).swapExactTokensForTokens(\r
wethAmount,\r
1,\r
path,\r
address(this),\r
block.timestamp + 300\r
);\r
}\r
\r
// ===== 管理功能 =====\r
\r
/**\r
* @notice 提取代币\r
* @param token 代币地址(address(0) = ETH)\r
*/\r
function withdraw(address token) external onlyOwner {\r
if (token == address(0)) {\r
uint256 balance = address(this).balance;\r
require(balance > 0, "No ETH");\r
payable(owner).transfer(balance);\r
emit TokensWithdrawn(address(0), balance);\r
} else {\r
uint256 balance = IERC20(token).balanceOf(address(this));\r
require(balance > 0, "No tokens");\r
IERC20(token).transfer(owner, balance);\r
emit TokensWithdrawn(token, balance);\r
}\r
}\r
\r
/**\r
* @notice 紧急提取所有资产\r
*/\r
function emergencyWithdraw() external onlyOwner {\r
// USDT\r
uint256 usdtBal = IERC20(USDT).balanceOf(address(this));\r
if (usdtBal > 0) IERC20(USDT).transfer(owner, usdtBal);\r
\r
// WETH\r
uint256 wethBal = IERC20(WETH).balanceOf(address(this));\r
if (wethBal > 0) IERC20(WETH).transfer(owner, wethBal);\r
\r
// ETH\r
uint256 ethBal = address(this).balance;\r
if (ethBal > 0) payable(owner).transfer(ethBal);\r
}\r
\r
// ===== 查询功能 =====\r
\r
/**\r
* @notice 获取所有合约地址\r
*/\r
function getAddresses() external pure returns (\r
address _pool,\r
address _uniswap,\r
address _sushiswap,\r
address _usdt,\r
address _weth\r
) {\r
return (POOL, UNISWAP, SUSHISWAP, USDT, WETH);\r
}\r
\r
/**\r
* @notice 获取合约余额\r
*/\r
function getBalances() external view returns (\r
uint256 usdtBalance,\r
uint256 wethBalance,\r
uint256 ethBalance\r
) {\r
return (\r
IERC20(USDT).balanceOf(address(this)),\r
IERC20(WETH).balanceOf(address(this)),\r
address(this).balance\r
);\r
}\r
\r
/**\r
* @notice 验证合约状态\r
*/\r
function verifyContract() external view returns (\r
bool isOwnerCorrect,\r
bool hasPoolCode,\r
bool hasUniswapCode,\r
bool hasSushiswapCode\r
) {\r
isOwnerCorrect = (owner != address(0));\r
hasPoolCode = (POOL.code.length > 0);\r
hasUniswapCode = (UNISWAP.code.length > 0);\r
hasSushiswapCode = (SUSHISWAP.code.length > 0);\r
}\r
\r
// ===== 接收 ETH =====\r
receive() external payable {}\r
}\r
\r
/**\r
* @title 使用说明\r
* \r
* 部署:\r
* 1. 在 Remix 编译此合约(0.8.10)\r
* 2. 直接部署,无需任何参数\r
* 3. 部署后调用 verifyContract() 确认一切正常\r
* \r
* 使用:\r
* 1. 调用 getAddresses() 查看所有地址\r
* 2. 调用 getBalances() 查看余额\r
* 3. 调用 requestFlashLoan(amount, buyOnUni) 执行套利\r
* 4. 调用 withdraw(token) 提取利润\r
* \r
* 优点:\r
* - 无参数构造函数,100% 能部署\r
* - 所有地址硬编码,清晰透明\r
* - Gas 便宜,执行快速\r
* \r
* 注意:\r
* - 确保在 Ethereum Mainnet 部署\r
* - 合约需要有 USDT 才能支付闪电贷手续费\r
* - 建议先小额测试\r
*/\r
"
}
},
"settings": {
"optimizer": {
"enabled": false,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
},
"remappings": []
}
}}
Submitted on: 2025-11-01 12:44:09
Comments
Log in to comment.
No comments yet.