Skip to content

Commit e926e5f

Browse files
committed
fixup! feat: add stake pools service
1 parent c7ed725 commit e926e5f

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

packages/cardano/src/wallet/lib/stakePoolService.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,17 @@ export const initStakePoolService = (props: StakePoolServiceProps): StakePoolPro
227227
let cachedData: Promise<StakePoolCachedData>;
228228
let fetchingData = false;
229229
let healthStatus = false;
230-
let index: Fuse<{ id: Cardano.PoolId }>;
230+
let fuzzyIndex: Fuse<{ id: Cardano.PoolId }>;
231231

232-
const createIndex = (stakePools: Cardano.StakePool[]) => {
232+
const createFuzzyIndex = (stakePools: Cardano.StakePool[]) => {
233233
const data = stakePools.map(({ id, metadata }) => {
234234
// metadata from BlockFrost API has more fields than required, extracting only the relevant ones to keep cache size small
235235
const { description, homepage, name, ticker } = metadata || {};
236236

237237
return { description, homepage, id, name, ticker };
238238
});
239239

240-
index = new Fuse(data, FUZZY_SEARCH_OPTIONS, Fuse.createIndex(FUZZY_SEARCH_OPTIONS.keys, data));
240+
fuzzyIndex = new Fuse(data, FUZZY_SEARCH_OPTIONS, Fuse.createIndex(FUZZY_SEARCH_OPTIONS.keys, data));
241241
};
242242

243243
const saveData = (data: StakePoolCachedData) => {
@@ -296,7 +296,7 @@ export const initStakePoolService = (props: StakePoolServiceProps): StakePoolPro
296296
stats: { qty: { activating: 0, active, retired: 0, retiring: retiringPools.length } }
297297
};
298298

299-
createIndex(stakePools);
299+
createFuzzyIndex(stakePools);
300300
saveData(data);
301301
cachedData = Promise.resolve(data);
302302
healthStatus = true;
@@ -353,10 +353,10 @@ export const initStakePoolService = (props: StakePoolServiceProps): StakePoolPro
353353
if (pledgeMet) result = result.filter((pool) => pool.pledge <= (pool.metrics?.stake.live || BigInt(0)));
354354

355355
if (text) {
356-
const fuzzy = index.search(text);
356+
const fuzzyResult = fuzzyIndex.search(text);
357357
const idMap = new Map(result.map((pool) => [pool.id, pool]));
358358

359-
result = fuzzy.map(({ item: { id } }) => idMap.get(id)).filter(Boolean) as Cardano.StakePool[];
359+
result = fuzzyResult.map(({ item: { id } }) => idMap.get(id)).filter(Boolean) as Cardano.StakePool[];
360360
}
361361

362362
if (sort) result.sort(getSorter(sort));
@@ -378,8 +378,9 @@ export const initStakePoolService = (props: StakePoolServiceProps): StakePoolPro
378378
// If the cache is present but expired, fetch the data in a fire and forget way.
379379
if (data.lastFetchTime < Date.now() - ONE_DAY) fetchData().catch(console.error);
380380

381-
// Create the index to make the fuzzy search to work against cached data.
382-
createIndex(data.stakePools);
381+
// If the cache is present, create the index to make the fuzzy search to work against cached data.
382+
// The fuzzy index is an in memory Fuse object which can't be serialized and saved in the local storage with the cache.
383+
createFuzzyIndex(data.stakePools);
383384
}
384385

385386
healthStatus = true;

0 commit comments

Comments
 (0)