@cypher-zk/sdk package exports a set of pure utility functions that let you derive UI state from on-chain data without additional RPC calls. Import them directly alongside your action calls - they are all synchronous and side-effect free.
Phase helpers
marketPhase
Computes the current lifecycle phase of a market from its on-chain account fields. No RPC call is made - pass the already-fetchedMarket account.
A fetched
MarketAccount (or any object satisfying MarketDeadlineInputs). All required fields are timestamps and state.Optional override for the current time in Unix seconds. Defaults to
BigInt(Math.floor(Date.now() / 1000)). Pass a fixed value in tests to simulate specific moments.MarketPhase string literals:
| Phase | When it applies | Available action |
|---|---|---|
"betting" | Active + before closeTime | placeBet |
"awaitingResolve" | Active + before resolutionDeadline | resolveMarket (resolver only) |
"pendingResolution" | PendingResolution + inside challenge window | flagResolution (anyone) |
"awaitingFinalize" | PendingResolution + window elapsed, not disputed | finalizeResolution (anyone) |
"disputed" | PendingResolution + flagged | adminOverrideResolution (admin only) |
"claimable" | Resolved + before claimDeadline | claimPayout |
"refundable" | Active + past resolutionDeadline, before refundDeadline | claimRefund |
"expired" | All deadlines elapsed | adminClaimRemaining (Cyphers team only) |
"cancelled" | State = Closed (no bets placed) | - |
Fee helpers
computeFees
Calculates the protocol fee, LP fee, and net amount for a given stake before submitting a bet. Mirrors the on-chain fee calculation exactly - use this to show users a fee breakdown without an RPC round-trip.Gross bet amount in USDC lamports (6 decimals).
Object with
protocolFeeRateBps and lpFeeRateBps, both numbers in basis points. Fetch live values from client.globalState.fetch().FeeBreakdown:
The original gross amount passed in.
Amount sent to the protocol treasury.
Amount credited to the market creator’s LP position.
Amount actually entered into the betting pool (
amount - protocolFee - lpFee).Question & label helpers
parseEmbeddedOptions
Strips the[Label1|Label2|…] suffix from a MultiOutcome question string. Always call this before rendering a question to avoid showing the bracket notation to users.
The raw on-chain question string, including any
[…] suffix.The question text with the
[…] suffix removed. Safe to render directly.Array of option label strings parsed from the suffix. Empty array if no suffix is present (YesNo markets).
getMarketOptionLabels
Returns the canonical bet-button labels for any market type. Handles YesNo (returns["NO", "YES"]), MultiOutcome with an embedded suffix, and the fallback case where no suffix was provided.
The fetched market account. The
marketType field determines whether YesNo defaults or embedded labels are used.The raw question string including the
[…] suffix. Do not pass the displayQuestion stripped version here.string[] - one label per outcome, in side-index order.
formatOutcome
Returns a human-readable string for the resolved outcome, ornull if the market is not yet resolved.
The fetched market account. Uses
market.outcome and market.marketType.Raw question string including the
[…] suffix for label extraction.string | null - the outcome label, or null when unresolved.
fetchMarketQuestions
Batch-fetches question strings for an array of markets from theirMarketQuestion PDAs (current v3 layout). Keys the result by market PDA base58 address.
An initialised
CypherClient instance.Array of
MarketAccount objects whose questions you want to fetch. The function batches all getMultipleAccounts calls into a single RPC request.Promise<Map<string, string>> - a Map keyed by market PDA base58 string, values are raw question strings (including any […] suffix).
Display name helpers
Use these functions instead of maintaining your own enum-to-string maps.marketCategoryName
marketStateName
marketTypeName
number and fall back to "Unknown(N)" for values not in the enum, so they will never throw.
Cancel eligibility
cancelEligibility
Client-side pre-flight check forcancelMarket. Returns { ok: true, reason: null } when the market is cancellable, or { ok: false, reason: string } with a human-readable explanation.
A partial or full
MarketAccount. Only state and totalBetsCount are read.true when the market can be cancelled.Human-readable explanation when
ok is false. Suitable for displaying directly in a tooltip or error banner.Error parsing
parseCypherError
Extracts a structuredParsedCypherError from any error thrown by SDK action calls. Handles AnchorError, SendTransactionError log parsing, and raw Error messages with Error Number: NNNN strings.
Any value caught in a
catch block.ParsedCypherError | null:
Numeric Cyphers error code (6000–6044).
Human-readable error name matching the
CypherErrorName union.Full error message string from
CYPHER_ERROR_MESSAGES.