Skip to main content

Manage User Obligations

Manage and retrieve user obligations, positions, and transaction history using the Kamino SDK and API.
Historical obligation data is only available via the API.

Get User Obligation Info

Retrieve detailed obligation information including deposits, borrows, and health metrics.
getUserObligation.ts
import { createSolanaRpc, address } from '@solana/kit';
import { KaminoMarket, VanillaObligation, PROGRAM_ID } from '@kamino-finance/klend-sdk';

const rpc = createSolanaRpc('https://api.mainnet-beta.solana.com');
const marketPubkey = address('7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF');
const userAddress = address('EZC9wzVCvihCsCHEMGADYdsRhcpdRYWzSCZAVegSCfqY');

const market = await KaminoMarket.load(rpc, marketPubkey, 400, PROGRAM_ID);

const kaminoObligation = await market!.getObligationByWallet(
userAddress,
new VanillaObligation(PROGRAM_ID)
);

if (!kaminoObligation) {
console.log('No obligation found for user:', userAddress);
} else {
console.log('Obligation Info:');
console.log('Owner:', kaminoObligation.state.owner.toString());

console.log('\nDeposits (Collateral):');
const deposits = [...kaminoObligation.deposits.values()];
deposits.forEach((deposit) => {
  console.log(`Reserve:`, deposit.reserveAddress.toString());
  console.log(`    Amount:`, deposit.amount);
  console.log(`    Market Value:`, deposit.marketValueRefreshed);
});

console.log('\nBorrows:');
const borrows = [...kaminoObligation.borrows.values()];
borrows.forEach((borrow) => {
  console.log(`Reserve:`, borrow.reserveAddress.toString());
  console.log(`    Amount:`, borrow.amount);
});

console.log('\nMetrics:');
console.log('Loan-to-Value:', kaminoObligation.loanToValue());
console.log('Borrow Utilization:', kaminoObligation.refreshedStats.borrowUtilization);
console.log('Number of Positions:', kaminoObligation.getNumberOfPositions());
}

View Code

Get All User Obligations

Retrieve all obligations for a user wallet, including different obligation types across the market.
getAllUserObligations.ts
import { createSolanaRpc, address } from '@solana/kit';
import { KaminoMarket, PROGRAM_ID, ObligationTypeTag } from '@kamino-finance/klend-sdk';

const rpc = createSolanaRpc('https://api.mainnet-beta.solana.com');
const marketPubkey = address('7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF');
const userWallet = address('Hs9ioQZ2pCUyvS18anwmBxjQJsZrMPShwTMLySD6Us3V');

const market = await KaminoMarket.load(rpc, marketPubkey, 400, PROGRAM_ID);

const obligations = await market.getAllUserObligations(userWallet);

if (obligations.length === 0) {
console.log('No obligations found for wallet:', userWallet);
} else {
console.log(`Found ${obligations.length} obligation(s):\n`);

obligations.forEach((obligation, i) => {
  console.log(`Obligation ${i + 1}:`);
  console.log('Address:', obligation.obligationAddress.toString());
  console.log('Type:', ObligationTypeTag[obligation.obligationTag]);
  console.log('Owner:', obligation.state.owner.toString());

  console.log('\nDeposits (Collateral):');
  const deposits = [...obligation.deposits.values()];
  deposits.forEach((deposit) => {
    console.log(`  Reserve:`, deposit.reserveAddress.toString());
    console.log(`    Amount:`, deposit.amount);
    console.log(`    Market Value:`, deposit.marketValueRefreshed);
  });

  console.log('\nBorrows:');
  const borrows = [...obligation.borrows.values()];
  borrows.forEach((borrow) => {
    console.log(`  Reserve:`, borrow.reserveAddress.toString());
    console.log(`    Amount:`, borrow.amount);
    console.log(`    Market Value:`, borrow.marketValueRefreshed);
  });

  console.log('\nMetrics:');
  console.log('Loan-to-Value:', obligation.loanToValue());
  console.log('Borrow Utilization:', obligation.refreshedStats.borrowUtilization);
  console.log('Number of Positions:', obligation.getNumberOfPositions());
});
}

View Code

Get All Obligations by Reserve

Retrieve all obligations that have deposits in a specific reserve. Useful for analyzing reserve utilization and user exposure.
getAllObligationsByReserve.ts
import { createSolanaRpc, address } from '@solana/kit';
import { KaminoMarket, PROGRAM_ID, ObligationTypeTag } from '@kamino-finance/klend-sdk';

// Note: This example requires a paid/dedicated RPC node. Public RPCs will hit rate limits.
const rpc = createSolanaRpc('https://api.mainnet-beta.solana.com');
const marketPubkey = address('7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF');
const reserveAddress = address('37Jk2zkz23vkAYBT66HM2gaqJuNg2nYLsCreQAVt5MWK');

const market = await KaminoMarket.load(rpc, marketPubkey, 400, PROGRAM_ID);

const obligations = await market.getAllObligationsByDepositedReserve(reserveAddress);

if (obligations.length === 0) {
console.log('No obligations found with deposits in reserve:', reserveAddress);
} else {
console.log(`Found ${obligations.length} obligation(s) with deposits in this reserve:\n`);

obligations.forEach((obligation, i) => {
  console.log(`Obligation ${i + 1}:`);
  console.log('Address:', obligation.obligationAddress.toString());
  console.log('Type:', ObligationTypeTag[obligation.obligationTag]);
  console.log('Owner:', obligation.state.owner.toString());

  const reserveDeposit = [...obligation.deposits.values()].find(
    (deposit) => deposit.reserveAddress.toString() === reserveAddress.toString()
  );

  if (reserveDeposit) {
    console.log('\nDeposit in this Reserve:');
    console.log('  Amount:', reserveDeposit.amount);
    console.log('  Market Value:', reserveDeposit.marketValueRefreshed);
  }

  const otherDeposits = [...obligation.deposits.values()].filter(
    (deposit) => deposit.reserveAddress.toString() !== reserveAddress.toString()
  );
  if (otherDeposits.length > 0) {
    console.log('\nOther Deposits:');
    otherDeposits.forEach((deposit) => {
      console.log('  Reserve:', deposit.reserveAddress.toString());
      console.log('    Market Value:', deposit.marketValueRefreshed);
    });
  }

  const borrows = [...obligation.borrows.values()];
  if (borrows.length > 0) {
    console.log('\nBorrows:');
    borrows.forEach((borrow) => {
      console.log('  Reserve:', borrow.reserveAddress.toString());
      console.log('    Market Value:', borrow.marketValueRefreshed);
    });
  }

  console.log('\nMetrics:');
  console.log('Loan-to-Value:', obligation.loanToValue());
  console.log('Number of Positions:', obligation.getNumberOfPositions());
  console.log();
});
}

View Code

Get User Transaction History

Access all transaction history for a user’s wallet across deposits, borrows, repayments, and liquidations.
getTransactionHistory.ts
const marketPubkey = 'DxXdAyU3kCjnyggvHmY5nAwg5cRbbmdyX3npfDMjjMek';
const userPubkey = 'EZC9wzVCvihCsCHEMGADYdsRhcpdRYWzSCZAVegSCfqY';

const API_BASE_URL = 'https://api.kamino.finance';

const url = `${API_BASE_URL}/v2/kamino-market/${marketPubkey}/users/${userPubkey}/transactions`;

const res = await fetch(url);

if (!res.ok) {
  throw new Error(`Request failed: ${res.status} ${res.statusText}`);
}

const data = await res.json();

console.log(data);
View Code

Additional Resources