Skip to main content
Glama
README.md
# QuoteGuard: pre-trade price check for Solana agents

An autonomous agent that swaps tokens trusts whatever quote it is handed. QuoteGuard is the check it runs first:
it takes a **Jupiter** swap quote, compares it with **CoinGecko's market price**, and returns **ALLOW**, **WARN** or **BLOCK**
with every figure and the CoinGecko endpoint it came from. It never signs and never holds keys.

It ships as three front ends over one core:

| Front end | For |
|---|---|
| **MCP server** (`quoteguard-mcp`): `check_swap`, `prepare_guarded_swap`, `market_price` | any MCP-capable agent (Claude, Cursor, Codex, ...) |
| **Solana Agent Kit plugin** (`createQuoteGuardPlugin`) with a `guardedTrade` method that wraps the kit's own `trade` | Agent Kit agents |
| **CLI** (`quoteguard check ...`) and a TypeScript library | scripts, CI, humans |

**Demo video (1:52):** https://youtu.be/LVAsdiwYZao (mp4 mirror: https://github.com/GBMRAFLI/quoteguard-solana/releases/download/v1.0.0/quoteguard-demo.mp4)

![ALLOW](docs/01-allow.png)
![BLOCK](docs/02-block.png)

> Built for the Superteam x CoinGecko "Long Tail: AI Agents with CoinGecko Data" hackathon track (idea 5, "price check for agent trades").
> **All code in this repository was written from 2 Dec 2026 onward, in a new repository. It is not an existing project.**

## Quick start (no API key needed)

```bash
git clone https://github.com/GBMRAFLI/quoteguard-solana && cd quoteguard-solana
npm install && npm run build
node dist/cli.js check --in SOL --out USDC --amount 10
node dist/cli.js check --in SOL --out BONK --amount 50000      # BLOCK: price impact and gap
node dist/cli.js check --in USDC --out WIF --amount 100 --max-gap-bps 5
bash demo/demo.sh                                              # the full demo, against live APIs
npm test                                                       # 21 offline tests
```

Needs Node 20+. Exit code is `3` on BLOCK so shell scripts and CI can gate on it. Add `--json` for the full report.

Optional: copy `.env.example` to `.env` and export the values. A **CoinGecko key is optional but recommended**:
without one, QuoteGuard uses the keyless endpoints, which are heavily rate limited and only cover CoinGecko-listed tokens.
With `COINGECKO_API_KEY` (a hackathon Analyst key is a Pro-plan key, so keep `COINGECKO_PLAN=pro`) it also uses CoinGecko's
onchain price endpoint, which covers long-tail Solana tokens that CoinGecko does not list.

### Use as an MCP server

```json
{ "mcpServers": { "quoteguard": { "command": "node", "args": ["/path/to/quoteguard-solana/dist/mcp.js"],
  "env": { "COINGECKO_API_KEY": "" } } } }
```

`demo/mcp-client.mjs` is a minimal MCP client that does exactly what an agent runtime does (spawn over stdio, list tools, call `check_swap`).

### Use with Solana Agent Kit

```ts
import { SolanaAgentKit } from "solana-agent-kit";
import TokenPlugin from "@solana-agent-kit/plugin-token";
import { createQuoteGuardPlugin } from "quoteguard-solana";

const agent = new SolanaAgentKit(wallet, RPC_URL, {}).use(TokenPlugin).use(createQuoteGuardPlugin());
const r = await agent.methods.guardedTrade(agent, { inputMint: "SOL", outputMint: "USDC", amount: "10", slippageBps: 50 });
// r.executed === false: blocked, r.report explains why. true: r.signature is from the kit's own trade().
```

The plugin also registers a `CHECK_SWAP_PRICE` action for LLM tool calling. The plugin matches the published
`Plugin` interface of `solana-agent-kit` 2.x (`name`, `methods`, `actions`, `initialize`) and the token plugin's
`trade(agent, outputMint: PublicKey, inputAmount, inputMint, slippageBps)` signature; it is tested with a stand-in agent,
not with a funded wallet.

## What it checks

`gap = (fairOut - quotedOut) / fairOut`, where `fairOut = amountIn x CoinGecko price(in) / CoinGecko price(out)`.

