diff --git a/apps/contract-verification/src/app/app.tsx b/apps/contract-verification/src/app/app.tsx index b133f9a291..aea4e607b5 100644 --- a/apps/contract-verification/src/app/app.tsx +++ b/apps/contract-verification/src/app/app.tsx @@ -4,7 +4,8 @@ import { ContractVerificationPluginClient } from './ContractVerificationPluginCl import { AppContext } from './AppContext' import DisplayRoutes from './routes' -import { ContractVerificationSettings, ThemeType, Chain, SubmittedContracts, VerificationReceipt, mergeChainSettingsWithDefaults } from './types' +import type { ContractVerificationSettings, ThemeType, Chain, SubmittedContracts, VerificationReceipt } from './types' +import { mergeChainSettingsWithDefaults } from './utils' import './App.css' import { CompilerAbstract } from '@remix-project/remix-solidity' diff --git a/apps/contract-verification/src/app/types/defaults.ts b/apps/contract-verification/src/app/types/defaults.ts deleted file mode 100644 index 5e7e95c093..0000000000 --- a/apps/contract-verification/src/app/types/defaults.ts +++ /dev/null @@ -1,46 +0,0 @@ -import type { ChainSettings, ContractVerificationSettings, SettingsForVerifier, VerifierSettings } from './SettingsTypes' -import { VerifierIdentifier, VERIFIERS } from './VerificationTypes' - -const DEFAULTS: SettingsForVerifier = { - Sourcify: { - apiUrl: 'https://sourcify.dev/server', - explorerUrl: 'https://repo.sourcify.dev', - }, - Etherscan: { - apiUrl: 'https://api.etherscan.io', - explorerUrl: 'https://etherscan.io', - apiKey: undefined, - }, - Blockscout: { - apiUrl: 'https://eth.blockscout.com', - }, -} - -export function mergeChainSettingsWithDefaults(chainId: string, userSettings: ContractVerificationSettings): ChainSettings { - const verifiers: SettingsForVerifier = {} - - for (const verifierId of VERIFIERS) { - const userSetting: VerifierSettings = userSettings.chains[chainId]?.verifiers[verifierId] - - if (userSetting) { - verifiers[verifierId] = { ...userSetting } - // Only apply default settings for Etherscan and Blockscout on mainnet - if (verifierId === 'Sourcify' || chainId === '1') { - // Add keys not defined by user - for (const key of Object.keys(DEFAULTS[verifierId])) { - if (!verifiers[verifierId][key]) { - verifiers[verifierId][key] = DEFAULTS[verifierId][key] - } - } - } - // Only apply default settings for Etherscan and Blockscout on mainnet - } else if (verifierId === 'Sourcify' || chainId === '1') { - verifiers[verifierId] = DEFAULTS[verifierId] - } - } - return { verifiers } -} - -export function validConfiguration(chainSettings: ChainSettings | undefined, verifierId: VerifierIdentifier) { - return !!chainSettings && !!chainSettings.verifiers[verifierId]?.apiUrl && (verifierId !== 'Etherscan' || !!chainSettings.verifiers[verifierId]?.apiKey) -} diff --git a/apps/contract-verification/src/app/types/index.ts b/apps/contract-verification/src/app/types/index.ts index 53bba8f934..6b783cddab 100644 --- a/apps/contract-verification/src/app/types/index.ts +++ b/apps/contract-verification/src/app/types/index.ts @@ -1,4 +1,3 @@ export * from './ThemeType' export * from './SettingsTypes' export * from './VerificationTypes' -export * from './defaults' diff --git a/apps/contract-verification/src/app/utils/default-apis.json b/apps/contract-verification/src/app/utils/default-apis.json new file mode 100644 index 0000000000..8166f07b8b --- /dev/null +++ b/apps/contract-verification/src/app/utils/default-apis.json @@ -0,0 +1,17 @@ +{ + "Sourcify": { + "apiUrl": "https://sourcify.dev/server", + "explorerUrl": "https://repo.sourcify.dev" + }, + "Etherscan": { + "1": { + "apiUrl": "https://api.etherscan.io", + "explorerUrl": "https://etherscan.io" + } + }, + "Blockscout": { + "1": { + "apiUrl": "https://eth.blockscout.com" + } + } +} \ No newline at end of file diff --git a/apps/contract-verification/src/app/utils/default-settings.ts b/apps/contract-verification/src/app/utils/default-settings.ts new file mode 100644 index 0000000000..8ea1efac22 --- /dev/null +++ b/apps/contract-verification/src/app/utils/default-settings.ts @@ -0,0 +1,28 @@ +import type { ChainSettings, ContractVerificationSettings, SettingsForVerifier, VerifierSettings } from '../types/SettingsTypes' +import { VerifierIdentifier, VERIFIERS } from '../types/VerificationTypes' +import DEFAULT_APIS from './default-apis.json' + +export function mergeChainSettingsWithDefaults(chainId: string, userSettings: ContractVerificationSettings): ChainSettings { + const verifiers: SettingsForVerifier = {} + + for (const verifierId of VERIFIERS) { + const userSetting: VerifierSettings = userSettings.chains[chainId]?.verifiers[verifierId] ?? {} + + verifiers[verifierId] = { ...userSetting } + + let defaultsForVerifier: VerifierSettings + if (verifierId === 'Sourcify') { + defaultsForVerifier = DEFAULT_APIS['Sourcify'] + } else { + defaultsForVerifier = DEFAULT_APIS[verifierId][chainId] ?? {} + } + + // Prefer user settings over defaults + verifiers[verifierId] = Object.assign({}, defaultsForVerifier, userSetting) + } + return { verifiers } +} + +export function validConfiguration(chainSettings: ChainSettings | undefined, verifierId: VerifierIdentifier) { + return !!chainSettings && !!chainSettings.verifiers[verifierId]?.apiUrl && (verifierId !== 'Etherscan' || !!chainSettings.verifiers[verifierId]?.apiKey) +} diff --git a/apps/contract-verification/src/app/utils/index.ts b/apps/contract-verification/src/app/utils/index.ts new file mode 100644 index 0000000000..c03fe198aa --- /dev/null +++ b/apps/contract-verification/src/app/utils/index.ts @@ -0,0 +1 @@ +export * from './default-settings' diff --git a/apps/contract-verification/src/app/views/LookupView.tsx b/apps/contract-verification/src/app/views/LookupView.tsx index d89f780bec..8656d0d3b2 100644 --- a/apps/contract-verification/src/app/views/LookupView.tsx +++ b/apps/contract-verification/src/app/views/LookupView.tsx @@ -1,6 +1,8 @@ import { useContext, useState } from 'react' import { SearchableChainDropdown, ContractAddressInput } from '../components' -import { LookupResponse, mergeChainSettingsWithDefaults, validConfiguration, VerifierIdentifier, VERIFIERS, type Chain } from '../types' +import { mergeChainSettingsWithDefaults, validConfiguration } from '../utils' +import type { LookupResponse, VerifierIdentifier, Chain } from '../types' +import { VERIFIERS } from '../types' import { AppContext } from '../AppContext' import { CustomTooltip } from '@remix-ui/helper' import { getVerifier } from '../Verifiers' diff --git a/apps/contract-verification/src/app/views/SettingsView.tsx b/apps/contract-verification/src/app/views/SettingsView.tsx index cab2a6f287..2c4e1c4d6e 100644 --- a/apps/contract-verification/src/app/views/SettingsView.tsx +++ b/apps/contract-verification/src/app/views/SettingsView.tsx @@ -1,7 +1,7 @@ import { useContext, useState } from 'react' import { SearchableChainDropdown, ConfigInput } from '../components' import type { VerifierIdentifier, Chain, VerifierSettings, ContractVerificationSettings } from '../types' -import { mergeChainSettingsWithDefaults } from '../types' +import { mergeChainSettingsWithDefaults } from '../utils' import { AppContext } from '../AppContext' export const SettingsView = () => { diff --git a/apps/contract-verification/src/app/views/VerifyView.tsx b/apps/contract-verification/src/app/views/VerifyView.tsx index b804e5cdc7..4a91f46762 100644 --- a/apps/contract-verification/src/app/views/VerifyView.tsx +++ b/apps/contract-verification/src/app/views/VerifyView.tsx @@ -2,7 +2,9 @@ import { useContext, useEffect, useState } from 'react' import { AppContext } from '../AppContext' import { SearchableChainDropdown, ContractDropdown, ContractAddressInput } from '../components' -import { mergeChainSettingsWithDefaults, type VerifierIdentifier, VERIFIERS, type Chain, type SubmittedContract, type VerificationReceipt, type VerifierInfo, validConfiguration } from '../types' +import type { VerifierIdentifier, Chain, SubmittedContract, VerificationReceipt, VerifierInfo } from '../types' +import { VERIFIERS } from '../types' +import { mergeChainSettingsWithDefaults, validConfiguration } from '../utils' import { useNavigate } from 'react-router-dom' import { ConstructorArguments } from '../components/ConstructorArguments' import { ContractDropdownSelection } from '../components/ContractDropdown' diff --git a/apps/contract-verification/tsconfig.json b/apps/contract-verification/tsconfig.json index bfdc10e859..4e9bd6bcbe 100644 --- a/apps/contract-verification/tsconfig.json +++ b/apps/contract-verification/tsconfig.json @@ -5,6 +5,7 @@ "allowJs": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, + "resolveJsonModule": true, // "strict": true }, "files": [],