Skip to content

Commit c7b62af

Browse files
committed
fix switch back to injected when chain id changes
1 parent f392eda commit c7b62af

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

apps/remix-ide/src/app/providers/injected-custom-provider.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@ export class InjectedCustomProvider extends InjectedProviderDefault {
88
rpcUrls: Array<string>
99
nativeCurrency: Record<string, any>
1010
blockExplorerUrls: Array<string>
11+
parent: string
1112

12-
constructor(provider: any, pluginName: string, chainName: string, chainId: string, rpcUrls: Array<string>, nativeCurrency?: Record<string, any>, blockExplorerUrls?: Array<string>) {
13+
constructor(provider: any, pluginName: string, chainName: string, chainId: string, rpcUrls: Array<string>, nativeCurrency?: Record<string, any>, blockExplorerUrls?: Array<string>, parent?: string) {
1314
super(provider, pluginName)
15+
this.parent = parent
1416
this.pluginName = pluginName
1517
this.chainName = chainName
1618
this.chainId = chainId
1719
this.rpcUrls = rpcUrls
1820
this.nativeCurrency = nativeCurrency
1921
this.blockExplorerUrls = blockExplorerUrls
22+
this.listenerChainChanged = (chainId: number) => {
23+
if (chainId !== parseInt(this.chainId)) {
24+
this.call('blockchain', 'changeExecutionContext', { context: this.parent })
25+
}
26+
}
2027
}
2128

2229
async init() {

apps/remix-ide/src/app/udapp/run-tab.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ export class RunTab extends ViewPlugin {
208208

209209
const addCustomInjectedProvider = async (position, event, name, displayName, networkId, urls, nativeCurrency?) => {
210210
// name = `${name} through ${event.detail.info.name}`
211-
await this.engine.register([new InjectedCustomProvider(event.detail.provider, name, displayName, networkId, urls, nativeCurrency)])
211+
const parent = 'injected-' + event.detail.info.name
212+
await this.engine.register([new InjectedCustomProvider(event.detail.provider, name, displayName, networkId, urls, nativeCurrency, [], parent)])
212213
await addProvider(position, name, displayName + ' - ' + event.detail.info.name, { isInjected: true, isVM: false, isRpcForkedState: false, fork: '' })
213214
}
214215
const registerInjectedProvider = async (event) => {

apps/remix-ide/src/blockchain/blockchain.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const profile = {
2525
name: 'blockchain',
2626
displayName: 'Blockchain',
2727
description: 'Blockchain - Logic',
28-
methods: ['getCode', 'getTransactionReceipt', 'addProvider', 'removeProvider', 'getCurrentFork', 'getAccounts', 'web3VM', 'web3', 'getProvider', 'getCurrentProvider', 'getCurrentNetworkStatus', 'getAllProviders', 'getPinnedProviders'],
28+
methods: ['getCode', 'getTransactionReceipt', 'addProvider', 'removeProvider', 'getCurrentFork', 'getAccounts', 'web3VM', 'web3', 'getProvider', 'getCurrentProvider', 'getCurrentNetworkStatus', 'getAllProviders', 'getPinnedProviders', 'changeExecutionContext'],
2929
version: packageJson.version
3030
}
3131

@@ -545,6 +545,10 @@ export class Blockchain extends Plugin {
545545
}
546546

547547
changeExecutionContext(context, confirmCb, infoCb, cb) {
548+
if (this.currentRequest && this.currentRequest.from && !this.currentRequest.from.startsWith('injected')) {
549+
// only injected provider can update the provider.
550+
return
551+
}
548552
if (context.context === 'item-another-chain') {
549553
this.call('manager', 'activatePlugin', 'environmentExplorer').then(() => this.call('tabs', 'focus', 'environmentExplorer'))
550554
} else {

libs/remix-ui/run-tab/src/lib/components/dropdownLabel.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { Provider } from '@remix-ui/environment-explorer'
21
import { CustomTooltip } from '@remix-ui/helper'
32
import React, { useEffect, useState } from 'react'
43
import { FormattedMessage } from 'react-intl'
5-
import { EnvDropdownLabelStateType, RunTabState } from '../types'
6-
import { current } from '@reduxjs/toolkit'
4+
import { RunTabState } from '../types'
75

86
export type DropDownLabelProps = {
97
label: string

0 commit comments

Comments
 (0)