generate-artifacts.js 2.06 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
#!/usr/bin/env node
const path = require('path')
const glob = require('glob')
const fs = require('fs')

;(async () => {
  console.log(`writing contract artifacts typescript file`)

  const getContractJsonFiles = (artifactsFolder) => {
    return glob.sync(`${artifactsFolder}/**/*.json`).filter((match) => {
      return !match.endsWith('.dbg.json')
    })
  }

  const evmArtifactPaths = getContractJsonFiles(
    path.resolve(__dirname, `../artifacts/contracts`)
  )
  const ovmArtifactPaths = getContractJsonFiles(
    path.resolve(__dirname, `../artifacts-ovm/contracts`)
  )

  const content = `
  /* eslint-disable @typescript-eslint/no-var-requires, no-empty */
  /*
  THIS FILE IS AUTOMATICALLY GENERATED.
  DO NOT EDIT.
  */

  ${ovmArtifactPaths
    .map((artifactPath) => {
      const artifact = require(artifactPath)
      const relPath = path.relative(__dirname, artifactPath)
      return `
      let ovm__${artifact.contractName}
      try {
        ovm__${artifact.contractName} = require('${relPath}')
      } catch {}
      `
    })
    .join('\n')}
  ${evmArtifactPaths
    .map((artifactPath) => {
      const artifact = require(artifactPath)
      const relPath = path.relative(__dirname, artifactPath)
      return `
      let ${artifact.contractName}
      try {
        ${artifact.contractName} = require('${relPath}')
      } catch {}
      `
    })
    .join('\n')}

  export const getContractArtifact = (name: string, ovm: boolean): any => {
    if (ovm) {
      return {
        ${ovmArtifactPaths
          .map((artifactPath) => {
            const artifact = require(artifactPath)
            return `${artifact.contractName}: ovm__${artifact.contractName}`
          })
          .join(',\n')}
      }[name]
    } else {
      return {
        ${evmArtifactPaths
          .map((artifactPath) => {
            const artifact = require(artifactPath)
            return `${artifact.contractName}: ${artifact.contractName}`
          })
          .join(',\n')}
      }[name]
    }
  }
  `

  fs.writeFileSync(`./src/contract-artifacts.ts`, content)
})().catch(console.error)