From 80c46f77e1bcd4274d33d34fb5d57d5d89cf649b Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Tue, 28 Nov 2023 13:31:15 +0100 Subject: [PATCH] Move network event to app root --- apps/etherscan/src/app/AppContext.tsx | 3 ++- apps/etherscan/src/app/app.tsx | 20 +++++++++++++------- apps/etherscan/src/app/views/HomeView.tsx | 3 ++- apps/etherscan/src/app/views/VerifyView.tsx | 18 +++--------------- apps/remix-ide/src/blockchain/blockchain.tsx | 2 +- 5 files changed, 21 insertions(+), 25 deletions(-) diff --git a/apps/etherscan/src/app/AppContext.tsx b/apps/etherscan/src/app/AppContext.tsx index f121406a67..69d967534d 100644 --- a/apps/etherscan/src/app/AppContext.tsx +++ b/apps/etherscan/src/app/AppContext.tsx @@ -20,5 +20,6 @@ export const AppContext = React.createContext({ themeType: 'dark' as ThemeType, setThemeType: (themeType: ThemeType) => { console.log('Calling Set Theme Type') - } + }, + networkName: '' }) diff --git a/apps/etherscan/src/app/app.tsx b/apps/etherscan/src/app/app.tsx index 5331b2a214..39e89e00d7 100644 --- a/apps/etherscan/src/app/app.tsx +++ b/apps/etherscan/src/app/app.tsx @@ -16,13 +16,12 @@ import './App.css' export const getNewContractNames = (compilationResult: CompilationResult) => { const compiledContracts = compilationResult.contracts - const result: string[] = [] + let result: string[] = [] for (const file of Object.keys(compiledContracts)) { - const filePathArray = file.split('/') - const newContractNames = filePathArray[filePathArray.length - 1] + const newContractNames = Object.keys(compiledContracts[file]) - result.push(newContractNames) + result = [...result, ...newContractNames] } return result @@ -32,9 +31,10 @@ const plugin = new EtherscanPluginClient() const App = () => { const [apiKey, setAPIKey] = useLocalStorage('apiKey', '') - const [receipts, setReceipts] = useLocalStorage('receipts', []) + const [receipts, setReceipts] = useLocalStorage('receipts', []) const [contracts, setContracts] = useState([]) const [themeType, setThemeType] = useState('dark') + const [networkName, setNetworkName] = useState('Loading...') const timer = useRef(null) const contractsRef = useRef(contracts) @@ -51,6 +51,11 @@ const App = () => { setContracts(uniqueContracts) }) + plugin.on('blockchain' as any, 'networkStatus', (result) => { + setNetworkName(`${result.network.name} ${result.network.id !== '-' ? `(Chain id: ${result.network.id})` : '(Not supported)'}`) + }) + // @ts-ignore + plugin.call('blockchain', 'getCurrentNetworkStatus').then((result: any) => setNetworkName(`${result.network.name} ${result.network.id !== '-' ? `(Chain id: ${result.network.id})` : '(Not supported)'}`)) }) }, []) @@ -114,10 +119,11 @@ const App = () => { contracts, setContracts, themeType, - setThemeType + setThemeType, + networkName }} > - + { plugin && } ) } diff --git a/apps/etherscan/src/app/views/HomeView.tsx b/apps/etherscan/src/app/views/HomeView.tsx index e9db144bd1..c08a021f99 100644 --- a/apps/etherscan/src/app/views/HomeView.tsx +++ b/apps/etherscan/src/app/views/HomeView.tsx @@ -10,7 +10,7 @@ import {VerifyView} from './VerifyView' export const HomeView: React.FC = () => { return ( - {({apiKey, clientInstance, setReceipts, receipts, contracts}) => { + {({apiKey, clientInstance, setReceipts, receipts, contracts, networkName}) => { return !apiKey ? ( { const newReceipts = [...receipts, receipt] setReceipts(newReceipts) }} + networkName={networkName} /> ) }} diff --git a/apps/etherscan/src/app/views/VerifyView.tsx b/apps/etherscan/src/app/views/VerifyView.tsx index adfd0e6cfb..ab6e7c641c 100644 --- a/apps/etherscan/src/app/views/VerifyView.tsx +++ b/apps/etherscan/src/app/views/VerifyView.tsx @@ -14,7 +14,8 @@ interface Props { client: PluginClient apiKey: string onVerifiedContract: (receipt: Receipt) => void - contracts: string[] + contracts: string[], + networkName: string } interface FormValues { @@ -23,27 +24,14 @@ interface FormValues { expectedImplAddress?: string } -export const VerifyView: React.FC = ({apiKey, client, contracts, onVerifiedContract}) => { +export const VerifyView: React.FC = ({apiKey, client, contracts, onVerifiedContract, networkName}) => { const [results, setResults] = useState('') - const [networkName, setNetworkName] = useState('Loading...') const [selectedContract, setSelectedContract] = useState('') const [showConstructorArgs, setShowConstructorArgs] = useState(false) const [isProxyContract, setIsProxyContract] = useState(false) const [constructorInputs, setConstructorInputs] = useState([]) const verificationResult = useRef({}) - useEffect(() => { - if (client && client.on) { - client.on('blockchain' as any, 'networkStatus', (result) => { - setNetworkName(`${result.network.name} ${result.network.id !== '-' ? `(Chain id: ${result.network.id})` : '(Not supported)'}`) - }) - } - return () => { - // To fix memory leak - if (client && client.off) client.off('blockchain' as any, 'networkStatus') - } - }, [client]) - useEffect(() => { if (contracts.includes(selectedContract)) updateConsFields(selectedContract) }, [contracts]) diff --git a/apps/remix-ide/src/blockchain/blockchain.tsx b/apps/remix-ide/src/blockchain/blockchain.tsx index 779ea27f0a..d3f667de1d 100644 --- a/apps/remix-ide/src/blockchain/blockchain.tsx +++ b/apps/remix-ide/src/blockchain/blockchain.tsx @@ -23,7 +23,7 @@ const profile = { name: 'blockchain', displayName: 'Blockchain', description: 'Blockchain - Logic', - methods: ['getCode', 'getTransactionReceipt', 'addProvider', 'removeProvider', 'getCurrentFork', 'getAccounts', 'web3VM', 'web3', 'getProvider'], + methods: ['getCode', 'getTransactionReceipt', 'addProvider', 'removeProvider', 'getCurrentFork', 'getAccounts', 'web3VM', 'web3', 'getProvider', 'getCurrentNetworkStatus'], version: packageJson.version }