Polyester Python SDK
Official Python SDK for Polyester APIs, built for trading bots, backend jobs, research notebooks, and automation.
Status: 0.5.0, pre-1.0: a minor release may include breaking changes (see
CHANGELOG.md). Proprietary license (not open source).
API-key only; no browser login or JWT flows.
Requires Python 3.11+.
Supported surface
| Capability | Supported |
|---|---|
| Public market data (spot config, trades, candles) | Yes |
| Order book snapshot + realtime | Yes |
| Market overview (list + subscribe) | Yes |
| Order book heatmap | Yes |
| API-key (Ed25519 signature) auth | Yes |
| Wallet / browser login | No |
| Session MFA enrollment and challenges | No |
| Profile (identity subscribe) | Yes |
| API keys (list/get/subscribe/local keypair generation) | Yes |
| Subaccounts (list/get/members/invites/activity/subscribe) | Yes |
| Address book (list/view/create/update/subscribe) | Yes |
| Policies (realtime subscribe) | Yes |
| Guard signer | Yes |
| VIP tiers + status | Yes |
| Spot fee rates | Yes |
| Trading rate limits | Yes |
| Balances, holds, equity history | Yes |
| Orders (create, cancel, modify, batch, cancel-all) | Yes |
| User trades | Yes |
| Triggers | Yes |
| Internal transfers | Yes |
| Transfer history | Yes |
| Deposit addresses | Yes |
| Trading / funding withdraws | Yes |
| Zipper deposit-withdraw config | Yes |
| Chain analytics | Yes |
| Lifecycle flows | Yes |
| Realtime account and market streams | Yes |
| Reference catalogs + wait-for-ready | Yes |
| Qty / price decimal + scaled-int inputs | Yes |
| Social verification | Yes |
| Account resolve / lookup | No |
Rows marked No are intentional for API-key SDKs (use the TypeScript browser client for wallet login and session MFA).
Full cross-language comparison: SDK capability matrix.
Rows marked Yes mean that an SDK wrapper exists; deployment authorization still applies. In particular, social-verification routes may require a JWT session or may not be mounted. Private streams require an Account ID and the corresponding API-key permission. A successful subscribe call means the token exchange and realtime handshake completed. Treat a structured permission denial as non-transient and update the API-key policy before retrying.
Install
PyPI: https://pypi.org/project/polyester-sdk/
pip install "polyester-sdk==0.5.0"
Realtime (Centrifugo) and on-chain Funding helpers are included by default.
The PyPI sdist/wheel do not include tests/. Full pytest (unit, hardening,
live) requires a git checkout of this repository.
For development from a git checkout:
git clone https://github.com/Fabric-Labs/polyester-sdk-python.git
cd polyester-sdk-python
python3.11 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
Examples
Runnable cookbook examples live in the sibling repo
polyester-examples-python
(REST market data, realtime streams, decimal + scaled-int order paths, and bots).
Quickstart
Create an API key in the Polyester app (API in the sidebar). Copy the key id and private key when shown. The private key is only displayed once. Open the key's Permissions, enable Spot trading, select the markets it may trade, and set a maximum order size appropriate for the strategy. For a subaccount-scoped key, attach an API-key policy that grants ledger reads for balances and private balance streams, plus Spot trading for order mutations. This is separate from the subaccount policy: authorization is the intersection of both policies, so configuring only the subaccount policy is insufficient.
import asyncio
from polyester import AsyncPolyester, POLYESTER_DEVNET_ENVIRONMENT
async def main() -> None:
async with AsyncPolyester(
environment=POLYESTER_DEVNET_ENVIRONMENT,
api_key_id="ak_...", # from API key creation
api_private_key="...", # 64-char hex secret from API key creation
default_account_id="...", # Profile → Account ID (see below)
) as client:
overview = await client.market_overview.list(limit=5)
for market in overview.markets:
last = market.last_price.ticks if market.last_price is not None else None
index = market.index_price.ticks if market.index_price is not None else None
print(market.symbol, last, index)
open_orders = await client.orders.list_open()
print(f"{len(open_orders.orders)} open orders")
asyncio.run(main())
Credentials
| Value | Where to find it | Constructor parameter |
|---|---|---|
| API key id | API → create or view key | api_key_id |
| API private key | Shown once when the key is created | api_private_key |
| Account ID | Profile → Account ID | default_account_id |
Pass an environment preset so API, websocket, RPC, and contract pins stay
together. POLYESTER_DEVNET_ENVIRONMENT is the constructor default.
POLYESTER_TESTNET_ENVIRONMENT targets public testnet (api.testnet.polyester.com,
chain 888169). Custom / VPC endpoints use create_polyester_environment or
environment.with_urls(...). api_url / ws_url remain as transport overrides.
Pass all credentials as constructor parameters. The SDK does not read
environment variables unless you pass them in yourself (or use from_env() in
scripts; see below). from_env() also reads POLYESTER_ENV=devnet|testnet.
api_private_key accepts the 64-character hex Ed25519 secret from key creation,
or raw 32-byte key material.
default_account_id is the Account ID string from your Profile page. Use the
value exactly as shown in the app. Do not use an internal numeric id.
An account username is optional; Account-ID-based authentication and private
channel scoping are valid without one.
default_account_id is optional for public market-data calls. It is required for
account-scoped operations such as private realtime channels, bucket transfers, and
some ledger writes.
Automatic request signing gives concurrent identical calls distinct authentication tuples.
Timestamps can lead the local clock by at most five seconds. The async client applies bounded,
cooperative backpressure without blocking the event loop. The low-level synchronous
polyester.auth.sign_request helper instead raises PolyesterRateLimitError immediately when
capacity is exhausted; honor retry_after. Neither path reuses a signature or drifts outside the
API's 10-second freshness window.
Connect resource_exhausted and HTTP 429 responses raise PolyesterRateLimitError. When the
server attaches polyester.ratelimit.v1.RateLimitDetail (top-level Connect detail or nested under
orders.v1.ErrorDetail.rate_limit), inspect error.detail for policy_class, scope,
operation_id, and presence-aware quota fields. retry_after prefers detail.retry_after_ms,
then Retry-After / Retry-After-Ms / Grpc-Retry-Pushback-Ms headers on HTTP paths. Preview
and batch rejections expose the same payload on OrderErrorDetail.rate_limit / batch item
rate_limit. That error payload is distinct from client.rate_limits
(ratelimit.v1.RateLimitService), which returns the public trading quota catalog and
authenticated account / API-key limits.
Authentication patterns
Recommended: explicit parameters
from polyester import AsyncPolyester
client = AsyncPolyester(
api_key_id="ak_...",
api_private_key="...",
default_account_id="YOUR_ACCOUNT_ID",
)
If your deployment stores secrets in environment variables, read them in your application and pass them to the constructor:
import os
from polyester import AsyncPolyester
client = AsyncPolyester(
api_key_id=os.environ["POLYESTER_API_KEY_ID"],
api_private_key=os.environ["POLYESTER_API_PRIVATE_KEY"],
default_account_id=os.environ["POLYESTER_ACCOUNT_ID"],
)
The plain AsyncPolyester(...) / Polyester(...) constructors never implicitly
read os.environ.
Scripts and local tests only: AsyncPolyester.from_env() and
Polyester.from_env() load POLYESTER_API_KEY_ID, POLYESTER_API_PRIVATE_KEY,
POLYESTER_ACCOUNT_ID, and optional POLYESTER_API_URL / POLYESTER_WS_URL
from the process environment. This is a convenience helper, not the primary
integration pattern.
Create and cancel orders
from polyester import AsyncPolyester
from polyester.models import ClientOrderId
async with AsyncPolyester(
api_key_id="ak_...",
api_private_key="...",
default_account_id="YOUR_ACCOUNT_ID",
default_sub_account_id="", # main account; omit subaccount scoping
) as client:
result = await client.orders.create(
symbol="BTC-USDT",
side="buy",
order_type="limit",
tif="gtc", # gtc, ioc, fok, or gtd (gtd requires expires_at)
qty="0.01",
price="100",
post_only=True,
client_order_id="my-bot-001",
)
print(result.status, result.order_id)
await client.orders.cancel(key=ClientOrderId("my-bot-001"))
client_order_id is optional (matches the API). Omit it for one-shot
creates. Set a stable non-empty value when you may need to reconcile after
an ambiguous transport/server failure. Without it you cannot tell whether the
first attempt admitted the order. After an unknown outcome, look up that id
(get / list_open) before creating again. A second create with the same id
is rejected (CONFLICT_DUPLICATE_CLIENT_ORDER_ID) even when the payload
matches. Only create again with that id if reconciliation shows the first
attempt did not admit. When the server knows the order that already holds the
id, the rejection carries it: PolyesterApiError.existing_order_id on a
single create, BatchCreateResultItem.existing_order_id on a batch create
item (empty otherwise).
Client order ids accept 1 to 36 ASCII letters, digits, ., _, :, /, and
-. Batch create, cancel, and replace accept at most 20 items. batch_create
always sends request_id (caller value or a generated batch-create-* key);
that batch-level id is the idempotency boundary, so per-item client_order_id
may be omitted. Treat a cancel
response as an admission acknowledgement and reconcile with list_open before
releasing local state.
Attached take-profit / stop-loss use the same friendly keys as the read
model (trigger_price, trigger_price_source, order_type, limit_price). Child execution is
market (default) or limit, not limit_ioc / limit_fok, and not a
nested child.execution wrapper.
result = await client.orders.create(
symbol="BTC-USDT",
side="buy",
order_type="limit",
tif="gtc",
qty="0.01",
price="100",
post_only=True,
attached_risk={
"take_profit": {"trigger_price": "140", "order_type": "market"},
"stop_loss": {"trigger_price": "80", "order_type": "market"},
"oco": True,
},
)
detail = await client.orders.get(
key=ClientOrderId(result.client_order_id),
include_attached_risk=True,
)
Create sizing is explicit: set exactly one of base qty or max_quote_debit
(a hard all-in quote-debit budget). Decimal/str quote budgets use the pair's
catalog quote_quantity_scale. Typed budgets must use
QuantityDomain.ORDER_QUOTE and embed that scale; construct with
Quantity.from_quote_decimal_str / from_quote_decimal / from_quote_scaled
and validate against client.catalogs.quote_quantity_scale_for_symbol.
quote_scale = client.catalogs.quote_quantity_scale_for_symbol("BTC-USDT")
assert quote_scale is not None # await client.wait_for_catalogs() first
result = await client.orders.create(
symbol="BTC-USDT",
side="buy",
order_type="market",
max_quote_debit=Quantity.from_quote_decimal_str(
"25.00", quote_scale, symbol="BTC-USDT"
),
)
Use orders.preview_order(...) to check current order admissibility and, when
available, resolved base quantity plus protected price bound before submitting.
Preview accepts the same public create kwargs and encodes them as an
OrderIntent (same wire contract as create). It does not return fee or
quote-debit estimates. protected_price_bound is a protective execution
boundary, not an expected fill price. Rejections carry stable labels such as
BAD_QTY plus field violations; evaluated_at_ms records when admission was
evaluated. Preview is not deployed on every API
host, so handle an unimplemented/not-found response and do not make Preview a
prerequisite for order submission.
Market orders are IOC and enforce a slippage-derived execution boundary. See
Market Order Price Protection
before overriding pair defaults. Set at most one of max_slippage_bps or
max_slippage_ticks. max_slippage_bps is 1–10000 for a buy and 1–9999 for a
sell. Omit both to keep the pair default. The fields are rejected on limit
orders and if both are supplied.
result = await client.orders.create(
symbol="BTC-USDT",
side="buy",
order_type="market",
qty="0.01",
max_slippage_bps=25, # or max_slippage_ticks=10; not both
)
Scaled transfer/withdraw AssetAmount inputs must carry their source scale.
AssetAmount.from_scaled(..., scale=None) is accepted for composition only and
fails closed on encoding unless the request's amount_scale /
quantity_scale is explicit.
Use decimal strings or Decimal for human-facing qty / price inputs.
Do not pass floats. ticks on Price means Polyester protocol price units
(fixed 1e9), not market tick-size alignment (server validates tick size).
Catalog hydration resolves symbol → symbol_id and quantity scales for
encoding; pair admission (tick/step/min qty/notional) stays on the API.
Order limits are read from the spot config. client.catalogs.order_limits_for_symbol("BTC-USDT")
(or get_spot_config().pair_order_limits) returns PairOrderLimits with
decimal strings max_limit_qty_base, max_market_qty_base and
max_order_notional_quote (None when the pair has no maximum), and
max_open_orders_per_pair. max_market_notional_quote caps the notional of
one market order, valued at a buy's protection price or a sell's reference
price (None means no cap). client.catalogs.max_open_orders_total (or
SpotConfig.max_open_orders_total) is the cross-pair resting-order limit.
The open-order counts are the venue defaults for a typical account, 0 when
unpublished; an account may have a lower limit of its own.
limits = client.catalogs.order_limits_for_symbol("BTC-USDT")
if limits is not None and limits.max_limit_qty_base is not None:
print("largest limit order:", limits.max_limit_qty_base, "BTC")
orders.preview_order(...) remains the authority for balances, reservations,
liquidity, and other stateful rules.
For bots (scaled integers)
Stay in integer space; no string round-trip:
from polyester import Price, Quantity
result = await client.orders.create(
symbol="BTC-USDT",
side="buy",
order_type="limit",
tif="gtc",
qty=Quantity.from_scaled(1_000_000, scale=8), # already wire units
price=Price.from_ticks(100_000_000_000), # 100.000000000 at 1e9
post_only=True,
)
# Reads expose the same types: order.price.ticks, order.orig_qty.scaled
# orig_qty is the current accepted total and changes after a successful modify.
Compatible values from fills/books can be passed back into writes when the instrument/domain matches.
orders.batch_replace(...) returns an admission receipt, not final execution.
The predecessor ID can be stale after admission; reconcile the successor IDs
and per-item phases by polling get_batch_replace_status. Use
is_batch_replace_settled(status) only to determine that all items have moved
to working, rejected, or terminal, not as a finality signal. Retry an
ambiguous batch request with the same request_id. Each admission and status
item carries action_taken: "replaced" when a successor order was admitted,
"amended" for a cancel-only outcome with no successor (keep tracking
old_order_id until it is terminal).
A replacement order's cum_qty includes the quantity filled by its
predecessors; Order.inherited_cum_qty is that carried-over part (zero for an
order never replaced).
An order says why it ended. A rejected order's reject_reason is the error
code the Orders API returns for the same refusal, without the ERROR_CODE_
prefix (for example POST_ONLY_CROSS or INSUFFICIENT_FUNDS). A canceled
order's cancel_reason is one of USER_REQUEST, IOC_REMAINDER,
MARKET_REMAINDER, STP_EXPIRE_MAKER, STP_EXPIRE_TAKER, STP_EXPIRE_BOTH,
REPLACED, GTD_EXPIRED, QTY_AT_OR_BELOW_FILLED, PAIR_DRAIN,
ATTACHED_RISK_REFUSED, CANCEL_ALL_AFTER, ACCOUNT_REPAIR,
TWAP_ROLLOVER, STRATEGY_CANCEL or SYSTEM. Both are None for every other
status. A number this SDK does not know yet reads UNKNOWN_ERROR_CODE(n) or
UNKNOWN_CANCEL_REASON(n); treat it as a generic refusal or end.
order = (await client.orders.get(key=ClientOrderId(client_order_id))).order
if order is not None and order.reject_reason == "POST_ONLY_CROSS":
... # reprice and resend
cancel_all(...) requests cancellation of every matching order. The result
lists up to 200 of them, oldest first, in cancelled_orders (each with
order_id and client_order_id); total_cancelled counts all of them and
truncated is true when the list is shorter. Each order's final state comes
from the order stream and reads. A dry run lists none. Retrying with the same
request_id within 120 seconds returns the original counts with an empty
cancelled_orders and listing_status="not_retained_read_open_orders"; the
empty list does not mean nothing was cancelled, so read open orders instead.
Your API key needs a policy that allows trading. Spot orders spend trading balance (see below).
User trade fees
UserTrade fee fields are fixed 18-decimal magnitudes of fee_asset
(fee_amount_e18, referral_share_amount_e18), not catalog asset-scaled
integers. Convert e18 → the fee asset's catalog scale before subtracting from a
BUY fill's base quantity.
Magnitudes are unsigned. Treat fee_amount_e18 as a debit unless
fee_is_rebate is true (then it is a credit). Proto3 omits false, so the
rebate flag is sparse on the wire.
client.fees.get_spot_fee_rates() returns the account target's current effective maker/taker
percents per listed symbol (optional symbol_id filter). That is the live effective-rate
surface, not a guaranteed quote of every future fill. The completed user-trade record still
owns the exact charge.
trades.list(after_match_id=...) is a durable fill-replay cursor and requires
symbol or symbol_id.
VIP, spot fees, and trading rate limits
Public catalog reads need no credentials. Authenticated VIP status, effective spot fees, and
account trading limits require an API key. GetVIPStatus has no subaccount selector: JWT and
API-key callers receive the owning root group only. USD amounts and fee percents are decimal
strings. Optional qualification metrics, timestamps, and next-tier thresholds stay omitted
when unset. VIPStatus.settled_volume_quality and average_aop_quality say how complete
each 30-day metric is: status is complete, price_unavailable (the metric is a lower
bound) or incomplete_history, with incomplete_hours out of 720. Both are None when the
assignment carries no qualification evidence. policy_class uses full protobuf enum names
(TRADING_RATE_LIMIT_CLASS_PLACE / _CANCEL). Rate-limit rules expose
vip_tier (not tier).
tiers = await client.vip.list_vip_tiers()
status = await client.vip.get_vip_status()
fees = await client.fees.get_spot_fee_rates()
catalog = await client.rate_limits.get_rate_limit_config()
limits = await client.rate_limits.get_trading_rate_limits()
Address book and social handles
Address-book reads (list_books, list_entries, get_view) and writes
(create_entry, update_entry, delete_entry, tag CRUD) are wrapped.
get_view(minimum_view_revision=...) asks the server for a newer snapshot;
the view and invalidation events expose view_revision.
Create and update accept new_tags so a tag can be created and attached in
the same request. Update is a durable PATCH: pass expected_revision from a
prior read and only the fields you set are selected. When tag_ids is also
set, the resulting set is those ids plus new_tags; otherwise new_tags are
appended to the current tags.
entry = await client.address_book.create_entry(
label="ops",
address="0x…",
polychain_chain_id=1,
new_tags=[{"name": "hot", "color": "#f00"}],
)
entry = await client.address_book.update_entry(
address_book_entry_id=entry.address_book_entry_id,
expected_revision=entry.revision,
new_tags=[{"name": "vip"}],
)
social_verification.start(..., handle="@alice") accepts an optional leading
@ on Twitter handles. The SDK forwards the handle as supplied.
Triggers
triggers.list(status=...) filters by lifecycle status. Valid values:
created, armed, running, completed, cancelled, failed, paused
Invalid status and event-type labels raise PolyesterValidationError.
Connect requests and responses use numeric symbol_id. Public methods still
accept display symbol strings and resolve them through the hydrated catalog
after wait_for_catalogs(). Unknown symbols fail closed. Only
get_spot_config returns both symbol and symbol_id; other Connect
payloads are ID-only. Display symbol on models such as market overview,
fees, triggers, and policy rules is filled from the catalog when available.
Unknown values raise PolyesterValidationError (they do not silently return
an empty list). Response status uses the same labels (British spelling
cancelled).
orders.get(..., include_attached_risk=True) returns policy data on
order.attached_risk (take-profit / stop-loss / trailing-stop). Independently,
include_attached_risk_state=True can return state-only leg wrappers when
policy inclusion is false. Each returned RiskLeg / TrailingStop exposes
that runtime AttachedRiskLegState at .state with status, nanosecond
armed/terminal timestamps, trigger_id, and child_order_id; absent policy
fields remain unset. Order also exposes post_only. Create/modify accept the
same policy shape as a dict or AttachedRisk.
A stop-loss or take-profit with order_type="market" takes an optional
slippage cap, max_slippage_ticks (Q9 ticks) or max_slippage_bps (1–10000
for a buy, 1–9999 for a sell), at most one; without one the pair's default
market slippage applies. A limit child
takes no cap (its limit price bounds it). Either side may use either child.
Trigger reads show the cap as max_slippage (ticks, as a price delta) or
max_slippage_bps; both unset means the pair default.
await client.triggers.create(
symbol="BTC-USDT",
trigger_type="stop_loss",
side="sell",
qty="0.01",
trigger_price="83000",
order_type="market",
max_slippage_bps=50,
)
A standalone trailing stop may sell or buy. A buy trailing stop tracks the
lowest price and fires when the price rises the trailing distance from it.
It requires limit_price, its maximum price: the most its child pays per
unit (a 9-decimal string or Decimal, or Price.from_ticks for ticks),
fixed at create, and at least activation_price when that is set. A sell
trailing stop takes no limit_price. Every trailing stop, standalone or
attached to an order, takes max_slippage_bps from 1 to 9999.
await client.triggers.create(
symbol="BTC-USDT",
trigger_type="trailing_stop",
side="buy",
qty="0.01",
trailing_distance_bps=100,
limit_price="85000",
)
Trigger reads show a buy trailing stop with side="buy",
order_type="limit", time_in_force="ioc" and its maximum price as
limit_price; details.trailing.trough_price is the lowest price tracked so
far (peak_price for a sell), and details.trailing.trigger_price is the
current threshold.
Stop-loss, take-profit and trailing-stop triggers, standalone or attached to
an order, watch the last trade price unless trigger_price_source names
another: "last" or "index". Spot markets have no mark price, so "mark"
raises PolyesterValidationError. Reads report the same label
(Trigger.trigger_price_source, RiskLeg.trigger_price_source,
TrailingStop.trigger_price_source), so a leg read from an order can be
passed back to create unchanged.
await client.triggers.create(
symbol="BTC-USDT",
trigger_type="stop_loss",
side="sell",
qty="0.01",
trigger_price="83000",
trigger_price_source="index",
)
When modifying a trigger, omit activation_price / max_slippage_ticks /
max_slippage_bps to preserve the current values. Send an explicit zero
(Price.from_ticks(0) or 0) to clear an existing activation price or
maximum-slippage cap. Modify max_slippage_bps is 1–9999 (the patch does not
say what it modifies, so it takes the cap shared by trailing stops and
sells); 0 clears it. Trailing distance in bps is 1–10000; trigger modify
accepts 0 to clear. A child's limit price is fixed at create, so modify
takes no limit_price: cancel the trigger and create a new one instead.
triggers.modify(...) and triggers.resume(...) do not need a symbol: without
symbol / symbol_id they target the trigger's own market. When given, it
must be that market, and an API key limited to specific markets must give it.
await client.triggers.modify(trigger_id=trigger_id, trigger_price="82500")
await client.triggers.resume(trigger_id=trigger_id)
orders.list_open(trigger_id=...) / orders.list_history(trigger_id=...)
return only child orders created by that trigger (TWAP/ladder slices).
Trigger-event fire_price is None for time-scheduled TWAP slice fires
(fire_price_ticks is optional on the wire). Canceled and failed triggers
expose typed cancel_reason / failure_reason labels (empty when unset).
Trigger.version is 1 for a trigger's first state and higher for every later
state change. Reads and realtime trigger updates carry the same version for
the same state; list results can lag realtime updates, and an update can
repeat after a reconnect. Keep the copy of each trigger with the highest
version and ignore any copy whose version is not higher than the one you hold.
Balances: funding vs trading
Ledger balances have separate funding and trading buckets per asset.
- An external deposit can stop in funding or continue to trading, depending on its configured route.
- Spot orders spend trading balance.
- Move funds funding → trading in the Polyester UI (Funding → Unified Trading) or on-chain via the funding wallet.
SDK notes:
- Funding → trading: on-chain
TradingGateway.deposit(not an API-key RPC). Either encode calldata or submit a UserOp: pass an owner EOA private key toPolyesterSmartAccount(SDK derives the Polyester Safe; no UI-exported owner key). - Funding → external: on-chain
FundingAccount.withdrawToChain(samepolyester.chain). - Funding → another user's funding wallet: on-chain
FundingAccount.UAssetTransfervia wallet/smart-account signing in the Polyester app (not an API-key RPC). - Trading → funding: prepare and persist an exact API-key signature with
client.trading_withdraws.prepare_api_key_to_funding(...), then callsubmit_prepared(...). One-callcreate_api_key_to_funding(...)is also available. - Trading → external destination check:
await client.trading_withdraws.validate_destination(destination_chain_id=..., destination_address=...)returns user-safevalid/code/message/canonical_destination_addresswithout creating a withdraw (create RPCs remain authoritative). - Trading → trading (another account):
client.internal_transfers.create(...).
from polyester.chain import (
POLYESTER_DEVNET_ENVIRONMENT,
PolyesterSmartAccount,
encode_trading_gateway_deposit,
encode_funding_withdraw_to_chain,
encode_withdraw_destination,
quote_zipper_fee,
)
account = PolyesterSmartAccount(owner_private_key="0x…") # caller-supplied EOA
# Funding → Trading
deposit = encode_trading_gateway_deposit(
trading_gateway=POLYESTER_DEVNET_ENVIRONMENT.contracts.trading_gateway_address,
u_asset_id="0x…",
quantity_scaled=10**18, # 1 USDT at 18 decimals
)
account.send_calls([deposit])
# Funding → external (quote Zipper fee first)
fee = quote_zipper_fee(
chain_id=6, # BSC testnet Zipper id
z_token="0x…",
zipper_endpoint=POLYESTER_DEVNET_ENVIRONMENT.contracts.zipper_endpoint_address,
)
withdraw = encode_funding_withdraw_to_chain(
funding_account=POLYESTER_DEVNET_ENVIRONMENT.contracts.funding_account_address,
chain_id=6,
z_token="0x…",
withdraw_destination=encode_withdraw_destination(address="0x…", is_case_sensitive=False),
z_amount=5 * 10**18,
max_fee=fee.fee + fee.fee // 10,
)
account.send_calls([withdraw])
Whitelist toggles / destination entries / GuardRegistry signer setup are also encoded under
polyester.chain (encode_add_allowed_external_destinations, …).
Pass default_account_id (your Profile Account ID) on the client for bucket
transfers and other account-scoped ledger operations.
Format u128 wire amounts with the public helper (18-decimal scale):
from polyester import format_ledger_u128
print(format_ledger_u128(balance.funding), format_ledger_u128(balance.trading))
Public market data
Public endpoints do not require an API key. Authenticated endpoints use the credentials above.
candles = await client.market_data.get_candles(symbol="BTC-USDT", timeframe="1m", limit=50)
current = await client.market_data.get_current_candle(symbol="BTC-USDT", timeframe="1m")
if current is not None:
print(current.close, current.is_closed)
trades = await client.market_data.get_trades(symbol="BTC-USDT", limit=20)
subscription = await client.market_data.subscribe_trades(symbol="BTC-USDT")
subscription.set_on_error(lambda error: print(f"realtime interruption: {error}"))
async with subscription:
async for trade in subscription:
print(trade.price.ticks if trade.price else None, trade.qty.scaled if trade.qty else None)
break
List and heatmap limit arguments must be integers (booleans and other types
are rejected for wire safety). Optional limits omit when unset so the server
default applies; allowed ranges are enforced by each API, not a global SDK cap.
Response fields named ts_ns are treated as epoch nanoseconds per the API
contract.
get_trades() lists trades newest first. Narrow it with start_time /
end_time (inclusive), side ("buy" or "sell" aggressor), and page with
page_token=result.next_page_token.
To backfill trades a live stream missed, walk by trade ID instead: oldest
first and gap-free. Start from after_trade_id=0 (or the last trade ID you
hold) and pass each result's next_after_trade_id to the next call. Every
trade up to complete_through_trade_id is returned, across trade-ID epoch
ends (epoch_ends). When next_after_trade_id equals the ID you sent,
nothing newer is available yet; retry later with backoff. after_trade_id
cannot be combined with the time range, side or page token.
after = 0
while True:
page = await client.market_data.get_trades(symbol="BTC-USDT", after_trade_id=after, limit=1000)
for trade in page.trades:
handle(trade)
if page.next_after_trade_id == after:
break # caught up; resume later from `after`
after = page.next_after_trade_id
orderbook.get(symbol=...) returns a depth snapshot. When its available is
False, the server is building the book or invalidated it: its levels are
unknown, not empty, so do not read the empty sides as no liquidity; request
the snapshot again later. Only a snapshot carries available: books from
orderbook.create_subscription(...) report the value of the last snapshot the
subscription applied, and deltas, resets included, do not change it.
book = await client.orderbook.get(symbol="BTC-USDT", depth=10)
if not book.available:
... # unknown book: retry later instead of quoting into an empty side
get_candles() returns row-oriented candles newest-first (an incomplete open
candle, when requested, is prepended). get_candles_columns() normalizes its
column response oldest-first. Sort explicitly by ts_sec before feeding a
chronological indicator.
Merged market overview stream (snapshot + live updates). The create call waits
for the WebSocket handshake and initial snapshot. With symbols=[...], the
snapshot and every live or buffered update keep only those markets:
sub = await client.market_overview.create_subscription(
on_error=lambda error: print(f"managed overview failed: {error}")
)
async with sub:
async for markets in sub:
print(len(markets), "rows")
break
Realtime delivery contract
- Realtime is binary-only. The client negotiates the
centrifuge-protobufWebSocket subprotocol and consumes protobuf publications from:protochannels; live orders use the private WebSocket (see Live orders below). ConnectRPC's optional JSON wire mode does not apply to realtime. Inbound WebSocket messages are capped at 4 MiB. - Market trade, user fill (
trades.subscribe), ledger transfer (transfers.subscribe) and trigger event (triggers.subscribe_events) channels publish batches. Each event of a batch is yielded on its own, in order. An event that fails to decode is reported throughset_on_error(...)without dropping the rest of its batch. Both legs of a ledger transfer shareLedgerTransfer.transfer_id; withaccount_codeit identifies a leg, and a repeated leg replaces the earlier copy. A trigger event is identified by(trigger_id, event_type, child_seq, ts_ns). For your own batched channel, userealtime.subscribe_proto_batch(channel, decode_batch=...). - Subscription queues are bounded. If the consumer falls behind, the SDK raises
PolyesterRealtimeOverflowErrorand faults the subscription; it does not silently drop updates. - Reconnects use capped exponential backoff with per-subscription jitter.
set_on_error(...)observes background feed interruptions; async iteration still raises terminal failures. - Orderbook sequence gaps trigger a REST snapshot refresh. Use
on_sequence_gap/on_reconnect/on_snapshot_refreshonorderbook.create_subscription(...)for recovery observability. - Managed snapshot-then-stream subscriptions disable transport auto-reconnect so they can rebuild REST state between reconnect attempts. Buffered publications survive a failed snapshot retry and are merged exactly once after recovery; cancellation closes the replacement socket. Managed create methods await the handshake and initial snapshot before returning.
- Private order stream quantities are raw scaled values. Their
Quantity.scalemay beNone; resolve the hydrated catalog scale withclient.catalogs.base_quantity_scale_for_symbol_id(order.symbol_id)(or by symbol), then callquantity.format(scale). Never guess or inject scale 8.
Live orders
client.orders.subscribe() streams an account's orders over the private
WebSocket (/v1/ws/private, binary protobuf, subprotocol
polyester.v1.proto). It authenticates with the same realtime tokens as the
other private streams. One connection carries every account, and each account
has one server subscription shared by all of its consumers.
from polyester.models import (
PrivateOrdersConnected,
PrivateOrdersDisconnected,
PrivateOrdersError,
PrivateOrdersReady,
PrivateOrdersSnapshot,
PrivateOrdersStale,
PrivateOrdersUnit,
)
orders = {order.order_id: order for order in (await client.orders.list_open()).orders}
subscription = await client.orders.subscribe() # default_account_id, or account_id=...
async with subscription:
async for event in subscription:
if isinstance(event, PrivateOrdersReady):
# The stream now reflects every order read made before the subscribe.
print("ready")
elif isinstance(event, PrivateOrdersSnapshot):
# The full live view: replace local state.
orders = {order.order_id: order for order in event.orders}
elif isinstance(event, PrivateOrdersUnit):
for order in event.orders:
orders[order.order_id] = order # keep the higher version
for order_id in event.removed_order_ids:
orders.pop(order_id, None) # left the live view; not a cancel
for change in event.events:
print(change) # PrivateOrderEvent or PrivateOrderCommandOutcome
elif isinstance(event, PrivateOrdersStale):
print("stale" if event.stale else "caught up")
elif isinstance(event, PrivateOrdersError):
print(f"recovering from {event.kind}: {event.error}")
subscribe()returns once the server confirmed the subscription; the first event isPrivateOrdersConnected. A failure before that raises (for examplePolyesterAuthErrorfor a refused token, orPolyesterRealtimeErrorwithcode == "FORBIDDEN").- Orders are the same
Ordermodel the order reads return. Two copies with the sameversionare identical, and a higherversionis newer. To merge with a read: readlist_open(), subscribe, wait forPrivateOrdersReady, and keep the higher version of each order. - A
PrivateOrderEventsays why an order ended, as the order's own fields do:reject_reasononrejected, andcancel_reasononcanceledand on thereplacedevent of the order a replace ended (REPLACED). Both areNoneotherwise, including on thereplacedevent that installs the successor. PrivateOrdersReadymeans the subscription reached the account's read boundary, sampled when the server received the subscribe: from then on it reflects every acknowledgement given before the subscribe. Wait for it before relying on the stream. It comes once per server subscription, so again after a resubscribe or reconnect, and a consumer that joins an account that is already ready gets it right after its catch-up snapshot.- A sequence gap, an expired token or a dropped connection resumes from the
last applied position. Consumers see
PrivateOrdersDisconnectedand thenPrivateOrdersConnected; a position the server can no longer resume is answered with a freshPrivateOrdersSnapshotthat replaces local state. Outcomes between the two states are not replayed; fills stay readable from trade history. PrivateOrdersStale(stale=True)means the account's live view is temporarily behind; it recovers by itself.- Recoverable failures arrive as
PrivateOrdersErrorevents and throughset_on_error(...). A failure that ends the stream (a refused token, a revoked permission, close code 4400 or 4403) is raised from iteration, withPolyesterRealtimeError.codeor.close_codeset. - Batch-replace progress has no stream: poll
client.orders.get_batch_replace_status(...)and watch each order's revisions on this stream.
Sync client
The sync Polyester client exposes the same service tree and constructor
parameters:
from polyester import Polyester
with Polyester(
api_key_id="ak_...",
api_private_key="...",
default_account_id="YOUR_ACCOUNT_ID",
) as client:
balances = client.balances.list()
Realtime subscriptions are available via subscribe_sync helpers on the sync
client. Private API-key policy snapshots use
client.policies.subscribe_api_policies_sync(...) (async:
await client.policies.subscribe_api_policies(...)).
Testing (contributors)
CI (no network):
python -m pytest tests/unit tests/hardening -q
Live selection (A7): public smoke vs credentialed suites are selectable via markers.
# Public smoke (read-only smoke; no mutation/funded)
python -m pytest -m "public_smoke" -q
# Named-environment pins vs live Zipper catalog (no API key)
python -m pytest tests/integration/test_environment_pins.py -q
# Credentialed live follows POLYESTER_ENV=devnet|testnet (and optional URL overrides)
# POLYESTER_ENV=testnet python -m pytest -m "credentialed and not mutation and not funded" -q
# Credentialed live integration (requires API-key env)
python -m pytest -m "credentialed and not mutation and not funded" -q
# Local L2 hardening only (no network)
python -m pytest tests/hardening -q
Live devnet tests use a local .env file in the test harness only. Fixtures
load values from env and pass them as explicit constructor parameters; the same
pattern application code should use.
cp .env.example .env
# fill in POLYESTER_API_KEY_ID, POLYESTER_API_PRIVATE_KEY, POLYESTER_ACCOUNT_ID
pip install -e ".[dev]"
python -m pytest tests/unit tests/hardening -q
./scripts/test_all.sh # optional: unit + live tiers
./scripts/smoke_realtime.sh # realtime unit + live heartbeat before release
Use python -m pytest (not bare pytest) so tests run in the same venv as pip install.
Set POLYESTER_TEST_MUTATION=1 for state-changing tests. Funded mutations
require both POLYESTER_TEST_MUTATION=1 and POLYESTER_TEST_FUNDED=1. For a
release-certification run, set POLYESTER_TEST_STRICT_LIVE=1; any skipped test
then fails instead of making an incomplete live run appear green. Missing or
malformed credentials must fail under strict live (not soft-skip as green). The
session printer scopes executed/skipped/failed counts to @pytest.mark.integration
tests and enforces POLYESTER_TEST_MIN_EXECUTED (default 5) when credentials are
present.
Legacy stress tests that use non-dry-run cancel_all require
POLYESTER_TEST_ACCOUNT_WIDE_CLEANUP=1. Set it only for a dedicated test
account; those tests may cancel every open order in their selected symbol.
CI requires every public Connect RPC in gen to be wrapped or listed in
sdk-coverage.toml. Contributors: python scripts/check_sdk_coverage.py.
CI fails when sdk-capabilities.json or the README capability table is stale;
refresh both with python scripts/check_sdk_coverage.py --write-capabilities
and commit the result.
Pre-release checklist (realtime changes):
cd polyester-sdk-python
python -m venv /tmp/polyester-pypi-test && source /tmp/polyester-pypi-test/bin/activate
pip install -e ".[dev]"
python -m pytest tests/unit -q
./scripts/smoke_realtime.sh
cd ../polyester-examples-python
pip install -e "../polyester-sdk-python"
python -m pytest -q
python3 examples/04_public_realtime_trades.py
python3 examples/05_public_orderbook_stream.py
Then bump the version, update CHANGELOG.md, build, and publish to PyPI. Install from the new wheel (not editable) and rerun smoke_realtime.sh once to confirm the published artifact.
Lifecycle transaction lookups
One chain transaction can reference multiple lifecycle flows when operations are bundled. Transaction lookups therefore return a page, not one flow:
page = await client.lifecycle.list_flows_by_tx(tx_hash=tx_hash, limit=50)
flow_ids = [flow.intent_id for flow in page.flows]
while page.next_page_token:
page = await client.lifecycle.list_flows_by_tx(
tx_hash=tx_hash,
limit=50,
page_token=page.next_page_token,
)
flow_ids.extend(flow.intent_id for flow in page.flows)
get_flow_by_tx(...) also returns the complete first page. Use
list_flows_by_tx(...) when following pagination.
Changelog
See CHANGELOG.md.
Transport
Connect RPC over HTTP via generated clients in src/polyester/gen/. Wire format
defaults to binary protobuf; pass wire_format="json" for debugging.
Some RPCs may return HTTP 404 on devnet. The SDK raises PolyesterRouteNotFoundError
with a clearer message than [unimplemented]: Not Found. HTTP 501 and Connect
unimplemented raise PolyesterNotImplementedError, the parent class of
PolyesterRouteNotFoundError; neither is retryable.
API-key rejections from the gateway carry a code. A request whose signed
timestamp falls outside the server's skew window raises
PolyesterTimestampSkewError (code == "TIMESTAMP_SKEW"). It is
retryable, and the gateway rejected it before any service saw it: a retry
signs again with a fresh timestamp. If it persists, fix the local clock. For
large bursts, bound concurrency so requests do not wait locally past the skew
window. Other gateway codes (SIGNATURE_INVALID, KEY_DISABLED,
IP_NOT_ALLOWED, ...) raise a non-retryable PolyesterAuthError whose
message is the code. A request whose deadline expires raises PolyesterTimeoutError, a
PolyesterTransportError: retryable, but a mutation may still have been
applied, so reconcile first (mutation_outcome_unknown(error) is true).
Metadata
Release files for polyester-sdk 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| polyester_sdk-0.5.0.tar.gz | 501.1 kB | With provenance | Details for polyester_sdk-0.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| polyester_sdk-0.5.0-py3-none-any.whl | Python 3 | none | any | Details for polyester_sdk-0.5.0-py3-none-any.whl |
Total release size: 1.1 MB
Release files / polyester_sdk-0.5.0.tar.gz
| Download URL | polyester_sdk-0.5.0.tar.gz |
|---|---|
| Size | 501.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
beed61e235b3ef1504e9c37cc9cf3b90726ed7af0dcd9cfe579b42d88d5bd44f
|
|
BLAKE2b-256 checksum How to use checksums |
8981eb6d3a02f6b5da3a7a66177f6d01aaca75ec8e5c00f911fde570eb50ab93
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logRelease files / polyester_sdk-0.5.0-py3-none-any.whl
| Download URL | polyester_sdk-0.5.0-py3-none-any.whl |
|---|---|
| Size | 601.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
196322d4a5f2c81cb5dd62d05be297372874e0a9779871239a90f19ad8515535
|
|
BLAKE2b-256 checksum How to use checksums |
6de991013bd134aadd5df767836879b7b1a9b8115bca707e54ac8e39e690a4f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logSecurity for polyester-sdk 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for polyester_sdk-0.5.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.