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

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

Load the Multiply Obligation

A Multiply position is its own obligation, addressed by the collateral and debt mints.
4

Get Scope Price Refresh Instructions

5

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.
6

Build the Repay Routes

The SDK builds one candidate transaction per swap route: flash borrow, repay the debt, withdraw TSLAx, swap it to USDC, repay the flash loan.
slippagePct also sizes the flash loan, so the worst-case swap fill still covers the repay. To repay all the debt and close the position, set isClosingPosition to true.
7

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.
8

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.
9

Sign and Send

The debt is reduced by selling TSLAx collateral, with no USDC needed in the wallet.