From 921fa123a4e022a383a8e74a521c27c13d6aa1bf Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 9 Mar 2022 12:24:32 +0100 Subject: [PATCH] try getting content from the compilation targets --- .../src/lib/compiler-fetch-and-compile.ts | 12 ++++++++++-- .../src/lib/helpers/fetch-etherscan.ts | 4 ++-- .../src/lib/helpers/fetch-sourcify.ts | 4 ++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libs/remix-core-plugin/src/lib/compiler-fetch-and-compile.ts b/libs/remix-core-plugin/src/lib/compiler-fetch-and-compile.ts index f8446886b7..3d5910d377 100644 --- a/libs/remix-core-plugin/src/lib/compiler-fetch-and-compile.ts +++ b/libs/remix-core-plugin/src/lib/compiler-fetch-and-compile.ts @@ -1,4 +1,3 @@ - import { Plugin } from '@remixproject/engine' import { compile } from '@remix-project/remix-solidity' import { util } from '@remix-project/remix-lib' @@ -76,6 +75,7 @@ export class FetchAndCompile extends Plugin { } } + targetPath = `${targetPath}/${network.id}/${contractAddress}` let data try { data = await fetchContractFromSourcify(this, network, contractAddress, targetPath) @@ -108,7 +108,15 @@ export class FetchAndCompile extends Plugin { const compData = await compile( compilationTargets, settings, - async (url, cb) => await this.call('contentImport', 'resolveAndSave', url).then((result) => cb(null, result)).catch((error) => cb(error.message))) + async (url, cb) => { + // we first try to resolve the content from the compilation target using a more appropiate path + const path = `${targetPath}/${url}` + if (compilationTargets[path] && compilationTargets[path].content) { + return cb(null, compilationTargets[path].content) + } else { + await this.call('contentImport', 'resolveAndSave', url).then((result) => cb(null, result)).catch((error) => cb(error.message)) + } + }) await this.call('compilerArtefacts', 'addResolvedContract', contractAddress, compData) return compData } catch (e) { diff --git a/libs/remix-core-plugin/src/lib/helpers/fetch-etherscan.ts b/libs/remix-core-plugin/src/lib/helpers/fetch-etherscan.ts index 647547ba47..25ddfc44ef 100644 --- a/libs/remix-core-plugin/src/lib/helpers/fetch-etherscan.ts +++ b/libs/remix-core-plugin/src/lib/helpers/fetch-etherscan.ts @@ -23,7 +23,7 @@ export const fetchContractFromEtherscan = async (plugin, network, contractAddres } if (typeof data.result[0].SourceCode === 'string') { - const fileName = `${targetPath}/${network.id}/${contractAddress}/${data.result[0].ContractName}.sol` + const fileName = `${targetPath}/${data.result[0].ContractName}.sol` await plugin.call('fileManager', 'setFile', fileName , data.result[0].SourceCode) compilationTargets[fileName] = { content: data.result[0].SourceCode } } else if (data.result[0].SourceCode && typeof data.result[0].SourceCode == 'object') { @@ -34,7 +34,7 @@ export const fetchContractFromEtherscan = async (plugin, network, contractAddres if (await plugin.call('contentImport', 'isExternalUrl', file)) { // nothing to do, the compiler callback will handle those } else { - const path = `${targetPath}/${network.id}/${contractAddress}/${file}` + const path = `${targetPath}/${file}` const content = (source as any).content await plugin.call('fileManager', 'setFile', path, content) compilationTargets[path] = { content } diff --git a/libs/remix-core-plugin/src/lib/helpers/fetch-sourcify.ts b/libs/remix-core-plugin/src/lib/helpers/fetch-sourcify.ts index 698c062b44..5765e7e9d6 100644 --- a/libs/remix-core-plugin/src/lib/helpers/fetch-sourcify.ts +++ b/libs/remix-core-plugin/src/lib/helpers/fetch-sourcify.ts @@ -13,7 +13,7 @@ export const fetchContractFromSourcify = async (plugin, network, contractAddress } // set the solidity contract code using metadata - await plugin.call('fileManager', 'setFile', `${targetPath}/${network.id}/${contractAddress}/metadata.json`, JSON.stringify(data.metadata, null, '\t')) + await plugin.call('fileManager', 'setFile', `${targetPath}/metadata.json`, JSON.stringify(data.metadata, null, '\t')) for (let file in data.metadata.sources) { const urls = data.metadata.sources[file].urls for (const url of urls) { @@ -24,7 +24,7 @@ export const fetchContractFromSourcify = async (plugin, network, contractAddress if (await plugin.call('contentImport', 'isExternalUrl', file)) { // nothing to do, the compiler callback will handle those } else { - const path = `${targetPath}/${network.id}/${contractAddress}/${file}` + const path = `${targetPath}/${file}` await plugin.call('fileManager', 'setFile', path, source.content) compilationTargets[path] = { content: source.content } }