parent
070ed17205
commit
0d51edfaa3
@ -0,0 +1,77 @@ |
||||
export const fetchContractFromBlockscout = async (plugin, endpoint, contractAddress, targetPath, shouldSetFile = true) => { |
||||
let data |
||||
const compilationTargets = {} |
||||
|
||||
try { |
||||
data = await fetch('https://' + endpoint + '/api?module=contract&action=getsourcecode&address=' + contractAddress) |
||||
data = await data.json() |
||||
console.log(data) |
||||
// blockscout api doc https://blockscout.com/poa/core/api-docs
|
||||
if (data.message === 'OK' && data.status === "1") { |
||||
if (data.result.length) { |
||||
if (!data.result[0].SourceCode || data.result[0].SourceCode === '') { |
||||
throw new Error(`contract not verified on Blockscout ${endpoint} network`) |
||||
} |
||||
// if (data.result[0].AdditionalSources
|
||||
// && Array.isArray(data.result[0].AdditionalSources)
|
||||
// && data.result[0].AdditionalSources.length > 0) {
|
||||
// let sourceCode = {
|
||||
// data.result[0].FileName
|
||||
// }
|
||||
// }
|
||||
// if (data.result[0].SourceCode.startsWith('{')) {
|
||||
// data.result[0].SourceCode = JSON.parse(data.result[0].SourceCode.replace(/(?:\r\n|\r|\n)/g, '').replace(/^{{/, '{').replace(/}}$/, '}'))
|
||||
// }
|
||||
} |
||||
} else throw new Error('unable to retrieve contract data ' + data.message) |
||||
} catch (e) { |
||||
throw new Error('unable to retrieve contract data: ' + e.message) |
||||
} |
||||
|
||||
if (!data || !data.result) { |
||||
return null |
||||
} |
||||
|
||||
if (data.result[0].FileName === '') { |
||||
const fileName = `${targetPath}/${data.result[0].ContractName}.sol` |
||||
if (shouldSetFile) await plugin.call('fileManager', 'setFile', fileName, data.result[0].SourceCode) |
||||
compilationTargets[fileName] = { content: data.result[0].SourceCode } |
||||
} else { |
||||
const sources = {} |
||||
sources[data.result[0].FileName] = data.result[0].SourceCode |
||||
if (data.result[0].AdditionalSources && Array.isArray(data.result[0].AdditionalSources)) { |
||||
for (const object of data.result[0].AdditionalSources) { |
||||
sources[object.Filename] = object.SourceCode |
||||
} |
||||
} |
||||
|
||||
for (let [file, source] of Object.entries(sources)) { |
||||
file = file.replace('browser/', '') // should be fixed in the remix IDE end.
|
||||
file = file.replace(/^\//g, '') // remove first slash.
|
||||
if (await plugin.call('contentImport', 'isExternalUrl', file)) { |
||||
// nothing to do, the compiler callback will handle those
|
||||
} else { |
||||
const path = `${targetPath}/${file}` |
||||
const content = source |
||||
if (shouldSetFile) await plugin.call('fileManager', 'setFile', path, content) |
||||
compilationTargets[path] = { content } |
||||
} |
||||
} |
||||
} |
||||
|
||||
let runs = 0 |
||||
try { |
||||
runs = parseInt(data.result[0].OptimizationRuns) |
||||
} catch (e) { } |
||||
const settings = { |
||||
version: data.result[0].CompilerVersion.replace(/^v/, ''), |
||||
language: 'Solidity', |
||||
evmVersion: data.result[0].EVMVersion.toLowerCase(), |
||||
optimize: data.result[0].OptimizationUsed === 'true', |
||||
runs |
||||
} |
||||
return { |
||||
settings, |
||||
compilationTargets |
||||
} |
||||
} |
Loading…
Reference in new issue