# Get Borrow Details of 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 <mark style="color:yellow;">Obligation</mark> account from the Kamino Lending program.\
\
You can directly copy paste this code on <mark style="color:yellow;">replit</mark> 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.

{% tabs %}
{% tab title="Code Snippet" %}
{% code overflow="wrap" %}

```typescript
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
```

{% endcode %}
{% endtab %}

{% tab title="Response" %}

```json
{
  "data": {
    "kamino_lending_Obligation": [
      {
        "owner": "FgPehj68tvGcGDkHp2LwjVz8WaJdJCtkW1wYwzo3j8i3",
        "borrows": [
          {
            "padding": "0",
            "padding2": ["0","0","0","0","0","0","0","0"],
            "borrowReserve": "EVbyPKrHG6WBfm4dLxLMJpUDY43cCAcHSpV3KYjKsktW",
            "marketValueSf": "6025906178204981648",
            "borrowedAmountSf": "35134630221936101800853625",
            "cumulativeBorrowRateBsf": {
              "value": [
                "1156067927245241416","0", "0", "0"
              ],
              "padding": [
                "0",
                "0"
              ]
            },
            "borrowFactorAdjustedMarketValueSf": "7532382722756227060"
          },
          {
            "padding": "0",
            "padding2": ["0","0","0","0","0","0","0","0"],
            "borrowReserve": "11111111111111111111111111111111",
            "marketValueSf": "0",
            "borrowedAmountSf": "0",
            "cumulativeBorrowRateBsf": {
              "value": [
                "0",
                "0",
                "0",
                "0"
              ],
              "padding": [
                "0",
                "0"
              ]
            },
            "borrowFactorAdjustedMarketValueSf": "0"
          },
          {
            "padding": "0",
            "padding2": ["0","0","0","0","0","0","0","0"],
            "borrowReserve": "11111111111111111111111111111111",
            "marketValueSf": "0",
            "borrowedAmountSf": "0",
            "cumulativeBorrowRateBsf": {
              "value": [
                "0",
                "0",
                "0",
                "0"
              ],
              "padding": [
                "0",
                "0"
              ]
            },
            "borrowFactorAdjustedMarketValueSf": "0"
          },
          {
            "padding": "0",
            "padding2": ["0","0","0","0","0","0","0","0"],
            "borrowReserve": "11111111111111111111111111111111",
            "marketValueSf": "0",
            "borrowedAmountSf": "0",
            "cumulativeBorrowRateBsf": {
              "value": [
                "0",
                "0",
                "0",
                "0"
              ],
              "padding": [
                "0",
                "0"
              ]
            },
            "borrowFactorAdjustedMarketValueSf": "0"
          },
          {
            "padding": "0",
            "padding2": ["0","0","0","0","0","0","0","0"],
            "borrowReserve": "11111111111111111111111111111111",
            "marketValueSf": "0",
            "borrowedAmountSf": "0",
            "cumulativeBorrowRateBsf": {
              "value": [
                "0",
                "0",
                "0",
                "0"
              ],
              "padding": [
                "0",
                "0"
              ]
            },
            "borrowFactorAdjustedMarketValueSf": "0"
          }
        ],
        "borrowedAssetsMarketValueSf": 6025906178204981000,
        "borrowFactorAdjustedDebtValueSf": 7532382722756227000
      }
    ]
  }
}
```

{% endtab %}
{% endtabs %}

The response returned contains a <mark style="color:yellow;">`borrows`</mark> 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 <mark style="color:yellow;">Reserve</mark> account. [get-reserve-details](https://docs.shyft.to/solana-indexers/case-studies/kamino/get-reserve-details "mention").
* `cumulativeBorrowRateBsf`:  The borrow rate used for calculating interest.&#x20;
* `borrowedAmountSf`: The amount of liquidity borrowed plus interest.
* &#x20;`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:&#x20;

{% hint style="info" %}
You can try out a **sample project** that gets all deposits and borrows of a wallet, [here](https://replit.com/@abhirup1/GetUserObligationsFromKamino).
{% endhint %}

* We query the Kamino Lending <mark style="color:yellow;">Obligation</mark> account using the where filter on the `owner` field, as illustrated in the above example. The query looks somewhat like this.

```graphql
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.

```json
{
  "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 <mark style="color:yellow;">Reserves</mark> account.

```graphql
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*.

```json
{
  "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"
      }
    ]
  }
}
```
