Skip to content

SDK

@sagitta/sdk is the one TypeScript package every Sagitta client runs on: sagitta.broker itself, the API, the keeper and the agent CLIs. It uses viem and nothing else that touches the network, and it needs no browser, Node or Bun API, so the same code runs everywhere.

It never holds a key and never sends anything. Reads take a viem PublicClient you create; builders hand back transactions for your own wallet to sign.

The package lives in Sagitta’s repository at packages/sdk and is not on npm. Without it, POST /api/arena builds the same transactions over HTTPS (see The trading API), and the contracts take the same calls directly (see Exit without the site).

The SDK names no contract address of its own. You pass a deployment: the chain id, the wrapped native coin (WHYPE) and the core contracts (Registry, MarketFactory, the Market router and its four facets, the SideToken implementation, BurnVault, timelock), plus Multicall3 if you want reads batched. The deployment record in packages/config holds all of them, and elysiumSagittaDeployment turns it into one, adding the signer set, every relayed feed and the burn’s contracts, which the payee guard refuses too. A build carries only the records its own code imports.

import { createPublicClient, http } from "viem";
import { ELYSIUM_DEPLOYMENT } from "@sagitta/config/deployments/99901";
import { ELYSIUM_INFRA } from "@sagitta/config/elysium";
import { elysiumSagittaDeployment, readMarket, tx, txContext, verifyTx } from "@sagitta/sdk";
const client = createPublicClient({ chain, transport: http(rpcUrl) });
// the mainnet record from packages/config (null until the deploy), with Multicall3 for batched reads
const deployment = elysiumSagittaDeployment(ELYSIUM_DEPLOYMENT!, ELYSIUM_INFRA.multicall3);
const view = await readMarket(client, market, { registry: deployment.core.registry }, deployment);
const ctx = txContext(deployment);
const mint = tx.commitMint(ctx, { market, side: 0, value: 5n * 10n ** 16n, recipient: me, refundTo: me }); // the 0.05 HYPE minimum
const ready = await verifyTx(client, ctx, mint); // required: the payee guard's on-chain half
// ready is { to, data, value, chainId, summary }: hand it to your wallet

Each read is typed from the contracts’ own ABIs and batched through Multicall3 when the deployment or the client’s chain names it. Every read sees one block: a batch goes in one Multicall3 call of up to 128 reads, and a bigger batch, a read without Multicall3, or a read that takes several calls (tickets, accounts, epochs) pins a block first.

Function What it returns
readMarket, readMarkets A market’s kind, feed, status, version, leverage, both pots, supplies and NAVs (navE36, wei per share times 1036), each side’s epoch and token, the SideToken implementation its factory clones, the queue, owed and accrued totals, and leverage now: L × the other pot ÷ your pot. Eight reads per market, nine with the mint gate
readEpochs, readAllEpochs Every epoch of a side: its token and, for a closed epoch, its conversion pool. See Epochs
readTickets, readTicket The pending tickets and where each stands: waiting, due (its reading has been applied) or forced (the market froze first)
readOwed What an account is owed on each market: bounties, referral fees, failed pushes
readMintPreview, readRedeemPreview, readConvertPreview The Market’s own previews
readAccount An account’s owed HYPE, referrer and holdings on both sides, older epochs’ tokens included with what each converts to now
readWork Each market’s needsWork(): the keeper work list
readListingTerms The factory’s seed, version and whether it lists, and a feed’s listing count

quoteMint, quoteRedeem, quoteConvert and quoteExit compute the Market’s previews locally from a market view, so a board can quote every market without a call per quote. They run the same settlement arithmetic as the contracts: a shared set of test vectors pins the TypeScript to the Solidity bit for bit, and a check against the live contracts compares every quote with the on-chain preview at the same block.

A quote is what a ticket would get at the market’s state now. The ticket fills at the first reading after its commit, whose move can still change the result; see Delayed NAV. An exit of an older epoch’s tokens pays what converting and then redeeming pays.

kernelParams(view) hands the market’s own constants to the what-if table, so its numbers use the market’s parameters rather than defaults.

