Skip to content
Verified contractExactLGPL-3.0-only

AaveV3Adapter

Address0x5f06a4b5263a06c4e1cde31eab118c06691fb0e5
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 10,000 runs
EVM version
osaka
Via IR
yes
License
LGPL-3.0-only
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,392,176
Creation transaction
0xd78c62e447…c36a0e3cdb

src/flashloan/adapters/AaveV3Adapter.sol

// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.30;

import { FlashloanCallbackLib } from '../FlashloanCallbackLib.sol';
import { FlashloanErrors } from '../FlashloanErrors.sol';
import { FlashloanLeg } from '../FlashloanLeg.sol';
import { FlashloanAction, IFlashloanAdapter, OP_APPROVE_OWED } from '../IFlashloanAdapter.sol';

/// @dev Aave v3 `Pool`: sends `amount` to `receiver`, calls `receiver.executeOperation`, which must
///      return `true`, then pulls `amount + premium` from `receiver`. `initiator` in the callback is
///      the caller of `flashLoanSimple`.
interface IAaveV3Pool {
    function flashLoanSimple(
        address receiverAddress,
        address asset,
        uint256 amount,
        bytes calldata params,
        uint16 referralCode
    )
        external;
}

interface IAaveV3FlashLoanSimpleReceiver {
    function executeOperation(
        address asset,
        uint256 amount,
        uint256 premium,
        address initiator,
        bytes calldata params
    )
        external
        returns (bool);
}

/// @title AaveV3Adapter
/// @custom:version 1.0.0
/// @notice Flashloan adapter for Aave v3 `flashLoanSimple` (and forks with the same interface).
///         Repayment: approve `owed` to the pool, which pulls it.
contract AaveV3Adapter is IFlashloanAdapter {
    function loanCall(
        FlashloanLeg calldata leg,
        address receiver,
        bytes calldata params
    )
        external
        pure
        returns (bytes memory)
    {
        return abi.encodeCall(IAaveV3Pool.flashLoanSimple, (receiver, leg.token, leg.amount, params, 0));
    }

    function parseCallback(
        address receiver,
        bytes calldata callback
    )
        external
        pure
        returns (uint256, uint256, uint256, bytes[] memory, bytes memory)
    {
        if (callback.length < 4 + 160 || bytes4(callback) != IAaveV3FlashLoanSimpleReceiver.executeOperation.selector) {
            revert FlashloanErrors.FlashloanCallbackRejected();
        }
        (address asset, uint256 amount, uint256 premium, address initiator) =
            abi.decode(callback[4:132], (address, uint256, uint256, address));
        (uint256 start, uint256 end) = FlashloanCallbackLib.bytesArg(callback, 4);
        (, FlashloanLeg memory leg) = FlashloanCallbackLib.legAt(callback[start:end]);
        if (asset != leg.token || amount != leg.amount || initiator != receiver) {
            revert FlashloanErrors.FlashloanCallbackRejected();
        }
        return (start, end, premium, new bytes[](0), abi.encode(true));
    }

    function repayActions(address, address, uint256) external pure returns (FlashloanAction[] memory actions) {
        actions = new FlashloanAction[](1);
        actions[0] = FlashloanAction({ op: OP_APPROVE_OWED, data: '' });
    }
}

src/flashloan/FlashloanCallbackLib.sol

// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.30;

import { FlashloanErrors } from './FlashloanErrors.sol';
import { FlashloanLeg } from './FlashloanLeg.sol';

