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

100% Statements 17/17
100% Branches 8/8
100% Functions 5/5
100% Lines 16/16

    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
    40
    41
    42
    43
    44
    454x
    4x

    4x
    17x

    34x

    34x

    4x
    35x
    15x

    15x
    8x

    20x

    20x
    9x

    35x

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

    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 !== bridgeState.tokens.erc721.length) {
          bridgeState.tokens.erc721 = bridgeState.tokens.erc721.concat(
            await getTokensRange(
              bridgeState,
              NFT_COLOR_BASE + bridgeState.tokens.erc721.length,
              NFT_COLOR_BASE + tokenCount
            )
          );
        }
      } else {
        const tokenCount = Number(
          await bridgeState.exitHandlerContract.methods.erc20TokenCount().call()
        );
        if (tokenCount !== bridgeState.tokens.erc20.length) {
          bridgeState.tokens.erc20 = bridgeState.tokens.erc20.concat(
            await getTokensRange(
              bridgeState,
              bridgeState.tokens.erc20.length,
              tokenCount
            )
          );
        }
      }

      return nft ? bridgeState.tokens.erc721 : bridgeState.tokens.erc20;
    };

    module.exports = getColors;