# Pricing and the wallet Two prices and one wallet, all set by the operator, all changeable remotely. | setting | meaning | | --- | --- | | `swap_fee_percent` / `swap_fee_bps` | the margin to list a swap, as a percentage of the BTC leg (0.5 means 0.5%, stored as 50 basis points) | | `min_fee_sats` | the floor, so a tiny swap still costs something | | `relay_sats_per_day` | what tunnel access costs per day, in sats | | `wallet` | a NIP-47 (Nostr Wallet Connect) connection string | The margin is the gateway operator's own number: the software computes the fee and does not negotiate it. It is bounded at 20% (`MaxFeeBPS`), because a mistyped `50` where `0.5` was meant is a fee nobody agreed to — the bound is enforced, not documented. ## Default: free Nothing is charged until an operator sets a price. A gateway with no margin lists swaps for nothing, a relay with no `rate_sats_per_day` admits every gateway it authorises, and a missing or unreadable ledger is treated as "not configured" rather than as a lockout. Charging is always a deliberate act. ## Setting them On a gateway, over the control channel that already updates it: ``` nostrino-admin -key admin.key -gateway -relay wss://swaprelay.smesh.lol \ -cmd billing-set -arg swap_fee_percent=0.5 -arg min_fee_sats=10 -arg relay_sats_per_day=1000 nostrino-admin ... -cmd billing # what it charges now, and a worked example ``` The reply carries the prices plus an example priced at one million sats, so an operator can check the arithmetic without doing it: ``` "example": {"btc_sats": 1000000, "listing_fee_sats": 5000, "swap_fee_bps": 50} ``` The wallet string is validated when set and stored exactly as given, in `/billing.json` with mode 0600. It is never echoed back: the reply says `"wallet_configured": true` and the wallet's public key and relay, which is enough to confirm it and useless to a thief. ## Failure modes that matter - An unknown setting is refused, not ignored. A typo that silently leaves the old price in place is worse than an error message. - A negative or out-of-range price is refused. - A missing file is an empty configuration: a new gateway starts charging nothing. - `health.billing` reports the same redacted view, so monitoring can watch the price without reading the file. ## The wallet `pkg/nwc` is a small NIP-47 client: `pay_invoice`, `make_invoice`, `get_balance`, `get_info`, over NIP-44 encrypted requests on kind 23194 and answers on 23195. The connection string is the whole integration — no node RPC, no macaroon, and the wallet side can revoke the connection. The way the money is meant to flow, and what is still to build: ``` maker --fee to list a swap--> gateway --sats/day for the tunnel--> relay taker --fee to close it-----> gateway ``` What exists now is the configuration and the client that can move sats. What does not exist yet is enforcement: the gateway quotes a fee but does not yet withhold a listing until it is paid, and the relay does not yet refuse an unpaid gateway. That is the next piece, and it needs both ends to agree on a payment reference before it can be switched on for either. ## Paying a relay: keysend with the gateway's key as the preimage A relay charges a gateway for the tunnel, at `relay_sats_per_day`. The payment is a **keysend**, and the preimage is chosen by the payer — 32 bytes, exactly the size of a Nostr public key — so the gateway uses **its own public key as the preimage**. That single choice removes the whole account layer: - the relay reads its wallet's incoming payments, sees the preimage, and knows which gateway paid; - the amount divided by the daily rate is how many seconds of access it bought; - no reference field, no invoice to correlate, no second message. `pkg/nwc` sends it with `PayKeysend(ctx, relayPubkey, gatewayPubkey, sats)`; the relay side is `billing.Ledger`, which credits time and answers `Paid`/`Remaining`. Two details that matter: - **Renewing early extends, it does not replace.** The new period starts at the end of the paid one. - **Crediting is idempotent by settle time.** Payments from one gateway share a preimage, so the wallet cannot tell two of them apart by preimage; the ledger keeps a per-gateway watermark of the newest settle time it has credited, so a rescan of the transaction list buys no extra time. A payment that is less than one second of access is refused rather than rounded to nothing. ## Listing a swap: invoice first, listing after When a margin is configured, the gateway does not republish a listing on the maker's behalf until it has been paid: ``` maker asks to list a swap (swap id, BTC leg) gateway: fee = margin × BTC leg, floored at min_fee_sats gateway: make_invoice(fee, description = "listing ") → bolt11 maker pays the bolt11 gateway: lookup_invoice(payment_hash) → settled_at gateway: publish the listing ``` The invoice's payment hash is the reference: the listing is bound to the payment, so a payment cannot be reused for a second listing and a listing cannot be published without one. Until `lookup_invoice` reports it settled, the swap is not lodged — which is the behaviour asked for, and the reason the quote is an invoice rather than a bare number. ## Blocking a paid gateway Payment buys time, not permission. A relay that has taken sats for a period may still refuse a gateway — for abuse, for a policy change, or because the operator says so — and the honest form of that is: stop serving, keep the record of what was paid, and let the operator settle it with the gateway. The ledger therefore stores `TotalSats` and `PaidUntil` independently of whether the gateway is currently allowed, and an operator block is a separate decision that does not erase the payment history. Complaints about that decision belong to the relay operator, who is the one being paid; the software does not try to arbitrate them. ## What is wired now - **Relay control (kind 23199, intercepted, never stored):** `t=subscription` answers the signer with `{paid, remaining_seconds, paid_until, rate_sats_per_day, blocked?}`; `t=config` (admin keys from `TORLY_ADMINS`) applies `rate_sats_per_day`, `block` and `unblock` to the ledger. - **Relay gate:** kinds 30333 and 23197 are refused unless the gateway is paid, with the operator's block list checked first. A relay whose ledger has no rate charges nobody and admits everybody it authorises. - **Credits loop:** `nostrino-relay-billing -wallet-file … -ledger …` scans the relay wallet's incoming payments and credits each gateway by its preimage, writing the ledger the relay reads. - **Gateway side:** `-cmd subscription` asks the relay for the countdown, `health.relays` carries it with a one-line warning (files outstanding time, lapsed, blocked), and `-cmd listing-quote` / `-cmd listing-settled` / `-cmd listings` implement the invoice-first gate: a quote records the invoice and its payment hash, and a listing is reported lodgable only after the wallet says the invoice settled. A zero fee is free and needs no invoice.