Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@project-serum/anchor": "0.19.1-beta.1",
"@project-serum/serum": "0.13.65",
"@pythnetwork/price-service-client": "1.9.0",
"@pythnetwork/pyth-lazer-sdk": "0.3.2",
"@pythnetwork/pyth-lazer-sdk": "1.0.0",
"@solana/spl-token": "0.3.7",
"@solana/web3.js": "1.92.3",
"@types/bn.js": "5.1.5",
Expand Down
121 changes: 64 additions & 57 deletions src/pythLazerSubscriber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Channel, PythLazerClient } from '@pythnetwork/pyth-lazer-sdk';
import {
JsonOrBinaryResponse,
PythLazerClient,
Channel,
} from '@pythnetwork/pyth-lazer-sdk';
import { DriftEnv, PerpMarkets } from '@drift-labs/sdk';
import { RedisClient } from '@drift/common/clients';
import * as axios from 'axios';
Expand All @@ -13,7 +17,6 @@ export class PythLazerSubscriber {

timeoutId?: NodeJS.Timeout;
receivingData = false;
isUnsubscribing = false;

marketIndextoPriceFeedIdChunk: Map<number, number[]> = new Map();
marketIndextoPriceFeedId: Map<number, number> = new Map();
Expand Down Expand Up @@ -43,17 +46,24 @@ export class PythLazerSubscriber {
}

for (const priceFeedIds of priceFeedIdsArrays) {
const filteredMarkets = markets.filter((market) =>
priceFeedIds.includes(market.pythLazerId!)
const filteredMarkets = markets.filter(
(market) =>
market.pythLazerId !== undefined &&
priceFeedIds.includes(market.pythLazerId)
);
for (const market of filteredMarkets) {
this.marketIndextoPriceFeedIdChunk.set(
market.marketIndex,
priceFeedIds
);
if (market.pythLazerId === undefined) {
throw new Error(
`Pyth Lazer ID not found for market index ${market.marketIndex}`
);
}
this.marketIndextoPriceFeedId.set(
market.marketIndex,
market.pythLazerId!
market.pythLazerId
);
}
}
Expand All @@ -65,52 +75,70 @@ export class PythLazerSubscriber {
return;
}

this.pythLazerClient = await PythLazerClient.create(
this.endpoints,
this.token
);
this.pythLazerClient = await PythLazerClient.create({
urls: this.endpoints,
token: this.token,
numConnections: 3,
});

this.pythLazerClient.addAllConnectionsDownListener(() => {
console.log(`All connections to pyth lazer are down`);
this.receivingData = false;
Comment on lines +85 to +86
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we maybe add a healthy indicator to show that it's unhealthy? then bots can look at it for their own health checks

});

let subscriptionId = 1;
for (const priceFeedIds of this.priceFeedIdsArrays) {
const feedIdsHash = this.hash(priceFeedIds);
this.feedIdHashToFeedIds.set(feedIdsHash, priceFeedIds);
this.subscriptionIdsToFeedIdsHash.set(subscriptionId, feedIdsHash);
this.pythLazerClient.addMessageListener((message) => {
this.receivingData = true;
clearTimeout(this.timeoutId);
switch (message.type) {
case 'json': {
if (message.value.type == 'streamUpdated') {
if (message.value.solana?.data) {
this.feedIdChunkToPriceMessage.set(
this.subscriptionIdsToFeedIdsHash.get(
this.pythLazerClient.addMessageListener(
(message: JsonOrBinaryResponse) => {
this.receivingData = true;
switch (message.type) {
case 'json': {
if (message.value.type == 'streamUpdated') {
if (message.value.solana?.data) {
const feedIdHash = this.subscriptionIdsToFeedIdsHash.get(
message.value.subscriptionId
)!,
message.value.solana.data
);
}
if (message.value.parsed?.priceFeeds) {
for (const priceFeed of message.value.parsed.priceFeeds) {
const price =
Number(priceFeed.price!) *
Math.pow(10, Number(priceFeed.exponent!));
this.feedIdToPrice.set(priceFeed.priceFeedId, price);
);
if (!feedIdHash) {
throw new Error(
`feedIdHash not found for subscriptionId ${message.value.subscriptionId}`
);
}
this.feedIdChunkToPriceMessage.set(
feedIdHash,
message.value.solana.data
);
}
if (message.value.parsed?.priceFeeds) {
for (const priceFeed of message.value.parsed.priceFeeds) {
if (!priceFeed.price || !priceFeed.exponent) {
throw new Error(
`price or exponent not found for priceFeed ${priceFeed.priceFeedId}`
);
}
const price =
Number(priceFeed.price) *
Math.pow(10, Number(priceFeed.exponent));
this.feedIdToPrice.set(priceFeed.priceFeedId, price);
}
}
}
break;
}
default: {
break;
}
break;
}
default: {
break;
}
}
this.setTimeout();
});
this.pythLazerClient.send({
);
this.pythLazerClient.subscribe({
type: 'subscribe',
subscriptionId,
priceFeedIds,
properties: ['price', 'bestAskPrice', 'bestBidPrice', 'exponent'],
chains: ['solana'],
formats: ['solana'],
deliveryFormat: 'json',
channel: this.subscribeChannel as Channel,
jsonBinaryEncoding: 'hex',
Expand All @@ -119,32 +147,11 @@ export class PythLazerSubscriber {
}

this.receivingData = true;
this.setTimeout();
}

protected setTimeout(): void {
this.timeoutId = setTimeout(async () => {
if (this.isUnsubscribing) {
// If we are in the process of unsubscribing, do not attempt to resubscribe
return;
}

if (this.receivingData) {
console.log(`No ws data from pyth lazer client resubscribing`);
await this.unsubscribe();
this.receivingData = false;
await this.subscribe();
}
}, this.resubTimeoutMs);
}

async unsubscribe() {
this.isUnsubscribing = true;
this.pythLazerClient?.shutdown();
this.pythLazerClient = undefined;
clearTimeout(this.timeoutId);
this.timeoutId = undefined;
this.isUnsubscribing = false;
}

hash(arr: number[]): string {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@
"@drift/common@file:./drift-common/common-ts":
version "1.0.0"
dependencies:
"@drift-labs/sdk" "file:../../../.cache/yarn/v6/[email protected]2d85c3d7-8b53-4964-8d6f-189fbf2c6c02-1749598341754/node_modules/@drift/protocol/sdk"
"@drift-labs/sdk" "file:../../Library/Caches/Yarn/v6/[email protected]8a205691-2bd2-4c6b-ac4b-f0d4543d5f2a-1749603527838/node_modules/@drift/protocol/sdk"
"@jest/globals" "29.3.1"
"@slack/web-api" "6.4.0"
"@solana/spl-token" "0.3.8"
Expand Down Expand Up @@ -1687,10 +1687,10 @@
dependencies:
bn.js "^5.2.1"

"@pythnetwork/pyth-lazer-sdk@0.3.2":
version "0.3.2"
resolved "https://registry.yarnpkg.com/@pythnetwork/pyth-lazer-sdk/-/pyth-lazer-sdk-0.3.2.tgz#5837ca483efe7f5380c2f079d9cd4cd20ec3704e"
integrity sha512-YpkbZvoq7aKGA0303QmDA8n9zS/7upQTOrhhi99+SUWXm3WI9PvmZilhYPp/hqHsXzKEieCwlfPqvO+nkCMXdQ==
"@pythnetwork/pyth-lazer-sdk@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@pythnetwork/pyth-lazer-sdk/-/pyth-lazer-sdk-1.0.0.tgz#86d36e621426e5ceea84b0031a31ec4d4ad32075"
integrity sha512-nCbfzHSgIJeg8cxKB5gomx2N3vW1shrPe2rJpKrPW4hezcOqknyzAg5JPKdzea5Zc5zM5MlQ+lc/opVDJp537Q==
dependencies:
"@isaacs/ttlcache" "^1.4.1"
"@solana/buffer-layout" "^4.0.1"
Expand Down