Close
Close a leveraged position by withdrawing all its equity: sell or redeem the
position assets, repay the loan including accrued interest and fees, and send
the remaining funds to the recipient. This uses the same withdrawStrategy
method as Manage → Withdraw, with
MAX_UINT256 selecting a full exit.
The Credit Account remains open and empty. Closing the position here means ending its exposure and debt, not permanently closing the Credit Account.
Select the position
Use the connected sdk, owner, and position from
Find your leveraged positions.
Refresh the selected position before preparing the exit.
Prepare the exit
import { MAX_UINT256 } from "@gearbox-protocol/sdk/onchain"; const prepared = await sdk.opportunities.prepare.withdrawStrategy(position, { amount: MAX_UINT256, to: owner, slippage, }); if (!prepared.ok) throw prepared.error;
| Field | Type | Meaning |
|---|---|---|
amount | bigint | MAX_UINT256 requests full repayment and withdrawal of the remaining balances |
to | Address | Recipient of the exit proceeds |
sourceToken Optional | Address | Position token to redeem; defaults to the most valuable non-phantom holding |
slippage Optional | number | Swap slippage in basis points; defaults to 0 |
quotaReserve Optional | number | Extra quota buffer in basis points; defaults to 0 |
For a full exit, tokenOut is ignored: proceeds use the market underlying,
unwrapped on RWA markets. The mF-ONE/frxUSD position returns frxUSD.
No wallet ERC-20 approval is needed.
If the account holds several assets, specify the intended redemption token as
sourceToken.
Choose instant or delayed
A successful preparation means at least one route is available. Select the route explicitly:
const route = "delayed"; // Choose "instant" to exit in one transaction const selected = prepared.data[route]; if (!selected) throw prepared.data.errors[route] ?? new Error("Route unavailable"); const sim = { ok: true as const, data: selected };
| Route | First transaction | When funds reach the recipient |
|---|---|---|
instant | Sells assets, repays debt, and withdraws the remainder | In that transaction |
delayed | Requests redemption and records the full-exit intent | After settlement, when you submit the finalization transaction |
A delayed exit is complete only after claiming, repayment, and withdrawal. See how delayed operations work for the lifecycle and the role of the recorded intent.
For delayed, selected.state projects the completed exit;
selected.delayed.afterRequest describes the account after the request itself.
Debt remains outstanding and the account can still be liquidated while waiting.
Save selected.delayed.record with the confirmed request for intent recovery
on older withdrawal compressors.
Build and execute
Continue with position, sim, and owner in
Build, preview, and check.
That produces the transaction's destination, calldata, and value. Signing and
execution belong to your chosen execution system.
For ordinary markets, this SDK flow calls the Credit Facade's
multicall(creditAccount, calls); RWA markets can route through the applicable
RWA factory. Use the transaction destination returned by the builder.
For a delayed exit, the first multicall submits
the redemption; a later multicall claims the proceeds, repays debt, and
withdraws the remainder. You do not call closeCreditAccount for this flow.
For the delayed route, monitor settlement after the request confirms, then claim and complete the operation.
Confirm completion
After an instant exit or delayed finalization confirms, refresh the position and
withdrawals. Verify that debt is zero, the intended request has no pending or
claimable balance, no material position assets or other redemption claims
remain, and the expected funds reached to. Check the receipt and
fresh per-chain read metadata; empty withdrawal arrays alone do not prove an exit.
The Credit Account may remain in the position list with zero debt and only dust balances. Monitor debt, balances, and transfers rather than waiting for the account to disappear.