Every builder returns { to, data, value, chainId } and a summary: the action, one line of text for a confirm dialog, the market, side, amount and epoch, and every address the call pays. The chain id comes from your deployment. A built transaction is not ready to send until verifyTx has checked it (see the payee guard).

Who Builders
Traders commitMint, commitRedeem, commitRedeemEpoch (an older epoch’s tokens), convert, claim, claimFor, donate, reclaim
Keepers execute, poke, retire, collectFees
Listers createFeedMarket, with readListingSalts (or listingSalts) for the side-token salts, and listerSalt and checkSalt beneath them

Every address a call pays is named: a mint’s or redemption’s recipient and refundTo, a claim’s to, an execute’s beneficiary. The Market would read a zero refundTo as the sender; the builders never send one. convert pays its sender and names no one. A listing checks both salts against the lister (a salt starts with the lister’s address or with 20 zero bytes) and sends the factory’s exact seed. listingSalts(lister, listingKey, nonce) derives a listing’s two salts from the listing key and the key’s listing nonce, so the same listing always gets the same ones and each later listing new ones; readListingSalts reads the nonce from the Registry and steps past any nonce whose side tokens already exist.

wireTx gives the API’s form of a verified transaction, with the value as a decimal string. It takes only what verifyTx returned.

simulateTx(client, from, tx) runs a transaction as an eth_call and returns its data, or the Sagitta error it reverted with. simulatePush(client, market, recipient, pushStipend) says whether the market’s HYPE push to a recipient would land. It runs the push the way the market does, with the same gas, so a smart wallet gets the answer the real push gives. A push that fails isn’t lost: the HYPE waits in owed until the recipient calls claim(to).

A market refuses a fixed list of payees itself: zero, itself, its own side tokens of every epoch so far and the next one, its relayed feed, the factory, the registry, the burn vault, Multicall3, WHYPE, the Market router it is a clone of and the router’s four facets. It can’t cheaply refuse another market or that market’s side tokens. A mint paid to one strands the shares, and HYPE paid to one stays owed to it for good, because a contract like that never claims. Neither can the signer set, a relayed feed or one of the burn’s contracts, which a market’s list leaves out too.

So the SDK refuses those payees in two steps, and both are required:

  1. Offline, in every builder, before it encodes anything. It uses the deployment and the markets you pass in (normally the indexer’s list): every core contract and the router’s four facets, WHYPE and Multicall3, the signer set and every relayed feed the deployment record names (HYPE in US dollars and each launch feed), the burn’s contracts (V4StateRoots, the pool oracle, the price source and the burner), each known market, its tokens, its feed, and each side’s token addresses up to eight epochs past the current one. Those don’t exist yet; the SDK computes where each market’s own factory will deploy them. The market the call goes to is always included.
  2. On chain, with verifyTx. It reads the market and the payees back out of the calldata, so it checks the call itself rather than its summary. The target must be a market the registry knows, and no payee may be a market the registry or the factory knows, a contract whose market() names one, or a contract whose owner() is the signer set. That covers every side token of every epoch of every market, and every relayed feed, one listed after the record was written included. A read that doesn’t answer, such as a rate-limited or timed-out call, fails the check. Only a revert or an empty reply counts as “not a side token” or “no owner”.

verifyTx returns the transaction, frozen, as a VerifiedTx. wireTx, which gives the form the API sends, takes nothing else, so a transaction that skipped step 2 can’t leave through it.

A refusal throws PayeeRefused, which names the argument, the address and what it is.

errorSelectors() maps every Sagitta error’s 4-byte selector to its name, and decodeSagittaError(data) decodes revert data from any Sagitta contract with its arguments. That includes the errors Solady raises from assembly, such as ETHTransferFailed when claim(to) names an address that refuses HYPE (the native coin, whatever the error’s name). sagittaEventsAbi holds every event of every contract once, for decoding logs.

The ABIs (marketAbi, marketFactoryAbi, registryAbi, burnVaultAbi, sideTokenAbi, timelockAbi, and for the relay signerSetAbi and relayedFeedAbi) are generated from the contracts’ build, with the errors only the deployed code declares added in. CI fails when they drift from the contracts.