All files / src/api/methods getColor.js

100% Statements 20/20
100% Branches 6/6
100% Functions 3/3
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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        9x 9x 9x   9x 53x   9x 18x 18x 18x 7x     11x 11x 11x 2x     9x 9x 9x 6x       3x            
const {
  INVALID_PARAMS,
  NFT_COLOR_BASE,
  NST_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 nstColors = await getColors(bridgeState, false, true);
  const nstColor = colorIndex(nstColors, address);
  if (nstColor > -1) {
    return `0x${(NST_COLOR_BASE + nstColor).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 */
};