Skip to content
Verified contractExactMIT

MevscanExecutor

Address0x43b977fcf4713b3a86a2e4e705eda9b262e1bdea
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,519,804
Creation transaction
0x5222f5fad5…4492d524e4

MevscanExecutor.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

import {VeloraSimulationAsset as Asset, VeloraSimulationAave as LendingPool} from "./VeloraSimulation.sol";

/// @notice Ethereum mainnet Aave V3 executor for Velora Augustus 6.2 ERC-20 loops.
contract MevscanExecutor {
    address public constant AAVE = 0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2;
    address public constant AUGUSTUS = 0x6A000F20005980200259B80c5102003040001068;
    address public constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
    address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

    address public immutable owner;
    address public operator;
    address public immutable profitRecipient;
    bytes32 private pendingLoan;
    bool private executing;
    bool public paused;

    struct Route {
        address[] tokens;
        uint256[] inputs;
        bytes[] swaps;
    }

    error Unauthorized();
    error Unavailable();
    error InvalidRoute();
    error InvalidCallback();
    error TokenCallFailed(address token);
    error InsufficientProceeds(uint256 leg, uint256 available, uint256 required);
    error InsufficientProfit(uint256 available, uint256 required);
    error SwapFailed(uint256 leg, bytes reason);

    event OperatorChanged(address indexed nextOperator);
    event PauseChanged(bool paused);
    event Executed(address indexed asset, uint256 amount, uint256 profit, address indexed recipient);

    constructor(address owner_, address operator_, address profitRecipient_) {
        if (block.chainid != 1 || owner_ == address(0) || operator_ == address(0)
            || profitRecipient_ == address(0) || profitRecipient_ == address(this)) revert InvalidRoute();
        owner = owner_;
        operator = operator_;
        profitRecipient = profitRecipient_;
    }

    modifier onlyOwner() {
        if (msg.sender != owner) revert Unauthorized();
        _;
    }

    function setOperator(address nextOperator) external onlyOwner {
        if (nextOperator == address(0)) revert InvalidRoute();
        operator = nextOperator;
        emit OperatorChanged(nextOperator);
    }

    function setPaused(bool value) external onlyOwner {
        paused = value;
        emit PauseChanged(value);
    }

    /// @param minProfit Minimum increase in the loan asset, in raw units.
    /// @param lastValidBlock Last Ethereum block at which this quote may run.
    /// @dev The operator includes gas and its desired net return in minProfit.
    function execute(address asset, uint256 amount, uint256 minProfit, uint256 lastValidBlock, Route calldata route)
        external returns (uint256 profit)
    {
        if (msg.sender != operator) revert Unauthorized();
        if (paused || executing || block.number > lastValidBlock) revert Unavailable();
        uint256 count = route.swaps.length;
        if ((asset != USDC && asset != WETH) || amount == 0 || minProfit == 0
            || count < 2 || count > 8 || route.inputs.length != count
            || route.tokens.length != count + 1 || route.tokens[0] != asset
            || route.tokens[count] != asset || route.inputs[0] != amount) revert InvalidRoute();

        executing = true;
        uint256 beforeBalance = Asset(asset).balanceOf(address(this));
        bytes memory data = abi.encode(route, minProfit);
        pendingLoan = keccak256(abi.encode(asset, amount, data));
        LendingPool(AAVE).flashLoanSimple(address(this), asset, amount, data, 0);
        if (pendingLoan != bytes32(0)) revert InvalidCallback();

        uint256 balance = Asset(asset).balanceOf(address(this));
        if (balance < beforeBalance + minProfit) {
            revert InsufficientProfit(balance > beforeBalance ? balance - beforeBalance : 0, minProfit);
        }
        profit = balance - beforeBalance;
        tokenCall(asset, abi.encodeWithSignature("transfer(address,uint256)", profitRecipient, profit));
        executing = false;
        emit Executed(asset, amount, profit, profitRecipient);
    }

    function executeOperation(address asset, uint256 amount, uint256 premium, address initiator, bytes calldata data)
        external returns (bool)
    {
        if (msg.sender != AAVE || initiator != address(this) || !executing
            || pendingLoan == bytes32(0) || pendingLoan != keccak256(abi.encode(asset, amount, data))) {
            revert InvalidCallback();
        }
        pendingLoan = bytes32(0);
        (Route memory route, uint256 minProfit) = abi.decode(data, (Route, uint256));
        uint256 available = amount;
        for (uint256 i; i < route.swaps.length; ++i) {
            uint256 input = route.inputs[i];
            address source = route.tokens[i];
            address target = route.tokens[i + 1];
            if (source == target || input == 0) revert InvalidRoute();
            if (input > available) revert InsufficientProceeds(i, available, input);

            uint256 beforeBalance = Asset(target).balanceOf(address(this));
            tokenCall(source, abi.encodeWithSignature("approve(address,uint256)", AUGUSTUS, input));
            (bool ok, bytes memory reason) = AUGUSTUS.call(route.swaps[i]);
            if (!ok) revert SwapFailed(i, reason);
            tokenCall(source, abi.encodeWithSignature("approve(address,uint256)", AUGUSTUS, 0));
            available = Asset(target).balanceOf(address(this)) - beforeBalance;
        }
        uint256 repayment = amount + premium;
        if (available < repayment + minProfit) {
            revert InsufficientProfit(available > repayment ? available - repayment : 0, minProfit);
        }
        tokenCall(asset, abi.encodeWithSignature("approve(address,uint256)", AAVE, repayment));
        return true;
    }

    function tokenCall(address token, bytes memory data) private {
        (bool ok, bytes memory result) = token.call(data);
        if (!ok || (result.length != 0 && (result.length != 32 || !abi.decode(result, (bool))))) {
            revert TokenCallFailed(token);
        }
    }
}

