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

100% Statements 16/16
100% Branches 4/4
100% Functions 3/3
100% Lines 15/15

    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
    282x
    2x
    2x

    2x
    29x

    2x
    12x
    12x
    12x
    5x

    7x
    7x
    7x
    6x

    1x

     const { INVALID_PARAMS, NFT_COLOR_BASE } = require('./constants');
    const getColors = require('./getColors');
    const { addrCmp } = require('../../utils');

    const colorIndex = (colors, address) =>
      colors.findIndex(c => addrCmp(c, address));

    module.exports = async (bridgeState, address) => {
      const erc20Colors = await getColors(bridgeState, false);
      const erc20Color = colorIndex(erc20Colors, address);
      if (erc20Color > -1) {
        return `0x${erc20Color.toString(16)}`;
      }

      const nftColors = await getColors(bridgeState, true);
      const nftColor = colorIndex(nftColors, address);
      if (nftColor > -1) {
        return `0x${(NFT_COLOR_BASE + nftColor).toString(16)}`;
      }

      /* eslint-disable no-throw-literal */
      throw {
        code: INVALID_PARAMS,
        message: 'Unknown token address',
      };
      /* eslint-enable no-throw-literal */
    };