Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

heavily optimize PythHttpClient and added solana unsubscribe #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
12 changes: 9 additions & 3 deletions src/PythConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class PythConnection {
priceAccountKeyToProductAccountKey: Record<string, string> = {}

callbacks: PythPriceCallback[] = []
subscriptionIDs: number[] = []

private handleProductAccount(key: PublicKey, account: AccountInfo<Buffer>) {
const { priceAccountKey, type, product } = parseProductData(account.data)
Expand Down Expand Up @@ -106,13 +107,15 @@ export class PythConnection {
this.handleAccount(account.pubkey, account.account, true, currentSlot)
}

this.connection.onProgramAccountChange(
const subscriptionID = this.connection.onProgramAccountChange(
this.pythProgramKey,
(keyedAccountInfo, context) => {
this.handleAccount(keyedAccountInfo.accountId, keyedAccountInfo.accountInfo, false, context.slot)
},
this.commitment,
)

this.subscriptionIDs.push(subscriptionID)
}

/** Register callback to receive price updates. */
Expand All @@ -122,9 +125,12 @@ export class PythConnection {

/** Stop receiving price updates. Note that this also currently deletes all registered callbacks. */
public async stop() {
// There's no way to actually turn off the solana web3 subscription x_x, but there should be.
// Leave this method in so we don't have to update our API when solana fixes theirs.
// In the interim, delete callbacks.
this.callbacks = []

// Unsubscribe from Solana subscriptions
for (const subscriptionID of this.subscriptionIDs) {
this.connection.removeProgramAccountChangeListener(subscriptionID)
}
}
}
6 changes: 4 additions & 2 deletions src/PythHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ export class PythHttpClient {
const prices = new Array<PriceData>()

// Retrieve data from blockchain
const accountList = await this.connection.getProgramAccounts(this.pythProgramKey, this.commitment)
const getProgramAccounts = this.connection.getProgramAccounts(this.pythProgramKey, this.commitment)
const getSlot = this.connection.getSlot(this.commitment)

const [accountList, currentSlot] = await Promise.all([getProgramAccounts, getSlot])

// Populate products and prices
const priceDataQueue = new Array<PriceData>()
const productAccountKeyToProduct = new Map<string, Product>()
const currentSlot = await this.connection.getSlot(this.commitment)

accountList.forEach((singleAccount) => {
const base = parseBaseData(singleAccount.account.data)
Expand Down