Skip to content

Commit

Permalink
add example code for each market
Browse files Browse the repository at this point in the history
  • Loading branch information
jarry-xiao committed Dec 21, 2023
1 parent 4d5e0ac commit ce9d015
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions sdk/examples/phoenix.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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 () {
Expand Down

0 comments on commit ce9d015

Please sign in to comment.