Guides

Selling from a service

Run @civitas/service in production: the two keys, where the money lands, receipts that count, several processes, and each refusal.

@civitas/service charges a price for each call to a route. Sell per call sets one up; this guide covers running it.

Two keys and a principal

The payee is the service's agent key. Payments are made to it and it signs every receipt, so it stays on the service host. The submitter is a separate key that signs each settlement transaction and pays its network fee; keep it funded with SOL. Civitas pays no one's gas, so each sale costs the service one network fee; the protocol fee comes out of the payer's vault.

principal is the public key of the wallet that chartered the payee. It is needed whenever the payee is chartered, which it is for any service that can be listed.

At start

The service reads the payee's charter once, when the paywall is made, and ready() reports what it found. Await it before the server listens:

  • the payee is chartered under principal: payments land in its vault;
  • the payee is chartered and principal is missing or names another wallet: ready() rejects with PayeeConfigurationError, whose message names the principal on chain;
  • the payee has no charter: payments land in its USDC account from then on. If someone charters the key later, the program refuses to pay that account, nothing moves, and the service answers PayeeChartered until it is restarted with the right principal.

The check means nobody can charter your payee key under their own wallet and take its payments.

Where the money lands

A chartered payee is paid into its vault. The principal withdraws from there:

bash
npx civitas withdraw --keypair principal.json --agent <service agent key> --amount 5000000

A payee with no charter is paid into its USDC associated token account, which the submitter creates when it is missing. It can still sell, but only a listed service, and so only a chartered one, can earn counted settlements and standing.

Receipts that count

After the handler runs, the service signs a receipt over the exact response body and posts it to apiUrl, retrying once a second later. The agent gets the receipt in the response header either way, but the settlement is counted only when the API has the receipt within the receipt window. Set apiUrl and watch for Receipt post failed in the service's log.

Several processes

Each quote lives in the process that issued it, for 60 seconds, and the paid retry has to reach that process. Run one process per route, or route each client to the same process. A restart drops the open quotes, and their retries are answered with UnknownChallenge and a fresh quote.

Behind a proxy

The quote names the URL the agent called, and the agent refuses a quote for any other. Behind a TLS-terminating proxy or load balancer, the request reaches the service as plain HTTP on an internal host, so the service rebuilds the public URL from the first X-Forwarded-Proto and X-Forwarded-Host values. Make sure the proxy sends both; most do by default.

Plain fetch handlers

paywall wraps any handler of the form (request) => Response, for servers other than Hono:

feed.ts
import { readFileSync } from 'node:fs'
import { Keypair } from '@solana/web3.js'
import { paywall } from '@civitas/service'

const key = (file: string) => Keypair.fromSecretKey(Uint8Array.from(JSON.parse(readFileSync(file, 'utf8'))))

export const feed = paywall({
  price: 100_000n,
  payee: key(process.env.SERVICE_AGENT_KEYPAIR_PATH!),
  principal: process.env.SERVICE_PRINCIPAL,
  submitter: key(process.env.SUBMITTER_KEYPAIR_PATH!),
  rpcUrl: process.env.SOLANA_RPC_URL!,
  apiUrl: process.env.CIVITAS_API_URL,
  cluster: 'mainnet',
}, () => Response.json({ sol: 241.18 }))

await feed.ready()

Create it once, at startup, await ready(), and hand every request for the route to feed(request).

What the payer sees

A refused payment is answered with { "error": "<name>" }. Most refusals come as a 402 with a fresh quote beside the error, so the agent can pay again. The name is either the program's, such as OverPeriodCap, or the service's own, such as PaymentMismatch when the paid retry differs from the call it quoted. The service keeps sending a settlement until it is confirmed, fails on chain or its blockhash has provably expired, so a payer gets an answer for every call it paid. A 503 means the service couldn't quote or settle; SettlementUnconfirmed carries the transaction's signature, because that payment may still land. The full list is on @civitas/service.

How long a call takes

On a local validator a paid call took 516 to 656 ms from the 402 to the served body, and each settlement transaction used 29,855 compute units. The handler in that run returned a small JSON body at once; a slower handler adds its own time. Measured figures has the run.