Plans
Plan Unloop Long
Create a plan to close or reduce a leveraged long position
POST
/
accounts
/
{account_id}
/
plan
/
unloop-long
Plan Unloop Long
curl --request POST \
--url https://api.example.com/accounts/{account_id}/plan/unloop-longimport requests
url = "https://api.example.com/accounts/{account_id}/plan/unloop-long"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://api.example.com/accounts/{account_id}/plan/unloop-long', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/accounts/{account_id}/plan/unloop-long",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/accounts/{account_id}/plan/unloop-long"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/accounts/{account_id}/plan/unloop-long")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/accounts/{account_id}/plan/unloop-long")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_bodyPlan Unloop Long
Creates a plan to close or reduce a leveraged long position on a Morpho market. Unlooping withdraws the exposure asset, swaps it for the backing asset, and repays the loan — repeatedly — to deleverage. Returns a plan ID and an EIP-712 digest that must be signed to authorize execution.Request
POST /accounts/:account_id/plan/unloop-long
Path parameters
| Parameter | Type | Description |
|---|---|---|
account_id | string | The sub-account ID |
Body parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
exposure_asset | string | Yes | Long exposure asset (e.g., "WETH") |
backing_asset | string | Yes | Loan currency asset (e.g., "USDC") |
market_id | string | Yes | Morpho market ID (0x-prefixed 32-byte hex) |
exposure_amount | string | Yes | Amount of exposure to close in the asset’s smallest unit |
backing_amount_to_exit | string | Yes | Target backing asset amount to withdraw after deleveraging |
min_swap_backing_amount | string | Yes | Minimum acceptable backing asset from swaps (slippage protection) |
pool_fee | integer | Yes | Uniswap pool fee tier (e.g., 500, 3000, 10000) |
network | string | Yes | Network name (e.g., "base", "mainnet") |
Examples
curl -X POST https://prime-api.legend.xyz/accounts/acc_2o0yiljtp378/plan/unloop-long \
-H "Authorization: Bearer $LEGEND_QUERY_KEY" \
-H "Content-Type: application/json" \
-d '{
"exposure_asset": "WETH",
"backing_asset": "USDC",
"market_id": "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc",
"exposure_amount": "1000000000000000000",
"backing_amount_to_exit": "500000000",
"min_swap_backing_amount": "490000000",
"pool_fee": 500,
"network": "base"
}'
const plan = await client.plan.unloopLong("acc_2o0yiljtp378", {
exposureAsset: "WETH",
backingAsset: "USDC",
marketId: "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc",
exposureAmount: "1000000000000000000",
backingAmountToExit: "500000000",
minSwapBackingAmount: "490000000",
poolFee: 500,
network: "base",
});
plan = await client.plan.unloop_long(
"acc_2o0yiljtp378",
exposure_asset="WETH",
backing_asset="USDC",
market_id="0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc",
exposure_amount="1000000000000000000",
backing_amount_to_exit="500000000",
min_swap_backing_amount="490000000",
pool_fee=500,
network="base",
)
use legend_client::UnloopLongParams;
let plan = client.plan.unloop_long("acc_2o0yiljtp378", &UnloopLongParams {
exposure_asset: "WETH".into(),
backing_asset: "USDC".into(),
market_id: "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc".into(),
exposure_amount: "1000000000000000000".into(),
backing_amount_to_exit: "500000000".into(),
min_swap_backing_amount: "490000000".into(),
pool_fee: 500,
network: "base".into(),
}).await?;
Response (201 Created)
{
"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 or invalid market ID |
| 400 | plan_failed | Could not generate a valid plan |
| 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 before expiry.
- The
market_idmust match the one used when opening the position with Plan Loop Long. - Set
min_swap_backing_amountto protect against slippage — the transaction will revert if swaps yield less than this amount.
⌘I
Plan Unloop Long
curl --request POST \
--url https://api.example.com/accounts/{account_id}/plan/unloop-longimport requests
url = "https://api.example.com/accounts/{account_id}/plan/unloop-long"
response = requests.post(url)
print(response.text)const options = {method: 'POST'};
fetch('https://api.example.com/accounts/{account_id}/plan/unloop-long', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/accounts/{account_id}/plan/unloop-long",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/accounts/{account_id}/plan/unloop-long"
req, _ := http.NewRequest("POST", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/accounts/{account_id}/plan/unloop-long")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/accounts/{account_id}/plan/unloop-long")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
response = http.request(request)
puts response.read_body