diff --git a/libs/remix-ui/run-tab/src/lib/actions/evmmap.ts b/libs/remix-ui/run-tab/src/lib/actions/evmmap.ts index e27df17cdb..672de51b5f 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/evmmap.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/evmmap.ts @@ -3,6 +3,12 @@ export type ChainInfo = { name: string } +export type ChainCompatibleInfo = { + chain: ChainInfo + minCompilerVersion: string + evmVersion: HardFork +} + export type HardFork = | 'cancun' | 'shanghai' @@ -37,7 +43,8 @@ export const evmMap: Map isChainCompatible(fork, chainId)) } -export function getCompatibleChain(fork: HardFork, chainId: number): ChainInfo | undefined { - const compatibleChains = getCompatibleChains(fork) - console.log('fork in getCompatibleChain', fork) - console.log('compatibleChains', compatibleChains) - return compatibleChains.find(chain => chain.id === chainId) +export function getCompatibleChain( + fork: HardFork, + chainId: number +): ChainCompatibleInfo | undefined { + const forkData = evmMap.get(fork) + if (!forkData) return undefined + + const compatibleChain = forkData.chainId.find(chain => chain.id === chainId) + if (compatibleChain) { + return { + chain: compatibleChain, + minCompilerVersion: forkData.minCompilerVersion, + evmVersion: fork + } + } + + return undefined; } diff --git a/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx b/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx index dbdbad5bbc..eed0eacf8b 100644 --- a/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx +++ b/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx @@ -172,20 +172,9 @@ export function ContractGUI(props: ContractGUIProps) { } } - const checkUrlLocationForEvmVersion = async () => { - // if the evmVersion is not provided in the url, we use the default value - // and the default would be the latest evmFork, which is now cancun. - // checking both url and compiler details - const url = window.location.href - const regVersion = url.match(/evmVersion=([a-zA-Z]+)/)?.[1] - const fetched = await props.getCompilerDetails() - return { regVersion, fetched } - } - const handleActionClick = async () => { props.getVersion() - const { regVersion, fetched } = await checkUrlLocationForEvmVersion() - console.log('checkUrlLocationForEvmVersion', { regVersion, fetched }) + await props.getCompilerDetails() if (deployState.deploy) { const proxyInitializeString = getMultiValsString(initializeFields.current) props.clickCallBack(props.initializerOptions.inputs.inputs, proxyInitializeString, ['Deploy with Proxy']) diff --git a/libs/remix-ui/run-tab/src/lib/components/environment.tsx b/libs/remix-ui/run-tab/src/lib/components/environment.tsx index 55149a8e9d..f5d3530f63 100644 --- a/libs/remix-ui/run-tab/src/lib/components/environment.tsx +++ b/libs/remix-ui/run-tab/src/lib/components/environment.tsx @@ -11,6 +11,16 @@ export function EnvironmentUI(props: EnvironmentProps) { Object.entries(props.providers.providerList.filter((provider) => { return provider.isInjected })) Object.entries(props.providers.providerList.filter((provider) => { return !(provider.isVM || provider.isInjected) })) + const EvaluateSelectionForCorrectness = async () => { + // if the evmVersion is not provided in the url, we use the default value + // and the default would be the latest evmFork, which is now cancun. + // checking both url and compiler details + const url = window.location.href + const regVersion = url.match(/evmVersion=([a-zA-Z]+)/)?.[1] + const fetched = await props.checkSelectionCorrectness() + return { regVersion, fetched } + } + const handleChangeExEnv = (env: string) => { const provider = props.providers.providerList.find((exEnv) => exEnv.name === env) const context = provider.name @@ -69,6 +79,9 @@ export function EnvironmentUI(props: EnvironmentProps) { onClick={() => { handleChangeExEnv(name) }} + onSelect={() => { + EvaluateSelectionForCorrectness() + }} data-id={`dropdown-item-${name}`} > diff --git a/libs/remix-ui/run-tab/src/lib/components/settingsUI.tsx b/libs/remix-ui/run-tab/src/lib/components/settingsUI.tsx index 295e3f966d..f7894042bd 100644 --- a/libs/remix-ui/run-tab/src/lib/components/settingsUI.tsx +++ b/libs/remix-ui/run-tab/src/lib/components/settingsUI.tsx @@ -1,5 +1,5 @@ // eslint-disable-next-line no-use-before-define -import React from 'react' +import React, { useEffect } from 'react' import { SettingsProps } from '../types' import { EnvironmentUI } from './environment' import { NetworkUI } from './network' @@ -10,9 +10,14 @@ import { ValueUI } from './value' export function SettingsUI(props: SettingsProps) { // this._deps.config.events.on('settings/personal-mode_changed', this.onPersonalChange.bind(this)) + useEffect(() => { + // listen for chainId change on window.ethereum and call EvaluateEnvironmentSelection + // (window as any).ethereum?.on('chainChanged', console.log('metamask did something')) //props.EvaluateEnvironmentSelection) + }, []) + return (
- + await checkEvmChainCompatibility() const returnCompatibleChain = async (evmVersion: HardFork, targetChainId: number) => { - return getCompatibleChain(evmVersion ?? 'cancun', targetChainId) + return getCompatibleChain(evmVersion ?? 'paris', targetChainId) // using paris evm as a default fallback version } - const checkEvmChainCompatibilityOkFunction = async (targetChainId: number, fetchDetails: any) => { + const checkEvmChainCompatibilityOkFunction = async (fetchDetails: ChainCompatibleInfo) => { const compilerParams = { - evmVersion: 'paris', + evmVersion: fetchDetails.evmVersion, optimize: false, language: 'Solidity', - runs: 200, - version: '0.8.27+commit.40a35a09' + runs: '200', + version: fetchDetails.minCompilerVersion } await plugin.call('solidity', 'setCompilerConfig', compilerParams) - const compilerState = await plugin.call('solidity', 'getCompilerState') - console.log('compilerState', compilerState) const currentFile = await plugin.call('fileManager', 'getCurrentFile') await plugin.call('solidity', 'compile', currentFile) } - const checkEvmChainCompatibilityCancelFunction = async (targetChainId: number, fetchDetails: any, currentFile: string) => { - () => { - console.log('cancel') - } - } - const checkEvmChainCompatibility = async () => { - const isVm = await plugin.call('blockchain', 'getProvider') // vms are exempt from this treatment - const fetchDetails = await plugin.call('solidity', 'getCompilerQueryParameters') //compiler details including evmVersion - console.log('isVm', isVm) - if (!isVm.startsWith('vm')) { + const isVm = await plugin.call('blockchain', 'getProvider') + const fetchDetails = await plugin.call('solidity', 'getCompilerQueryParameters') + const compilerState = await plugin.call('solidity', 'getCompilerState') + console.log('compilerState', compilerState) + console.log('runTab', runTab) + if (!isVm.startsWith('vm') && compilerState.target !== null) { //vms are exempt from this treatment & if no contract file is open, don't do anything const targetChainId = runTab.chainId ? parseInt(runTab.chainId) : runTab.chainId - console.log('targetChainId', runTab.chainId) - const IsCompatible = isChainCompatible(fetchDetails.evmVersion ?? 'cancun', targetChainId) - console.log('evmVersion matches everywhere', fetchDetails.evmVersion) - console.log('compiler stuff', fetchDetails) - console.log('chain is compatible', IsCompatible) - const currentFile = await plugin.call('fileManager', 'getCurrentFile') if (!IsCompatible) { - console.log('chain is undefined') + const chain = await returnCompatibleChain(fetchDetails.evmVersion, targetChainId) + console.log('chain obtained', { chain, targetChainId, fetchDetails }) //show modal plugin.call('notification', 'modal', { id: 'evm-chainId-incompatible', @@ -161,8 +150,8 @@ export function RunTabUI(props: RunTabProps) { modalType: 'modal', okLabel: 'Switch EVM and Recompile', cancelLabel: 'Cancel', - okFn: checkEvmChainCompatibilityOkFunction, - cancelFn: checkEvmChainCompatibilityCancelFunction + okFn: () => checkEvmChainCompatibilityOkFunction(chain), + cancelFn: () => {} }) } } @@ -351,6 +340,7 @@ export function RunTabUI(props: RunTabProps) { networkName={runTab.networkName} personalMode={runTab.personalMode} selectExEnv={runTab.selectExEnv} + EvaluateEnvironmentSelection={checkEvmChainCompatibility} accounts={runTab.accounts} setAccount={setAccountAddress} setUnit={setUnitValue} diff --git a/libs/remix-ui/run-tab/src/lib/types/index.ts b/libs/remix-ui/run-tab/src/lib/types/index.ts index b7edc0c64c..e95b6de359 100644 --- a/libs/remix-ui/run-tab/src/lib/types/index.ts +++ b/libs/remix-ui/run-tab/src/lib/types/index.ts @@ -125,6 +125,7 @@ export interface RunTabState { export interface SettingsProps { runTabPlugin: RunTab, selectExEnv: string, + EvaluateEnvironmentSelection: any accounts: { loadedAccounts: Record, selectedAccount: string, @@ -159,6 +160,7 @@ export interface SettingsProps { } export interface EnvironmentProps { + checkSelectionCorrectness: any runTabPlugin: RunTab, selectedEnv: string, providers: {