# MarketFactory.sol

Singleton factory that creates and manages prediction markets.

## `createMarket(priceId, duration, batchInterval, minLots)`

* Registers a new market in OrderBook via `registerMarket()`
* Requires MARKET\_CREATOR\_ROLE
* Stores MarketMeta with lifecycle tracking
* Emits `MarketCreated` event

### Parameters

| Param           | Description                                   | Default  |
| --------------- | --------------------------------------------- | -------- |
| `priceId`       | Pyth price feed ID (bytes32)                  | required |
| `duration`      | Market duration in seconds                    | required |
| `batchInterval` | Batch clearing interval (0 = use default 60s) | 60s      |
| `minLots`       | Minimum order size (0 = use default 1)        | 1        |

## State Machine

```
Open → Closed → Resolving → Resolved
                               ↓
Open → Closed ─────────────→ Cancelled
```

### Transitions

| From        | To        | Trigger          | Condition                       |
| ----------- | --------- | ---------------- | ------------------------------- |
| Open        | Closed    | `closeMarket()`  | `block.timestamp >= expiryTime` |
| Closed      | Resolving | `setResolving()` | ADMIN\_ROLE (PythResolver)      |
| Resolving   | Resolved  | `setResolved()`  | ADMIN\_ROLE (PythResolver)      |
| Open/Closed | Cancelled | `cancelMarket()` | 24h after expiry, no resolution |

## Market Registry

* `getActiveMarketCount()` — number of markets in Open state
* `getClosedMarketCount()` — number of closed markets
* `getResolvedMarketCount()` — number of resolved markets

## Admin Functions

| Function             | Access      | Description                              |
| -------------------- | ----------- | ---------------------------------------- |
| `pauseFactory()`     | ADMIN\_ROLE | Emergency pause on market creation       |
| `setDefaultParams()` | ADMIN\_ROLE | Update default batch interval + min lots |
| `setCreationBond()`  | ADMIN\_ROLE | Update required creation bond            |
| `setFeeCollector()`  | ADMIN\_ROLE | Update protocol fee collector address    |

## Access Control

* **ADMIN\_ROLE:** PythResolver (for `setResolving`, `setResolved`, `payResolverBounty`)
* **DEFAULT\_ADMIN\_ROLE:** protocol admin (pause, parameter updates)
* `closeMarket()` and `cancelMarket()` are permissionless
* Market creation requires MARKET\_CREATOR\_ROLE

## Events

```solidity
event MarketCreated(uint256 indexed factoryMarketId, uint256 indexed orderBookMarketId, bytes32 priceId, int64 strikePrice, uint256 expiryTime, address indexed creator);
event MarketClosed(uint256 indexed factoryMarketId);
event MarketStateChanged(uint256 indexed factoryMarketId, MarketState newState);
event FactoryPaused(bool paused);
event DefaultParamsUpdated(uint256 batchInterval, uint128 minLots);
event CreationBondUpdated(uint256 newBond);
event FeeCollectorUpdated(address indexed collector);
event ResolverBountyPaid(uint256 indexed factoryMarketId, address indexed resolver, uint256 amount);
```


---

# 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://docs.strike.pm/smart-contracts/market-factory.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.
