From 40f907d95febe0c8a7c480f4c4ad9f355030c22d Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 23 Feb 2023 17:18:39 +0100 Subject: [PATCH] set the provider only when it is initiated --- .../src/tests/transactionExecution.test.ts | 49 ++++++++++++++++++- apps/remix-ide/src/blockchain/providers/vm.js | 33 +++++++------ .../src/blockchain/providers/worker-vm.ts | 13 ++++- 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts b/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts index f22f843e9e..d91ec90bee 100644 --- a/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts +++ b/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts @@ -213,7 +213,28 @@ module.exports = { 'uint256 num': '24' } }) - .end() + }, + + 'Should switch to the mainnet VM fork and execute a tx to query ENS #group5': function (browser: NightwatchBrowser) { + let addressRef + browser + .addFile('mainnet_ens.sol', sources[7]['mainnet_ens.sol']) + .clickLaunchIcon('solidity') + .setSolidityCompilerVersion('soljson-v0.8.17+commit.8df45f5f.js') + .clickLaunchIcon('udapp') + .switchEnvironment('vm-mainnet-fork') + .waitForElementPresent('select[data-id="runTabSelectAccount"] option[value="0xdD870fA1b7C4700F2BD7f44238821C26f7392148"]') // wait for the udapp to load the list of accounts + .selectContract('MyResolver') + .createContract('') + .clickInstance(0) + .getAddressAtPosition(0, (address) => { + addressRef = address + }) + .clickFunction('resolve - call') + .perform((done) => { + browser.verifyCallReturnValue(addressRef, ['0:address: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045']) + .perform(() => done()) + }) } } @@ -431,5 +452,31 @@ contract C { } }` } + }, { + 'mainnet_ens.sol': { + content: + ` + import "https://github.com/ensdomains/ens-contracts/blob/master/contracts/utils/NameEncoder.sol"; + + abstract contract ENS { + function resolver(bytes32 node) public virtual view returns (Resolver); + } + + abstract contract Resolver { + function addr(bytes32 node) public virtual view returns (address); + } + + contract MyResolver { + // Same address for Mainet, Ropsten, Rinkerby, Gorli and other networks; + ENS ens = ENS(0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e); + + function resolve() public view returns(address) { + (,bytes32 node) = NameEncoder.dnsEncodeName("vitalik.eth"); + Resolver resolver = ens.resolver(node); + return resolver.addr(node); + } + } + ` + } } ] diff --git a/apps/remix-ide/src/blockchain/providers/vm.js b/apps/remix-ide/src/blockchain/providers/vm.js index 96466065ca..a4a600bbf8 100644 --- a/apps/remix-ide/src/blockchain/providers/vm.js +++ b/apps/remix-ide/src/blockchain/providers/vm.js @@ -24,27 +24,30 @@ class VMProvider { this.worker = new Worker(new URL('./worker-vm', import.meta.url)) const provider = this.executionContext.getProviderObject() - this.worker.postMessage({ cmd: 'init', fork: this.executionContext.getCurrentFork(), nodeUrl: provider?.options['nodeUrl'], blockNumber: provider?.options['blockNumber']}) - let incr = 0 const stamps = {} this.worker.addEventListener('message', (msg) => { - if (stamps[msg.data.stamp]) { + if (msg.data.cmd === 'sendAsyncResult' && stamps[msg.data.stamp]) { stamps[msg.data.stamp](msg.data.error, msg.data.result) + } else if (msg.data.cmd === 'initiateResult') { + if (!msg.data.error) { + this.provider = { + sendAsync: (query, callback) => { + const stamp = Date.now() + incr + incr++ + stamps[stamp] = callback + this.worker.postMessage({ cmd: 'sendAsync', query, stamp }) + } + } + this.web3 = new Web3(this.provider) + extend(this.web3) + this.accounts = {} + this.executionContext.setWeb3(this.executionContext.getProvider(), this.web3) + } } }) - this.provider = { - sendAsync: (query, callback) => { - const stamp = Date.now() + incr - incr++ - stamps[stamp] = callback - this.worker.postMessage({ cmd: 'sendAsync', query, stamp }) - } - } - this.web3 = new Web3(this.provider) - extend(this.web3) - this.accounts = {} - this.executionContext.setWeb3(this.executionContext.getProvider(), this.web3) + + this.worker.postMessage({ cmd: 'init', fork: this.executionContext.getCurrentFork(), nodeUrl: provider?.options['nodeUrl'], blockNumber: provider?.options['blockNumber']}) } // TODO: is still here because of the plugin API diff --git a/apps/remix-ide/src/blockchain/providers/worker-vm.ts b/apps/remix-ide/src/blockchain/providers/worker-vm.ts index 55ebb013a1..7473e6ce37 100644 --- a/apps/remix-ide/src/blockchain/providers/worker-vm.ts +++ b/apps/remix-ide/src/blockchain/providers/worker-vm.ts @@ -7,7 +7,18 @@ self.onmessage = (e: MessageEvent) => { case 'init': { provider = new Provider({ fork: data.fork, nodeUrl: data.nodeUrl, blockNumber: data.blockNumber }) - if (provider) provider.init() + provider.init().then(() => { + self.postMessage({ + cmd: 'initiateResult', + stamp: data.stamp + }) + }).catch((error) => { + self.postMessage({ + cmd: 'initiateResult', + error, + stamp: data.stamp + }) + }) break } case 'sendAsync':