inspect.ts 2.24 KB
Newer Older
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
import { task } from 'hardhat/config'
import * as types from 'hardhat/internal/core/params/argumentTypes'

const getFullyQualifiedName = (
  sourceName: string,
  contractName: string
): string => {
  return `${sourceName}:${contractName}`
}

task('inspect')
  .addPositionalParam(
    'contractName',
    'Name of the contract to inspect',
    undefined,
    types.string,
    false
  )
  .addPositionalParam(
    'field',
    'Compiler output field to inspect',
    undefined,
    types.string,
    false
  )
  .setAction(async (args, hre) => {
    const artifact = await hre.artifacts.readArtifact(args.contractName)
    const fqn = getFullyQualifiedName(
      artifact.sourceName,
      artifact.contractName
    )
    const buildInfo = await hre.artifacts.getBuildInfo(fqn)
    const info =
      buildInfo.output.contracts[artifact.sourceName][artifact.contractName]
    if (!info) {
      throw new Error(`Cannot find build info for ${fqn}`)
    }

    try {
      switch (args.field) {
        case 'abi': {
          const abi = info.abi
          console.log(JSON.stringify(abi, null, 2))
          break
        }
        case 'bytecode': {
          const bytecode = info.evm.bytecode.object
          console.log('0x' + bytecode)
          break
        }
        case 'deployedBytecode': {
          const bytecode = info.evm.deployedBytecode.object
          console.log('0x' + bytecode)
          break
        }
        case 'storageLayout': {
          const storageLayout = (info as any).storageLayout
          console.log(JSON.stringify(storageLayout, null, 2))
          break
        }
        case 'methodIdentifiers': {
          const methodIdentifiers = info.evm.methodIdentifiers
          console.log(JSON.stringify(methodIdentifiers, null, 2))
          break
        }
        default: {
          console.log(
            JSON.stringify(
              {
                error: `Unsupported field ${args.field}`,
              },
              null,
              2
            )
          )
          break
        }
      }
    } catch (e) {
      console.log(
        JSON.stringify(
          {
            error: `Cannot find ${args.field}, be sure to enable it in compiler settings`,
          },
          null,
          2
        )
      )
    }
  })