{
    "id": "trail.aave.leveraged_position",
    "schemaVersion": "0.1.0",
    "name": "Open leveraged position on Aave V3 (flashloan)",
    "summary": "Aave V3 equivalent of trail.morpho.leveraged_position. Flashloan borrowedAsset → swap to collateral → supply collateral → borrow back to repay. Atomic.",
    "family": "btc-carry",
    "chains": ["ethereum", "base", "arbitrum", "optimism"],
    "params": [
        { "name": "borrowedAsset", "type": "address", "required": true, "description": "Asset to flashloan + ultimately borrow on Aave (e.g. USDC)." },
        { "name": "collateralAsset", "type": "address", "required": true, "description": "Aave-supported collateral asset (e.g. cbBTC, WBTC, wstETH)." },
        { "name": "flashAmount", "type": "uint256", "required": true, "description": "Flashloan size in borrowedAsset base units." },
        { "name": "interestRateMode", "type": "uint16", "required": false, "default": 2, "description": "Aave V3 interest rate mode: 1=stable, 2=variable. Default 2 (variable)." }
    ],
    "execution": {
        "mode": "atomic",
        "flashloanStep": 0,
        "steps": [
            {
                "kind": "leaf",
                "id": "fl",
                "description": "Aave V3 simpleFlashLoan of borrowedAsset.",
                "protocol": "aaveFlashloanAdapter",
                "action": "flashloan",
                "args": { "asset": "$borrowedAsset", "amount": "$flashAmount" }
            },
            {
                "kind": "leaf",
                "description": "Approve the DEX adapter to spend borrowedAsset.",
                "protocol": "magpie",
                "action": "approve",
                "args": { "asset": "$borrowedAsset" }
            },
            {
                "kind": "leaf",
                "id": "swap",
                "description": "Swap borrowedAsset → collateralAsset via the DEX adapter.",
                "protocol": "magpie",
                "action": "swap_or_mint",
                "args": { "in": "$borrowedAsset", "out": "$collateralAsset", "amountIn": "$flashAmount" }
            },
            {
                "kind": "leaf",
                "description": "Approve Aave V3 Pool to pull the collateral.",
                "protocol": "erc20",
                "action": "approve",
                "args": { "asset": "$collateralAsset", "spender": "aaveV3Pool" }
            },
            {
                "kind": "leaf",
                "description": "Supply collateral to Aave V3.",
                "protocol": "aaveV3",
                "action": "supply",
                "args": { "asset": "$collateralAsset", "amount": "$swap.amountOut", "onBehalfOf": "$vault" }
            },
            {
                "kind": "leaf",
                "description": "Borrow borrowedAsset back from Aave to repay the flashloan.",
                "protocol": "aaveV3",
                "action": "borrow",
                "args": { "asset": "$borrowedAsset", "amount": "$flashAmount", "interestRateMode": "$interestRateMode", "onBehalfOf": "$vault" }
            }
        ]
    },
    "preconditions": [
        "Vault has aaveFlashloanAdapter clone for $borrowedAsset.",
        "Vault has approve+swap+supply+borrow leaves enabled for both assets on Aave V3.",
        "Aave V3 has the borrowedAsset/collateralAsset pair enabled (most majors are; check on chain)."
    ],
    "risks": [
        "Aave V3 oracle staleness or e-mode misconfig can cause borrow to revert or land at unexpected HF.",
        "Variable interest rate (mode 2) can spike if utilization jumps. Mode 1 (stable) is opt-in but typically capped."
    ],
    "llm_doc": "Aave V3 flavor of leveraged-position-open. Use this for BTC/ETH carry trades where the collateral side is on Aave (cbBTC, WBTC, wstETH, etc.) and the borrowed side is a stable. Same mental model as trail.morpho.leveraged_position but routes through aaveFlashloanAdapter + aaveV3 pool. Available on all 4 chains; Morpho-only chains can't use this."
}
