Skip to content

Commit e2a80d3

Browse files
committed
exposing getStrategiesByPairs
1 parent b87ffc0 commit e2a80d3

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/chain-cache/ChainCache.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,25 @@ export class ChainCache extends (EventEmitter as new () => TypedEventEmitter<Cac
216216
return this._strategiesByPair[key];
217217
}
218218

219+
public async getStrategiesByPairs(pairs: TokenPair[]): Promise<
220+
{
221+
pair: TokenPair;
222+
strategies: EncodedStrategy[];
223+
}[]
224+
> {
225+
const result: {
226+
pair: TokenPair;
227+
strategies: EncodedStrategy[];
228+
}[] = [];
229+
for (const pair of pairs) {
230+
const strategies = await this.getStrategiesByPair(pair[0], pair[1]);
231+
if (strategies) {
232+
result.push({ pair, strategies });
233+
}
234+
}
235+
return result;
236+
}
237+
219238
public getStrategyById(id: BigNumberish): EncodedStrategy | undefined {
220239
return this._strategiesById[id.toString()];
221240
}

src/strategy-management/Toolkit.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
EncodedStrategyBNStr,
2929
MatchType,
3030
MatchOptions,
31+
TokenPair,
3132
} from '../common/types';
3233
import { DecimalFetcher, Decimals } from '../utils/decimals';
3334

@@ -327,6 +328,79 @@ export class Toolkit {
327328
return strategies;
328329
}
329330

331+
/**
332+
* Gets all the strategies that belong to pairs in the given list.
333+
* If the cache is synced, it will return the strategies from the cache.
334+
* Otherwise, it will fetch the strategies from the chain.
335+
*
336+
* @param {TokenPair[]} pairs - List of pairs to get strategies for.
337+
*
338+
* @returns {Promise<{
339+
* pair: TokenPair;
340+
* strategies: Strategy[];
341+
* }[]>} An array of pairs and their strategies.
342+
*/
343+
public async getStrategiesByPairs(pairs: TokenPair[]): Promise<
344+
{
345+
pair: TokenPair;
346+
strategies: Strategy[];
347+
}[]
348+
> {
349+
logger.debug('getStrategiesByPairs called', arguments);
350+
351+
let encodedStrategies:
352+
| {
353+
pair: TokenPair;
354+
strategies: EncodedStrategy[];
355+
}[]
356+
| undefined;
357+
358+
if (this._cache) {
359+
encodedStrategies = await this._cache.getStrategiesByPairs(pairs);
360+
}
361+
362+
if (encodedStrategies) {
363+
logger.debug('getStrategiesByPairs fetched from cache');
364+
} else {
365+
logger.debug('getStrategiesByPairs fetching from chain');
366+
encodedStrategies = await this._api.reader.strategiesByPairs(pairs);
367+
}
368+
369+
const decodedStrategies: {
370+
pair: TokenPair;
371+
strategies: (DecodedStrategy & {
372+
id: BigNumber;
373+
encoded: EncodedStrategy;
374+
})[];
375+
}[] = encodedStrategies.map(({ pair, strategies }) => ({
376+
pair,
377+
strategies: strategies.map(decodeStrategy),
378+
}));
379+
380+
const strategies: {
381+
pair: TokenPair;
382+
strategies: Strategy[];
383+
}[] = await Promise.all(
384+
decodedStrategies.map(async ({ pair, strategies }) => ({
385+
pair,
386+
strategies: await Promise.all(
387+
strategies.map(async (strategy) => {
388+
return await parseStrategy(strategy, this._decimals);
389+
})
390+
),
391+
}))
392+
);
393+
394+
logger.debug('getStrategiesByPairs info:', {
395+
pairs,
396+
encodedStrategies,
397+
decodedStrategies,
398+
strategies,
399+
});
400+
401+
return strategies;
402+
}
403+
330404
/**
331405
* Gets the strategies that are owned by the user.
332406
* It does so by reading the voucher token and

0 commit comments

Comments
 (0)