Get Borrow Details of a Wallet

Fetch details of all borrows by a wallet.

There are two Kamino account types that we are interested in. They are

  • Obligation, which stores a wallet's borrowed and deposited assets.

  • Reserve, which stores detail about the actual asset that is being borrowed or deposited.

We can get all lending details for a particular wallet, by querying the Obligation account from the Kamino Lending program. You can directly copy paste this code on replit and see it in action. The owner field refers to the wallet which is being queried, and we can filter this field to get the desired result.

const SHYFT_API_KEY = "YOUR_API_KEY"; //enter your SHYFT API Key
async function fetchObligationLendings(ownerAddress: string) {
  //more relevant fields can be cherry picked
  const operationsDoc = `
      query MyQuery {
        kamino_lending_Obligation(
          where: {owner: {_eq: ${JSON.stringify(ownerAddress)}}}
        ) {
          owner
          borrows
          borrowedAssetsMarketValueSf
          borrowFactorAdjustedDebtValueSf
        }
      }
    `; //graphQl query
  const result = await fetch(
    `https://programs.shyft.to/v0/graphql/accounts?api_key=${SHYFT_API_KEY}&network=mainnet-beta`,
    {
      method: "POST",
      body: JSON.stringify({
        query: operationsDoc,
        variables: {},
        operationName: "MyQuery",
      }),
    }
  );

  return await result.json();
}

async function getData(address: string) {
  const { errors, data } = await fetchObligationLendings(address);
  
  if (errors) {
    console.error(errors);
  }
  
  console.log(JSON.stringify(data));
}

getData("FgPehj68tvGcGDkHp2LwjVz8WaJdJCtkW1wYwzo3j8i3");
//wallet for which lendings are being fetched

The response returned contains a borrows field, which is an array of borrow details for the user. Each item in that array contains the following:

  • borrowReserve: The address of the reserve from which the asset is borrowed. The asset details can be retrieved by querying the Reserve account. Get Reserve Details.

  • cumulativeBorrowRateBsf: The borrow rate used for calculating interest.

  • borrowedAmountSf: The amount of liquidity borrowed plus interest.

  • marketValueSf: The liquidity market value in quote currency.

An Example to get Borrow details for a wallet

Consider the example, if we are trying to get all borrows for the wallet HNqJtqudHDWiWHWg3RH7FPamf8dyXjFwJVPmfRDMDyjE. The steps are as follows:

You can try out a sample project that gets all deposits and borrows of a wallet, here.

  • We query the Kamino Lending Obligation account using the where filter on the owner field, as illustrated in the above example. The query looks somewhat like this.

query MyQuery {
  kamino_lending_Obligation(where: {owner: {_eq: "HNqJtqudHDWiWHWg3RH7FPamf8dyXjFwJVPmfRDMDyjE"}}) {
    borrows
    pubkey
    tag
  }
}
  • Once successful, we get all the Obligation details for the wallet, which contains the borrow details. Each Item in the borrows array contains the borrowedAmountSf, which is the amount borrowed (in Scaled fraction) and borrowReserve, which contains the liquidity details.

{
  "data": {
    "kamino_lending_Obligation": [
      {
        "borrows": [
          {
            "padding": "0",
            "padding2": ["0","0","0","0","0","0","0"],
            "borrowReserve": "Ga4rZytCpq1unD4DbEJ5bkHeUz9g3oh9AAFEi6vSauXp",//reserve address
            "marketValueSf": "115840195581198642917",
            "borrowedAmountSf": "115869161712935259635828580", //borrowed amount plus interest
            "cumulativeBorrowRateBsf": {
              "value": ["1177132707471237533","0","0","0"],
              "padding": ["0","0"]
            },
            "borrowFactorAdjustedMarketValueSf": "115840195581198642917"
          }
        ]
      }
     ]
  }
}
  • The borrowReserve returned in the previous step is the pubkey of a reserve account, which keeps track of the liquidity. We query the reserve details from the Kamino Lending Reserves account.

query MyQuery {
  kamino_lending_Reserve(
    where: {pubkey: {_eq: "Ga4rZytCpq1unD4DbEJ5bkHeUz9g3oh9AAFEi6vSauXp"}}
  ) {
    liquidity
    pubkey
  }
}
  • Once successfully executed, this returns the mintPubkey which is the token address of the liquidity in this reserve. We can further user SHYFT’s SDK to get the token name, symbol and decimals.

{
  "data": {
    "kamino_lending_Reserve": [
      {
        "liquidity": {
          "feeVault": "rywFxeqHfCWL5iGq9cDxutwiiR2TabZgzv8aRM1Lceq", 
          "mintPubkey": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", //token address
          "supplyVault": "GENey8es3EgGiNTM8H8gzA3vf98haQF8LHiYFyErjgrv",
          "mintDecimals": "6", //decimals
          "marketPriceSf": "1152864008411412232",
          "availableAmount": "14304198739155",
          "borrowedAmountSf": "46874931934370590975849021213088",
          "pendingReferrerFeesSf": "0",
          "absoluteReferralRateSf": "0",
          "borrowLimitCrossedSlot": "0",
          "cumulativeBorrowRateBsf": {
            "value": [
              "1179082184865788199",
              "0",
              "0",
              "0"
            ]
          },
          "depositLimitCrossedSlot": "0",
          "marketPriceLastUpdatedTs": "1710439495",
          "accumulatedProtocolFeesSf": "59297420613860366241481245022",
          "accumulatedReferrerFeesSf": "0"
        },
        "pubkey": "Ga4rZytCpq1unD4DbEJ5bkHeUz9g3oh9AAFEi6vSauXp"
      }
    ]
  }
}

Last updated