Skip to content

Commit 5e8b65b

Browse files
optimize rpc calls (#71)
1 parent 74bc5d2 commit 5e8b65b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/PythConnection.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ export class PythConnection {
121121
* each time a Pyth price account is updated.
122122
*/
123123
public async start() {
124-
let accounts = await this.connection.getProgramAccounts(this.pythProgramKey, this.commitment)
125-
const currentSlot = await this.connection.getSlot(this.commitment)
124+
const accSlotProm = await Promise.all([
125+
this.connection.getProgramAccounts(this.pythProgramKey, this.commitment),
126+
this.connection.getSlot(this.commitment),
127+
])
128+
let accounts = accSlotProm[0]
129+
const currentSlot = accSlotProm[1]
126130
// Handle all accounts once since we need to handle product accounts
127131
// at least once
128132
for (const account of accounts) {

src/PythHttpClient.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ export class PythHttpClient {
5151
const prices = new Array<PriceData>()
5252

5353
// Retrieve data from blockchain
54-
const accountList = await this.connection.getProgramAccounts(this.pythProgramKey, this.commitment)
54+
const [accountList, currentSlot] = await Promise.all([
55+
this.connection.getProgramAccounts(this.pythProgramKey, this.commitment),
56+
this.connection.getSlot(this.commitment),
57+
])
5558

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

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

127-
const currentSlot = await currentSlotPromise
127+
const [accountInfos, currentSlot] = await Promise.all([
128+
this.connection.getMultipleAccountsInfo(priceAccounts, this.commitment),
129+
this.connection.getSlot(this.commitment),
130+
])
131+
128132
for (let i = 0; i < priceAccounts.length; i++) {
129133
// Declare local variable to silence typescript warning; otherwise it thinks accountInfos[i] can be undefined
130134
const accInfo = accountInfos[i]

0 commit comments

Comments
 (0)