Skip to main content

Repay with collateral reduces debt in a single atomic transaction by converting collateral into the debt token without requiring additional funds. The protocol flash borrows the debt token, repays the debt, withdraws the freed collateral, swaps it to the debt token via KSwap, and repays the flash loan—all in one transaction.

Repay with Collateral using KSwap

Reduce debt in a single transaction by converting collateral using a flash loan and KSwap.
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, initialize RPC connections, and set up the market and SDKs.
3

Find Collateral Reserve

Dynamically discover the collateral token mint by searching the market reserves by symbol.
4

Fetch Multiply Lookup Tables

Retrieve the multiply-specific lookup tables from Kamino’s CDN.
5

Configure Repay Parameters

Set the amount of debt to repay using collateral and the slippage tolerance.
Some assets may require higher slippage than others.
6

Setup Lookup Tables

Initialize the user’s lookup table and execute any required setup transactions.
Setup transactions extend the lookup table with addresses specific to the position. This is a one-time operation per collateral/debt pair.
7

Fetch Existing Obligation

Retrieve the current obligation and display position details.
This example uses MultiplyObligation. For standard borrow positions, use VanillaObligation instead.
8

Build Repay Instructions

Generate repay with collateral instructions with scope price refresh and compute budget.
9

Simulate Routes

Prepare lookup tables, simulate all routes to filter out failures.
10

Select Best Route

Compare passing routes by price and select the best one.
11

Build, Sign, and Send Transaction

Wait briefly to avoid rate limiting, get a fresh blockhash, compress the transaction with LUTs, and send it.
The repay with collateral transaction is complete. Collateral was sold to repay debt without requiring external funds.

Helper Functions

The tutorial requires KSwap helper functions for quote retrieval and swap execution.

Borrow and Multiply SDK Methods

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