/// @title FlashloanCallbackLib
/// @notice Calldata readers for adapters. Both revert `FlashloanCallbackRejected` on malformed input.
library FlashloanCallbackLib {
    /// @notice Locate the contents of the ABI-encoded `bytes` argument at head position `argIndex`
    ///         of `data` (a full call: 4-byte selector, then the arguments).
    function bytesArg(bytes calldata data, uint256 argIndex) internal pure returns (uint256 start, uint256 end) {
        uint256 head = 4 + 32 * argIndex;
        if (data.length < head + 32) revert FlashloanErrors.FlashloanCallbackRejected();
        uint256 offset = uint256(bytes32(data[head:head + 32]));
        if (offset > data.length) revert FlashloanErrors.FlashloanCallbackRejected();
        uint256 lengthAt = 4 + offset;
        if (data.length < lengthAt + 32) revert FlashloanErrors.FlashloanCallbackRejected();
        uint256 length = uint256(bytes32(data[lengthAt:lengthAt + 32]));
        start = lengthAt + 32;
        if (length > data.length - start) revert FlashloanErrors.FlashloanCallbackRejected();
        end = start + length;
    }

    /// @notice Read the leg index and that one leg from `params = uint8(i) ‖ outer calldata`, where
    ///         the outer calldata is the flight's `executeWithFlashloan` or
    ///         `executeWithFlashloanEnabling` call (both carry the legs at head word 1), without
    ///         copying the program.
    function legAt(bytes calldata params) internal pure returns (uint256 i, FlashloanLeg memory leg) {
        // Index byte, selector, and the three argument heads.
        if (params.length < 5 + 96) revert FlashloanErrors.FlashloanCallbackRejected();
        i = uint8(params[0]);
        bytes calldata args = params[5:];
        uint256 offset = uint256(bytes32(args[32:64]));
        if (offset > args.length - 32) revert FlashloanErrors.FlashloanCallbackRejected();
        uint256 count = uint256(bytes32(args[offset:offset + 32]));
        if (i >= count) revert FlashloanErrors.FlashloanCallbackRejected();
        uint256 at = offset + 32 + i * 160;
        if (args.length < at + 160) revert FlashloanErrors.FlashloanCallbackRejected();
        leg = abi.decode(args[at:at + 160], (FlashloanLeg));
    }
}

src/flashloan/FlashloanErrors.sol

// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.30;

/// @title FlashloanErrors
/// @custom:version 1.2.0
/// @notice Errors of the flashloan runtime, its adapters, the governor and the proxy's flashloan
///         entries. Kept out of `VmErrors` so the VM's bytecode does not change with this feature.
library FlashloanErrors {
    /// @notice New flashloans are paused by the governor.
    error FlashloanPaused();
    /// @notice The `runtime` argument is not this proxy's immutable runtime.
    error FlashloanRuntimeMismatch(address runtime);
    /// @notice Zero legs, or more than the runtime supports.
    error FlashloanLegCount(uint256 count);
    /// @notice A flashloan is in flight on this proxy (a second flight, or an adapter change).
    error FlashloanAlreadyActive();
    /// @notice A lender callback failed verification: no callback pending, adapter rejection
    ///         (selector, token, amount, initiator), params offsets, commitment, depth, sender, or
    ///         the principal did not arrive.
    error FlashloanCallbackRejected();
    /// @notice Lender `leg` reported `fee`, above the owner-authorized `maxFee`.
    error FlashloanFeeTooHigh(uint256 leg, uint256 fee, uint256 maxFee);
    /// @notice The governor does not list `adapter`.
    error FlashloanAdapterNotListed(address adapter);
    /// @notice The proxy's owner has not enabled `adapter`, or its code changed since.
    error FlashloanAdapterNotEnabled(address adapter);
    /// @notice The proxy's owner disabled `adapter`. Only `setFlashloanAdapters` or
    ///         `setFlashloanAdaptersBySig` enables it again; a flight never does.
    error FlashloanAdapterDisabled(address adapter);
    /// @notice Enabling an address that has no code.
    error FlashloanAdapterNoCode(address adapter);
    /// @notice The owner approved a code hash for `adapter`, in a signature or by an earlier enable,
    ///         that its current code does not match.
    error FlashloanAdapterCodeMismatch(address adapter);
    /// @notice `adapter` described an invalid repayment.
    error FlashloanAdapterInvalid(address adapter);
    /// @notice Leg `leg` names an adapter other than an earlier leg with the same provider and token.
    error FlashloanLegConflict(uint256 leg);
    /// @notice The owner's signed adapter change expired at `deadline`.
    error FlashloanSignatureExpired(uint256 deadline);
    /// @notice The adapter change is not signed by the proxy's owner with the current nonce.
    error FlashloanSignatureInvalid();
    /// @notice The first lender returned without the program having run.
    error FlashloanIncomplete();
    /// @notice At settlement the proxy holds `available` of `token`, below aggregate `owed`.
    error FlashloanRepaymentShort(address token, uint256 owed, uint256 available);
    /// @notice A lender repaid by approval returned without consuming its allowance; `leg` is the
    ///         first leg of that `(provider, token)` group.
    error FlashloanAllowanceLeft(uint256 leg);
    /// @notice A flight function was called on the runtime directly instead of through a proxy.
    error FlashloanNotDelegated();

    /// @notice Governor: caller is not the owner.
    error GovernorNotOwner();
    /// @notice Governor: caller is neither the owner nor the guardian.
    error GovernorNotOwnerOrGuardian();
    /// @notice Governor: caller is not the pending owner.
    error GovernorNotPendingOwner();
    /// @notice Governor: zero address where an account is required.
    error GovernorZeroAddress();
}

