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

Optimize rpc calls #71

Merged
merged 1 commit into from
Jun 20, 2023
Merged
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
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