> For the complete documentation index, see [llms.txt](https://docs.uniocean.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.uniocean.io/forecast-platform/settlement/settlement-process.md).

# Settlement Process

Settlement finalizes a market and determines rewards. It is a fully automated, multi-step process that ensures accuracy, fairness, and security through independent validator consensus.

```mermaid
flowchart TD
    A[Market Ends] --> B[Fetch Oracle Price]
    B --> C[Rank Predictions]
    C --> D[Compute Rewards]
    D --> E[Build Merkle Tree]
    E --> F[Submit to Validators]
    F --> G[Collect Validator Signatures]
    G --> H[Submit Merkle Root On-Chain]
    H --> I[Auto Claim Rewards]
    I --> J[Generate Audit Report]
```

## Steps

### 1. Event Close Detection

The settlement system continuously monitors all active markets. When a market's close time arrives, the system automatically initiates settlement.

If the service restarts for any reason, a catchup process identifies any markets that should have been settled but were missed, and processes them automatically.

### 2. Oracle Price Resolution

The settlement price is fetched from Pyth Network using a Verifiable Attestation (VAA). The VAA includes a cryptographic proof that the price was genuinely produced by the oracle at the correct timestamp.

A configurable time window (typically 10 seconds) around the market close time is used to locate the closest valid price attestation.

### 3. Prediction Retrieval

All on-chain predictions for the market are retrieved from blockchain indexers. These indexers have already catalogued every prediction transaction submitted during the market's live period.

### 4. Ranking

Forecasts are ranked by accuracy using the same deterministic algorithm as the live rankings:

1. Calculate distance from oracle price
2. Apply cryptographic tie-breaker for equal distances
3. Assign sequential ranks

The same algorithm runs identically across all settlement nodes, ensuring consensus.

### 5. Reward Calculation

The UCPRA algorithm distributes the reward pool:

* Top 51% of predictions are identified as winners
* Rankers receive power-law distributed rewards
* Bucket Winners receive guaranteed multiplier rewards (1.4x–1.6x)

### 6. Merkle Tree Construction

A Merkle tree is built from all winner rewards. Each leaf contains a wallet address and reward amount. The Merkle root — a single cryptographic fingerprint — represents the entire reward dataset.

This allows efficient on-chain verification without storing every individual reward on the blockchain.

### 7. Validator Consensus (BFT)

Before the Merkle root is submitted on-chain, it must be verified by independent validator nodes.

```mermaid
flowchart LR
    A[Settlement Service] --> B[Validator 1]
    A --> C[Validator 2]
    A --> D[Validator 3]
    A --> E[Validator 4]
    A --> F[Validator 5]
    B --> G[Quorum Check ≥3/5]
    C --> G
    D --> G
    E --> G
    F --> G
    G --> H[On-Chain Submission]
```

Each validator independently:

1. Fetches all predictions from blockchain indexers
2. Retrieves the oracle close price
3. Recomputes rankings using the same algorithm
4. Regenerates the Merkle tree
5. Compares its computed Merkle root with the proposed root
6. Signs the settlement only if results match

A quorum of at least 3 out of 5 validators must agree. If any validator computes a different result, it refuses to sign. This Byzantine Fault Tolerant (BFT) approach means:

* No single compromised node can approve fraudulent results
* Settlement requires majority consensus
* Each validator operates independently with its own signing keys

### 8. On-Chain Submission

Once enough validator signatures are collected, the settlement transaction is submitted to the blockchain. This transaction includes:

* The Merkle root
* The oracle price attestation (VAA)
* Validator signatures

The smart contract verifies the validator signatures and records the settlement permanently.

### 9. Reward Distribution

After on-chain settlement, rewards are automatically claimed for all winners. Users do not need to take any action.

### 10. Audit Report Generation

A comprehensive settlement receipt is generated containing:

* Market metadata (asset, duration, timestamps)
* Final oracle price and source
* Complete participant ranking
* Reward amounts for each winner
* Merkle root and proof data
* Validator signatures

This report is stored permanently and made available for public verification.

## Settlement Decisions

| Condition                   | Action                                      |
| --------------------------- | ------------------------------------------- |
| ≥ 15 predictions            | Normal settlement with reward distribution  |
| < 15 predictions            | Market cancelled, all participants refunded |
| Oracle unavailable          | Market cancelled, all participants refunded |
| Settlement timeout (10 min) | Market cancelled, all participants refunded |

## Settlement Timing

Settlement typically completes within minutes after a market closes. The process is fully automated and does not require human intervention.

| Phase                   | Typical Duration |
| ----------------------- | ---------------- |
| Oracle price fetch      | 1–5 seconds      |
| Ranking computation     | Under 1 second   |
| Validator consensus     | 10–30 seconds    |
| On-chain submission     | 5–30 seconds     |
| Auto-claim execution    | 1–5 minutes      |
| Audit report generation | 10–30 seconds    |

***

## Related Pages

* [Oracle Price](/forecast-platform/settlement/oracle-price.md)
* [Merkle Proofs](/forecast-platform/settlement/merkle-proofs.md)
* [Auto Claims](/forecast-platform/settlement/auto-claims.md)
* [Auto Refunds](/forecast-platform/settlement/auto-refunds.md)
* [Public Verification](/forecast-platform/transparency/public-verification.md)
