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

100% Statements 31/31
100% Branches 21/21
100% Functions 2/2
100% Lines 30/30

    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    641x
    1x
    1x
    1x

    5x

    1x
    9x
    1x

    8x
    8x
    8x

    4x
    4x
    4x
    4x
    2x
    2x

    2x
    2x

    3x
    3x
    2x
    2x
    2x
    2x
    2x
    1x

    1x

    1x

    1x

     const getColor = require('./getColor');
    const { INVALID_PARAMS } = require('./constants');
    const { isNFT } = require('../../utils');
    const { BigInt } = require('jsbi-utils');

    const formatUint256 = n => `0x${n.toString(16).padStart(64, '0')}`;

    /* eslint-disable no-throw-literal */
    module.exports = async (bridgeState, txObj, tag) => {
      if (tag !== 'latest') {
        throw {
          code: INVALID_PARAMS,
          message: 'Only call for latest block is supported',
        };
      }

      const method = txObj.data.substring(0, 10);
      const paramsData = txObj.data.slice(34);
      switch (method) {
        // balanceOf(address)
        case '0x70a08231': {
          const color = parseInt(await getColor(bridgeState, txObj.to), 16);
          const address = `0x${paramsData}`;
          const balances = bridgeState.currentState.balances[color] || {};
          if (isNFT(color)) {
            const nfts = balances[address] || [];
            return formatUint256(nfts.length);
          }

          const balance = BigInt(balances[address] || 0);
          return formatUint256(balance);
        }

        // tokenOfOwnerByIndex(address,uint256)
        case '0x2f745c59': {
          const color = parseInt(await getColor(bridgeState, txObj.to), 16);
          if (isNFT(color)) {
            const address = `0x${paramsData.substring(0, 40)}`;
            const index = parseInt(paramsData.substring(40), 16);
            const balances = bridgeState.currentState.balances[color] || {};
            const nfts = balances[address] || [];
            if (!nfts[index]) {
              throw {
                code: INVALID_PARAMS,
                message: 'Index overflow',
              };
            }
            return formatUint256(nfts[index]);
          }

          throw {
            code: INVALID_PARAMS,
            message: 'Only for NFT',
          };
        }
        default:
          throw {
            code: INVALID_PARAMS,
            message: `Method call ${method} is not supported`,
          };
      }
    };
    /* eslint-enable */