> For the complete documentation index, see [llms.txt](https://docs.zestprotocol.com/start/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zestprotocol.com/start/stacks-market-v2-smart-contracts-overview/v2-contracts/market-contracts/market.md).

# Market

**Purpose:** Core lending protocol. Manages collateral, borrowing, repayment, liquidation, and oracle integration. Routes operations to vaults.

## List of Core Functions

**Admin**

* set-pause-liquidation — Pause/unpause liquidations with optional grace period
* set-liquidation-grace-period — Set asset-specific liquidation grace period
* set-max-confidence-ratio — Update oracle confidence threshold

**Queries**

* get-pause-liquidation — Check if liquidations paused
* get-liquidation-grace-end — Get global liquidation grace period end time
* get-liquidation-grace-period-asset — Get asset-specific grace period
* get-max-confidence-ratio — Get oracle confidence threshold
* oracle-last-update — Get last oracle update timestamp
* get-cached-indexes — Get cached borrow/liquidity indexes for asset
* call-ststx-ratio — Get current stSTX/STX ratio from oracle

**User Operations**

* collateral-add — Add collateral to position
* collateral-remove — Remove collateral from position
* supply-collateral-add — Supply underlying + add as collateral (combined operation)
* collateral-remove-redeem — Remove collateral + redeem to underlying (combined operation)
* borrow — Borrow against collateral
* repay — Repay borrowed debt
* liquidate — Liquidate unhealthy position
* liquidate-multi — Batch liquidate multiple positions
* liquidate-redeem — Liquidate + automatically redeem vault shares to underlying

***

## Function Parameters

### set-pause-liquidation

Pause or unpause liquidations globally. Requires DAO authorization.

| Parameter    | Type | Description                                                                 |
| ------------ | ---- | --------------------------------------------------------------------------- |
| paused       | bool | True to pause liquidations, false to unpause                                |
| grace-period | uint | Grace period in seconds before liquidations active (applies when unpausing) |

Returns: (response bool uint) — Success or error

Authorization: DAO executor only

***

### set-liquidation-grace-period

Set asset-specific liquidation grace period.

| Parameter    | Type | Description                                                       |
| ------------ | ---- | ----------------------------------------------------------------- |
| id           | uint | Asset ID                                                          |
| grace-period | uint | Grace period in seconds before liquidations active for this asset |

Returns: (response bool uint) — Success or error

Authorization: DAO executor only

***

### set-max-confidence-ratio

Update oracle confidence threshold (BPS). Default 1000 = 10%.

| Parameter | Type | Description                                     |
| --------- | ---- | ----------------------------------------------- |
| ratio     | uint | Confidence ratio in basis points (10000 = 100%) |

Returns: (response bool uint) — Success or error

Authorization: DAO executor only

Note: Confidence interval must be ≤ ratio × price

***

### get-pause-liquidation

Check if liquidations are currently paused.

| Parameter | Type | Description   |
| --------- | ---- | ------------- |
| (none)    | -    | No parameters |

Returns: (response bool uint) — True if paused, false otherwise

***

### get-liquidation-grace-end

Get global liquidation grace period end timestamp.

| Parameter | Type | Description   |
| --------- | ---- | ------------- |
| (none)    | -    | No parameters |

Returns: (response (optional uint) uint) — Grace period end time or none

***

### get-liquidation-grace-period-asset

Get asset-specific liquidation grace period end time.

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| id        | uint | Asset ID    |

Returns: (response (optional uint) uint) — Grace period end time or none

***

### get-max-confidence-ratio

Get oracle confidence threshold in basis points.

| Parameter | Type | Description   |
| --------- | ---- | ------------- |
| (none)    | -    | No parameters |

Returns: uint — Confidence ratio (BPS)

***

### oracle-last-update

Get last oracle update timestamp for specific feed.

| Parameter | Type                                 | Description                             |
| --------- | ------------------------------------ | --------------------------------------- |
| f         | { type: (buff 1), ident: (buff 32) } | Oracle feed identifier (type + feed ID) |

Returns: (optional uint) — Last update timestamp or none

***

### get-cached-indexes

Get cached borrow and liquidity indexes for asset.

| Parameter | Type | Description |
| --------- | ---- | ----------- |
| aid       | uint | Asset ID    |

Returns: (optional { index: uint, lindex: uint }) — Indexes or none

***

### call-ststx-ratio

Get current stSTX/STX ratio from oracle.

| Parameter | Type | Description   |
| --------- | ---- | ------------- |
| (none)    | -    | No parameters |

Returns: (response uint uint) — stSTX ratio (6 decimals) or error

***

### collateral-add

Add collateral to user's position. Can optionally update Pyth price feeds.

| Parameter   | Type                            | Description                                |
| ----------- | ------------------------------- | ------------------------------------------ |
| ft          |                                 | Asset token contract                       |
| amount      | uint                            | Amount to add as collateral                |
| price-feeds | (optional (list 3 (buff 8192))) | Optional Pyth price feed updates (up to 3) |

Returns: (response uint uint) — Updated collateral amount or error

Authorization: User must be contract-caller = tx-sender

Note: Updates position health after operation

***

### collateral-remove

Remove collateral from user's position.

| Parameter   | Type                            | Description                             |
| ----------- | ------------------------------- | --------------------------------------- |
| ft          |                                 | Asset token contract                    |
| amount      | uint                            | Amount to remove                        |
| receiver    | (optional principal)            | Optional recipient (defaults to caller) |
| price-feeds | (optional (list 3 (buff 8192))) | Optional Pyth price feed updates        |

Returns: (response uint uint) — Remaining collateral amount or error

Authorization: User must be contract-caller = tx-sender

Note: Checks position health remains adequate after removal

***

### supply-collateral-add

Combined operation: supply underlying to vault + add resulting shares as collateral.

| Parameter   | Type                            | Description                                         |
| ----------- | ------------------------------- | --------------------------------------------------- |
| ft          |                                 | Underlying asset token contract                     |
| amount      | uint                            | Amount of underlying to supply                      |
| min-shares  | uint                            | Minimum vault shares expected (slippage protection) |
| price-feeds | (optional (list 3 (buff 8192))) | Optional Pyth price feed updates                    |

Returns: (response uint uint) — Vault shares received and added as collateral or error

Authorization: User must be contract-caller = tx-sender

***

### collateral-remove-redeem

Combined operation: remove collateral (vault shares) + redeem to underlying.

| Parameter      | Type                            | Description                                       |
| -------------- | ------------------------------- | ------------------------------------------------- |
| ft             |                                 | Vault token contract (zToken)                     |
| amount         | uint                            | Amount of vault shares to remove and redeem       |
| min-underlying | uint                            | Minimum underlying expected (slippage protection) |
| receiver       | (optional principal)            | Optional recipient (defaults to caller)           |
| price-feeds    | (optional (list 3 (buff 8192))) | Optional Pyth price feed updates                  |

Returns: (response uint uint) — Underlying tokens received or error

Authorization: User must be contract-caller = tx-sender

***

### borrow

Borrow assets against collateral. Creates or increases debt position.

| Parameter   | Type                            | Description                             |
| ----------- | ------------------------------- | --------------------------------------- |
| ft          |                                 | Asset token contract to borrow          |
| amount      | uint                            | Amount to borrow                        |
| receiver    | (optional principal)            | Optional recipient (defaults to caller) |
| price-feeds | (optional (list 3 (buff 8192))) | Optional Pyth price feed updates        |

Returns: (response bool uint) — Success or error

Authorization: User must be contract-caller = tx-sender

Constraints:

* Must have sufficient collateral
* Position must remain healthy after borrow
* Cannot liquidate same block as borrow

***

### repay

Repay borrowed debt, reducing user's debt position.

| Parameter    | Type                 | Description                                        |
| ------------ | -------------------- | -------------------------------------------------- |
| ft           |                      | Asset token contract to repay                      |
| amount       | uint                 | Amount to repay (will be capped at actual debt)    |
| on-behalf-of | (optional principal) | Optional account to repay for (defaults to caller) |

Returns: (response uint uint) — Actual amount repaid or error

Note: Automatically caps repayment at user's actual debt

***

### liquidate

Liquidate an unhealthy position, seizing collateral in exchange for repaying debt.

| Parameter               | Type                            | Description                                                   |
| ----------------------- | ------------------------------- | ------------------------------------------------------------- |
| borrower                | principal                       | Address of position to liquidate                              |
| collateral-ft           |                                 | Collateral asset to seize                                     |
| debt-ft                 |                                 | Debt asset to repay                                           |
| debt-amount             | uint                            | Amount of debt to repay                                       |
| min-collateral-expected | uint                            | Minimum collateral to receive (slippage protection)           |
| receiver                | (optional principal)            | Optional recipient for seized collateral (defaults to caller) |
| price-feeds             | (optional (list 3 (buff 8192))) | Optional Pyth price feed updates                              |

Returns: (response { debt: uint, collateral: uint } uint) — Amounts or error

Conditions:

* Liquidations not paused
* Position must be unhealthy
* Grace period must have passed
* Cannot liquidate same block as borrow

***

### liquidate-multi

Batch liquidate multiple positions atomically.

| Parameter | Type            | Description                                                                                     |
| --------- | --------------- | ----------------------------------------------------------------------------------------------- |
| positions | (list 64 {...}) | List of position specs (borrower, collateral-ft, debt-ft, debt-amount, min-collateral-expected) |

Returns: (response (list 64 (response {...} uint)) uint) — List of results per position

Note: Price feeds not supported in batch - update prices separately or use individual liquidate()

***

### liquidate-redeem

Liquidate position and automatically redeem seized collateral (if vault shares) to underlying.

| Parameter               | Type                            | Description                                        |
| ----------------------- | ------------------------------- | -------------------------------------------------- |
| borrower                | principal                       | Address of position to liquidate                   |
| collateral-ft           |                                 | Collateral asset (vault token) to seize and redeem |
| debt-ft                 |                                 | Debt asset to repay                                |
| debt-amount             | uint                            | Amount of debt to repay                            |
| min-underlying-expected | uint                            | Minimum underlying expected after redemption       |
| receiver                | (optional principal)            | Optional recipient (defaults to caller)            |
| price-feeds             | (optional (list 3 (buff 8192))) | Optional Pyth price feed updates                   |

Returns: (response { debt: uint, underlying: uint } uint) — Debt repaid and underlying received

Note: Combines liquidation + redemption for gas efficiency
