Skip to content

Commit

Permalink
Example cleanup (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipzeta authored Dec 28, 2023
1 parent cfb17d0 commit 8bd03bf
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 218 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Version changes are pinned to SDK releases.

## [1.17.1]

- Example cleanup. ([#336](https://github.com/zetamarkets/sdk/pull/336))
- Fetch min lot sizes from state. ([#331](https://github.com/zetamarkets/sdk/pull/331))

## [1.17.0]
Expand Down
25 changes: 5 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,12 @@ For examples sake, we want to see the orderbook for SOL perps

```ts
const asset = constants.Asset.SOL;
await Exchange.updateOrderbook(asset);

// Orderbook updates automatically using websockets
console.log(Exchange.getOrderbook(asset));

// You can also forcefully poll the orderbook account for an update, although websockets should be sufficient
Exchange.getPerpMarket(asset).forceFetchOrderbook();
```

```ts
Expand Down Expand Up @@ -558,25 +562,6 @@ You can change this via setting `Exchange.pollInterval`.

This will poll `Pricing` and zeta `State` accounts.

### Market orderbook polling

Users can elect to poll markets at a certain frequency too. This has a default poll interval of `constants.DEFAULT_MARKET_POLL_INTERVAL`. (5 seconds).

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.

```ts
// 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);
```

### Client polling and throttle

`CrossClient` has a default poll interval of `constants.DEFAULT_CLIENT_POLL_INTERVAL` (set to 20 seconds).
Expand Down
5 changes: 3 additions & 2 deletions examples/basic/basic-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ async function main() {
utils.displayState();

// Show current orderbook for a market.
await Exchange.updateOrderbook(tradingAsset);
console.log(`${tradingAsset} Market orderbook:`);
console.log(Exchange.getOrderbook(tradingAsset));

Expand All @@ -105,8 +104,10 @@ async function main() {
{ tifOptions: {}, orderType: types.OrderType.LIMIT } // Extra optional parameters
);

// Allow orderbook to update (it uses a websocket subscription)
await utils.sleep(1000);

// See our order in the orderbook.
await Exchange.updateOrderbook(tradingAsset);
console.log(`${tradingAsset} Market orderbook after our order:`);
console.log(Exchange.getOrderbook(tradingAsset));

Expand Down
2 changes: 1 addition & 1 deletion examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"dependencies": {
"@zetamarkets/anchor": "0.26.0-versioned",
"@solana/web3.js": "^1.68.0",
"@zetamarkets/sdk": "1.4.2",
"@zetamarkets/sdk": "1.16.8",
"buffer-layout": "^1.2.2",
"dotenv": "^10.0.0",
"ts-node": "^7.0.1",
Expand Down
113 changes: 10 additions & 103 deletions examples/cranking/cranking-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,19 @@ async function main() {
: 30000
);

// Crank all markets.
/**
* Cranks the serum dex event queue for each zeta market. This will process trades that consist of maker fills.
* All other events are atomically processed at time of call such as taker fills and cancels.
* Functionality here will keep track of markets that are currently being cranked, markets that have empty event queues
* as well as allowing specification of whether only live markets are being cranked.
* This will flush event queues completely upon call.
* This function will poll all market event queues asynchronously so is quite expensive in terms of RPC requests per second.
* Use crankExchangeThrottle if you are running into rate limit issues.
*/
setInterval(
async function () {
assetList.map(async (asset) => {
await crankExchange(asset);
// Use this instead of `crankExchange` if you wish to throttle your cranking.
/*
await crankExchangeThrottled(
asset,
process.env.CRANK_EXCHANGE_THROTTLE_MS
? parseInt(process.env.CRANK_EXCHANGE_THROTTLE_MS)
: 1000
);
*/
await utils.crankMarket(asset);
});
},
process.env.CRANK_EXCHANGE_INTERVAL
Expand Down Expand Up @@ -161,98 +160,6 @@ async function updatePricing() {
);
}

/**
* Cranks the serum dex event queue for each zeta market. This will process trades that consist of maker fills.
* All other events are atomically processed at time of call such as taker fills and cancels.
* Functionality here will keep track of markets that are currently being cranked, markets that have empty event queues
* as well as allowing specification of whether only live markets are being cranked.
* This will flush event queues completely upon call.
* This function will poll all market event queues asynchronously so is quite expensive in terms of RPC requests per second.
* Use crankExchangeThrottle if you are running into rate limit issues.
*/
async function crankExchange(asset: constants.Asset) {
let market = Exchange.getPerpMarket(asset);
let eventQueue = await market.serumMarket.loadEventQueue(
Exchange.provider.connection
);

if (eventQueue.length > 0 && !crankingMarkets[market.marketIndex]) {
crankingMarkets[market.marketIndex] = true;
try {
while (eventQueue.length != 0) {
try {
await utils.crankMarket(asset);
} catch (e) {
console.error(
`Cranking failed on market ${market.marketIndex}, ${e}`
);
}

let currLength = eventQueue.length;

eventQueue = await market.serumMarket.loadEventQueue(
Exchange.provider.connection
);

let numCranked = currLength - eventQueue.length;
console.log(
`[${assets.assetToName(
asset
)}] Cranked ${numCranked} events for market ${market.marketIndex}`
);
}
} catch (e) {
console.error(`${e}`);
}
crankingMarkets[market.marketIndex] = false;
}
}

/**
* Iteratively cranks each market event queue.
* Allows an optional argument for `throttleMs` which is the duration it will sleep after each market crank.
*/
async function crankExchangeThrottled(
asset: constants.Asset,
throttleMs: number
) {
let market = Exchange.getPerpMarket(asset);
let eventQueue = await market.serumMarket.loadEventQueue(
Exchange.provider.connection
);
if (eventQueue.length > 0 && !crankingMarkets[market.marketIndex]) {
crankingMarkets[market.marketIndex] = true;
try {
while (eventQueue.length != 0) {
try {
await utils.crankMarket(asset);
} catch (e) {
console.error(
`Cranking failed on market ${market.marketIndex}, ${e}`
);
}

let currLength = eventQueue.length;

eventQueue = await market.serumMarket.loadEventQueue(
Exchange.provider.connection
);

let numCranked = currLength - eventQueue.length;
console.log(
`[${assets.assetToName(
asset
)}] Cranked ${numCranked} events for market ${market.marketIndex}`
);
}
} catch (e) {
console.error(`${e}`);
}
crankingMarkets[market.marketIndex] = false;
await sleep(throttleMs);
}
}

async function applyFunding() {
let marginAccPubkeys = [];
try {
Expand Down
2 changes: 1 addition & 1 deletion examples/cranking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"dependencies": {
"@zetamarkets/anchor": "0.26.0-versioned",
"@solana/web3.js": "^1.68.0",
"@zetamarkets/sdk": "1.4.2",
"@zetamarkets/sdk": "1.16.8",
"buffer-layout": "^1.2.2",
"dotenv": "^10.0.0",
"ts-node": "^7.0.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/liquidator-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"@zetamarkets/anchor": "0.26.0-versioned",
"@solana/web3.js": "^1.68.0",
"@solana/spl-token": "^0.1.6",
"@zetamarkets/sdk": "1.4.2",
"@zetamarkets/sdk": "1.16.8",
"dotenv": "^10.0.0",
"ts-node": "^7.0.1",
"typescript": "^4.4.3"
Expand Down
Loading

0 comments on commit 8bd03bf

Please sign in to comment.