{"version":3,"sources":["../../src/utils/truffle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAA8B,eAAe,EAAE,GAAG,EAAE,QAAQ,EAAE,mBAAsB;AAIpG,UAAU,OAAO;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,eAAe,EAAE,eAAe,CAAC;CAClC;AAED,UAAU,QAAQ;IAChB,GAAG,EAAE,GAAG,CAAC;IACT,QAAQ,EAAE,GAAG,CAAC;IACd,gBAAgB,EAAE,GAAG,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,QAAQ,CAAC;IACnB,MAAM,EAAE,GAAG,CAAC;IACZ,OAAO,EAAE,GAAG,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC;AAyHD,iBAAS,qBAAqB,CAAC,SAAS,EAAE,KAAK,CAAC,QAAQ,CAAC,OAYxD;AAED,OAAO,EAAE,qBAAqB,EAAE,CAAC","file":"truffle.d.ts","sourcesContent":["import { Address, ChainURI, ContractInstance, TransactionHash, ABI, Compiler } from 'ethpm/package';\nimport isEqual from 'lodash.isequal';\n\n\ninterface Network {\n  address: Address;\n  transactionHash: TransactionHash;\n}\n\ninterface Artifact {\n  abi: ABI;\n  bytecode: any;\n  deployedBytecode: any;\n  contractName: string;\n  compiler: Compiler;\n  devdoc: any;\n  userdoc: any;\n  metadata: string;\n  networks: Record<string, Network>;\n}\n\n\nfunction parseBytecode(bytecode: string) {\n  // detect link references in bytecode\n  if (!bytecode.includes(\"_\")) {\n    return {\n      bytecode: bytecode,\n    }\n  }\n\n  // todo: hard coded length...\n  // todo: support link dependencies\n  const linkRefs = []\n  while (bytecode.indexOf(\"_\") > 0) {\n    const index = bytecode.indexOf(\"_\")\n    const placeholder = bytecode.substring(index, index+40)\n    const contractName = placeholder.replace(/_/g, \"\")\n    let alreadyStored = false\n    for (const storedRef of linkRefs) {\n      if (storedRef.name === contractName) {\n        storedRef.offsets.push(index)\n        alreadyStored = true\n      }\n    }\n    if (!alreadyStored) {\n      const linkRef = {\n        name: contractName,\n        length: 20,\n        offsets: [index]\n      }\n      linkRefs.push(linkRef)\n    }\n    alreadyStored = false\n    bytecode = bytecode.substr(0, index) + \"0000000000000000000000000000000000000000\" + bytecode.substr(index + 40, bytecode.length)\n  }\n  return {\n    bytecode: bytecode,\n    linkReferences: linkRefs,\n  }\n}\n\nfunction parseTruffleArtifactsToContractTypes(artifacts: Array<Artifact>) {\n  const contractTypes: Record<string, any> = {}\n  for (const artifact of artifacts) {\n    const config = {\n      ...(artifact.abi) && {abi: artifact.abi},\n      // todo: no contractName since truffle doesn't support aliasing\n      ...(artifact.deployedBytecode) && {runtimeBytecode: parseBytecode(artifact.deployedBytecode)},\n      ...(artifact.bytecode) && {deploymentBytecode: parseBytecode(artifact.bytecode)},\n      ...(artifact.devdoc) && {devdoc: artifact.devdoc},\n      ...(artifact.userdoc) && {userdoc: artifact.userdoc},\n    }\n    contractTypes[artifact.contractName] = config\n  }\n  return contractTypes\n}\n\n\nfunction parseTruffleArtifactsToCompilers(artifacts: Array<Artifact>) {\n  const compilers: Array<any> = [];\n  for (const artifact of artifacts) {\n    let metadata;\n    if (typeof artifact.metadata !== \"undefined\") {\n      metadata = JSON.parse(artifact.metadata);\n    }\n    if (typeof artifact.compiler !== \"undefined\") {\n      const newCompiler: Compiler = {\n        name: artifact.compiler.name,\n        version: artifact.compiler.version,\n        settings: {\n          optimize: metadata.settings.optimizer.enabled\n        }\n      }\n      // insert compiler information object if not already in compilers array\n      let compilerAssigned = false;\n      for (const existingCompiler of compilers) {\n        const clone = Object.assign({}, existingCompiler);\n        delete clone.contractTypes;\n        if (isEqual(newCompiler, clone) && !compilerAssigned) {\n          existingCompiler.contractTypes.push(artifact.contractName);\n          compilerAssigned = true;\n        }\n      }\n      if (!compilerAssigned) {\n        newCompiler.contractTypes = [artifact.contractName];\n        compilers.push(newCompiler);\n      }\n    }\n  }\n  return compilers\n}\n\nfunction parseTruffleArtifactsToDeployments(artifacts: Array<Artifact>) {\n  const allDeployments: Record<string, Record<string, ContractInstance>> = {}\n  for (const artifact of artifacts) {\n    for (const [blockchainUri, deploymentData] of Object.entries(artifact.networks)) {\n      let currentUri = blockchainUri\n      const ethpmDeploymentData = {\n        contractType: artifact.contractName,\n        address: deploymentData.address,\n        transaction: deploymentData.transactionHash,\n        runtimeBytecode: undefined,\n        block: undefined\n      }\n      for (const storedUri of Object.keys(allDeployments)) {\n        // todo: validate latest block hash is used - needs w3\n        if (storedUri.startsWith(currentUri.split(\"/block/\")[0])) {\n          currentUri = storedUri\n        }\n      }\n      if (typeof allDeployments[currentUri] !== \"undefined\") {\n        allDeployments[currentUri][artifact.contractName] = ethpmDeploymentData\n      } else {\n        allDeployments[currentUri] = {[artifact.contractName]: ethpmDeploymentData}\n      }\n    }\n  }\n  return allDeployments\n}\n\nfunction parseTruffleArtifacts(artifacts: Array<Artifact>) {\n  const composedArtifacts: any = {}\n  composedArtifacts.contractTypes = parseTruffleArtifactsToContractTypes(artifacts)\n  const compilers = parseTruffleArtifactsToCompilers(artifacts)\n  if (compilers.length > 0) {\n    composedArtifacts.compilers = compilers\n  }\n  const deployments = parseTruffleArtifactsToDeployments(artifacts)\n  if (Object.entries(deployments).length !== 0) {\n    composedArtifacts.deployments = deployments\n  }\n  return composedArtifacts\n}\n\nexport { parseTruffleArtifacts };\n"]}