# Architecture Overview

## Contract System

evently is composed of independent smart contracts deployed on MegaETH mainnet.

```
+---------------------------+
|   EventlyProfiles.sol     |  ← Identity, stats, referrals, swap points
+------------+--------------+
             | reads/writes profile data
+------------v--------------+
|   EventlyMarketsV3.sol    |  ← Prediction markets (LMSR b=200 + CLOB)
|       v3.2 post-audit     |    Pending deployment post professional audit
+---------------------------+
```

The Markets contract is fully independent — it only reads USDm balances and approvals from the ERC-20 contract. The Profiles contract tracks identity and onchain social layer.

***

## Deployed Addresses (Mainnet — Chain ID 4326)

| Contract               | Address                                          |
| ---------------------- | ------------------------------------------------ |
| EventlyMarketsV3       | TBD (pending deployment post professional audit) |
| EventlyProfiles (v1.3) | `0x9F0708145BCCD1F5B16F610cB8a75A63fA4A9a24`     |
| USDm                   | `0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7`     |
| MegaNames              | `0x5B424C6CCba77b32b9625a6fd5A30D409d20d997`     |
| Admin wallet           | `0x21CbC99a2E8c68F1C2955991E07c0C22ea895Da1`     |

***

## Design Principles

* **Pull payment pattern** — winners pull funds via `redeemWinnings()` / `claimCancelRefund()`; no push transfers that can fail
* **Checks-Effects-Interactions (CEI)** — state reset before all external calls
* **ReentrancyGuard** — custom `_locked` mutex on all state-mutating external functions
* **No upgradeability** — contracts are immutable; new versions are redeployed
* **Transparency** — all events emitted for full on-chain auditability

***

## EventlyMarketsV3 — Key Architecture

LMSR AMM (b=200 USDm) as market maker of last resort. CLOB (Central Limit Order Book) with bids and asks matched first at price-time priority.

```
BUY order  → CLOB asks filled first → remainder to LMSR AMM
SELL order → CLOB bids filled first → remainder rests as limit ask
           → OR: sellToAMM() for instant AMM execution
```

ERC-1155 shares: 1 winning share = 1 USDm at redemption. Solvency guaranteed by LMSR invariant: `poolBalance + subsidyDeposited >= total winning shares`.

AI audit complete (7 tools, v3.2). No open blockers. Awaiting professional audit (Pashov/CertiK).

***

## Gas Configuration (MegaETH-Specific)

MegaETH has near-zero gas costs (\~1ms blocks, \~100K TPS). Transaction configuration in the frontend:

```ts
maxFeePerGas: 1_000_000n     // 1 gwei
maxPriorityFeePerGas: 0n
```

***

## Off-Chain Infrastructure

| Component     | Tech                         | Purpose                                             |
| ------------- | ---------------------------- | --------------------------------------------------- |
| Database      | Supabase (PostgreSQL)        | Market metadata, user profiles, trade history cache |
| Frontend      | Next.js + Tailwind + wagmi   | React app at evently.market                         |
| Real-time     | `watchContractEvent` (wagmi) | \~1ms block polling for live trade feed             |
| Bridge/Swap   | LiFi SDK                     | 60+ routes to MegaETH                               |
| Market import | Polymarket CLOB API          | Live odds comparison                                |
