# Architecture

<figure><img src="https://1945048287-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqwQzYDQSbGeudICjMddT%2Fuploads%2FkAnwZDr0fOWl8pF7W2yj%2F4mica_protocol_v4_condensed.svg?alt=media&#x26;token=3870abe4-9e4a-45b9-bf22-11721f82b7ea" alt=""><figcaption></figcaption></figure>

## 4Mica MVP Call Flow

### Participants

* **Client (C)**
* **Server (S)**
* **Facilitator (4Mica Core Service) (F)**
* **Vault (4Mica Core Contract) (B)**

***

### 1. Deposit

1. Client deposits `val` to Vault (`B`).
2. Vault emits `"deposit"` event to Facilitator (`F`).
3. The facilitator adds collateral for the client.

Vault at the alpha release is the core contract of 4Mica, and in later releases, would be a capital-efficient vault.&#x20;

***

### 2. Transaction Phase

#### 0th Request

* Client requests service from Server.
* Server responds with `402 Payment Required` and a `tab_id`.
* Client records `start_ts`.

#### ith Request

1. Client sends service request with a signed promise.
2. Server verifies promise via Facilitator.
   * If promise is invalid → decline.
   * If insufficient funds → decline.
   * Else:
     * Facilitator aggregates and locks promise.
     * Server executes the service request.
     * Client receives answer.

***

### 3. Settlement (Client Pays Server)

1. Client pays server on-chain (`val`, `tid`, `req_id`).
2. Facilitator monitors blockchain for transactions.
3. If transaction matches promise:
   * Record payment, update collateral lock.
   * Notify Vault to register payment.
4. Vault updates status.

***

### 4. Remuneration (If Client Fails to Pay)

1. Server triggers timeout and requests remuneration from Vault.
2. Vault verifies request:
   * Invalid → error.
   * Valid → pays server from client deposit.
3. Facilitator unlocks the locked collateral that was used to back the line of credit for the tab.
4. Vault emits `"remunerated"` event.

***

### 5. Withdrawal

1. Client requests withdrawal from Vault.
   * If `val > deposit` → error.
   * Otherwise, Facilitator reserves funds.
2. After `WITHDRAWAL_LATENCY`:
   * Client executes withdrawal:
     * If valid → transfer funds and update deposit/collateral.
     * If invalid → error.
   * Client may cancel withdrawal request:
     * Vault and Facilitator remove reservation.
