@@ -28,6 +28,7 @@ import {
2828 EncodedStrategyBNStr ,
2929 MatchType ,
3030 MatchOptions ,
31+ TokenPair ,
3132} from '../common/types' ;
3233import { 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