Skip to content

Commit

Permalink
optimize rpc calls (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptopapi997 authored Jun 20, 2023
1 parent 74bc5d2 commit 5e8b65b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/PythConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ export class PythConnection {
* each time a Pyth price account is updated.
*/
public async start() {
let accounts = await this.connection.getProgramAccounts(this.pythProgramKey, this.commitment)
const currentSlot = await this.connection.getSlot(this.commitment)
const accSlotProm = await Promise.all([
this.connection.getProgramAccounts(this.pythProgramKey, this.commitment),
this.connection.getSlot(this.commitment),
])
let accounts = accSlotProm[0]
const currentSlot = accSlotProm[1]
// Handle all accounts once since we need to handle product accounts
// at least once
for (const account of accounts) {
Expand Down
14 changes: 9 additions & 5 deletions src/PythHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ export class PythHttpClient {
const prices = new Array<PriceData>()

// Retrieve data from blockchain
const accountList = await this.connection.getProgramAccounts(this.pythProgramKey, this.commitment)
const [accountList, currentSlot] = await Promise.all([
this.connection.getProgramAccounts(this.pythProgramKey, this.commitment),
this.connection.getSlot(this.commitment),
])

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

// Initialize permission field as undefined
let permissionData
Expand Down Expand Up @@ -121,10 +123,12 @@ export class PythHttpClient {
*/
public async getAssetPricesFromAccounts(priceAccounts: PublicKey[]): Promise<PriceData[]> {
const priceDatas: PriceData[] = []
const currentSlotPromise = this.connection.getSlot(this.commitment)
const accountInfos = await this.connection.getMultipleAccountsInfo(priceAccounts, this.commitment)

const currentSlot = await currentSlotPromise
const [accountInfos, currentSlot] = await Promise.all([
this.connection.getMultipleAccountsInfo(priceAccounts, this.commitment),
this.connection.getSlot(this.commitment),
])

for (let i = 0; i < priceAccounts.length; i++) {
// Declare local variable to silence typescript warning; otherwise it thinks accountInfos[i] can be undefined
const accInfo = accountInfos[i]
Expand Down

0 comments on commit 5e8b65b

Please sign in to comment.