try getting content from the compilation targets

pull/2156/head
yann300 3 years ago
parent a6c69d4ec4
commit 921fa123a4
  1. 12
      libs/remix-core-plugin/src/lib/compiler-fetch-and-compile.ts
  2. 4
      libs/remix-core-plugin/src/lib/helpers/fetch-etherscan.ts
  3. 4
      libs/remix-core-plugin/src/lib/helpers/fetch-sourcify.ts

@ -1,4 +1,3 @@
import { Plugin } from '@remixproject/engine' import { Plugin } from '@remixproject/engine'
import { compile } from '@remix-project/remix-solidity' import { compile } from '@remix-project/remix-solidity'
import { util } from '@remix-project/remix-lib' import { util } from '@remix-project/remix-lib'
@ -76,6 +75,7 @@ export class FetchAndCompile extends Plugin {
} }
} }
targetPath = `${targetPath}/${network.id}/${contractAddress}`
let data let data
try { try {
data = await fetchContractFromSourcify(this, network, contractAddress, targetPath) data = await fetchContractFromSourcify(this, network, contractAddress, targetPath)
@ -108,7 +108,15 @@ export class FetchAndCompile extends Plugin {
const compData = await compile( const compData = await compile(
compilationTargets, compilationTargets,
settings, 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) await this.call('compilerArtefacts', 'addResolvedContract', contractAddress, compData)
return compData return compData
} catch (e) { } catch (e) {

@ -23,7 +23,7 @@ export const fetchContractFromEtherscan = async (plugin, network, contractAddres
} }
if (typeof data.result[0].SourceCode === 'string') { 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) await plugin.call('fileManager', 'setFile', fileName , data.result[0].SourceCode)
compilationTargets[fileName] = { content: data.result[0].SourceCode } compilationTargets[fileName] = { content: data.result[0].SourceCode }
} else if (data.result[0].SourceCode && typeof data.result[0].SourceCode == 'object') { } 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)) { if (await plugin.call('contentImport', 'isExternalUrl', file)) {
// nothing to do, the compiler callback will handle those // nothing to do, the compiler callback will handle those
} else { } else {
const path = `${targetPath}/${network.id}/${contractAddress}/${file}` const path = `${targetPath}/${file}`
const content = (source as any).content const content = (source as any).content
await plugin.call('fileManager', 'setFile', path, content) await plugin.call('fileManager', 'setFile', path, content)
compilationTargets[path] = { content } compilationTargets[path] = { content }

@ -13,7 +13,7 @@ export const fetchContractFromSourcify = async (plugin, network, contractAddress
} }
// set the solidity contract code using metadata // 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) { for (let file in data.metadata.sources) {
const urls = data.metadata.sources[file].urls const urls = data.metadata.sources[file].urls
for (const url of urls) { for (const url of urls) {
@ -24,7 +24,7 @@ export const fetchContractFromSourcify = async (plugin, network, contractAddress
if (await plugin.call('contentImport', 'isExternalUrl', file)) { if (await plugin.call('contentImport', 'isExternalUrl', file)) {
// nothing to do, the compiler callback will handle those // nothing to do, the compiler callback will handle those
} else { } else {
const path = `${targetPath}/${network.id}/${contractAddress}/${file}` const path = `${targetPath}/${file}`
await plugin.call('fileManager', 'setFile', path, source.content) await plugin.call('fileManager', 'setFile', path, source.content)
compilationTargets[path] = { content: source.content } compilationTargets[path] = { content: source.content }
} }

Loading…
Cancel
Save