{
  "id": "trail.morpho.unwind",
  "schemaVersion": "0.1.0",
  "name": "Close leveraged position on Morpho (flashloan)",
  "summary": "Atomically close a Morpho leveraged position via flashloan: borrow the debt amount, repay the market, withdraw the collateral, swap collateral → borrowedAsset, repay the flashloan. Net: position fully closed in one tx.",
  "family": "syusd",
  "chains": ["ethereum", "base"],
  "params": [
    { "name": "borrowedAsset",   "type": "address",       "required": true, "description": "Asset borrowed against the position (matches the original leverage open)." },
    { "name": "collateralAsset", "type": "address",       "required": true, "description": "Collateral asset to withdraw." },
    { "name": "market",          "type": "morpho_market", "required": true, "description": "Morpho market currently holding the position." },
    { "name": "repayAmount",     "type": "uint256",       "required": true, "description": "Amount of borrowedAsset to flashloan + repay (= the full debt position to fully close)." },
    { "name": "collateralAmount","type": "uint256",       "required": true, "description": "Amount of collateral to withdraw (= the full supplied collateral to fully close)." }
  ],
  "execution": {
    "mode": "atomic",
    "flashloanStep": 0,
    "steps": [
      {
        "kind": "leaf",
        "id": "fl",
        "description": "Flashloan borrowedAsset to settle the debt.",
        "protocol": "morphoFlashloanAdapter",
        "action": "flashloan",
        "args": { "asset": "$borrowedAsset", "amount": "$repayAmount" }
      },
      {
        "kind": "leaf",
        "description": "Approve Morpho to pull the repay amount.",
        "protocol": "erc20",
        "action": "approve",
        "args": { "asset": "$borrowedAsset", "spender": "morphoBlue" }
      },
      {
        "kind": "leaf",
        "description": "Repay the market debt — frees the collateral position.",
        "protocol": "morphoBlue",
        "action": "repay",
        "args": { "market": "$market", "amount": "$repayAmount", "onBehalfOf": "$vault" }
      },
      {
        "kind": "leaf",
        "description": "Withdraw the now-unencumbered collateral.",
        "protocol": "morphoBlue",
        "action": "withdrawCollateral",
        "args": { "market": "$market", "amount": "$collateralAmount", "onBehalfOf": "$vault", "receiver": "$vault" }
      },
      {
        "kind": "leaf",
        "description": "Approve the DEX adapter to spend the collateral.",
        "protocol": "magpie",
        "action": "approve",
        "args": { "asset": "$collateralAsset" }
      },
      {
        "kind": "leaf",
        "id": "swap",
        "description": "Swap collateralAsset → borrowedAsset to repay the flashloan.",
        "protocol": "magpie",
        "action": "swap_or_mint",
        "args": {
          "in":  "$collateralAsset",
          "out": "$borrowedAsset",
          "amountIn": "$collateralAmount"
        }
      }
    ]
  },
  "preconditions": [
    "Vault holds the supply+debt position described by $market.",
    "Vault has flashloan + repay + withdrawCollateral + swap leaves enabled for the involved assets."
  ],
  "risks": [
    "If the swap returns less than $repayAmount of borrowedAsset, the flashloan cannot repay and the entire tx reverts (intended).",
    "Oracle staleness during the repay step may push HF below 1 momentarily — only matters if repay reverts."
  ],
  "llm_doc": "Counterpart to trail.morpho.leveraged_position. Use this when the user wants to fully unwind / CLOSE / exit a Morpho leveraged position. Flashloan → repay → withdraw → swap-back → repay-flashloan, all in one tx. For PARTIAL deleveraging, the strategist should call this with proportional $repayAmount and $collateralAmount (caller responsibility — this trail doesn't compute the proportional values)."
}
