{
  "id": "trail.morpho.leveraged_position",
  "schemaVersion": "0.1.0",
  "name": "Open leveraged position on Morpho (flashloan)",
  "summary": "Use a Morpho flashloan to atomically open a leveraged collateral position: borrow, swap to collateral, supply, then borrow back to repay the flashloan. Result: long collateral with debt, leverage = flashAmount / initialDeposit.",
  "family": "syusd",
  "chains": ["ethereum", "base"],
  "params": [
    { "name": "borrowedAsset",    "type": "address",       "required": true,  "description": "Asset to flashloan + ultimately borrow from the market (e.g. USDC, USDe). Must match a flashloan adapter leaf on the vault." },
    { "name": "collateralAsset",  "type": "address",       "required": true,  "description": "Collateral to supply to Morpho (e.g. siUSD, scUSD)." },
    { "name": "market",           "type": "morpho_market", "required": true,  "description": "Morpho market id or full MarketParams 5-tuple matching (collateralAsset/borrowedAsset)." },
    { "name": "flashAmount",      "type": "uint256",       "required": true,  "description": "Flashloan size in borrowedAsset base units. Drives the leverage." },
    { "name": "expectedCollateral","type": "uint256",      "required": false, "description": "Expected collateral after swap, in collateralAsset base units. Optional sanity-check arg for the supply leg." }
  ],
  "execution": {
    "mode": "atomic",
    "flashloanStep": 0,
    "steps": [
      {
        "kind": "leaf",
        "id": "fl",
        "description": "Flashloan borrowedAsset from the vault's dedicated Morpho flashloan adapter.",
        "protocol": "morphoFlashloanAdapter",
        "action": "flashloan",
        "args": { "asset": "$borrowedAsset", "amount": "$flashAmount" }
      },
      {
        "kind": "leaf",
        "description": "Approve the DEX adapter (Magpie) to spend borrowedAsset.",
        "protocol": "magpie",
        "action": "approve",
        "args": { "asset": "$borrowedAsset" }
      },
      {
        "kind": "leaf",
        "id": "swap",
        "description": "Swap borrowedAsset → collateralAsset via the DEX adapter. The mint+stake variant is handled by the same leaf type when target is a yield-bearing wrapper (e.g. scUSD).",
        "protocol": "magpie",
        "action": "swap_or_mint",
        "args": {
          "in":  "$borrowedAsset",
          "out": "$collateralAsset",
          "amountIn": "$flashAmount"
        }
      },
      {
        "kind": "leaf",
        "description": "Approve Morpho Blue to pull the collateral.",
        "protocol": "erc20",
        "action": "approve",
        "args": { "asset": "$collateralAsset", "spender": "morphoBlue" }
      },
      {
        "kind": "leaf",
        "description": "Supply collateral to the Morpho market.",
        "protocol": "morphoBlue",
        "action": "supplyCollateral",
        "args": { "market": "$market", "amount": "$swap.amountOut", "onBehalfOf": "$vault" }
      },
      {
        "kind": "leaf",
        "description": "Borrow borrowedAsset from the same market — proceeds auto-repay the flashloan inside the callback.",
        "protocol": "morphoBlue",
        "action": "borrow",
        "args": { "market": "$market", "amount": "$flashAmount", "onBehalfOf": "$vault", "receiver": "$vault" }
      }
    ]
  },
  "preconditions": [
    "Vault has a morphoFlashloanAdapter clone for $borrowedAsset.",
    "Vault has approve+swap leaves enabled for ($borrowedAsset → $collateralAsset) via Magpie.",
    "Vault has erc20.approve + morphoBlue.supplyCollateral + morphoBlue.borrow leaves enabled for $market.",
    "Target LTV must remain under the market's LLTV after the operation."
  ],
  "risks": [
    "Slippage on the DEX swap can push the resulting LTV above target (use expectedCollateral or a slippage guard).",
    "Oracle staleness on Morpho (especially during volatile windows) may cause the borrow leg to fail.",
    "Depeg risk on collateral asset — if $collateralAsset breaks peg, position becomes liquidatable."
  ],
  "llm_doc": "Open a leveraged spot position on Morpho via a flashloan bracket. The mental model: 'borrow X, swap into collateral, supply collateral, borrow X back' — all atomically. Use this trail when the user wants to ENTER a Morpho-collateralized leveraged position (syUSD-loop style, infini-loop style, sUSDe-loop style). Net leverage = (initialDeposit + flashAmount) / initialDeposit. Counterpart for closing: trail.morpho.unwind."
}
