const ethers = require("ethers");

/**
 * Example of a method that resolves the contract
 * names of method calls routed through an EtherRouter style contract. This function
 * gets bound to the `this` property of eth-gas-reporter's ProxyResolver class and
 * inherits its resources including helpers to match methods to contracts and a way
 * of making synchronous calls to the client.
 *
 * Queries EtherRouter for address of resolver.
 * Queries Resolver for contract address w/ method signature.
 * Returns contract name matching the resolved address.
 * @param  {Object} transaction result of web3.eth.getTransaction
 * @return {String}             contract name
 */
function etherRouter(transaction) {
  let contractAddress;
  let contractName;

  try {
    const ABI = ["function resolver()", "function lookup(bytes4 sig)"];
    const iface = new ethers.utils.Interface(ABI);
    const signature = transaction.input.slice(0, 10);

    // EtherRouter.resolver
    const resolverAddress = this.sync.call(
      {
        to: transaction.to,
        data: iface.functions.resolver.encode([])
      },
      transaction.blockNumber
    );

    // Resolver.lookup(sig)
    contractAddress = this.sync.call(
      {
        to: ethers.utils.hexStripZeros(resolverAddress),
        data: iface.functions.lookup.encode([signature])
      },
      transaction.blockNumber
    );
  } catch (err) {
    this.unresolvedCalls++;
    return;
  }

  // Geth returns `undefined` for Truffle's solidity tests here.
  if (contractAddress) {
    contractAddress = ethers.utils.hexStripZeros(contractAddress);
    contractName = this.data.getNameByAddress(contractAddress);

    // Try to resolve by deployedBytecode
    if (contractName) return contractName;
    else return this.resolveByDeployedBytecode(contractAddress);
  }
}

module.exports = etherRouter;

@@ -81,19 +81,16 @@
 contract
-%0A *
  names o
@@ -119,16 +119,20 @@
 through 
+%0A * 
 an Ether
@@ -167,19 +167,16 @@
 function
-%0A *
  gets bo
@@ -205,16 +205,20 @@
 erty of 
+%0A * 
 eth-gas-
@@ -251,19 +251,16 @@
 lass and
-%0A *
  inherit
@@ -285,16 +285,20 @@
 cluding 
+%0A * 
 helpers 
@@ -336,19 +336,16 @@
 nd a way
-%0A *
  of maki
@@ -390,114 +390,257 @@
 %0A * 
-Queries EtherRouter for address of resolv
+Helper methods of this type receive a web3 transaction object representing a tx the report
 er
-.
 %0A * 
-Queries Resolver for contract address w/ method signature.
+could not deterministically associate with any contract. They rely on your knowledge%0A * of a proxy contract's API to derive the correct contract name.%0A *
 %0A * 
@@ -1027,86 +1027,598 @@
     
-const signature = transaction.input.slice(0, 10);%0A%0A    // EtherRouter.resolver
+%0A    // The tx passed to this method had input data which didn't map to any methods on%0A    // the contract it was sent to. It's possible the tx's %60to%60 address points to  %0A    // an EtherRouter contract which is designed to forward calls. We'll grab the %0A    // method signature and ask the router if it knows who the intended recipient is.%0A    const signature = transaction.input.slice(0, 10);%0A%0A    // EtherRouter has a public state variable called %60resolver()%60 which stores the %0A    // address of a contract which maps method signatures to their parent contracts.%0A    // Lets fetch it ....
 %0A   
@@ -1797,16 +1797,35 @@
 %0A%0A    //
+ Now we'll call the
  Resolve
@@ -1825,17 +1825,20 @@
 Resolver
-.
+'s %60
 lookup(s
@@ -1840,16 +1840,110 @@
 kup(sig)
+%60 method to get the address of the contract %0A    // our tx was actually getting forwarded to. 
 %0A    con
@@ -2141,16 +2141,67 @@
 %0A    );%0A
+  // Don't forget this is all a bit speculative...%0A
   %7D catc
@@ -2264,65 +2264,239 @@
  // 
-Geth returns %60undefined%60 for Truffle's solidity tests her
+With the correct address, we can use the ProxyResolver class's %0A  // data.getNameByAddress and/or resolveByDeployedBytecode methods %0A  // (both are available in this scope, bound to %60this%60) to derive %0A  // the target contract's nam
 e.%0A 

Make EtherRouter comments very explicit