default API key

pull/2450/head^2
Aniket-Engg 2 years ago committed by Aniket
parent dc80c727c1
commit b31589da16
  1. 7
      libs/remix-core-plugin/src/lib/helpers/fetch-etherscan.ts
  2. 61
      libs/remix-ui/workspace/src/lib/actions/index.ts

@ -1,8 +1,11 @@
export const fetchContractFromEtherscan = async (plugin, network, contractAddress, targetPath) => { export const fetchContractFromEtherscan = async (plugin, network, contractAddress, targetPath, key?) => {
let data let data
const compilationTargets = {} const compilationTargets = {}
let etherscanKey
const etherscanKey = await plugin.call('config', 'getAppParameter', 'etherscan-access-token') if (!key) etherscanKey = await plugin.call('config', 'getAppParameter', 'etherscan-access-token')
else etherscanKey = key
if (etherscanKey) { if (etherscanKey) {
const endpoint = network.id == 1 ? 'api.etherscan.io' : 'api-' + network.name + '.etherscan.io' const endpoint = network.id == 1 ? 'api.etherscan.io' : 'api-' + network.name + '.etherscan.io'
data = await fetch('https://' + endpoint + '/api?module=contract&action=getsourcecode&address=' + contractAddress + '&apikey=' + etherscanKey) data = await fetch('https://' + endpoint + '/api?module=contract&action=getsourcecode&address=' + contractAddress + '&apikey=' + etherscanKey)

@ -63,42 +63,39 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
const route = window.location.pathname const route = window.location.pathname
if (route.startsWith('/address/0x') && route.length === 51) { if (route.startsWith('/address/0x') && route.length === 51) {
const contractAddress = route.split('/')[2] const contractAddress = route.split('/')[2]
plugin.call('notification', 'toast', `Looking for contract address ${contractAddress} on different networks`)
let data let data
try { try {
const etherscanKey = await plugin.call('config', 'getAppParameter', 'etherscan-access-token') let etherscanKey = await plugin.call('config', 'getAppParameter', 'etherscan-access-token')
if (!etherscanKey) { if (!etherscanKey) etherscanKey = '2HKUX5ZVASZIKWJM8MIQVCRUVZ6JAWT531'
plugin.call('notification', 'toast', `Cannot look for contract address ${contractAddress}. Etherscan access token not found. Go to Remix settings page and save an access token.`) const networks = [
await basicWorkspaceInit(workspaces, workspaceProvider) {id: 1, name: 'mainnet'},
} else { {id: 3, name: 'ropsten'},
const networks = [ {id: 4, name: 'rinkeby'},
{id: 1, name: 'mainnet'}, {id: 42, name: 'kovan'},
{id: 3, name: 'ropsten'}, {id: 5, name: 'goerli'}
{id: 4, name: 'rinkeby'}, ]
{id: 42, name: 'kovan'}, plugin.call('notification', 'toast', `Looking for contract address ${contractAddress} on different networks`)
{id: 5, name: 'goerli'} let found = false
] for (const network of networks) {
plugin.call('notification', 'toast', `Looking for contract address ${contractAddress} on different networks`) const target = `/${network.name}`
let found = false try{
for (const network of networks) { data = await fetchContractFromEtherscan(plugin, network, contractAddress, target, etherscanKey)
const target = `/${network.name}` } catch (error) {
try{ if ((error.message.startsWith('contract not verified on Etherscan') || error.message.startsWith('unable to retrieve contract data')) && network.id !== 5)
data = await fetchContractFromEtherscan(plugin, network, contractAddress, target) continue
} catch (error) { else {
if ((error.message.startsWith('contract not verified on Etherscan') || error.message.startsWith('unable to retrieve contract data')) && network.id !== 5) if (!found) await basicWorkspaceInit(workspaces, workspaceProvider)
continue break
else {
if (!found) await basicWorkspaceInit(workspaces, workspaceProvider)
break
}
} }
found = true
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))
} }
found = true
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) { } catch (error) {
await basicWorkspaceInit(workspaces, workspaceProvider) await basicWorkspaceInit(workspaces, workspaceProvider)

Loading…
Cancel
Save