Skip to content

Commit 9f6f307

Browse files
committed
pass the endpoint URL
1 parent 7fd1994 commit 9f6f307

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

libs/remix-core-plugin/src/lib/helpers/fetch-etherscan.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
export const fetchContractFromEtherscan = async (plugin, network, contractAddress, targetPath, shouldSetFile = true, etherscanKey?) => {
1+
2+
export type Network = {
3+
id: number
4+
name: string
5+
}
6+
7+
export const fetchContractFromEtherscan = async (plugin, endpoint: string | Network, contractAddress, targetPath, shouldSetFile = true, etherscanKey?) => {
28
let data
39
const compilationTargets = {}
410
if (!etherscanKey) etherscanKey = await plugin.call('config', 'getAppParameter', 'etherscan-access-token')
511
if (!etherscanKey) etherscanKey = '2HKUX5ZVASZIKWJM8MIQVCRUVZ6JAWT531'
612

713
if (etherscanKey) {
8-
const endpoint = network.id == 1 ? 'api.etherscan.io' : 'api-' + network.name + '.etherscan.io'
14+
if (typeof endpoint === 'object' && endpoint !== null && 'id' in endpoint && 'name' in endpoint) {
15+
endpoint = endpoint.id == 1 ? 'api.etherscan.io' : 'api-' + endpoint.name + '.etherscan.io'
16+
}
917
try {
1018
data = await fetch('https://' + endpoint + '/api?module=contract&action=getsourcecode&address=' + contractAddress + '&apikey=' + etherscanKey)
1119
data = await data.json()
1220
// etherscan api doc https://docs.etherscan.io/api-endpoints/contracts
1321
if (data.message === 'OK' && data.status === "1") {
1422
if (data.result.length) {
15-
if (data.result[0].SourceCode === '') throw new Error(`contract not verified on Etherscan ${network.name} network`)
23+
if (data.result[0].SourceCode === '') throw new Error(`contract not verified on Etherscan ${endpoint}`)
1624
if (data.result[0].SourceCode.startsWith('{')) {
1725
data.result[0].SourceCode = JSON.parse(data.result[0].SourceCode.replace(/(?:\r\n|\r|\n)/g, '').replace(/^{{/, '{').replace(/}}$/, '}'))
1826
}

libs/remix-ui/workspace/src/lib/actions/index.tsx

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export type UrlParametersType = {
2929
opendir: string,
3030
blockscout: string,
3131
ghfolder: string
32+
endpoint: string
3233
}
3334

3435
const basicWorkspaceInit = async (workspaces: { name: string; isGitRepo: boolean; }[], workspaceProvider) => {
@@ -136,39 +137,25 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
136137
try {
137138
let etherscanKey = await plugin.call('config', 'getAppParameter', 'etherscan-access-token')
138139
if (!etherscanKey) etherscanKey = '2HKUX5ZVASZIKWJM8MIQVCRUVZ6JAWT531'
139-
const networks = [
140-
{ id: 1, name: 'mainnet' },
141-
{ id: 11155111, name: 'sepolia' }
142-
]
143-
let found = false
144140
const workspaceName = 'code-sample'
145141
let filePath
146142
const foundOnNetworks = []
147-
for (const network of networks) {
148-
const target = `/${network.name}/${contractAddress}`
149-
try {
150-
data = await fetchContractFromEtherscan(plugin, network, contractAddress, target, false, etherscanKey)
151-
} catch (error) {
152-
if ((error.message.startsWith('contract not verified on Etherscan') || error.message.startsWith('unable to retrieve contract data')) && network.id !== 5)
153-
continue
154-
else {
155-
if (!found) await basicWorkspaceInit(workspaces, workspaceProvider)
156-
break
157-
}
158-
}
159-
found = true
160-
foundOnNetworks.push(network.name)
161-
if (await workspaceExists(workspaceName)) workspaceProvider.setWorkspace(workspaceName)
162-
else await createWorkspaceTemplate(workspaceName, 'code-template')
163-
plugin.setWorkspace({ name: workspaceName, isLocalhost: false })
164-
dispatch(setCurrentWorkspace({ name: workspaceName, isGitRepo: false }))
165-
count = count + (Object.keys(data.compilationTargets)).length
166-
for (filePath in data.compilationTargets)
167-
await workspaceProvider.set(filePath, data.compilationTargets[filePath]['content'])
168-
169-
if (data.config) {
170-
await workspaceProvider.set('compiler_config.json', JSON.stringify(data.config, null, '\t'))
171-
}
143+
const endpoint = params.endpoint || 'api.etherscan.io'
144+
try {
145+
data = await fetchContractFromEtherscan(plugin, endpoint, contractAddress, '', false, etherscanKey)
146+
} catch (error) {
147+
await basicWorkspaceInit(workspaces, workspaceProvider)
148+
}
149+
if (await workspaceExists(workspaceName)) workspaceProvider.setWorkspace(workspaceName)
150+
else await createWorkspaceTemplate(workspaceName, 'code-template')
151+
plugin.setWorkspace({ name: workspaceName, isLocalhost: false })
152+
dispatch(setCurrentWorkspace({ name: workspaceName, isGitRepo: false }))
153+
count = count + (Object.keys(data.compilationTargets)).length
154+
for (filePath in data.compilationTargets)
155+
await workspaceProvider.set(filePath, data.compilationTargets[filePath]['content'])
156+
157+
if (data.config) {
158+
await workspaceProvider.set('compiler_config.json', JSON.stringify(data.config, null, '\t'))
172159
}
173160

174161
plugin.on('filePanel', 'workspaceInitializationCompleted', async () => {

0 commit comments

Comments
 (0)