Skip to main content

A multiply deposit creates a leveraged collateral position in a single, atomic transaction by combining an initial deposit with temporary liquidity.

When a multiply deposit is initiated, the protocol first temporarily borrows the debt token (for example, USDC) through a flash loan. The borrowed amount is swapped into the collateral token (for example, TSLAx) using KSwap (Kamino’s token swap SDK). The resulting collateral is then deposited into Kamino Lend. With the collateral in place, the position borrows the debt token against it, and the borrowed amount is used to repay the flash loan before the transaction completes. The result is a leveraged position with increased exposure to the collateral token and an outstanding debt balance.

Multiply Deposit with xStocks

Create a leveraged position in a single atomic transaction using Klend’s multiply feature and KSwap multi-route swap optimization.
1

Import Dependencies

Use @kamino-finance/kswap-sdk 2.9.0. It’s the last version on @solana/kit 2, which @kamino-finance/klend-sdk 13 uses; 3.x moved to kit 8.
2

Load Configuration and Initialize SDKs

3

Set Up a New Wallet

The leverage functions expect the wallet’s Kamino user metadata to exist. A wallet that has never used Kamino sends one setup transaction first.
4

Load the Multiply Obligation

A Multiply position is its own obligation, addressed by the collateral and debt mints. It’s null until the first deposit creates it.
5

Get Scope Price Refresh Instructions

6

Define the Swap Helpers

The SDK asks for swaps through two callbacks: a quoter that prices the swap and a swapper that returns its instructions. Both use every KSwap route, sorted by output.
priceAInB must be the mid price (amountOut / amountIn, each in token units), before slippage. The SDK applies slippagePct itself, so a guaranteed or minimum-out price counts slippage twice and sizes the position wrong. getBestRoute currently returns no routes, so use getAllRoutes, which in kswap-sdk 2.9.0 takes the router context as a second argument.
7

Build the Leverage Routes

The SDK builds one candidate transaction per swap route: flash borrow USDC, swap it to TSLAx, deposit, borrow USDC, repay the flash loan.
selectedTokenMint is the token you deposit: the debt token (USDC) here, or the collateral token. quoteBufferBps inflates only the amount used for the quote; 0 quotes the exact swap amount and leaves price moves to the slippage setting.
8

Fetch Lookup Tables

Fetch Kamino’s lookup tables for each candidate transaction, one request per candidate.
/luts/find-minimal takes one transaction’s worth of addresses (between 64 and 128), so a single request for all candidates is rejected.
9

Pick the Route That Fits

Compress each candidate with its route’s lookup tables and Kamino’s, then take the best-output one that fits in a transaction.
The best-output route often has too many accounts to fit, even compressed: in testing only one or two of five to ten routes did. The slippage setting covers the gap between the best route’s quote and the one sent.
10

Sign and Send

The 2x TSLAx position is open: TSLAx collateral with a USDC debt, created in one transaction.

Transaction Flow

Asset Flow

1

Initial State

  • Wallet: 10 USDC
  • Position: None
2

Flash Borrow

  • Flash loan: 10 USDC (temporary)
  • Available: 20 USDC total
3

Swap & Deposit

  • Swap 20 USDC to TSLAx via KSwap
  • Deposit TSLAx as collateral
4

Borrow & Repay

  • Borrow 10 USDC against collateral
  • Repay 10 USDC flash loan
5

Final Position

  • Collateral: TSLAx ($20 value)
  • Debt: 10 USDC
  • Net Equity: 10 USDC
  • Leverage: 2x

Key Concepts

Flash loans are uncollateralized loans that must be borrowed and repaid within the same transaction. They enable:
  • Zero upfront capital: Borrow large amounts without collateral
  • Atomic execution: All operations succeed or fail together
  • Leverage creation: Bootstrap leveraged positions in one transaction
  • Risk-free: Transaction reverts if flash loan cannot be repaid
The MultiplyObligation is a specialized obligation type that:
  • Defines position type: Multiply vs Leverage vs Vanilla
  • Derives unique address: Via PDA using collateral/debt mints
  • Supports multiple positions: Create different obligations with same tokens using ID parameter
  • Creates new obligation: When none exists (indicated by obligation: null)
KSwap provides optimal swap execution through:
  • Multi-DEX routing: Tests routes across Jupiter, OKX, dFlow, and other DEX aggregators
  • Route choice: Sorted by output; the best-output route that fits in one transaction is sent
  • Slippage protection: Each route enforces a minimum output within the slippage setting
  • Automatic ATA creation: Handles token account creation and SOL wrapping/unwrapping

Borrow and Multiply SDK Methods

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

Deposit and Borrow

Learn basic deposit and borrow operations

Market Data

Read market metrics and reserve data

KSwap SDK

Multi-DEX swap routing SDK

Scope SDK

Oracle price aggregation SDK