🧱
Typescript SDK
We've open sourced our SDK for Devnet and Mainnet. Our SDK is a typescript library that can be used to interact with our Zeta program smart contract.
This page will go over the basics - loading up everything, seeing exchange state, placing/cancelling orders as well as some advanced functionality such as callbacks and events. For extra information, check out:
npm install @zetamarkets/sdk
You can change this via
Exchange.getZetaGroupMarkets(asset).pollInterval
.Users have to subscribe to a market index for polling to be done on it. This is because each market requires 2 RPC requests, so polling all markets can easily hit rate limits if not on a dedicated provider.
// Subscribe to a market index.
Exchange.subscribeMarket(asset, index);
// Unsubscribe to a market index.
Exchange.unsubscribeMarket(asset, index);
// Manually poll a market index.
await Exchange.updateOrderbook(asset, index);
CrossClient
has a default poll interval of constants.DEFAULT_CLIENT_POLL_INTERVAL
(set to 20 seconds).You can change this via
client.setPollInterval()
.This is almost how often the SDK will call
await client.updateState()
, which is the manual way of polling user state.There is a timer that on default fires every 2 seconds, checking the last poll timestamp. If time greater than client.pollInterval has elapsed or there is a pending update, it will poll.
Pending update refers to a margin account websocket change callback. (The SDK subscribes to user
CrossMarginAccount
on CrossClient.load
.)This will do multiple things (
client.updateState()
):- 1.Fetch user margin account (
client.account
). - 2.Update user orders (this will poll the market orderbook for each market that the user has a non zero position or open orders in -
client.getOrders(asset)
). - 3.Update user positions (
client.getPositions(asset)
).
This timer can be modified via
client.setPolling(intervalSeconds)
.Tying into this, the motivation behind this complexity is that if a user is asynchronously placing and cancelling orders across multiple markets, you may receive multiple margin account callbacks across consecutive slots.
If each call back polls relevant markets for the latest user order state (2 polls per market), you can easily hit rate limits.
If
throttle
is set to true
, in CrossClient.load
, then this timer allows users to batch client polling to the next timer interval (i.e. optimistically, 5 consecutive slot updates will only trigger 1 poll).Alternatively,
throttle
can be set to false
, and client.updateState
will be called on every margin account change and ensure you have the latest state at all times.When you want to shut down or restart the client, call this to disconnect the respective websockets.
// Close exchange object.
await Exchange.close();
// Close client object.
await client.close();
Last modified 4mo ago