All files / src/api/methods getCode.js

100% Statements 25/25
100% Branches 8/8
100% Functions 1/1
100% Lines 22/22

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 39 40 41 42 43 44 45 463x 3x   3x 3x 3x     3x 8x 1x           7x   7x 7x 13x 2x       5x 5x 9x 2x       3x 3x 5x 2x       1x            
const { INVALID_PARAMS } = require('./constants');
const BYTECODES = require('../../tx/applyTx/ercBytecode.js');
 
const CODE_ERC20 = `0x${BYTECODES.ERC20_BYTECODE.toString('hex')}`;
const CODE_ERC721 = `0x${BYTECODES.ERC721_BYTECODE.toString('hex')}`;
const CODE_ERC1948 = `0x${BYTECODES.ERC1948_BYTECODE.toString('hex')}`;
 
/* eslint-disable no-throw-literal */
module.exports = async (bridgeState, contractAddr, tag) => {
  if (tag !== 'latest') {
    throw {
      code: INVALID_PARAMS,
      message: 'Only call for latest block is supported',
    };
  }
 
  const tokenAddr = contractAddr.toLowerCase();
 
  let len = bridgeState.tokens.erc20.length;
  for (let i = 0; i < len; i += 1) {
    if (bridgeState.tokens.erc20[i].toLowerCase() === tokenAddr) {
      return CODE_ERC20;
    }
  }
 
  len = bridgeState.tokens.erc721.length;
  for (let i = 0; i < len; i += 1) {
    if (bridgeState.tokens.erc721[i].toLowerCase() === tokenAddr) {
      return CODE_ERC721;
    }
  }
 
  len = bridgeState.tokens.erc1948.length;
  for (let i = 0; i < len; i += 1) {
    if (bridgeState.tokens.erc1948[i].toLowerCase() === tokenAddr) {
      return CODE_ERC1948;
    }
  }
 
  throw {
    code: INVALID_PARAMS,
    message: 'Contract not found',
  };
};
/* eslint-enable */