Skip to main content

Deleveraging a multiply position reduces your leverage by selling collateral to repay debt in a single atomic transaction. This operation uses flash loans and KSwap to convert TSLAx collateral into USDC to repay debt without requiring external funds.

Multiply positions use MultiplyObligation which derives a unique PDA (Program Derived Address) based on your wallet, market, collateral token (TSLAx), and debt token (USDC). Each collateral/debt pair creates a separate obligation address, allowing you to have multiple multiply positions simultaneously.

Deleverage Multiply Position with xStocks

Reduce leverage on a TSLAx multiply position by selling collateral to repay USDC debt.
1

Import Dependencies

Import the required packages for Solana RPC communication, Kamino SDK operations, KSwap routing, Scope oracle, and transaction building.
2

Load Configuration and Initialize SDKs

Load the keypair and initialize RPC connections, market, Scope oracle, and KSwap SDK.
3

Find TSLAx Reserve and Fetch Multiply LUTs

Dynamically discover the TSLAx reserve and fetch multiply-specific lookup tables from Kamino’s CDN.
4

Configure Deleverage Parameters

Set the withdrawal amount and slippage tolerance.
Some assets may require higher slippage than others.
5

Get User Lookup Table

Retrieve the user-specific lookup table created during the first multiply deposit.
6

Fetch Multiply Obligation and Position Data

Create the multiply obligation type, derive its PDA, and fetch current position data.
The obligation PDA is derived from the market address, wallet address, collateral mint (TSLAx), and debt mint (USDC). This means a TSLAx/USDC multiply position has a different address than a NVDAx/USDC multiply position for the same wallet.
7

Setup Scope Oracle and Price Data

Get Scope oracle refresh instructions and price ratios for the swap.
8

Build Deleverage Instructions with Multiple Routes

Set compute budget and build withdrawal with leverage instructions using KSwap for multi-route optimization.
getWithdrawWithLeverageIxs handles the complex flow: flash borrow debt token → repay debt → withdraw freed collateral → swap collateral to debt token → repay flash loan. All in one atomic transaction.
9

Simulate Routes and Select Best

Prepare lookup tables, simulate all routes, and select the best performing route based on price.
Simulation-based route selection ensures the transaction will succeed before sending it. Routes that fail simulation are filtered out, and the best price among passing routes is selected.
10

Build, Sign, and Send Transaction

Wait briefly to avoid rate limiting, get a fresh blockhash, compress the transaction with LUTs, and send it.
The deleverage transaction is complete. Your leverage is reduced, debt is partially repaid, and remaining value is available as USDC or collateral based on the selectedTokenMint parameter.

Borrow and Multiply SDK Methods

Multiply operations use MultiplyObligation. Vanilla operations use VanillaObligation. The obligation type determines which PDA is derived for your position.