Repay with collateral reduces or eliminates debt in a single, atomic transaction by converting collateral into the debt token without requiring additional funds.
When a repay with collateral operation is initiated, the protocol temporarily borrows the debt token (for example, CASH) through a flash loan and uses it to repay the existing debt. As the debt is reduced, a portion of the collateral token (for example, SOL) becomes withdrawable and is withdrawn from Kamino Lend. The withdrawn collateral is then swapped into the debt token using KSwap (Kamino’s token swap SDK), and the proceeds are used to repay the flash loan before the transaction completes. The result is a position with reduced debt and reduced collateral, without requiring additional funds from the user.
Repay with Collateral using KSwap
Reduce debt in a single transaction by converting collateral using a flash loan and KSwap.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, and KSwap for routing./path/to/your/keypair.json with the desired keypair path. The KSwap SDK enables multi-DEX routing for optimal swap execution.Fetch Repay with Collateral Lookup Tables
Fetch repay-specific Address Lookup Tables (LUTs) from Kamino’s CDN to compress transaction size.Check for Existing Obligation
Retrieve the obligation for this wallet. The obligation must exist to perform repay with collateral.Setup User Lookup Table
Generate setup instructions for creating or extending a user-specific lookup table.setupTxsIxs will be empty and the loop won’t execute.Build Multi-Route Swap Instructions
Set the transaction compute budget and priority fee for reliable execution. Then use KSwap to generate multiple swap route options for converting collateral to debt token.Select Best Route
Prepare the Klend lookup tables for transaction compression.Fetch Blockhash and Load Lookup Tables
Fetch a fresh blockhash for the transaction.Find and Fetch Missing Lookup Tables
Extract all account addresses used in the transaction instructions.Build, Sign, and Send Transaction
Build the transaction message with all instructions and compress it using the lookup tables.Helper Functions
The tutorial requires KSwap helper functions for route building.kswap_utils.ts
kswap_utils.ts
Transaction Flow
View Transaction Flow Diagram
View Transaction Flow Diagram
Asset Flow
Repay 3 CASH Debt: Step-by-Step
Repay 3 CASH Debt: Step-by-Step
Repay & Withdraw
- Repay 3 CASH debt
- Debt reduced from 5 CASH to 2 CASH
- Withdraw freed 0.03 SOL collateral
Key Concepts
Flash Loans
Flash Loans
- Zero upfront capital: Reduce debt without providing additional funds
- Atomic execution: All operations succeed or fail together
- Debt reduction: Convert collateral to debt token in one transaction
- Risk-free: Transaction reverts if flash loan cannot be repaid
Vanilla Obligation Type
Vanilla Obligation Type
VanillaObligation is a standard obligation type that:- Standard positions: General-purpose collateral and debt tracking
- Required for repay: Must exist before performing repay with collateral
- Retrieved by wallet: Automatically finds your obligation address
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
- Repay-specific LUTs: Fetched from Kamino CDN per collateral/debt pair
- Critical for complex txns: Repay operations wouldn’t fit without LUTs
KSwap Multi-Route Optimization
KSwap Multi-Route Optimization
- Route simulation: Tests each route for success server-side
- Transaction size selection: Prioritizes routes that fit within transaction limits
- Manual filtering: Provides simulation data for custom route selection logic
- Slippage protection: Guarantees minimum output amount
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 repay transactions to avoid false errors