# Agentic Flashnet Trading

## ✨ Guide: Agentic Flashnet Trading

Build AI agents that **trade on Flashnet** — the decentralized trading protocol on Spark. Instant swaps between Spark BTC and Spark-based tokens, powered by an offchain orderbook with on-chain settlement.

### What you'll build

An agent that can:

1. **Get swap quotes** between Spark BTC and Spark tokens
2. **Execute trades** on Flashnet via the Xverse swap engine
3. **Run DCA strategies** — periodic small buys at regular intervals
4. **Rebalance a portfolio** between BTC and stablecoins on Spark
5. **Manage treasury** — sweep profits back to cold BTC storage

### Prerequisites

* Xverse Agentic Wallet CLI installed (`npm i -g @secretkeylabs/xverse-wallet-cli`)
* A funded wallet with BTC on Spark (see [Getting Started](/xverse-agentic-wallet/getting-started.md))

### Why Flashnet?

Flashnet is a trading protocol built on Spark that combines an offchain orderbook with fast, low-fee on-chain settlement. For agents, this means:

* **Instant settlement** — swaps complete in seconds, not minutes
* **Low fees** — no Bitcoin L1 confirmation overhead
* **BTC-native** — trade between BTC and Spark-based tokens without leaving the Bitcoin ecosystem
* **Programmatic access** — the Xverse swap engine routes through Flashnet automatically

***

### Step 1: Fund Spark with BTC

Your agent needs BTC on Spark to trade. Check and fund:

```bash
# Check Spark balance
xverse-wallet spark balance --json

# If empty, deposit from L1
xverse-wallet spark deposit --amount 0.005 --yes --json
# Wait ~30 min for confirmations, then:
xverse-wallet spark claim --json
```

Or if you have USDB or other Spark tokens, you're already set to swap.

### Step 2: Get swap quotes

Use `swap quote` to get pricing from Flashnet and other available providers:

```bash
# BTC → USDB on Spark
xverse-wallet swap quote --from spark:BTC --to spark:USDB --amount 0.001 --json

# USDB → BTC on Spark
xverse-wallet swap quote --from spark:USDB --to spark:BTC --amount 50 --json
```

The response includes quotes from all available providers with rates, output amounts, and fees. Your agent can compare and pick the best.

### Step 3: Execute a swap

```bash
# Preview (dry run)
xverse-wallet swap execute --from spark:BTC --to spark:USDB --amount 0.001 --json

# Execute (auto-selects best quote)
xverse-wallet swap execute --from spark:BTC --to spark:USDB --amount 0.001 --yes --json

# Or specify a provider
xverse-wallet swap execute --from spark:BTC --to spark:USDB --amount 0.001 --provider <code> --yes --json
```

#### Options

| Option              | Description                           |
| ------------------- | ------------------------------------- |
| `--from <token>`    | Source token (e.g. `spark:BTC`)       |
| `--to <token>`      | Destination token (e.g. `spark:USDB`) |
| `--amount <value>`  | Amount of the source token            |
| `--provider <code>` | Use a specific provider               |
| `--slippage <pct>`  | Slippage tolerance (percentage)       |
| `--yes`             | Execute (omit for dry run)            |

### Step 4: DCA strategy — periodic buys

A Dollar-Cost Averaging agent buys a fixed amount at regular intervals, regardless of price:

```bash
# Run daily (or on any schedule):
xverse-wallet swap execute --from spark:USDB --to spark:BTC --amount 10 --yes --json
```

**Agent pseudocode:**

```
every 24 hours:
    1. Check USDB balance on Spark
       spark balance --json

    2. If balance >= target_amount:
       swap execute --from spark:USDB --to spark:BTC --amount <target> --yes --json

    3. Log the trade result

    4. Optionally sweep accumulated BTC to cold storage:
       spark withdraw --amount <excess> --speed fast --yes --json
```

### Step 5: Portfolio rebalancing

An agent that maintains a target allocation between BTC and stablecoins:

```bash
# 1. Check current allocation
xverse-wallet portfolio --json

# 2. If overweight BTC, swap excess to USDB
xverse-wallet swap execute --from spark:BTC --to spark:USDB --amount 0.0005 --yes --json

# 3. If underweight BTC, swap USDB back
xverse-wallet swap execute --from spark:USDB --to spark:BTC --amount 25 --yes --json

# 4. Verify new allocation
xverse-wallet portfolio --json
```

### Step 6: Sweep to cold BTC storage

Periodically move profits from Spark to Bitcoin L1:

```bash
xverse-wallet spark withdraw --amount 0.01 --speed fast --yes --json
```

***

### Putting it all together: trading agent loop

```
┌──────────────────────────────────────────────────┐
│               Flashnet Trading Agent              │
│                                                   │
│  1. Check portfolio & Spark balances              │
│     portfolio --json                              │
│     spark balance --json                          │
│                                                   │
│  2. Get quotes for target trade                   │
│     swap quote --from spark:BTC --to spark:USDB   │
│                                                   │
│  3. Execute if conditions met                     │
│     swap execute ... --yes --json                 │
│                                                   │
│  4. Verify result                                 │
│     spark balance --json                          │
│                                                   │
│  5. Periodically sweep to cold BTC storage        │
│     spark withdraw --amount <btc> --speed fast    │
│                                                   │
│  Loop on schedule or market signal.               │
└──────────────────────────────────────────────────┘
```

### Available Spark trading pairs

Run `swap quote` to discover currently available pairs. Common routes:

| From            | To              | Settlement |
| --------------- | --------------- | ---------- |
| `spark:BTC`     | `spark:USDB`    | Instant    |
| `spark:USDB`    | `spark:BTC`     | Instant    |
| Any Spark token | Any Spark token | Instant    |

### What's next

* Combine Flashnet trading with [Lightning commerce](/xverse-agentic-wallet/guides/agentic-lightning-commerce.md) for agents that both trade and accept payments
* Add [Starknet trading](/xverse-agentic-wallet/guides/agentic-starknet-trading.md) for deeper liquidity across AVNU DEX pairs
* Explore [treasury management](/xverse-agentic-wallet/capabilities/portfolio-and-treasury.md) for cross-chain fund sweeping


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.xverse.app/xverse-agentic-wallet/guides/agentic-flashnet-trading.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
