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.Import Dependencies
Import the required packages for Solana RPC communication, Kamino SDK operations, KSwap routing, and transaction building.Load Configuration and Initialize SDKs
Load the keypair and initialize RPC connections, the market, Scope for oracle prices, and KSwap for routing.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.Find TSLAx Reserve in Market
Dynamically discover the TSLAx token mint by searching the market reserves by symbol.Fetch Multiply Lookup Tables
Fetch multiply-specific Address Lookup Tables (LUTs) from Kamino’s CDN for the TSLAx/USDC pair to compress transaction size.Configure Multiply Parameters
Set the deposit amount, leverage multiplier, and slippage tolerance.Setup User Lookup Table
Generate setup instructions for creating or extending a user-specific lookup table with addresses needed for multiply operations.setupTxsIxs will be empty and the loop won’t execute.Check for Existing Obligation
Check if a multiply obligation already exists for this collateral/debt pair.null, a new obligation will be created using a flash loan to bootstrap the position.Get Scope Price Refresh Instructions
Fetch latest oracle prices for accurate collateral and debt valuation.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.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.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.Build and Send Transaction
Fetch a fresh blockhash, prepare lookup tables, and send the transaction.Helper Functions
The tutorial requires KSwap helper functions for price fetching and route building.kswap_utils.ts
kswap_utils.ts
Transaction Flow
View Transaction Flow Diagram
View Transaction Flow Diagram
Asset Flow
2x Leveraged TSLAx Position: Step-by-Step
2x Leveraged TSLAx Position: Step-by-Step
Initial State
- Wallet: 10 USDC
- Position: None
Flash Borrow
- Flash loan: 10 USDC (temporary)
- Available: 20 USDC total
Swap & Deposit
- Swap 20 USDC to TSLAx via KSwap
- Deposit TSLAx as collateral
Borrow & Repay
- Borrow 10 USDC against collateral
- Repay 10 USDC flash loan
Final Position
- Collateral: TSLAx ($20 value)
- Debt: 10 USDC
- Net Equity: 10 USDC
- Leverage: 2x
Key Concepts
Flash Loans
Flash Loans
- 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
Multiply Obligation Type
Multiply Obligation Type
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)
Lookup Tables (LUTs)
Lookup Tables (LUTs)
- 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
Transaction Simulation
- 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 Multi-Route Optimization
KSwap Multi-Route Optimization
- 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
Commitment Levels
Commitment Levels
processed: Fastest, used for sending (no false timeout errors)confirmed: Medium speed, confirmed by majority of cluster stakefinalized: Slowest, used for blockhash (maximum security, ~32 blocks)- Recommended: Use
processedfor multiply transactions to avoid false errors
Borrow and Multiply SDK Methods
MultiplyObligation. Vanilla operations use VanillaObligation. The obligation type determines which PDA is derived for your position.