diff --git a/apps/remix-ide/src/app/tabs/hardhat-provider.js b/apps/remix-ide/src/app/tabs/hardhat-provider.js index 0df52d74ea..fe1923474f 100644 --- a/apps/remix-ide/src/app/tabs/hardhat-provider.js +++ b/apps/remix-ide/src/app/tabs/hardhat-provider.js @@ -20,6 +20,10 @@ export default class HardhatProvider extends Plugin { this.blockchain = blockchain } + onDeactivation () { + this.provider = null + } + hardhatProviderDialogBody () { return yo`
@@ -29,22 +33,32 @@ export default class HardhatProvider extends Plugin { } sendAsync (data) { - modalDialogCustom.prompt('Hardhat node request', this.hardhatProviderDialogBody(), 'http://127.0.0.1:8545', (target) => { - this.blockchain.setProviderFromEndpoint(target, 'Hardhat Provider', (alertMsg) => { - console.log('target-->', target) - this.provider = new Web3.providers.HttpProvider(target) - return new Promise((resolve, reject) => { - if (this.provider) { - this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](data, (error, message) => { - if (error) return reject(error) - resolve(message) - }) - } else { - resolve({ jsonrpc: '2.0', result: [], id: data.id }) - } - }) - }) + return new Promise((resolve, reject) => { + if (!this.provider) { + modalDialogCustom.prompt('Hardhat node request', this.hardhatProviderDialogBody(), 'http://127.0.0.1:8545', (target) => { + this.provider = new Web3.providers.HttpProvider(target) + sendAsyncInternal(this.provider, data, resolve, reject) + }, () => { + console.log('cancel clicked', this.provider) + console.log('inside if--->', this.blockchain.getProvider()) + console.log('inside if-2-->', this.blockchain.getCurrentProvider()) + this.call('udapp', 'setEnvironmentMode', this.blockchain.getProvider()) + }) + } else { + sendAsyncInternal(this.provider, data, resolve, reject) + } + }) + } +} + +const sendAsyncInternal = (provider, data, resolve, reject) => { + if (provider) { + provider[provider.sendAsync ? 'sendAsync' : 'send'](data, (error, message) => { + if (error) return reject(error) + resolve(message) }) + } else { + resolve({ jsonrpc: '2.0', result: [], id: data.id }) } } diff --git a/apps/remix-ide/src/app/tabs/runTab/settings.js b/apps/remix-ide/src/app/tabs/runTab/settings.js index 7885c6cc72..dd726c09be 100644 --- a/apps/remix-ide/src/app/tabs/runTab/settings.js +++ b/apps/remix-ide/src/app/tabs/runTab/settings.js @@ -244,20 +244,16 @@ class SettingsUI { } setExecutionContext (context) { - if (context === 'Hardhat Provider') { - this.blockchain.changeExecutionContext(context) - } else { - this.blockchain.changeExecutionContext(context, () => { - modalDialogCustom.prompt('External node request', this.web3ProviderDialogBody(), 'http://127.0.0.1:8545', (target) => { - this.blockchain.setProviderFromEndpoint(target, context, (alertMsg) => { - if (alertMsg) addTooltip(alertMsg) - this.setFinalContext() - }) - }, this.setFinalContext.bind(this)) - }, (alertMsg) => { - addTooltip(alertMsg) + this.blockchain.changeExecutionContext(context, () => { + modalDialogCustom.prompt('External node request', this.web3ProviderDialogBody(), 'http://127.0.0.1:8545', (target) => { + this.blockchain.setProviderFromEndpoint(target, context, (alertMsg) => { + if (alertMsg) addTooltip(alertMsg) + this.setFinalContext() + }) }, this.setFinalContext.bind(this)) - } + }, (alertMsg) => { + addTooltip(alertMsg) + }, this.setFinalContext.bind(this)) } web3ProviderDialogBody () {