# Querier

Querier Contract Address: [inj1kfjff5f0xjy7gece36watkqtscpycv666tqq7t](https://injscan.com/contract/inj1kfjff5f0xjy7gece36watkqtscpycv666tqq7t/)

The **Querier Contract** is a quality of life service developed to provide simplified protocol queries. It provides advanced, aggregated query functionality by interacting with various on-chain modules such as the token, market, and price oracle contracts.

## Interrelation with Other Contracts

The Neptune Querier contract depends on and interacts with:

* **Token Contract:**\
  Queries the weighted stake essential for account health calculations.
* **Market Contract:**\
  Provides account states, collateral, debt information, and market parameters.
* **Price Oracle Contract:**\
  Supplies current asset pricing data required for evaluating collateral and debt.

## Query Messages

### 1. Get Account Health

The querier contract calls the token contract via a smart query (`GetHealthWeightedStake`) to obtain the **weighted stake** of an account. This value is fundamental for computing the account's overall health.

The **Account Health** is conceptually computed as:

$$
\text{Account Health} = \frac{\text{Weighted Collateral Value + Weighted Stake}}{\text{Debt Value}}
$$

A value of less than 1 indicates that the account is **undercollateralized** and may require liquidation.

Query: `get_account_health`

**Purpose:**\
Retrieves the health of a specified account by aggregating collateral, debt, and weighted stake information.

**Query Input:**

```json
{
  "get_account_health": {
    "addr": "inj................",
    "account_index": 2
  }
}
```

| Parameter       | Type    | Description             | Required |
| --------------- | ------- | ----------------------- | -------- |
| `addr`          | `Addr`  | Injetive wallet address | Yes      |
| `account_index` | `uint8` | sub account's index ID  | Yes      |

**Query Response:**

```json
{
    "0.98"
}
```

*Here, a result of "0.98" indicates that the account health is 98% of the safe threshold. Values below 1 denote undercollateralization and places the account in a liquidable state.*

| Parameter | Type         | Description            |
| --------- | ------------ | ---------------------- |
| response  | `Decimal256` | Current account health |

### 2. Get Collateral Discount

For accounts with health below 1, the protocol applies discounts on collateral assets. The discount calculation is based on:

* Protocol parameters from the market configuration.
* The specific collateral state.
* The computed account health.

This process is encapsulated in the `get_discount` function. Only accounts with $$\text{health} < 1$$ qualify, as healthy accounts (with health ≥ 1) do not receive discounts.

Query: `get_collateral_discount`

**Purpose:** Calculates the applicable discounts for collateral assets held by an undercollateralized account.

**Query Input:**

```json
{
    "get_collateral_discounts": {
        "addr": "inj................",
        "account_index": 0
    }
}
```

<table><thead><tr><th>Parameter</th><th width="145">Type</th><th>Description</th><th>Required</th></tr></thead><tbody><tr><td><code>addr</code></td><td><code>addr</code></td><td>Injetive wallet address</td><td>Yes</td></tr><tr><td><code>account_index</code></td><td><code>uint8</code></td><td>sub account's index ID</td><td>Yes</td></tr></tbody></table>

**Query Response:**

```json
{
    "result": [
        {
            "asset_info": { "type": "native_token", "denom": "inj" },
            "discount": "0.10"
        },
        {
            "asset_info": { "type": "token", "contract_addr": "inj834l..." },
            "discount": "0.05"
        }
    ]
}
```

*This indicates that the native `inj` asset receives a 10% discount, while the specified CW20 token receives a 5% discount.*

<table><thead><tr><th>Parameter</th><th width="159">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>asset_info</code></td><td><code>AssetInfo</code></td><td>Object containing the type of token and either a denom or a contract address</td></tr><tr><td><p><code>asset_info.</code></p><p><strong><code>type</code></strong></p></td><td><code>string</code></td><td><code>"native_token"</code> or <code>"token"</code></td></tr><tr><td><p><code>asset_info.</code></p><p><strong><code>denom</code></strong></p></td><td><code>string</code></td><td>Denomination of the token (if <code>type</code> = <code>native_token</code>)</td></tr><tr><td><p><code>asset_info.</code></p><p><strong><code>contract_addr</code></strong></p></td><td><code>addr</code></td><td>Contract address of the token (if <code>type</code> = <code>token</code>)</td></tr><tr><td><code>discount</code></td><td><code>decimal256</code></td><td>The discount value for this particular collateral token</td></tr></tbody></table>

### 3. Get Liquidation Request

When an account is undercollateralized (account health is < 1), a **liquidation request** is computed to determine the amount of debt to be repaid and collateral to be received. This involves:

* Determining the accounts repayable debt.
* Identifying which collaterals are available for liquidation.
* Calculating:
  * **Ask Assets:** The collateral assets to be liquidated.
  * **Offer Assets:** The corresponding debt amounts to be repaid.

The final liquidation request, if valid, contains both mappings and only returns a result if:

* There is a non-zero debt to be repaid.
* There is a non-zero collateral liquidation amount.

Query: `get_liquidation_request`

**Purpose:** Generates a liquidation request for an account that is determined to be undercollateralized. This request details both the collateral (ask assets) to be liquidated and the debt (offer assets) to be repaid.

**Query Input:**

```json
{
    "get_liquidation_request": {
        "account_addr": "inj834l...",
        "account_index": 0,
        "collateral_priority": [
            { "type": "native_token", "denom": "inj" }
        ],
        "debt_priority": [
            { "type": "native_token", "denom": "peggy0xdAC17F958D2ee523a2206206994597C13D831ec7" }
        ]
    }
}
```

<table><thead><tr><th width="241">Parameter</th><th width="151">Type</th><th>Description</th><th>Required</th></tr></thead><tbody><tr><td><code>account_addr</code></td><td><code>Addr</code></td><td>The account address to be checked or liquidated</td><td>Yes</td></tr><tr><td><code>account_index</code></td><td><code>uint8</code></td><td>The sub-account index ID</td><td>Yes</td></tr><tr><td><code>collateral_priority</code></td><td><code>AssetInfo[]</code></td><td>Ordered list of collateral token types to be liquidated first, second, etc</td><td>No</td></tr><tr><td><p><code>collateral_priority.</code></p><p><strong><code>type</code></strong></p></td><td><code>string</code></td><td>The collateral token "type". Can be <code>"native_token"</code> or <code>"token"</code></td><td>Yes</td></tr><tr><td><p><code>collateral_priority.</code></p><p><strong><code>denom</code></strong></p></td><td><code>string</code></td><td>The collateral token address. Can be <code>denom</code> or <code>token_address</code></td><td>Yes</td></tr><tr><td><code>debt_priority</code></td><td><code>AssetInfo[]</code></td><td>Ordered list of debts to be repaid first, second, etc</td><td>No</td></tr><tr><td><p><code>debt_priority.</code></p><p><strong><code>type</code></strong></p></td><td><code>string</code></td><td>The debt token "type". Can be <code>"native_token"</code> or <code>"token"</code></td><td>Yes</td></tr><tr><td><p><code>debt_priority.</code></p><p><strong><code>denom</code></strong></p></td><td><code>string</code></td><td>The debt token address. Can be <code>denom</code> or <code>token_address</code></td><td>Yes</td></tr></tbody></table>

**Query Response:**

```json
{
  "ask_assets": {
    "inj": {
      "amount": "1000000000000000000",
      "min_discount": "0.10"
    }
  },
  "offer_assets": {
    "peggy0xdAC17F958D2ee523a2206206994597C13D831ec7": "500000000000000000"
  }
}
```

*If the account is healthy or no liquidation is triggered, the query returns `null`.*

| Parameter                                                                                                    | Type                                        | Description                                            |
| ------------------------------------------------------------------------------------------------------------ | ------------------------------------------- | ------------------------------------------------------ |
| `ask_assets`                                                                                                 | `NeptuneMap<AssetInfo, LiquidationAmounts>` | Object listing the collateral assets to be liquidated  |
| <p><code>ask\_assets.</code></p><p><strong><code>denom</code></strong></p>                                   | `object`                                    | Collateral details for a specific token                |
| <p><code>ask\_assets.</code></p><p><code>denom.</code></p><p><strong><code>amount</code></strong></p>        | `Uint256`                                   | The amount of collateral to be liquidated              |
| <p><code>ask\_assets.</code></p><p><code>denom.</code></p><p><strong><code>min\_discount</code></strong></p> | `Decimal256`                                | Any applicable discount applied to collateral value    |
| `offer_assets`                                                                                               | `NeptuneMap<AssetInfo, Uint256>`            | Object listing the debt to be repaid                   |
| <p><code>offer\_assets.</code></p><p><strong><code>denom</code></strong></p>                                 | `Uint256`                                   | The amount of debt that must be repaid for liquidation |

### 4. Get Market Metadata

The contract also computes key market ratios:

* **Utilization Ratio:**

$$
\text{Utilization Ratio} = \frac{\text{Debt Pool Balance}}{\text{Lending Principal}}
$$

* **Redemption Ratio:**

$$
\text{Redemption Ratio} = \frac{\text{Lending Principal}}{\text{Total Receipt Token Supply}}
$$

These ratios are essential for monitoring market performance and liquidity.

Query: `get_market_metadata`

**Purpose:** Retrieves metadata for available markets, including the utilization and redemption ratios.

**Query Input:**

```json
{
  "get_market_metadata": {
    "start_after": {
      "native_token": {
        "denom": "inj"
      }
    },
    "limit": 2
  }
}
```

<table><thead><tr><th width="182">Parameter</th><th>Type</th><th width="296">Description</th><th>Required</th></tr></thead><tbody><tr><td><code>start_after</code></td><td><code>AssetInfo</code></td><td>Object specifying which token to start after</td><td>No</td></tr><tr><td><p><code>start_after.</code></p><p><strong><code>native_token</code></strong></p></td><td><code>object</code></td><td>Token type. Can be <code>native_token</code> or <code>token</code></td><td>Yes</td></tr><tr><td><p><code>start_after.</code></p><p><code>native_token.</code></p><p><strong><code>denom</code></strong></p></td><td><code>string</code></td><td>Token address. Can be <code>denom</code> or <code>contract_address</code></td><td>Yes</td></tr><tr><td><code>limit</code></td><td><code>uint32</code></td><td>How many records to fetch starting after target token</td><td>No</td></tr></tbody></table>

**Query Response:**

```json
[
  [
    {
      "native_token": {
        "denom": "peggy0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
      }
    },
    {
      "utilization_ratio": "0.434835991165652783",
      "redemption_ratio": "1.029245984846468095"
    }
  ],
  [
    {
      "native_token": {
        "denom": "peggy0xdAC17F958D2ee523a2206206994597C13D831ec7"
      }
    },
    {
      "utilization_ratio": "0.836150542983626702",
      "redemption_ratio": "1.203947857769176135"
    }
  ]
]
```

| Parameter                                                                                 | Type         | Description                                         |
| ----------------------------------------------------------------------------------------- | ------------ | --------------------------------------------------- |
| `native_token`                                                                            | `AssetInfo`  | Object specifying token denom and market metadata   |
| <p><code>native\_token.</code></p><p><strong><code>denom</code></strong></p>              | `string`     | Token address. Can be `denom` or `contract_address` |
| <p><code>native\_token.</code></p><p><strong><code>utilization\_ratio</code></strong></p> | `Decimal256` | The current utilization ratio for the tokens market |
| <p><code>native\_token.</code></p><p><strong><code>redemption\_ratio</code></strong></p>  | `Decimal256` | The current redemption ratio from nToken<>Token     |
