src/redux/providers/tokensActions.js
function fetchTokensData (tokenRegContract, tokenIndexes) {
  return (dispatch, getState) => {
    const { api, tokens } = getState();
    const allTokens = Object.values(tokens);

    const tokensIndexesMap = allTokens
      .reduce((map, token) => {
        map[token.index] = token;
        return map;
      }, {});

    const fetchedTokenIndexes = allTokens
      .filter((token) => token.fetched)
      .map((token) => token.index);

    const fullIndexes = [];
    const partialIndexes = [];

    tokenIndexes.forEach((tokenIndex) => {
      if (fetchedTokenIndexes.includes(tokenIndex)) {
        partialIndexes.push(tokenIndex);
      } else {
        fullIndexes.push(tokenIndex);
      }
    });

    log.debug('need to fully fetch', fullIndexes);
    log.debug('need to partially fetch', partialIndexes);

    const fullPromise = fetchTokensInfo(api, tokenRegContract, fullIndexes);
    const partialPromise = fetchTokensImages(api, tokenRegContract, partialIndexes)
      .then((imagesResult) => {
        return imagesResult.map((image, index) => {
          const tokenIndex = partialIndexes[index];
          const token = tokensIndexesMap[tokenIndex];

          return { ...token, image };
        });
      });

    return Promise.all([ fullPromise, partialPromise ])
      .then(([ fullResults, partialResults ]) => {
        log.debug('fetched', { fullResults, partialResults });

        return []
          .concat(fullResults, partialResults)
          .filter(({ address }) => !/0x0*$/.test(address))
          .reduce((tokens, token) => {
            const { id } = token;

            tokens[id] = token;
            return tokens;
          }, {});
      })
      .then((tokens) => {
        dispatch(setTokens(tokens));
      });
  };
}
function fetchTokensData (tokenRegContract, tokenIndexes) {
  return (dispatch, getState) => {
    const { api, tokens } = getState();
    const allTokens = Object.values(tokens);

    const tokensIndexesMap = allTokens
      .reduce((map, token) => {
        map[token.index] = token;
        return map;
      }, {});

    const fetchedTokenIndexes = allTokens
      .filter((token) => token.fetched)
      .map((token) => token.index);

    const fullIndexes = [];
    const partialIndexes = [];

    tokenIndexes.forEach((tokenIndex) => {
      if (fetchedTokenIndexes.includes(tokenIndex)) {
        partialIndexes.push(tokenIndex);
      } else {
        fullIndexes.push(tokenIndex);
      }
    });

    log.debug('need to fully fetch', fullIndexes);
    log.debug('need to partially fetch', partialIndexes);

    const fullPromise = fetchTokensInfo(api, tokenRegContract, fullIndexes);
    const partialPromise = fetchTokensImages(api, tokenRegContract, partialIndexes)
      .then((imagesResult) => {
        return imagesResult.map((image, index) => {
          const tokenIndex = partialIndexes[index];
          const token = tokensIndexesMap[tokenIndex];

          return { ...token, image };
        });
      });

    return Promise.all([ fullPromise, partialPromise ])
      .then(([ fullResults, partialResults ]) => {
        log.debug('fetched', { fullResults, partialResults });

        return []
          .concat(fullResults, partialResults)
          .filter(({ address }) => !/0x0*$/.test(address))
          .reduce((tokens, token) => {
            const { id } = token;

            tokens[id] = token;
            return tokens;
          }, {});
      })
      .then((tokens) => {
        dispatch(setTokens(tokens));
      });
  };
}
function fetchTokensData ( tokenRegContract , tokenIndexes ) { return ( dispatch , getState ) => { const { api , tokens } = getState ( ) ; const allTokens = Object . values ( tokens ) ; const tokensIndexesMap = allTokens . reduce ( ( map , token ) => { map [ token . index ] = token ; return map ; } , { } ) ; const fetchedTokenIndexes = allTokens . filter ( ( token ) => token . fetched ) . map ( ( token ) => token . index ) ; const fullIndexes = [ ] ; const partialIndexes = [ ] ; tokenIndexes . forEach ( ( tokenIndex ) => { if ( fetchedTokenIndexes . includes ( tokenIndex ) ) { partialIndexes . push ( tokenIndex ) ; } else { fullIndexes . push ( tokenIndex ) ; } } ) ; log . debug ( 'need to fully fetch' , fullIndexes ) ; log . debug ( 'need to partially fetch' , partialIndexes ) ; const fullPromise = fetchTokensInfo ( api , tokenRegContract , fullIndexes ) ; const partialPromise = fetchTokensImages ( api , tokenRegContract , partialIndexes ) . then ( ( imagesResult ) => { return imagesResult . map ( ( image , index ) => { const tokenIndex = partialIndexes [ index ] ; const token = tokensIndexesMap [ tokenIndex ] ; return { ... token , image } ; } ) ; } ) ; return Promise . all ( [ fullPromise , partialPromise ] ) . then ( ( [ fullResults , partialResults ] ) => { log . debug ( 'fetched' , { fullResults , partialResults } ) ; return [ ] . concat ( fullResults , partialResults ) . filter ( ( { address } ) => ! / 0x0*$ / . test ( address ) ) . reduce ( ( tokens , token ) => { const { id } = token ; tokens [ id ] = token ; return tokens ; } , { } ) ; } ) . then ( ( tokens ) => { dispatch ( setTokens ( tokens ) ) ; } ) ; } ; }
Split the given token indexes between those for whom
we already have some info, and thus just need to fetch
the image, and those for whom we don't have anything and
need to fetch all the info.
Split the given token indexes between those for whom we already have some info and thus just need to fetch the image and those for whom we don t have anything and need to fetch all the info .