| Code | Severity | Meaning (default) |
|---|---|---|
| `PRICE_GAP_TOO_WIDE` | block | quote is worse than CoinGecko market price by more than `maxGapBps` (150 bps) |
| `PRICE_GAP_ELEVATED` | warn | worse than half the limit |
| `QUOTE_TOO_GOOD` | warn | quote is *better* than market by more than the limit: stale/wrong reference or a trap token |
| `PRICE_IMPACT_HIGH` | block | Jupiter price impact above `maxPriceImpactBps` (100 bps) |
| `STALE_REFERENCE` | warn | CoinGecko price older than 600 s |
| `LOW_VOLUME` / `LARGE_VS_VOLUME` | warn | output token 24h volume under $100k, or trade above 1% of it |
| `SOURCES_DISAGREE` | warn | with a key: CoinGecko onchain and aggregated prices differ by more than 100 bps |
| `NO_REFERENCE_PRICE`, `REFERENCE_UNAVAILABLE`, `NO_QUOTE`, `TX_WALLET_MISMATCH` | block | **fail closed**: if it cannot verify, it does not allow |

All limits are parameters (`policy` in the library, `maxGapBps` / `maxPriceImpactBps` in the MCP tools, `--max-gap-bps` in the CLI).

`prepare_guarded_swap` goes one step further: only when the verdict allows it, it asks Jupiter to build the swap, **decodes the
unsigned versioned transaction with `@solana/web3.js`**, checks the fee payer is the requested wallet, and returns the
unsigned base64 transaction for the user's wallet to sign. A transaction built for another wallet is discarded (`TX_WALLET_MISMATCH`).

![guarded swap](docs/03-guarded-swap.png)

## How CoinGecko API is used

CoinGecko is the **referee**: the product has no opinion about price except CoinGecko's.

- `GET /simple/token_price/solana` (keyless): USD price, 24h volume and `last_updated_at` for both mints, with `precision=full` so tiny prices (BONK at $0.0000021) are not rounded.
- `GET /onchain/simple/networks/solana/token_price/{mint}` (key): onchain DEX-derived price for tokens CoinGecko does not list; used as the primary reference when available and cross-checked against the aggregated price.
- `GET /coins/solana/contract/{mint}` (keyless): token decimals, needed to convert raw amounts exactly.
- Every report lists each CoinGecko price used with its endpoint, value, volume and timestamp. Nothing in a verdict is invented: figures come from CoinGecko or Jupiter, and the arithmetic is in `src/guard.ts`.
- Attribution: every CLI/MCP output ends with "Price data powered by CoinGecko API" and a link, per the attribution guide.

Measured with the keyless API on 2 Dec 2026: `/onchain/*` returned HTTP 401 without a key, and the keyless limit is easy to hit when running several checks in a row; QuoteGuard caches prices for 20 s and retries 429s with backoff. Use a key for anything beyond a demo.

## How Solana is used

- **Solana mints and amounts**: tokens are Solana SPL mints; amounts are converted with exact integer math from each mint's decimals.
- **Jupiter on Solana**: live swap quotes (routes across Solana DEXes, price impact) and swap-transaction building.
- **Solana transactions**: the guarded flow returns an unsigned **versioned transaction** that is decoded with `@solana/web3.js` (fee payer, signers, instruction count) before anyone is asked to sign it.
- **Solana Agent Kit**: a plugin that gates the kit's own `trade` behind the price check.
- Decimals fall back to a Solana RPC `getTokenSupply` call for tokens CoinGecko does not describe.

## Tests

`npm test` runs 21 offline tests (no network, no keys): amount math, ALLOW / WARN / BLOCK for each reason code, key and demo-plan
request headers, decimals fallbacks, fail-closed behaviour when CoinGecko is down, has no price, or Jupiter has no route,
the unsigned-transaction checks, the Agent Kit plugin, and an end-to-end MCP test that starts the real server over stdio against local stand-ins for CoinGecko and Jupiter.
`node scripts/live-check.mjs` is an opt-in smoke test against the real APIs.

## Limits, stated plainly

- It checks price sanity, not token safety (no honeypot or authority checks). Pair it with a token-security check.
- Keyless mode only knows CoinGecko-listed tokens; unlisted tokens are blocked, not guessed (a CoinGecko key unlocks onchain prices).
- The default thresholds are starting points. CoinGecko's price is an aggregate, so tiny gaps against any DEX quote are normal.
- It never signs or sends. Wiring a signer is the caller's job.
- The Jupiter keyless endpoint rate-limits too; set `JUPITER_API_KEY` for heavier use.

## Layout

```
src/guard.ts       core check + guarded swap + report formatting
src/coingecko.ts   CoinGecko client (keyless / demo / pro, onchain, decimals, cache)
src/jupiter.ts     quote + unsigned swap build
src/mcp.ts         MCP server        src/cli.ts   CLI        src/plugin.ts   Solana Agent Kit plugin
test/              21 tests          demo/        demo.sh + MCP client
```

MIT licensed.

Data: price data powered by [CoinGecko API](https://www.coingecko.com/en/api).