From 4531a818b881e57e5a34eca071df236583f78cea Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 24 Apr 2024 10:41:28 +0200 Subject: [PATCH] clean and fix discrepancy between injected keyword and specific extension --- .../providers/injected-provider-default.tsx | 2 +- apps/remix-ide/src/app/udapp/run-tab.js | 18 +-- apps/remix-ide/src/blockchain/blockchain.tsx | 48 +++---- .../src/blockchain/providers/injected.ts | 4 - .../src/blockchain/providers/node.ts | 4 - apps/remix-ide/src/blockchain/providers/vm.ts | 6 +- apps/remix-ide/src/remixAppManager.js | 2 - .../debugger-ui/src/lib/debugger-ui.tsx | 2 +- .../run-tab/src/lib/actions/account.ts | 2 +- .../run-tab/src/lib/actions/events.ts | 47 ++++--- .../run-tab/src/lib/components/account.tsx | 118 +++++++++--------- .../src/lib/components/universalDappUI.tsx | 2 +- .../run-tab/src/lib/types/blockchain.d.ts | 2 - .../run-tab/src/lib/types/injected.d.ts | 1 - libs/remix-ui/run-tab/src/lib/types/node.d.ts | 1 - libs/remix-ui/run-tab/src/lib/types/vm.d.ts | 1 - 16 files changed, 111 insertions(+), 149 deletions(-) diff --git a/apps/remix-ide/src/app/providers/injected-provider-default.tsx b/apps/remix-ide/src/app/providers/injected-provider-default.tsx index 15cc4ff7d8..c2a0f0a440 100644 --- a/apps/remix-ide/src/app/providers/injected-provider-default.tsx +++ b/apps/remix-ide/src/app/providers/injected-provider-default.tsx @@ -25,7 +25,7 @@ export class InjectedProviderDefaultBase extends InjectedProvider { } const profile = { - name: 'injected', + name: 'injected', // the name will be overwritten in the constructor. displayName: 'Injected Provider', kind: 'provider', description: 'injected Provider', diff --git a/apps/remix-ide/src/app/udapp/run-tab.js b/apps/remix-ide/src/app/udapp/run-tab.js index fd5c7800b6..8f66d03a0b 100644 --- a/apps/remix-ide/src/app/udapp/run-tab.js +++ b/apps/remix-ide/src/app/udapp/run-tab.js @@ -155,26 +155,28 @@ export class RunTab extends ViewPlugin { }) } - const addCustomInjectedProvider = async (position, event, name, networkId, urls, nativeCurrency) => { + const addCustomInjectedProvider = async (position, event, name, displayName, networkId, urls, nativeCurrency) => { // name = `${name} through ${event.detail.info.name}` await this.engine.register([new InjectedCustomProvider(event.detail.provider, name, networkId, urls, nativeCurrency)]) - await addProvider(position, name, name, true, false, false) + await addProvider(position, name, displayName, true, false, false) } const registerInjectedProvider = async (event) => { console.log('registerInjectedProvider', event) - await this.engine.register([new InjectedProviderDefault(event.detail.provider, event.detail.info.name)]) - await addProvider(0, event.detail.info.name, 'Injected Provider - ' + event.detail.info.name, true, false, false) + const name = 'injected-' + event.detail.info.name + const displayName = 'Injected Provider - ' + event.detail.info.name + await this.engine.register([new InjectedProviderDefault(event.detail.provider, name)]) + await addProvider(0, name, displayName, true, false, false) if (event.detail.info.name === 'MetaMask') { - await addCustomInjectedProvider(5, event, 'L2 - Optimism', '0xa', ['https://mainnet.optimism.io']) - await addCustomInjectedProvider(6, event, 'L2 - Arbitrum One', '0xa4b1', ['https://arb1.arbitrum.io/rpc']) - await addCustomInjectedProvider(7, event, 'Sepolia Testnet', '0xaa36a7', [], + await addCustomInjectedProvider(5, event, 'injected-metamask-optimism', 'L2 - Optimism', '0xa', ['https://mainnet.optimism.io']) + await addCustomInjectedProvider(6, event, 'injected-metamask-arbitrum', '0xa4b1', ['https://arb1.arbitrum.io/rpc']) + await addCustomInjectedProvider(7, event, 'injected-metamask-sepolia', '0xaa36a7', [], { "name": "Sepolia ETH", "symbol": "ETH", "decimals": 18 }) - await addCustomInjectedProvider(8, event, 'Ephemery Testnet', '', ['https://otter.bordel.wtf/erigon', 'https://eth.ephemeral.zeus.fyi'], + await addCustomInjectedProvider(8, event, 'injected-metamask-ephemery', 'Ephemery Testnet', '', ['https://otter.bordel.wtf/erigon', 'https://eth.ephemeral.zeus.fyi'], { "name": "Ephemery ETH", "symbol": "ETH", diff --git a/apps/remix-ide/src/blockchain/blockchain.tsx b/apps/remix-ide/src/blockchain/blockchain.tsx index fa57a1d58b..ff0b5647f4 100644 --- a/apps/remix-ide/src/blockchain/blockchain.tsx +++ b/apps/remix-ide/src/blockchain/blockchain.tsx @@ -61,6 +61,7 @@ export class Blockchain extends Plugin { } providers: {[key: string]: VMProvider | InjectedProvider | NodeProvider} transactionContextAPI: TransactionContextAPI + registeredPluginEvents: string[] // NOTE: the config object will need to be refactored out in remix-lib constructor(config: Config) { @@ -91,6 +92,7 @@ export class Blockchain extends Plugin { this.networkcallid = 0 this.networkStatus = {network: {name: ' - ', id: ' - '}} + this.registeredPluginEvents = [] this.setupEvents() this.setupProviders() } @@ -103,34 +105,23 @@ export class Blockchain extends Plugin { onActivation() { this.active = true - this.on('injected', 'chainChanged', () => { - this.detectNetwork((error, network) => { - this.networkStatus = {network, error} - this._triggerEvent('networkStatus', [this.networkStatus]) - }) - }) - - this.on('injected-trustwallet', 'chainChanged', () => { - this.detectNetwork((error, network) => { - this.networkStatus = {network, error} - this._triggerEvent('networkStatus', [this.networkStatus]) - }) - }) - - this.on('walletconnect', 'chainChanged', () => { - this.detectNetwork((error, network) => { - this.networkStatus = {network, error} - this._triggerEvent('networkStatus', [this.networkStatus]) - }) + this.on('manager', 'pluginActivated', (plugin) => { + if (plugin && plugin.name && (plugin.name.startsWith('injected') || plugin.name === 'walletconnect')) { + this.on(plugin.name, 'chainChanged', () => { + this.detectNetwork((error, network) => { + this.networkStatus = {network, error} + this._triggerEvent('networkStatus', [this.networkStatus]) + }) + }) + } }) } onDeactivation() { this.active = false - this.off('injected', 'chainChanged') - this.off('injected-trustwallet', 'chainChanged') - this.off('walletconnect', 'chainChanged') - this.off('walletconnect', 'accountsChanged') + for (const plugin of this.registeredPluginEvents) { + this.off(plugin, 'chainChanged') + } } setupEvents() { @@ -175,6 +166,7 @@ export class Blockchain extends Plugin { getCurrentProvider() { const provider = this.getProvider() if (provider && provider.startsWith('vm')) return this.providers['vm'] + if (provider && provider.startsWith('injected')) return this.providers['injected'] if (this.providers[provider]) return this.providers[provider] return this.providers.web3 // default to the common type of provider } @@ -534,16 +526,6 @@ export class Blockchain extends Plugin { return this.executionContext.getCurrentFork() } - isWeb3Provider() { - const isVM = this.executionContext.isVM() - const isInjected = this.getProvider() === 'injected' - return !isVM && !isInjected - } - - isInjectedWeb3() { - return this.getProvider() === 'injected' - } - signMessage(message, account, passphrase, cb) { this.getCurrentProvider().signMessage(message, account, passphrase, cb) } diff --git a/apps/remix-ide/src/blockchain/providers/injected.ts b/apps/remix-ide/src/blockchain/providers/injected.ts index 905e77559a..8ef55f8093 100644 --- a/apps/remix-ide/src/blockchain/providers/injected.ts +++ b/apps/remix-ide/src/blockchain/providers/injected.ts @@ -48,8 +48,4 @@ export class InjectedProvider { cb(e.message) } } - - getProvider () { - return 'injected' - } } diff --git a/apps/remix-ide/src/blockchain/providers/node.ts b/apps/remix-ide/src/blockchain/providers/node.ts index fec159f11f..9c7c538307 100644 --- a/apps/remix-ide/src/blockchain/providers/node.ts +++ b/apps/remix-ide/src/blockchain/providers/node.ts @@ -55,8 +55,4 @@ export class NodeProvider { cb(e.message) } } - - getProvider () { - return this.executionContext.getProvider() - } } diff --git a/apps/remix-ide/src/blockchain/providers/vm.ts b/apps/remix-ide/src/blockchain/providers/vm.ts index e44b67946a..8c210260fa 100644 --- a/apps/remix-ide/src/blockchain/providers/vm.ts +++ b/apps/remix-ide/src/blockchain/providers/vm.ts @@ -134,9 +134,5 @@ export class VMProvider { this.web3.eth.sign(message, account) .then(signedData => cb(null, bytesToHex(messageHash), signedData)) .catch(error => cb(error)) - } - - getProvider () { - return this.executionContext.getProvider() - } + } } diff --git a/apps/remix-ide/src/remixAppManager.js b/apps/remix-ide/src/remixAppManager.js index a4387ca47d..47c4af6202 100644 --- a/apps/remix-ide/src/remixAppManager.js +++ b/apps/remix-ide/src/remixAppManager.js @@ -52,7 +52,6 @@ let requiredModules = [ // services + layout views + system views 'ganache-provider', 'foundry-provider', 'basic-http-provider', - 'injected', 'injected-trustwallet', 'injected-optimism-provider', 'injected-arbitrum-one-provider', @@ -122,7 +121,6 @@ export function isNative(name) { 'injected-arbitrum-one-provider', 'injected-skale-chaos-testnet-provider', 'injected-ephemery-testnet-provider', - 'injected', 'doc-gen', 'doc-viewer', 'circuit-compiler', diff --git a/libs/remix-ui/debugger-ui/src/lib/debugger-ui.tsx b/libs/remix-ui/debugger-ui/src/lib/debugger-ui.tsx index 6ea35adb1c..b07760ea31 100644 --- a/libs/remix-ui/debugger-ui/src/lib/debugger-ui.tsx +++ b/libs/remix-ui/debugger-ui/src/lib/debugger-ui.tsx @@ -106,7 +106,7 @@ export const DebuggerUI = (props: DebuggerUIProps) => { const providerChanged = () => { debuggerModule.onEnvChanged((provider) => { setState((prevState) => { - const isLocalNodeUsed = !provider.startsWith('vm') && provider !== 'injected' + const isLocalNodeUsed = !provider.startsWith('vm') && !provider.startsWith('injected') return {...prevState, isLocalNodeUsed: isLocalNodeUsed} }) }) diff --git a/libs/remix-ui/run-tab/src/lib/actions/account.ts b/libs/remix-ui/run-tab/src/lib/actions/account.ts index e210cfe48c..e1b121faf1 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/account.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/account.ts @@ -30,7 +30,7 @@ export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch< } const provider = plugin.blockchain.getProvider() - if (provider === 'injected') { + if (provider && provider.startsWith('injected')) { const selectedAddress = plugin.blockchain.getInjectedWeb3Address() if (!(Object.keys(loadedAccounts).includes(toChecksumAddress(selectedAddress)))) setAccount(dispatch, null) } diff --git a/libs/remix-ui/run-tab/src/lib/actions/events.ts b/libs/remix-ui/run-tab/src/lib/actions/events.ts index 14334adad2..31120b4f5c 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/events.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/events.ts @@ -116,9 +116,29 @@ export const setupEvents = (plugin: RunTab, dispatch: React.Dispatch) => { }) }) - plugin.on('manager', 'pluginActivated', (plugin: Plugin) => { - if (plugin.name === 'remixd') { + plugin.on('manager', 'pluginActivated', (activatedPlugin: Plugin) => { + if (activatedPlugin.name === 'remixd') { dispatch(setRemixDActivated(true)) + } else { + if (activatedPlugin && activatedPlugin.name.startsWith('injected')) { + plugin.on(activatedPlugin.name, 'accountsChanged', (accounts: Array) => { + const accountsMap = {} + accounts.map(account => { accountsMap[account] = shortenAddress(account, '0')}) + dispatch(fetchAccountsListSuccess(accountsMap)) + }) + } else if (activatedPlugin && activatedPlugin.name === 'walletconnect') { + plugin.on('walletconnect', 'accountsChanged', async (accounts: Array) => { + const accountsMap = {} + + await Promise.all(accounts.map(async (account) => { + const balance = await plugin.blockchain.getBalanceInEther(account) + const updated = shortenAddress(account, balance) + + accountsMap[account] = updated + })) + dispatch(fetchAccountsListSuccess(accountsMap)) + }) + } } }) @@ -150,29 +170,6 @@ export const setupEvents = (plugin: RunTab, dispatch: React.Dispatch) => { dispatch(clearRecorderCount()) }) - plugin.on('injected', 'accountsChanged', (accounts: Array) => { - const accountsMap = {} - accounts.map(account => { accountsMap[account] = shortenAddress(account, '0')}) - dispatch(fetchAccountsListSuccess(accountsMap)) - }) - - plugin.on('injected-trustwallet', 'accountsChanged', (accounts: Array) => { - const accountsMap = {} - accounts.map(account => { accountsMap[account] = shortenAddress(account, '0')}) - dispatch(fetchAccountsListSuccess(accountsMap)) - }) - - plugin.on('walletconnect', 'accountsChanged', async (accounts: Array) => { - const accountsMap = {} - - await Promise.all(accounts.map(async (account) => { - const balance = await plugin.blockchain.getBalanceInEther(account) - const updated = shortenAddress(account, balance) - - accountsMap[account] = updated - })) - dispatch(fetchAccountsListSuccess(accountsMap)) - }) setInterval(() => { fillAccountsList(plugin, dispatch) diff --git a/libs/remix-ui/run-tab/src/lib/components/account.tsx b/libs/remix-ui/run-tab/src/lib/components/account.tsx index 21105223a6..5abd3a277a 100644 --- a/libs/remix-ui/run-tab/src/lib/components/account.tsx +++ b/libs/remix-ui/run-tab/src/lib/components/account.tsx @@ -26,69 +26,69 @@ export function AccountUI(props: AccountProps) { useEffect(() => { props.setAccount('') - switch (selectExEnv) { - case 'injected': + if (selectExEnv && selectExEnv.startsWith('injected')) { setPlusOpt({ classList: 'udapp_disableMouseEvents', title: intl.formatMessage({id: 'udapp.injectedTitle'}) }) - break - - case 'vm-cancun': - setPlusOpt({ - classList: '', - title: intl.formatMessage({id: 'udapp.createNewAccount'}) - }) - break - - case 'vm-paris': - setPlusOpt({ - classList: '', - title: intl.formatMessage({id: 'udapp.createNewAccount'}) - }) - break - - case 'vm-london': - setPlusOpt({ - classList: '', - title: intl.formatMessage({id: 'udapp.createNewAccount'}) - }) - break - - case 'vm-berlin': - setPlusOpt({ - classList: '', - title: intl.formatMessage({id: 'udapp.createNewAccount'}) - }) - break - - case 'vm-shanghai': - setPlusOpt({ - classList: '', - title: intl.formatMessage({id: 'udapp.createNewAccount'}) - }) - break - - case 'web3': - if (!personalMode) { - setPlusOpt({ - classList: 'disableMouseEvents', - title: intl.formatMessage({id: 'udapp.web3Title'}) - }) - } else { - setPlusOpt({ - classList: '', - title: intl.formatMessage({id: 'udapp.createNewAccount'}) - }) - } - break - - default: - setPlusOpt({ - classList: 'disableMouseEvents', - title: intl.formatMessage({id: 'udapp.defaultTitle'}, {selectExEnv}) - }) - } + } else { + switch (selectExEnv) { + case 'vm-cancun': + setPlusOpt({ + classList: '', + title: intl.formatMessage({id: 'udapp.createNewAccount'}) + }) + break + + case 'vm-paris': + setPlusOpt({ + classList: '', + title: intl.formatMessage({id: 'udapp.createNewAccount'}) + }) + break + + case 'vm-london': + setPlusOpt({ + classList: '', + title: intl.formatMessage({id: 'udapp.createNewAccount'}) + }) + break + + case 'vm-berlin': + setPlusOpt({ + classList: '', + title: intl.formatMessage({id: 'udapp.createNewAccount'}) + }) + break + + case 'vm-shanghai': + setPlusOpt({ + classList: '', + title: intl.formatMessage({id: 'udapp.createNewAccount'}) + }) + break + + case 'web3': + if (!personalMode) { + setPlusOpt({ + classList: 'disableMouseEvents', + title: intl.formatMessage({id: 'udapp.web3Title'}) + }) + } else { + setPlusOpt({ + classList: '', + title: intl.formatMessage({id: 'udapp.createNewAccount'}) + }) + } + break + + default: + setPlusOpt({ + classList: 'disableMouseEvents', + title: intl.formatMessage({id: 'udapp.defaultTitle'}, {selectExEnv}) + }) + } + } }, [selectExEnv, personalMode]) const newAccount = () => { diff --git a/libs/remix-ui/run-tab/src/lib/components/universalDappUI.tsx b/libs/remix-ui/run-tab/src/lib/components/universalDappUI.tsx index 701958f230..9362f1bcd8 100644 --- a/libs/remix-ui/run-tab/src/lib/components/universalDappUI.tsx +++ b/libs/remix-ui/run-tab/src/lib/components/universalDappUI.tsx @@ -297,7 +297,7 @@ export function UniversalDappUI(props: UdappProps) { - {props.exEnvironment === 'injected' && {props.editInstance(props.instance)}}>} + {props.exEnvironment.indexOf('Injected Provider') > -1 && {props.editInstance(props.instance)}}>} { props.isPinnedContract && props.instance.pinnedAt ? (
diff --git a/libs/remix-ui/run-tab/src/lib/types/blockchain.d.ts b/libs/remix-ui/run-tab/src/lib/types/blockchain.d.ts index a0b6bd0636..dbfa9ac9eb 100644 --- a/libs/remix-ui/run-tab/src/lib/types/blockchain.d.ts +++ b/libs/remix-ui/run-tab/src/lib/types/blockchain.d.ts @@ -47,8 +47,6 @@ export class Blockchain extends Plugin { * @return {String} - fork name */ getCurrentFork(): string; - isWeb3Provider(): boolean; - isInjectedWeb3(): boolean; signMessage(message: any, account: any, passphrase: any, cb: any): void; web3(): any; getTxListener(opts: any): any; diff --git a/libs/remix-ui/run-tab/src/lib/types/injected.d.ts b/libs/remix-ui/run-tab/src/lib/types/injected.d.ts index 2c2c4fb3fe..63e81fbb0e 100644 --- a/libs/remix-ui/run-tab/src/lib/types/injected.d.ts +++ b/libs/remix-ui/run-tab/src/lib/types/injected.d.ts @@ -8,5 +8,4 @@ declare class InjectedProvider { getBalanceInEther(address: any): Promise; getGasPrice(cb: any): void; signMessage(message: any, account: any, _passphrase: any, cb: any): void; - getProvider(): string; } diff --git a/libs/remix-ui/run-tab/src/lib/types/node.d.ts b/libs/remix-ui/run-tab/src/lib/types/node.d.ts index 84169e3be7..326a3f0596 100644 --- a/libs/remix-ui/run-tab/src/lib/types/node.d.ts +++ b/libs/remix-ui/run-tab/src/lib/types/node.d.ts @@ -9,5 +9,4 @@ declare class NodeProvider { getBalanceInEther(address: any): Promise; getGasPrice(cb: any): void; signMessage(message: any, account: any, passphrase: any, cb: any): void; - getProvider(): any; } diff --git a/libs/remix-ui/run-tab/src/lib/types/vm.d.ts b/libs/remix-ui/run-tab/src/lib/types/vm.d.ts index ff602d4f26..2700a50783 100644 --- a/libs/remix-ui/run-tab/src/lib/types/vm.d.ts +++ b/libs/remix-ui/run-tab/src/lib/types/vm.d.ts @@ -12,5 +12,4 @@ declare class VMProvider { getBalanceInEther(address: any): Promise; getGasPrice(cb: any): void; signMessage(message: any, account: any, _passphrase: any, cb: any): void; - getProvider(): string; }