Skip to main content
Return borrowed assets to reduce or close positions. The SDK handles transaction building, instruction creation for debt repayment.

Repay

Return borrowed assets into the reserve.
1

Import Dependencies

Import the required packages for Solana RPC communication, Kamino SDK operations, and Kit transaction building.
import "dotenv/config";
import {
	createSolanaRpc,
	address,
	pipe,
	createTransactionMessage,
	setTransactionMessageFeePayerSigner,
	setTransactionMessageLifetimeUsingBlockhash,
	appendTransactionMessageInstructions,
	signTransactionMessageWithSigners,
	sendTransactionWithoutConfirmingFactory,
	getSignatureFromTransaction,
} from "@solana/kit";
import {
	KaminoMarket,
	KaminoAction,
	VanillaObligation,
	PROGRAM_ID,
} from "@kamino-finance/klend-sdk";
import BN from "bn.js";
import { getKeypair } from "../../utils/keypair";
import { confirmTransactionViaPolling } from "../../utils/tx";
@solana/kit provides modern utilities for RPC, transaction building, and signing. @kamino-finance/klend-sdk contains market operation methods.
2

Initialize RPC and Load Market

Initialize the RPC connection and load the Kamino market instance.
const signer = await getKeypair();
const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
const marketPubkey = address("7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF");
const market = await KaminoMarket.load(rpc, marketPubkey, 400);
KaminoMarket.load() fetches the current market state including all reserve data and configuration.
3

Fetch Obligation and Build Repay Instructions

Fetch the existing obligation and generate repay instructions for the specified token and amount.
const usdtMint = address("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB");
const repayAmount = new BN(1_000_000); // 1.00 USDT (6 decimals)

const currentSlot = await rpc.getSlot().send();

// Fetch the existing obligation from the blockchain
const obligation = await market!.getObligationByWallet(
	address(signer.address),
	new VanillaObligation(PROGRAM_ID)
);

if (!obligation) {
	throw new Error(
		"No obligation found for this wallet. You must have an active borrow to repay."
	);
}

const repayAction = await KaminoAction.buildRepayTxns(
	market!,
	repayAmount,
	usdtMint,
	signer,
	new VanillaObligation(PROGRAM_ID),
	true, // useV2Ixs
	undefined, // scopeRefreshConfig
	currentSlot, // currentSlot
	signer, // payer
	1_000_000, // extraComputeBudget
	true, // includeAtaIxs
	false // requestElevationGroup
);
4

Build and Sign Transaction

Combine all instructions and build the transaction using Kit’s functional pipe pattern.
// Combine all instructions
const allInstructions = [
	...(repayAction.setupIxs || []),
	...(repayAction.lendingIxs || []),
	...(repayAction.cleanupIxs || []),
];

// Get fresh blockhash
const { value: latestBlockhash } = await rpc
	.getLatestBlockhash({
		commitment: "finalized",
	})
	.send();

// Build transaction using functional pipe pattern
const transactionMessage = pipe(
	createTransactionMessage({ version: 0 }),
	(tx) => setTransactionMessageFeePayerSigner(signer, tx),
	(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
	(tx) => appendTransactionMessageInstructions(allInstructions, tx)
);

// Sign transaction
const signedTransaction = await signTransactionMessageWithSigners(
	transactionMessage
);
All setup, lending, and cleanup instructions are combined in a single transaction for repayment.
5

Send and Confirm Transaction

Send the transaction and confirm it using HTTP polling.
// Get signature
const signature = getSignatureFromTransaction(signedTransaction);

// Send transaction
const sendTransaction = sendTransactionWithoutConfirmingFactory({ rpc });
await sendTransaction(signedTransaction, {
	commitment: "confirmed",
	skipPreflight: true,
	maxRetries: 3n,
});

// Confirm transaction
await confirmTransactionViaPolling(rpc, signature);

console.log("Repay successful! Signature:", signature);
Repaying reduces your debt and improves your loan-to-value (LTV) ratio, making your position healthier and reducing liquidation risk.
  1. Assets are transferred from your wallet to the reserve pool
  2. Your borrow amount decreases in your obligation
  3. You stop paying interest on the repaid amount
  4. Your loan-to-value (LTV) ratio decreases (healthier position)
  5. Your available borrowing capacity increases
Repay some of your debt to improve your LTV:
  • Repaying entire borrowed amount + accrued interest eliminates debt
  • Full repayment allows you to withdraw all collateral
  • You must repay the principal + accrued interest
  • Interest accrues continuously