src/flashloan/FlashloanLeg.sol

// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.30;

/// @notice One flashloan leg as the backend encodes it: `(address,address,address,uint256,uint256)`.
///         `adapter` is the stateless contract that describes how to borrow from and repay
///         `provider`; the proxy's owner must have enabled it and the governor must list it.
///         `maxFee` is the owner-authorized ceiling for the lender-reported fee.
struct FlashloanLeg {
    address adapter;
    address provider;
    address token;
    uint256 amount;
    uint256 maxFee;
}

src/flashloan/IFlashloanAdapter.sol

// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.30;

import { FlashloanLeg } from './FlashloanLeg.sol';

/// @dev The proxy calls the leg's `provider` with `data`.
uint8 constant OP_CALL_PROVIDER = 0;
/// @dev The proxy transfers `owed` of the group's token to `provider`; `data` must be empty.
uint8 constant OP_TRANSFER_OWED = 1;
/// @dev The proxy approves `owed` of the group's token to `provider`; `data` must be empty. The
///      allowance must be zero again when the flight ends.
uint8 constant OP_APPROVE_OWED = 2;

/// @notice One step of repaying a `(provider, token)` group.
struct FlashloanAction {
    uint8 op;
    bytes data;
}

/// @title IFlashloanAdapter
/// @notice Describes how to borrow from one lender protocol. The flashloan runtime calls these
///         functions with `STATICCALL` and makes every described call itself, from the proxy, and
///         only to the leg's `provider`; an adapter never runs with the proxy's authority.
interface IFlashloanAdapter {
    /// @notice The calldata that starts the loan of `leg`. The runtime sends it to `leg.provider`.
    /// @param receiver The proxy that borrows (the address the lender must pay and call back).
    /// @param params Opaque bytes the lender must echo unchanged in its callback.
    function loanCall(
        FlashloanLeg calldata leg,
        address receiver,
        bytes calldata params
    )
        external
        view
        returns (bytes memory callData);

    /// @notice Read a callback the proxy received; revert if it is not this adapter's callback for
    ///         the leg its `params` name.
    /// @param receiver The proxy that received the callback.
    /// @param callback The full calldata the lender sent.
    /// @return paramsStart Offset of the echoed `params` inside `callback`.
    /// @return paramsEnd End offset (exclusive) of the echoed `params` inside `callback`.
    /// @return fee The fee the lender charges for this leg.
    /// @return claimCalls Calls to make to the provider before the runtime checks that the principal
    ///         arrived (empty for lenders that pay before calling back).
    /// @return returnData The raw bytes the callback returns to the lender.
    function parseCallback(
        address receiver,
        bytes calldata callback
    )
        external
        view
        returns (
            uint256 paramsStart,
            uint256 paramsEnd,
            uint256 fee,
            bytes[] memory claimCalls,
            bytes memory returnData
        );

    /// @notice The ordered actions that repay `owed` of `token` to `provider`. Exactly one action
    ///         is a payment (`OP_TRANSFER_OWED` or `OP_APPROVE_OWED`).
    function repayActions(
        address provider,
        address token,
        uint256 owed
    )
        external
        view
        returns (FlashloanAction[] memory actions);
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 10000,
    "enabled": true
  },
  "evmVersion": "osaka",
  "remappings": [
    "forge-std/=lib/forge-std/src/",
    "@chimera/=lib/setup-helpers/lib/chimera/src/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "chimera/=lib/chimera/src/",
    "ds-test/=lib/chimera/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "halmos-cheatcodes/=lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "setup-helpers/=lib/setup-helpers/src/",
    "solady/=lib/solady/src/",
    "solady-test/=lib/solady/test/",
    "@recon/=lib/setup-helpers/src/",
    "create3-factory/=lib/create3-factory/",
    "createx-forge/=lib/createx-forge/",
    "solmate/=lib/create3-factory/lib/solmate/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.