temporarily block provider

pull/5370/head
yann300 3 years ago committed by joseph izang
parent 2d0830cd94
commit 23dce94569
  1. 22
      apps/remix-ide/src/app/tabs/hardhat-provider.js
  2. 2
      apps/remix-ide/src/remixAppManager.js

@ -1,6 +1,7 @@
import * as packageJson from '../../../../../package.json' import * as packageJson from '../../../../../package.json'
import { Plugin } from '@remixproject/engine' import { Plugin } from '@remixproject/engine'
import Web3 from 'web3' import Web3 from 'web3'
import toaster from '../ui/tooltip'
const yo = require('yo-yo') const yo = require('yo-yo')
const modalDialogCustom = require('../ui/modal-dialog-custom') const modalDialogCustom = require('../ui/modal-dialog-custom')
@ -17,11 +18,13 @@ export default class HardhatProvider extends Plugin {
constructor (blockchain) { constructor (blockchain) {
super(profile) super(profile)
this.provider = null this.provider = null
this.blocked = false // used to block any call when trying to recover after a failed connection.
this.blockchain = blockchain this.blockchain = blockchain
} }
onDeactivation () { onDeactivation () {
this.provider = null this.provider = null
this.blocked = false
} }
hardhatProviderDialogBody () { hardhatProviderDialogBody () {
@ -41,21 +44,14 @@ export default class HardhatProvider extends Plugin {
sendAsync (data) { sendAsync (data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.blocked) return reject(new Error('provider temporarily blocked'))
// If provider is not set, allow to open modal only when provider is trying to connect // If provider is not set, allow to open modal only when provider is trying to connect
if (!this.provider || data.method === 'net_listening') { if (!this.provider) {
modalDialogCustom.prompt('Hardhat node request', this.hardhatProviderDialogBody(), 'http://127.0.0.1:8545', (target) => { modalDialogCustom.prompt('Hardhat node request', this.hardhatProviderDialogBody(), 'http://127.0.0.1:8545', (target) => {
this.provider = new Web3.providers.HttpProvider(target) this.provider = new Web3.providers.HttpProvider(target)
this.sendAsyncInternal(data, resolve, reject) this.sendAsyncInternal(data, resolve, reject)
}, () => { }, () => {
// If 'cancel' is clicked while trying to connect, handle it in custom manner this.sendAsyncInternal(data, resolve, reject)
if (data.method === 'net_listening') resolve({ jsonrpc: '2.0', result: 'canceled', id: data.id })
else {
// When node is abruptly stopped, modal will appear
// On which clicking on 'Cancel' will set the Envrionment to VM
this.blockchain.changeExecutionContext('vm')
this.provider = this.blockchain.getCurrentProvider()
reject(new Error('Connection canceled'))
}
}) })
} else { } else {
this.sendAsyncInternal(data, resolve, reject) this.sendAsyncInternal(data, resolve, reject)
@ -68,9 +64,13 @@ export default class HardhatProvider extends Plugin {
// Check the case where current environment is VM on UI and it still sends RPC requests // Check the case where current environment is VM on UI and it still sends RPC requests
// This will be displayed on UI tooltip as 'cannot get account list: Environment Updated !!' // This will be displayed on UI tooltip as 'cannot get account list: Environment Updated !!'
if (this.blockchain.getProvider() !== 'Hardhat Provider' && data.method !== 'net_listening') return reject(new Error('Environment Updated !!')) if (this.blockchain.getProvider() !== 'Hardhat Provider' && data.method !== 'net_listening') return reject(new Error('Environment Updated !!'))
this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](data, (error, message) => { this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](data, async (error, message) => {
if (error) { if (error) {
this.blocked = true
modalDialogCustom.alert('Hardhat', `Error while connecting to the hardhat provider: ${error.message}`)
await this.call('udapp', 'setEnvironmentMode', 'vm')
this.provider = null this.provider = null
setTimeout(_ => this.blocked = false, 1000)
return reject(error) return reject(error)
} }
resolve(message) resolve(message)

@ -14,7 +14,7 @@ const requiredModules = [ // services + layout views + system views
const dependentModules = ['git', 'hardhat'] // module which shouldn't be manually activated (e.g git is activated by remixd) const dependentModules = ['git', 'hardhat'] // module which shouldn't be manually activated (e.g git is activated by remixd)
export function isNative (name) { export function isNative (name) {
const nativePlugins = ['vyper', 'workshops', 'debugger', 'remixd', 'menuicons', 'solidity'] const nativePlugins = ['vyper', 'workshops', 'debugger', 'remixd', 'menuicons', 'solidity', 'hardhat-provider']
return nativePlugins.includes(name) || requiredModules.includes(name) return nativePlugins.includes(name) || requiredModules.includes(name)
} }

Loading…
Cancel
Save