Skip to content

Commit edc9a53

Browse files
martykantomasklim
authored andcommitted
chore(blockchain-link): cache solana epoch
1 parent 3c07bce commit edc9a53

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

packages/blockchain-link/src/workers/solana/index.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import type * as MessageTypes from '@trezor/blockchain-link-types/src/messages';
5050
import { CustomError } from '@trezor/blockchain-link-types/src/constants/errors';
5151
import { MESSAGES, RESPONSES } from '@trezor/blockchain-link-types/src/constants';
5252
import { solanaUtils } from '@trezor/blockchain-link-utils';
53-
import { BigNumber, createLazy } from '@trezor/utils';
53+
import { BigNumber, createDeferred, createLazy } from '@trezor/utils';
5454
import {
5555
transformTokenInfo,
5656
tokenProgramsInfo,
@@ -298,6 +298,23 @@ const getAccountInfo = async (
298298
)
299299
.send();
300300

301+
const getEpoch = async (): Promise<number> => {
302+
const cachedEpoch = await request.state.cache.get('epoch');
303+
304+
if (cachedEpoch) {
305+
return cachedEpoch;
306+
}
307+
308+
// for parallel requests we store the promise in the cache immediately
309+
const deferred = createDeferred<number>();
310+
request.state.cache.set('epoch', deferred.promise, 3_600_000);
311+
312+
const { epoch } = await api.rpc.getEpochInfo().send();
313+
deferred.resolve(Number(epoch));
314+
315+
return deferred.promise;
316+
};
317+
301318
const tokenAccounts = (
302319
await Promise.all(
303320
Object.values(tokenProgramsInfo).map(programInfo =>
@@ -355,7 +372,7 @@ const getAccountInfo = async (
355372
owner: accountInfo?.owner,
356373
rent: Number(rent),
357374
solStakingAccounts: stakingData?.stakingAccounts,
358-
solEpoch: stakingData?.epoch,
375+
solEpoch: await getEpoch(),
359376
};
360377
}
361378
}

packages/blockchain-link/src/workers/state.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { CustomError } from '@trezor/blockchain-link-types/src/constants/errors';
22
import type { SubscriptionAccountInfo } from '@trezor/blockchain-link-types';
3+
import { Cache } from '@trezor/utils';
34

45
export class WorkerState {
56
addresses: string[];
67
accounts: SubscriptionAccountInfo[];
78
subscription: { [key: string]: unknown };
9+
cache: Cache;
10+
811
constructor() {
912
this.addresses = [];
1013
this.accounts = [];
1114
this.subscription = {};
15+
this.cache = new Cache();
1216
}
1317

1418
private validateAddresses(addr: string[]) {

packages/blockchain-link/src/workers/utils.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,5 @@ export const getSolanaStakingData = async (descriptor: string, isTestnet: boolea
4343
}
4444
const { result: stakingAccounts } = delegations;
4545

46-
const epochInfo = await solanaClient.getEpochInfo();
47-
if (!epochInfo || !epochInfo.result) {
48-
throw new Error('Failed to fetch epoch info');
49-
}
50-
const { epoch } = epochInfo.result;
51-
52-
return { stakingAccounts, epoch };
46+
return { stakingAccounts };
5347
};

0 commit comments

Comments
 (0)