# Buy from Cortex — a Walkthrough

A step-by-step guide to seeing Cortex work end-to-end on Base Sepolia. Roughly 10 minutes from cold start to a receipt + fulfillment record on your wallet's dashboard.

Three roles are involved. You can play all three from one wallet for the walkthrough, or split them across three browsers/wallets to feel the real handoff.

- **Merchant** registers a service and commits a quote.
- **Agent owner** sets a spending policy.
- **Agent** signs the payment.

Everything lives on Base Sepolia; nothing requires mainnet ETH.

---

## Prereqs

- A wallet with a small amount of Base Sepolia ETH (~0.001 is plenty). MetaMask, Rabby, or Coinbase Smart Wallet all work — connect any of them through `cortex.projectaegis.ai`'s header.
- 10 minutes.

If you'd rather not register your own merchant, skip step 1 and use any of the four demo merchants seeded on testnet (Cortex Weather Co, Cortex Reports Co, Cortex Hat Co, Cortex Analyst Co — see [`/dashboard/merchant`](https://cortex.projectaegis.ai/dashboard/merchant) after connecting).

---

## 1. Register a merchant + service

Open [`https://cortex.projectaegis.ai/onboarding`](https://cortex.projectaegis.ai/onboarding).

1. Connect your wallet using the **Connect Wallet** button in the header. The bar in the page body should switch from amber (disconnected) to showing your address.
2. Stay on the **Merchant setup** top tab → **Merchant** sub-tab.
3. Fill in the form. Defaults are usable for a smoke test:
   - **Merchant name** — anything, e.g. "Walkthrough Co"
   - **Website** / **Support contact** — your URLs / email, optional
   - **Payout address** — auto-filled with your connected wallet. Leave as-is for the walkthrough.
   - **Refund policy** — pre-filled with a reasonable default
4. Click **Publish profile**. This POSTs the merchant JSON to the hosted catalogs endpoint, gets back a canonical URI + keccak hash. You'll see a green "Merchant profile" panel appear with the hosted link.
5. Click **Register merchant**. Your wallet will prompt — this is the onchain `CommerceRegistry.registerMerchant(payout, uri, hash)` call. Confirm.
6. After the receipt confirms (a few seconds on Sepolia), the page shows the new merchant ID (e.g. `Merchant #42`) and a Basescan link to the tx.

Switch to the **Service** sub-tab.

7. Fill in the service form. Defaults again work:
   - **Service ID** — `walkthrough-v1`
   - **Service name**, **Endpoint**, **Method**, **Capability tag**, **Description**
8. Click **Publish metadata**, then **Publish catalog**, then **Register service**. Three transactions, but only the last is onchain (the first two are catalog uploads).

You now own a merchant with a service, both visible at [`cortex.projectaegis.ai/dashboard/merchant`](https://cortex.projectaegis.ai/dashboard/merchant).

---

## 2. Set up an agent + policy

Switch to the **Agent setup** top tab.

1. **Identity** sub-tab → fill in agent name + capabilities. Cortex auto-hosts the profile JSON to `/catalogs` by default — no need to provide a URI. Click **Register agent**.
2. **Spending policy** sub-tab → click **+ Add policy**. The form is a list view; you can have many policies per agent.
3. Choose **Any merchant** for broad authority, or pick a specific merchant payout to scope the policy to one merchant. Facilitators check `(merchant payout, token, facilitator)` at payment time; exact merchant policies override the any-merchant policy.
4. **Payment token**: paste an ERC-20 token address (USDC on Base Sepolia, or just the zero address `0x0000…` for native ETH).
5. **Facilitator**: zero address if none.
6. Set per-payment and daily budget. For testing, `1000000` per payment / `10000000` per day is fine.
7. Click **Save policy (allowed)**. Wallet prompt — confirm.

The policy now appears as a row in the list view. You can edit or revoke it from there.

---

## 3. Run a purchase

Open [`https://cortex.projectaegis.ai/purchase-simulator`](https://cortex.projectaegis.ai/purchase-simulator).

1. The **Registered context** panel at the top auto-fills from your wallet: pick the merchant + service + agent you just registered.
2. Scroll to the **Quote** card. Most fields are pre-filled. Pick a payment rail (**Direct transfer** is the simplest for the walkthrough — native ETH, no facilitator).
3. Click **Commit quote**. Wallet prompt — confirm. The onchain `commitQuote(...)` call records canonical quote terms.
4. After the commit confirms, the **Pay & settle** card on the right shows the right combination of buttons for your rail. For native ETH transfer, click **Send native ETH** and confirm.
5. Click **Record receipt** (now enabled). Confirm. The merchant side records the receipt and links it to the quote hash.
6. Click **Record fulfillment**. For an API service this is a hash of "the response was delivered"; for a physical good it's the encrypted shipping receipt envelope.

That's the full flow. Three transactions onchain (commit, pay, receipt) + an optional fulfillment record.

---

## 4. Verify

Open [`https://cortex.projectaegis.ai/dashboard`](https://cortex.projectaegis.ai/dashboard). The protocol dashboard refreshes and you'll see your receipt under **Recent receipts**. Click any row → the details modal shows every indexed field with copy buttons for each.

Switch to [`/dashboard/merchant`](https://cortex.projectaegis.ai/dashboard/merchant) to see this from the merchant side; [`/dashboard/agent`](https://cortex.projectaegis.ai/dashboard/agent) for the agent owner side.

For programmatic verification, hit the hosted API directly:

```bash
# your merchants
curl "https://cortex-api.projectaegis.ai/merchants?owner=<your-address>&active=true"

# your agent's policies
curl "https://cortex-api.projectaegis.ai/accounts/<your-address>/policies"

# all receipts where your agent paid
curl "https://cortex-api.projectaegis.ai/receipts?agent=<your-address>&limit=50"

# protocol-wide analytics
curl "https://cortex-api.projectaegis.ai/analytics/commerce"
```

---

## What this proved

You just executed every load-bearing primitive in Cortex against real Base Sepolia contracts:

- `CommerceRegistry.registerMerchant` + `registerService` — onchain merchant + service identity
- `PolicyModule.setPaymentPolicy` — agent owner authorized a spending policy keyed to a merchant payout or any merchant
- `CommerceRegistry.commitQuote` — canonical quote hash bound to all 12 quote fields
- A wallet transaction settled the payment
- `CommerceRegistry.recordReceipt` + `recordFulfillment` — onchain proof of settlement and delivery

All four payment rails (direct transfer, swap, facilitator, x402) work the same way — the **Pay & settle** card swaps which buttons show up based on what you picked.

For a programmatic version of the same flow, see [`ops/sdk-examples/commerce-flow.ts`](../ops/sdk-examples/commerce-flow.ts) and [`ops/sdk-examples/payment-rails.ts`](../ops/sdk-examples/payment-rails.ts) — both are runnable end-to-end against Sepolia.

For the agent runtime (MCP) version, the same operations are exposed as tools in [`mcp/src/server.ts`](../mcp/src/server.ts) so any MCP-compatible agent runtime can drive this without UI.
