# All files / src/api/methods getColors.js

100% Statements 19/19
100% Branches 8/8
100% Functions 5/5
100% Lines 18/18

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    403x
    3x

    3x
    3x

    3x
    6x

    12x

    12x

    3x
    25x
    10x

    10x
    3x

    15x

    15x
    3x

    25x

    3x
     const { NFT_COLOR_BASE } = require('./constants');
    const { range } = require('../../utils');

    let erc20Tokens = [];
    let nftTokens = [];

    const getTokensRange = (bridgeState, from, to) => {
      return Promise.all(
        range(from, to - 1).map(i =>
          bridgeState.exitHandlerContract.methods.tokens(i).call()
        )
      ).then(tokens => tokens.map(o => o.addr.toLowerCase()));
    };

    const getColors = async (bridgeState, nft) => {
      if (nft) {
        const tokenCount = Number(
          await bridgeState.exitHandlerContract.methods.nftTokenCount().call()
        );
        if (tokenCount !== nftTokens.length) {
          nftTokens = await getTokensRange(
            bridgeState,
            NFT_COLOR_BASE,
            NFT_COLOR_BASE + tokenCount
          );
        }
      } else {
        const tokenCount = Number(
          await bridgeState.exitHandlerContract.methods.erc20TokenCount().call()
        );
        if (tokenCount !== erc20Tokens.length) {
          erc20Tokens = await getTokensRange(bridgeState, 0, tokenCount);
        }
      }

      return nft ? nftTokens : erc20Tokens;
    };

    module.exports = getColors;