Skip to content

AMM: Automated Market Maker for pod tokens #193

Description

@melvincarvalho

Summary

Add a constant-product AMM (Uniswap v2 style) for trading sats ↔ pod tokens. The pod acts as the pool custodian — no smart contracts needed. Every swap advances the MRC20 trail on Bitcoin.

Why

The current .buy endpoint sells tokens at a fixed rate (--pay-rate). An AMM enables dynamic price discovery based on supply and demand. Combined with multi-token support (#192), this creates a full DEX inside a Solid pod.

Design

Endpoint: POST /pay/.amm

// Swap sats for tokens
{ "action": "swap", "sell": "sat", "amount": 100 }

// Swap tokens for sats
{ "action": "swap", "sell": "PODS", "amount": 10 }

// Add liquidity
{ "action": "add-liquidity", "sats": 1000, "tokens": 100 }

// Remove liquidity
{ "action": "remove-liquidity", "shares": 50 }

// GET /pay/.amm → pool state
{ "reserveSats": 10000, "reserveTokens": 1000, "k": 10000000, "price": 10, "fee": "0.3%", "lpShares": {...} }

Pricing formula

cost = (reserveSats * amount) / (reserveTokens - amount)  // + 0.3% fee to LPs

What already exists

  • MRC20 token trails with transferToken({ from, to, amount }) — user-to-user transfers
  • Webledger for sat balance tracking
  • Pod escrow pattern (secondary market .sell/.swap already does this)
  • NIP-98 auth on all endpoints

Components (~150 LOC)

Component LOC
Pool state (JSON file) ~20
Constant product swap ~30
Slippage protection (maxCost/minReceived params) ~10
Add/remove liquidity ~50
LP share accounting ~30
Fee (0.3% to LPs) ~10

Agent use case

An AI agent can read the pool state (GET /pay/.amm), calculate optimal trade size, and execute — no wallet UI needed. Multi-pod arbitrage becomes possible: if PODS trades at different prices on two pods, agents arb via withdraw/deposit.

UI

A Solid pane (see #184) could visualize the pool: price curve, depth, slippage calculator, LP positions. The server handles atomicity; the pane handles presentation.

Cross-chain AMM: testnet3 ↔ testnet4

The AMM doesn't have to be sats ↔ tokens. It can pool two different chains' coins — making the pod a cross-chain DEX.

Scenario: t3sat ↔ t4sat

Both Bitcoin testnet3 and testnet4 are running. Users deposit UTXOs from either chain. The pod tracks separate balances and the AMM prices one against the other.

Deposit testnet3 UTXO  →  credited as t3sat
Deposit testnet4 UTXO  →  credited as t4sat

Pool: 1000 t3sat × 5000 t4sat = k
Price: 1 t3sat = 5 t4sat

Swap 100 t3sat → receive ~454 t4sat (constant product)

Why this is a great first test

  • Both are free coins — zero risk
  • Tests multi-chain deposits, AMM, and per-chain balances together
  • Creates real price discovery between two testnet economies
  • Any agent with a faucet UTXO can participate
  • Same code paths — both are Bitcoin, both use mempool API

What it requires

The TXO URI already encodes the chain — txo:btc:... can include network info. The deposit handler parses the chain, verifies against the right explorer, and credits the right balance type.

Step LOC
Multi-chain deposits (parse chain from TXO URI) ~40
Per-chain balances in webledger (t3sat, t4sat) ~20
AMM pool between two balance types ~150
Total ~210

Extends to any UTXO chain

The same pattern works for testnet4 ↔ litecoin, testnet4 ↔ signet, or any pair of UTXO chains. The pod becomes a cross-chain exchange — no Ethereum, no wrapped tokens, no bridges. Just two UTXO chains, a pricing formula, and a Solid pod in the middle.

Depends on

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions