Skip to content

Commit f30613e

Browse files
authored
Merge pull request #1762 from near/v7/speed-up-sequential-promises
perf: speed up some `Account` methods by resolving `Promises` in parallel
2 parents 2911624 + c711103 commit f30613e

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

.changeset/huge-paths-lay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"near-api-js": patch
3+
---
4+
5+
Speed up some `Account` methods by merging sequential `Promises` into `Promise.all()` for slightly better performance

src/accounts/account.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,17 @@ export class Account {
179179
* balance, storage usage, and code hash
180180
*/
181181
public async getState(): Promise<AccountState> {
182-
const protocolConfig = await this.provider.experimental_protocolConfig({
183-
finality: DEFAULT_FINALITY,
184-
});
185-
const state = await this.provider.viewAccount({
186-
accountId: this.accountId,
187-
blockQuery: {
182+
const [protocolConfig, state] = await Promise.all([
183+
this.provider.experimental_protocolConfig({
188184
finality: DEFAULT_FINALITY,
189-
},
190-
});
185+
}),
186+
this.provider.viewAccount({
187+
accountId: this.accountId,
188+
blockQuery: {
189+
finality: DEFAULT_FINALITY,
190+
},
191+
}),
192+
]);
191193

192194
const costPerByte = BigInt(protocolConfig.runtime_config.storage_amount_per_byte);
193195
const usedOnStorage = BigInt(state.storage_usage) * costPerByte;
@@ -272,11 +274,13 @@ export class Account {
272274

273275
const pk = PublicKey.from(publicKey);
274276

275-
const accessKey = await this.getAccessKey(pk);
277+
const [accessKey, block] = await Promise.all([
278+
this.getAccessKey(pk),
279+
this.provider.viewBlock({
280+
finality: DEFAULT_FINALITY,
281+
}),
282+
]);
276283

277-
const block = await this.provider.viewBlock({
278-
finality: DEFAULT_FINALITY,
279-
});
280284
const recentBlockHash = block.header.hash;
281285

282286
const nonce = BigInt(accessKey.nonce) + 1n;
@@ -322,14 +326,15 @@ export class Account {
322326

323327
const pk = PublicKey.from(publicKey);
324328

325-
const accessKey = await this.getAccessKey(pk);
326-
const nonce = BigInt(accessKey.nonce) + 1n;
327-
328-
const { header } = await this.provider.viewBlock({
329-
finality: DEFAULT_FINALITY,
330-
});
329+
const [accessKey, block] = await Promise.all([
330+
this.getAccessKey(pk),
331+
this.provider.viewBlock({
332+
finality: DEFAULT_FINALITY,
333+
}),
334+
]);
331335

332-
const maxBlockHeight = BigInt(header.height) + BigInt(blockHeightTtl);
336+
const nonce = BigInt(accessKey.nonce) + 1n;
337+
const maxBlockHeight = BigInt(block.header.height) + BigInt(blockHeightTtl);
333338

334339
return buildDelegateAction({
335340
receiverId,

0 commit comments

Comments
 (0)