---
name: Mneme Context Before Trade
description: Pull the agent's relevant memories, dreams, and recent stream events from Mneme before a swap / send / LP / perp / predict, so the trade decision is grounded.
var: "the trade intent (token, amount, side, or natural-language description)"
tags: [memory, mneme, pre-trade]
---

> **${var}** — Trade intent. Free text or structured. Examples:
> - `"swap 100 USDC for MNEME"`
> - `"long ETH 2x on hyperliquid"`
> - `"add liquidity to ETH/USDC pool"`

## Goal

Before MetaMask Agentic Wallet signs anything, give the operator (and the LLM driving the trade) the relevant slice of the agent's memory. Past trades on this token, recent dreams that mention it, live stream activity from the relevant contract.

## Steps

### 1. Auth check

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

### 2. Extract the focus token / contract

From `${var}`, identify:
- `TOKEN` — symbol (e.g. `MNEME`, `ETH`)
- `CONTRACT` — optional 0x... if present
- `SIDE` — `buy` / `sell` / `long` / `short` / `add_liquidity` / etc.

If ambiguous, default to the most-mentioned ticker.

### 3. Pull recent memories that mention the token

```bash
curl -sS -X POST "$GATEWAY/v1/sql" \
  -H "Authorization: ApiKey $MNEME_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$(jq -n --arg q "%$TOKEN%" '{query:"SELECT id, text, created_at FROM memories WHERE text ILIKE $1 ORDER BY created_at DESC LIMIT 10", params:[$q]}')"
```

### 4. Pull the last 5 dreams (Mneme's async LLM reflection)

```bash
curl -sS "$GATEWAY/v1/dreams?limit=5" \
  -H "Authorization: ApiKey $MNEME_API_KEY"
```

Filter for any whose `title` or `body` references the token.

### 5. Pull recent stream events for the relevant contract (if any)

If you know the contract address, find any active streams that match and pull the last hour:

```bash
curl -sS "$GATEWAY/v1/streams" \
  -H "Authorization: ApiKey $MNEME_API_KEY"
```

Then for any matching `target_table`:

```bash
curl -sS -X POST "$GATEWAY/v1/sql" \
  -H "Authorization: ApiKey $MNEME_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$(jq -n --arg t "$TARGET_TABLE" '{query: ("SELECT * FROM \"" + $t + "\" WHERE block_ts > now() - interval '1 hour' ORDER BY block_ts DESC LIMIT 20"), params:[]}')"
```

### 6. Synthesize → output

Print a compact pre-trade context block:

```
[pre-trade context for $TOKEN]
  3 prior memories
  1 recent dream — "..."
  17 stream events in last hour
  net: 4 inflows, 13 outflows (whale-watch: 0x...)

  past trade outcome on $TOKEN:
    [last 3 executed mandates for this token, if any]

  recommended next:
    [one-line synthesis — what the data suggests]
```

The next skill in the chain — usually `mneme-mandate-create` — reads this context and writes a more grounded mandate.

## Notes

- This skill **does not execute** anything. It's read-only context.
- If Mneme returns zero matches, output `(no prior context on $TOKEN — first trade)` honestly. Don't fabricate.
- Pair tightly with `mneme-mandate-create` (downstream) and `mneme-log-trade` (post-execution) for a complete loop.
