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

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

Load Configuration and Initialize SDKs

Load the keypair and initialize RPC connections, the market, Scope for oracle prices, and KSwap for routing.
The xStocks Market LUT can be found via the Markets API: https://api.kamino.finance/v2/kamino-market?programId=KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD. The Scope SDK provides oracle price data, and the KSwap SDK enables multi-DEX routing for optimal swap execution.
3

Find TSLAx Reserve in Market

Dynamically discover the TSLAx token mint by searching the market reserves by symbol.
This dynamic discovery approach allows the code to work across different markets without hardcoding token mint addresses. The reserve symbol identifies the asset (TSLAx for tokenized Tesla stock), and we retrieve its mint address programmatically.
4

Fetch Multiply Lookup Tables

Fetch multiply-specific Address Lookup Tables (LUTs) from Kamino’s CDN for the TSLAx/USDC pair to compress transaction size.
Lookup Tables store frequently-used addresses, allowing transactions to reference 1-byte indexes instead of 32-byte addresses. This reduces transaction size from ~3096 bytes to under 1232 bytes, which is critical for complex multiply operations.
5

Configure Multiply Parameters

Set the deposit amount, leverage multiplier, and slippage tolerance.
Some assets may require higher slippage than others.
6

Setup User Lookup Table

Generate setup instructions for creating or extending a user-specific lookup table with addresses needed for multiply operations.
Execute the setup transactions if any are needed. The loop builds, signs, and sends each setup transaction, waiting 2 seconds between each to ensure proper ordering.
Setup transactions extend the lookup table with addresses specific to the multiply position. This is a one-time operation per collateral/debt pair - if the LUT already exists, setupTxsIxs will be empty and the loop won’t execute.
7

Check for Existing Obligation

Check if a multiply obligation already exists for this collateral/debt pair.
If an obligation exists, the transaction will add to the existing position. If null, a new obligation will be created using a flash loan to bootstrap the position.
8

Get Scope Price Refresh Instructions

Fetch latest oracle prices for accurate collateral and debt valuation.
9

Get Price from Scope Oracle and Build Routes

Fetch the latest USDC/TSLAx price ratio from the Scope oracle (already loaded in reserves). Set the transaction compute budget and priority fee for reliable execution. Then use KSwap to generate multiple swap route options with the quoter and swapper helper functions. See the Helper Functions section below for the implementation of these utilities.
Using Scope oracle prices (getOracleMarketPrice()) is more reliable than external price APIs because Scope aggregates multiple oracle sources and is the same price feed used by Kamino Lend for position health calculations.
10

Simulate Routes and Select Best

Prepare the Klend lookup tables for transaction compression, then simulate all routes to find the best one that will succeed on-chain.
Simulation-based route selection tests each route on-chain before execution to ensure it will succeed. Routes that fail simulation are filtered out, and the best passing route is selected by price. If no routes pass simulation, the first route is used as a fallback.
11

Build and Send Transaction

Fetch a fresh blockhash, prepare lookup tables, and send the transaction.
The multiply deposit transaction is complete, resulting in a leveraged position with 2x exposure to TSLAx, all executed atomically in a single transaction.

Helper Functions

The tutorial requires KSwap helper functions for price fetching and route building.

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)
Address Lookup Tables reduce transaction size by:
  • Storing common addresses: Frequently-used program accounts, reserves, oracles
  • Index-based references: 1-byte index instead of 32-byte address
  • Transaction compression: Enables complex transactions to fit within the 1232-byte limit
  • Multiply-specific LUTs: Fetched from Kamino CDN per collateral/debt pair
  • Critical for complex txns: Multiply operations wouldn’t fit without LUTs
Transaction simulation tests routes before execution by:
  • Pre-validation: Running the transaction on-chain without committing state
  • Error detection: Identifying routes that would fail due to slippage, insufficient liquidity, or account errors
  • Best route selection: Choosing the route with the best price among passing simulations
  • Fallback strategy: Using the first route if no simulations pass (edge case)
  • Reliability improvement: Reduces failed transactions and improves user experience
KSwap provides optimal swap execution through:
  • Multi-DEX routing: Tests routes across Jupiter, OKX, dFlow, and other DEX aggregators
  • Price optimization: Selects the route with the best guaranteed output amount
  • Slippage protection: Ensures minimum output amount within tolerance
  • Automatic ATA creation: Handles token account creation and SOL wrapping/unwrapping
Solana commitment levels affect confirmation speed:
  • processed: Fastest, used for sending (no false timeout errors)
  • confirmed: Medium speed, confirmed by majority of cluster stake
  • finalized: Slowest, used for blockhash (maximum security, ~32 blocks)
  • Recommended: Use processed for multiply transactions to avoid false errors

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