# SettlementEngine

Settles Dual Investment positions by reading oracle prices at/near expiry, calculating the winning side, executing payout in the correct pToken, and burning the position.

## Responsibilities

* Enforce settlement window after `expiry`.
* Fetch or cache settlement price (`SimplePriceOracle`).
* Compute winning pToken based on strike and direction;&#x20;
* payout 1:1 notional in winning cToken units.
* Withdraw/mint directly where possible; else withdraw from source and swap to target before minting.

## Key Functions

* `settlePosition(tokenId, user)`
  * Validates window, balance; gets price; determines winning cToken; burns position; executes payout via VaultExecutor.
* `batchSettlePositions(tokenIds, users)` best‑effort loop using internal helper to continue on failures.
* Views: `canSettlePosition(tokenId)`, `getSettlementInfo(tokenId)`.
* Admin: `setSettlementWindow(seconds)`, `emergencySetSettlementPrice(tokenId, price)`.

## Payout Path

1. Try `withdrawUnderlyingFromProtocol(payoutCToken, underlyingOutRequested)`; if >0, `mintCTokensTo(payoutCToken, user, withdrawn)`.
2. Else withdraw from source (`position.cTokenIn`), swap to payout underlying, then `mintCTokensTo` the user.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://peridot-finance.gitbook.io/peridot-protocol/technical-architecture/evm/dual-investment/settlementengine.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
