Skip to main content
The @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.
Charge a fixed amount per request, regardless of input size.
'flat'
Discriminant. Always 'flat'.
string
Fixed price in atomic units, as a decimal integer string.

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)

Computes the price of a request in atomic units using the given pricing model and work estimate. All arithmetic uses 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)

Generates a deterministic HCS-14 Universal Agent Identifier from the agent’s identity fields. The core 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)

Converts an atomic-unit amount into a human-readable string without floating-point arithmetic.

parseAmount(human, decimals)

Parses a human-readable decimal string into atomic units. The inverse of formatAmount(). Uses only integer arithmetic — no floats.
parseAmount() silently truncates fractional digits beyond decimals. For example, parseAmount('0.123456789', 8) returns 12_345_678n, discarding the ninth decimal digit. Always validate user-supplied amounts before passing them to this function.

estimateChatInput(body)

Produces a deterministic work estimate from an OpenAI-style chat completion request body. Uses a simple 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.
Pass the same body object to both estimateChatInput() and the paid call() to ensure the estimate used for quoting matches the actual request, giving you predictable pricing.