From e5dedfcda5def2216f46670c10fc82d1203d10d4 Mon Sep 17 00:00:00 2001 From: Aniket-Engg Date: Thu, 16 Jun 2022 12:09:40 +0530 Subject: [PATCH] fetch from testnets --- .../src/lib/helpers/fetch-etherscan.ts | 2 +- .../workspace/src/lib/actions/index.ts | 34 ++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) 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 32264c32fa..b8e4434c9b 100644 --- a/libs/remix-core-plugin/src/lib/helpers/fetch-etherscan.ts +++ b/libs/remix-core-plugin/src/lib/helpers/fetch-etherscan.ts @@ -10,7 +10,7 @@ export const fetchContractFromEtherscan = async (plugin, network, contractAddres // etherscan api doc https://docs.etherscan.io/api-endpoints/contracts if (data.message === 'OK' && data.status === "1") { if (data.result.length) { - if (data.result[0].SourceCode === '') throw new Error('contract not verified in Etherscan') + if (data.result[0].SourceCode === '') throw new Error(`contract not verified on Etherscan ${network.name} network`) if (data.result[0].SourceCode.startsWith('{')) { data.result[0].SourceCode = JSON.parse(data.result[0].SourceCode.replace(/(?:\r\n|\r|\n)/g, '').replace(/^{{/,'{').replace(/}}$/,'}')) } diff --git a/libs/remix-ui/workspace/src/lib/actions/index.ts b/libs/remix-ui/workspace/src/lib/actions/index.ts index 4b19f75948..0188219d1e 100644 --- a/libs/remix-ui/workspace/src/lib/actions/index.ts +++ b/libs/remix-ui/workspace/src/lib/actions/index.ts @@ -63,20 +63,28 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React. const route = window.location.pathname if (route.startsWith('/address/0x') && route.length === 51) { const contractAddress = route.split('/')[2] - const network = {id: 1, name: 'main'} - const target = `/${network.name}` - try { - const data = await fetchContractFromEtherscan(plugin, network, contractAddress, target) - await createWorkspaceTemplate('etherscan-code-sample', 'code-template') - plugin.setWorkspace({ name: 'etherscan-code-sample', isLocalhost: false }) - dispatch(setCurrentWorkspace('etherscan-code-sample')) - const filePath = Object.keys(data.compilationTargets)[0] - await workspaceProvider.set(filePath, data.compilationTargets[filePath]['content']) - plugin.on('editor', 'editorMounted', async () => await plugin.fileManager.openFile(filePath)) - } catch (error) { - if(error.message.includes('unable to retrieve contract data') || error.message.includes('unable to try fetching the source code')) + const networks = [ + {id: 1, name: 'mainnet'}, + {id: 3, name: 'ropsten'}, + {id: 4, name: 'rinkeby'}, + {id: 42, name: 'kovan'}, + {id: 5, name: 'goerli'} + ] + plugin.call('notification', 'toast', `Looking for contract address ${contractAddress} on different networks`) + for (const network of networks) { + const target = `/${network.name}` + try { + const data = await fetchContractFromEtherscan(plugin, network, contractAddress, target) + await createWorkspaceTemplate('etherscan-code-sample', 'code-template') + plugin.setWorkspace({ name: 'etherscan-code-sample', isLocalhost: false }) + dispatch(setCurrentWorkspace('etherscan-code-sample')) + const filePath = Object.keys(data.compilationTargets)[0] + await workspaceProvider.set(filePath, data.compilationTargets[filePath]['content']) + plugin.on('editor', 'editorMounted', async () => await plugin.fileManager.openFile(filePath)) + } catch (error) { plugin.call('notification', 'toast', error.message) - await basicWorkspaceInit(workspaces, workspaceProvider) + await basicWorkspaceInit(workspaces, workspaceProvider) + } } } } else {