All files / solidity-coverage/lib abi.js

100% Statements 44/44
78.57% Branches 11/14
100% Functions 6/6
100% Lines 42/42

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991x 1x         8x 8x   8x                         8x 6x 6x     8x 42x 42x     8x               17x 17x 17x   17x 43x 43x   43x 10x         43x   43x 9x     43x     17x       17x 17x 17x   17x 3x 3x 3x     17x       17x 17x 17x 17x 17x 17x 17x             17x       1x
const ethersABI = require("@ethersproject/abi");
const difflib = require('difflib');
 
class AbiUtils {
 
  diff(orig={}, cur={}){
    let plus = 0;
    let minus = 0;
 
    const unifiedDiff = difflib.unifiedDiff(
      orig.humanReadableAbiList,
      cur.humanReadableAbiList,
      {
        fromfile: orig.contractName,
        tofile: cur.contractName,
        fromfiledate: `sha: ${orig.sha}`,
        tofiledate: `sha: ${cur.sha}`,
        lineterm: ''
      }
    );
 
    // Count changes (unified diff always has a plus & minus in header);
    if (unifiedDiff.length){
      plus = -1;
      minus = -1;
    }
 
    unifiedDiff.forEach(line => {
      if (line[0] === `+`) plus++;
      if (line[0] === `-`) minus++;
    })
 
    return {
      plus,
      minus,
      unifiedDiff
    }
  }
 
  toHumanReadableFunctions(contract){
    const human = [];
    const ethersOutput = new ethersABI.Interface(contract.abi).functions;
    const signatures = Object.keys(ethersOutput);
 
    for (const sig of signatures){
      const method = ethersOutput[sig];
      let returns = '';
 
      method.outputs.forEach(output => {
        (returns.length)
          ? returns += `, ${output.type}`
          : returns += output.type;
      });
 
      let readable = `${method.type} ${sig} ${method.stateMutability}`;
 
      if (returns.length){
        readable += ` returns (${returns})`
      }
 
      human.push(readable);
    }
 
    return human;
  }
 
  toHumanReadableEvents(contract){
    const human = [];
    const ethersOutput = new ethersABI.Interface(contract.abi).events;
    const signatures = Object.keys(ethersOutput);
 
    for (const sig of signatures){
      const method = ethersOutput[sig];
      const readable = `${ethersOutput[sig].type} ${sig}`;
      human.push(readable);
    }
 
    return human;
  }
 
  generateHumanReadableAbiList(_artifacts, sha){
    const list = [];
    Eif (_artifacts.length){
      for (const item of _artifacts){
        const fns = this.toHumanReadableFunctions(item);
        const evts = this.toHumanReadableEvents(item);
        const all = fns.concat(evts);
        list.push({
          contractName: item.contractName,
          sha: sha,
          humanReadableAbiList: all
        })
      }
    }
    return list;
  }
}
 
module.exports = AbiUtils;