diff --git a/sdk/examples/phoenix.ts b/sdk/examples/phoenix.ts index 7eea7ffca..ad90d45d0 100644 --- a/sdk/examples/phoenix.ts +++ b/sdk/examples/phoenix.ts @@ -1,38 +1,53 @@ import { Connection, PublicKey } from '@solana/web3.js'; -import { BASE_PRECISION, PRICE_PRECISION, PhoenixSubscriber } from '../src'; +import { BASE_PRECISION, L2Level, PRICE_PRECISION, PhoenixSubscriber } from '../src'; import { PROGRAM_ID } from '@ellipsis-labs/phoenix-sdk'; export async function listenToBook(): Promise { const connection = new Connection('https://api.mainnet-beta.solana.com'); - const phoenixSubscriber = new PhoenixSubscriber({ - connection, - programId: PROGRAM_ID, - marketAddress: new PublicKey( - 'Ew3vFDdtdGrknJAVVfraxCA37uNJtimXYPY4QjnfhFHH' - ), - accountSubscription: { - type: 'websocket', - }, - }); - - await phoenixSubscriber.subscribe(); - - for (let i = 0; i < 10; i++) { + for (const market of [ + '4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg', // SOL/USDC + 'Ew3vFDdtdGrknJAVVfraxCA37uNJtimXYPY4QjnfhFHH', // ETH/USDC + '2sTMN9A1D1qeZLF95XQgJCUPiKe5DiV52jLfZGqMP46m', // PYTH/USDC + 'BRLLmdtPGuuFn3BU6orYw4KHaohAEptBToi3dwRUnHQZ', // JTO/USDC + ]) { + const phoenixSubscriber = new PhoenixSubscriber({ + connection, + programId: PROGRAM_ID, + marketAddress: new PublicKey(market), + accountSubscription: { + type: 'websocket', + }, + }); + + await phoenixSubscriber.subscribe(); + const bids = phoenixSubscriber.getL2Levels("bids"); const asks = phoenixSubscriber.getL2Levels("asks"); - console.log("bids"); - for (const bid of bids) { - console.log(bid.price.toNumber() / PRICE_PRECISION.toNumber(), bid.size.toNumber() / BASE_PRECISION.toNumber()); + let bid: L2Level | null = null; + for (const b of bids) { + bid = b; + break; } - console.log("asks"); - for (const ask of asks) { - console.log(ask.price.toNumber() / PRICE_PRECISION.toNumber(), ask.size.toNumber() / BASE_PRECISION.toNumber()); + let ask: L2Level | null = null; + for (const a of asks) { + ask = a; + break; } - await new Promise((r) => setTimeout(r, 2000)); + + console.log("market", market); + console.log( + (bid?.size.toNumber() || 0) / BASE_PRECISION.toNumber(), + (bid?.price.toNumber() || 0) / PRICE_PRECISION.toNumber(), + "@", + (ask?.price.toNumber() || (1 << 53) - 1) / PRICE_PRECISION.toNumber(), + (ask?.size.toNumber() || 0) / BASE_PRECISION.toNumber() + ); + console.log(); + + await phoenixSubscriber.unsubscribe(); } - await phoenixSubscriber.unsubscribe(); } (async function () {