---
name: Mneme Log Trade
description: After MetaMask Agentic Wallet executes a trade, write the result back to Mneme as an event row so future runs can query it.
var: "execution result json (tx_hash, gas, kind, intent, slippage, ...)"
tags: [memory, mneme, post-trade]
---

> **${var}** — JSON describing what just executed. Required: `tx_hash`. Optional but recommended: `kind`, `from_token`, `to_token`, `amount`, `received_amount`, `slippage`, `gas_used`, `protocol`, `block_number`.

## Goal

Make every MetaMask Agentic Wallet trade queryable later — from `/chat` ("what's my P&L on MNEME this week"), from `/find` (hybrid vector + graph), from `/beam` (real-time SSE). Trade history becomes a first-class memory citizen, not an opaque audit log.

## Steps

### 1. Auth check

```bash
if [ -z "$MNEME_API_KEY" ]; then
  echo "MNEME_API_KEY not set."
  exit 1
fi
GATEWAY="${MNEME_GATEWAY:-https://gateway.mnemedb.dev}"
```

### 2. Parse the var

```bash
RESULT='${var}'

TX=$(echo "$RESULT" | jq -r '.tx_hash')
[ -z "$TX" ] || [ "$TX" = "null" ] && { echo "missing tx_hash"; exit 1; }
```

### 3. INSERT into the `events` table

The `events` default table is exactly the shape for this — `kind` text + `payload` jsonb + `created_at`:

```bash
curl -sS -X POST "$GATEWAY/v1/rows/events" \
  -H "Authorization: ApiKey $MNEME_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$(jq -n --argjson r "$RESULT" '{kind: "metamask_trade", payload: $r}')"
```

### 4. (Optional) Close the mandate

If the trade was driven by a Mneme Mandate, mark it executed:

```bash
MANDATE_ID=$(echo "$RESULT" | jq -r '.mandate_id // empty')
if [ -n "$MANDATE_ID" ]; then
  curl -sS -X POST "$GATEWAY/v1/mandates/$MANDATE_ID/execute" \
    -H "Authorization: ApiKey $MNEME_API_KEY" \
    -H "Content-Type: application/json" \
    -d "$(jq -n --arg t "$TX" '{tx_hash: $t}')"
fi
```

### 5. Output

```
✓ trade logged → events #<id>
  tx:   <tx_hash on basescan>
  kind: <swap | send | ...>
  mandate #<id> → executed   (if applicable)
```

## Notes

- Future `/chat` calls now know about this trade — try asking `/chat "what tokens did I touch today"`.
- Mneme Beam will tick this INSERT live, so any dashboard subscribed to your schema sees the trade in real time.
- If you want richer typed columns, create a custom `trades` table via `POST /v1/tables` and INSERT into that instead. The pattern is the same.
