> ## Documentation Index
> Fetch the complete documentation index at: https://docs.legend.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Plan Migrate

> Create a plan to migrate a supply position between protocols

# Plan Migrate

Creates a plan to migrate a supply position from one DeFi protocol to another. This atomically withdraws from the source protocol and deposits into the destination protocol. Returns a plan ID and an EIP-712 digest that must be signed to authorize execution.

## Request

```
POST /accounts/:account_id/plan/migrate
```

### Path parameters

| Parameter    | Type   | Description        |
| ------------ | ------ | ------------------ |
| `account_id` | string | The sub-account ID |

### Body parameters

| Parameter                      | Type    | Required | Description                                                                                    |
| ------------------------------ | ------- | -------- | ---------------------------------------------------------------------------------------------- |
| `amount`                       | string  | Yes      | Amount to migrate in the asset's smallest unit                                                 |
| `asset`                        | string  | Yes      | Asset to migrate (e.g., `"USDC"`)                                                              |
| `from_protocol`                | string  | Yes      | Source protocol: `"compound"`, `"aave"`, or `"morpho_vault"`                                   |
| `to_protocol`                  | string  | Yes      | Destination protocol: `"compound"`, `"aave"`, or `"morpho_vault"`                              |
| `network`                      | string  | Yes      | Target network (e.g., `"base"`, `"mainnet"`). Funds on other chains are bridged automatically. |
| `from_market`                  | string  | No       | Source market reference (required for `morpho_vault`)                                          |
| `to_market`                    | string  | No       | Destination market reference (required for `morpho_vault`)                                     |
| `migrate_only_supply_balances` | boolean | No       | If `true`, only migrate supply balances (default: `false`)                                     |

## Examples

### Compound to Aave

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://prime-api.legend.xyz/accounts/acc_2o0yiljtp378/plan/migrate \
    -H "Authorization: Bearer $LEGEND_QUERY_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "amount": "1000000",
      "asset": "USDC",
      "from_protocol": "compound",
      "to_protocol": "aave",
      "network": "base"
    }'
  ```

  ```typescript TypeScript theme={null}
  const plan = await client.plan.migrate("acc_2o0yiljtp378", {
    amount: "1000000",
    asset: "USDC",
    fromProtocol: "compound",
    toProtocol: "aave",
    network: "base",
  });
  ```

  ```python Python theme={null}
  plan = await client.plan.migrate(
      "acc_2o0yiljtp378",
      amount="1000000",
      asset="USDC",
      from_protocol="compound",
      to_protocol="aave",
      network="base",
  )
  ```

  ```rust Rust theme={null}
  use legend_client::MigrateParams;

  let plan = client.plan.migrate("acc_2o0yiljtp378", &MigrateParams {
      amount: "1000000".into(),
      asset: "USDC".into(),
      from_protocol: "compound".into(),
      to_protocol: "aave".into(),
      network: "base".into(),
      ..Default::default()
  }).await?;
  ```
</CodeGroup>

### Aave to Morpho Vault

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://prime-api.legend.xyz/accounts/acc_2o0yiljtp378/plan/migrate \
    -H "Authorization: Bearer $LEGEND_QUERY_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "amount": "1000000",
      "asset": "USDC",
      "from_protocol": "aave",
      "to_protocol": "morpho_vault",
      "to_market": "steakhouse-usdc",
      "network": "base"
    }'
  ```

  ```typescript TypeScript theme={null}
  const plan = await client.plan.migrate("acc_2o0yiljtp378", {
    amount: "1000000",
    asset: "USDC",
    fromProtocol: "aave",
    toProtocol: "morpho_vault",
    toMarket: "steakhouse-usdc",
    network: "base",
  });
  ```

  ```python Python theme={null}
  plan = await client.plan.migrate(
      "acc_2o0yiljtp378",
      amount="1000000",
      asset="USDC",
      from_protocol="aave",
      to_protocol="morpho_vault",
      to_market="steakhouse-usdc",
      network="base",
  )
  ```

  ```rust Rust theme={null}
  use legend_client::MigrateParams;

  let plan = client.plan.migrate("acc_2o0yiljtp378", &MigrateParams {
      amount: "1000000".into(),
      asset: "USDC".into(),
      from_protocol: "aave".into(),
      to_protocol: "morpho_vault".into(),
      to_market: Some("steakhouse-usdc".into()),
      network: "base".into(),
      ..Default::default()
  }).await?;
  ```
</CodeGroup>

```json Response (201 Created) theme={null}
{
  "plan_id": "pln_71mcui082png",
  "details": {
    "quark_operation_actions": [...],
    "eip712_data": {
      "digest": "0x8a3f...b7c2",
      "domain_separator": "0x...",
      "hash_struct": "0x..."
    }
  },
  "expires_at": "2025-06-15T10:32:00Z"
}
```

### Response fields

| Field                        | Type   | Description                                                 |
| ---------------------------- | ------ | ----------------------------------------------------------- |
| `plan_id`                    | string | Unique identifier for this plan (`pln_` prefix)             |
| `details`                    | object | Transaction details including EIP-712 signing data          |
| `details.eip712_data.digest` | string | The hash to sign with the account's signer key              |
| `expires_at`                 | string | ISO 8601 timestamp — plan must be executed before this time |

## Errors

| Status | Code             | Description                                                                                      |
| ------ | ---------------- | ------------------------------------------------------------------------------------------------ |
| 400    | `invalid_params` | Missing required parameters, unsupported protocol, or missing market reference for morpho\_vault |
| 400    | `plan_failed`    | Could not generate a valid plan (e.g., insufficient balance in source)                           |
| 400    | `no_wallet`      | Account has no wallets                                                                           |
| 408    | `timeout`        | Folio computation timed out                                                                      |

## Notes

* Plans expire after 2 minutes. Sign and execute via [Execute Plan](/api-reference/plan-execute) before expiry.
* The withdrawal and deposit happen atomically in a single transaction.
* When using `morpho_vault` as source or destination, you must provide the corresponding `from_market` or `to_market` parameter.
* Set `migrate_only_supply_balances` to `true` to prevent migrating reward or other non-supply balances.
