@agora402/shared package is the single source of truth for every data structure and utility function that crosses the boundary between buyers, sellers, and the registry. All types come with runtime validation and full TypeScript inference from the same definition. The utility functions handle pricing arithmetic, amount formatting, UAID generation, and token estimation — all without floating-point arithmetic touching money.
Installation and Import
Types
ServiceListing
The record a seller publishes to the HCS registry topic. One listing describes an entire service: who receives payments, where the service lives, and which endpoints it sells.
EndpointSpec
Describes a single sellable HTTP endpoint within a ServiceListing.
PaymentOptionSpec
One way to pay for an endpoint — a specific combination of network, asset, and pricing model.
PricingModel
A discriminated union of three pricing strategies. All monetary amounts are stored as decimal strings to prevent floating-point precision loss.
- flat
- per-token
- per-unit
QuoteRequest
Sent by a buyer to the seller’s /a2a/quote endpoint to request a price for a specific amount of work.
Quote
Returned by the seller in response to a QuoteRequest. The seller signs the quote so the buyer can verify it was not tampered with in transit.
Receipt
Written to the receipts HCS topic by the seller after every successful payment settlement. See the ReceiptLedger reference for how to read and audit receipts.
Utility Functions
priceFor(model, estimate)
BigInt — tinybars never pass through a float. Token costs are rounded up per 1,000-token block.
Both buyers and sellers call
priceFor() with the same pricing model and the same estimate. This reproducibility is what makes the quote handshake trustworthy: neither side can unilaterally inflate the price.generateUaid(input)
aid component is Base58(SHA-384(canonical JSON)), so the same identity always produces the same UAID and the identifier is collision-resistant.
formatAmount(amount, decimals, symbol)
parseAmount(human, decimals)
formatAmount(). Uses only integer arithmetic — no floats.
estimateChatInput(body)
ceil(characters / 4) heuristic (~4 characters per token for English text). Both buyers and sellers call this function so the estimate is consistent across both sides of the price negotiation.
unknown
required
An OpenAI-style request body. The function reads
body.messages[].content (string or JSON-serialisable) and body.prompt (string fallback). body.max_tokens sets maxOutputTokens; defaults to 256 if absent or non-positive.