diff --git a/apps/remix-ide/src/app/tabs/abstract-provider.tsx b/apps/remix-ide/src/app/tabs/abstract-provider.tsx index 0e97180747..204c6ce0be 100644 --- a/apps/remix-ide/src/app/tabs/abstract-provider.tsx +++ b/apps/remix-ide/src/app/tabs/abstract-provider.tsx @@ -74,9 +74,16 @@ export abstract class AbstractProvider extends Plugin { // the modal has been canceled/hide const result = data.method === 'net_listening' ? 'canceled' : [] resolve({ jsonrpc: '2.0', result: result, id: data.id }) + this.switchAway(false) return } this.provider = new ethers.providers.JsonRpcProvider(value) + try { + await this.provider.ready + } catch (e) { + this.switchAway(true) + return + } this.sendAsyncInternal(data, resolve, reject) } else { this.sendAsyncInternal(data, resolve, reject) @@ -84,6 +91,22 @@ export abstract class AbstractProvider extends Plugin { }) } + private async switchAway (showError) { + this.blocked = true + if (showError) { + const modalContent: AlertModal = { + id: this.profile.name, + title: this.profile.displayName, + message: `Error while connecting to the provider, provider not connected`, + } + this.call('notification', 'alert', modalContent) + } + await this.call('udapp', 'setEnvironmentMode', { context: 'vm', fork: 'london' }) + this.provider = null + setTimeout(_ => { this.blocked = false }, 1000) // we wait 1 second for letting remix to switch to vm + return + } + private async sendAsyncInternal (data: JsonDataRequest, resolve: SuccessRequest, reject: RejectRequest): Promise { if (this.provider) { // Check the case where current environment is VM on UI and it still sends RPC requests @@ -94,16 +117,6 @@ export abstract class AbstractProvider extends Plugin { const result = await this.provider.send(data.method, data.params) resolve({ jsonrpc: '2.0', result, id: data.id }) } catch (error) { - this.blocked = true - const modalContent: AlertModal = { - id: this.profile.name, - title: this.profile.displayName, - message: `Error while connecting to the provider: ${error.message}`, - } - this.call('notification', 'alert', modalContent) - await this.call('udapp', 'setEnvironmentMode', { context: 'vm', fork: 'london' }) - this.provider = null - setTimeout(_ => { this.blocked = false }, 1000) // we wait 1 second for letting remix to switch to vm reject(error) } } else {