generate-deployed-artifacts.ts 1.77 KB
Newer Older
1 2 3
import path from 'path'
import fs from 'fs'

4 5
import glob from 'glob'

6 7 8 9 10
/**
 * Script for automatically generating a TypeScript file for retrieving deploy artifact JSON files.
 * We do this to make sure that this package remains browser compatible.
 */
const main = async () => {
11
  let content = `
12
  /* eslint-disable */
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
  /*
  THIS FILE IS AUTOMATICALLY GENERATED.
  DO NOT EDIT.
  */
  `

  const deploymentNames = fs
    .readdirSync(path.resolve(__dirname, '../deployments'), {
      withFileTypes: true,
    })
    .filter((entry) => {
      return entry.isDirectory()
    })
    .map((entry) => {
      return entry.name
    })

  const artifactNames = []
Indeavr's avatar
Indeavr committed
31
  const pattern = /\\/g
32

33 34 35 36 37 38 39 40 41 42
  for (const deploymentName of deploymentNames) {
    const deploymentArtifacts = glob.sync(
      path.join(
        path.resolve(__dirname, '../deployments'),
        deploymentName,
        '/*.json'
      )
    )

    for (const artifactPath of deploymentArtifacts) {
Indeavr's avatar
Indeavr committed
43 44 45
      const relPath = path
        .relative(__dirname, artifactPath)
        .replace(pattern, '/')
46 47 48 49 50 51 52
      const contractName = path.basename(artifactPath, '.json')
      const artifactName = `${deploymentName}__${contractName}`.replace(
        /-/g,
        '_'
      )
      artifactNames.push(artifactName)

53 54 55 56 57
      // eslint-disable-next-line @typescript-eslint/no-var-requires
      const artifact = require(relPath)
      content += `const ${artifactName} = { abi: ${JSON.stringify(
        artifact.abi
      )}, address: '${artifact.address}' }\n`
58 59 60 61 62 63
    }
  }

  content += `
  export const getDeployedContractArtifact = (name: string, network: string): any => {
    return {
64
      ${artifactNames.join(',\n')}
65 66 67 68 69
    }[(network + '__' + name).replace(/-/g, '_')]
  }
  `

  fs.writeFileSync(`./src/contract-deployed-artifacts.ts`, content)
70 71 72
}

main()