From 4df8bfd26a62f668c6e4dcbd3c5dfc6903cf55dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20=C5=BDuni=C4=8D?= <36313686+gregpr07@users.noreply.github.com> Date: Thu, 25 Aug 2022 09:29:25 +0100 Subject: [PATCH 1/2] heavily optimize PythHttpClient --- src/PythHttpClient.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/PythHttpClient.ts b/src/PythHttpClient.ts index 6e9ad47..d403939 100644 --- a/src/PythHttpClient.ts +++ b/src/PythHttpClient.ts @@ -41,12 +41,14 @@ export class PythHttpClient { const prices = new Array() // 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() const productAccountKeyToProduct = new Map() - const currentSlot = await this.connection.getSlot(this.commitment) accountList.forEach((singleAccount) => { const base = parseBaseData(singleAccount.account.data) From 47afb3f7d17ab09f0c3517e45464c04ebcf442e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20=C5=BDuni=C4=8D?= <36313686+gregpr07@users.noreply.github.com> Date: Sat, 27 Aug 2022 11:12:18 +0100 Subject: [PATCH 2/2] solana has support for unsubscribing events --- src/PythConnection.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/PythConnection.ts b/src/PythConnection.ts index caa1f18..68ac18f 100644 --- a/src/PythConnection.ts +++ b/src/PythConnection.ts @@ -37,6 +37,7 @@ export class PythConnection { priceAccountKeyToProductAccountKey: Record = {} callbacks: PythPriceCallback[] = [] + subscriptionIDs: number[] = [] private handleProductAccount(key: PublicKey, account: AccountInfo) { const { priceAccountKey, type, product } = parseProductData(account.data) @@ -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. */ @@ -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) + } } }