VeloraSimulation.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

interface VeloraSimulationAsset {
    function balanceOf(address account) external view returns (uint256);
}

interface VeloraSimulationAave {
    function flashLoanSimple(address receiver, address asset, uint256 amount, bytes calldata data, uint16 code) external;
}

// Runtime for read-only Ethereum eth_call state overrides.
contract VeloraSimulation {
    address constant AAVE = 0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2;
    address constant AUGUSTUS = 0x6A000F20005980200259B80c5102003040001068;

    function run(address asset, uint256 amount, bytes calldata route) external returns (uint256, uint256) {
        uint256 startGas = gasleft();
        uint256 beforeBalance = VeloraSimulationAsset(asset).balanceOf(address(this));
        VeloraSimulationAave(AAVE).flashLoanSimple(address(this), asset, amount, route, 0);
        return (VeloraSimulationAsset(asset).balanceOf(address(this)) - beforeBalance, startGas - gasleft());
    }

    function executeOperation(address asset, uint256 amount, uint256 fee, address initiator, bytes calldata data)
        external returns (bool)
    {
        require(msg.sender == AAVE && initiator == address(this), "Invalid Aave callback");
        (address[] memory tokens, uint256[] memory inputs, bytes[] memory swaps) =
            abi.decode(data, (address[], uint256[], bytes[]));
        uint256 count = swaps.length;
        require(count >= 2 && count <= 8 && inputs.length == count && tokens.length == count + 1, "Invalid route sizes");
        require(tokens[0] == asset && tokens[count] == asset && inputs[0] == amount, "Invalid loan route");
        uint256 available = amount;
        for (uint256 i; i < count; ++i) {
            require(inputs[i] <= available && tokens[i] != tokens[i + 1], "Insufficient leg proceeds");
            approve(tokens[i], AUGUSTUS, inputs[i]);
            uint256 beforeBalance = VeloraSimulationAsset(tokens[i + 1]).balanceOf(address(this));
            (bool success, bytes memory reason) = AUGUSTUS.call(swaps[i]);
            if (!success) {
                assembly ("memory-safe") { revert(add(reason, 32), mload(reason)) }
            }
            approve(tokens[i], AUGUSTUS, 0);
            available = VeloraSimulationAsset(tokens[i + 1]).balanceOf(address(this)) - beforeBalance;
        }
        require(available >= amount + fee, "Repayment shortfall");
        approve(asset, AAVE, amount + fee);
        return true;
    }

    function approve(address asset, address spender, uint256 amount) private {
        (bool success, bytes memory result) =
            asset.call(abi.encodeWithSignature("approve(address,uint256)", spender, amount));
        require(success && (result.length == 0 || abi.decode(result, (bool))), "Token approval failed");
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.