From ff4880e4dd79535e9f29e99841e78a372267ae8f Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 30 Dec 2019 14:43:18 -0500 Subject: [PATCH 01/19] extra confirmation dialog out of udapp-ui --- src/app/udapp/run-tab.js | 2 +- src/app/ui/universal-dapp-ui.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/app/udapp/run-tab.js b/src/app/udapp/run-tab.js index f07d0d8c839..f9cfc583bcd 100644 --- a/src/app/udapp/run-tab.js +++ b/src/app/udapp/run-tab.js @@ -49,7 +49,7 @@ export class RunTab extends LibraryPlugin { } onActivationInternal () { - this.udappUI = new UniversalDAppUI(this.udapp, this.logCallback, this.executionContext) + this.udappUI = new UniversalDAppUI(this.blockchain, this.udapp, this.logCallback, this.executionContext) this.udapp.resetAPI({ getAddress: (cb) => { cb(null, $('#txorigin').val()) diff --git a/src/app/ui/universal-dapp-ui.js b/src/app/ui/universal-dapp-ui.js index 0818b6006ea..509bbb68749 100644 --- a/src/app/ui/universal-dapp-ui.js +++ b/src/app/ui/universal-dapp-ui.js @@ -15,7 +15,8 @@ var txFormat = remixLib.execution.txFormat var TreeView = require('./TreeView') var txCallBacks = require('./sendTxCallbacks') -function UniversalDAppUI (udapp, logCallback, executionContext) { +function UniversalDAppUI (blockchain, udapp, logCallback, executionContext) { + this.blockchain = blockchain this.udapp = udapp this.logCallback = logCallback this.compilerData = {contractsDetails: {}} From 71a083e2eadedae46563f0aa82b73057ee9cb6ef Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 30 Dec 2019 16:14:10 -0500 Subject: [PATCH 02/19] move udapp function call to blockchain module --- src/app/tabs/runTab/model/blockchain.js | 39 +++++++++++++++++++++ src/app/ui/universal-dapp-ui.js | 46 +++---------------------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/src/app/tabs/runTab/model/blockchain.js b/src/app/tabs/runTab/model/blockchain.js index 60fcae6a489..599f9a38b80 100644 --- a/src/app/tabs/runTab/model/blockchain.js +++ b/src/app/tabs/runTab/model/blockchain.js @@ -265,6 +265,45 @@ class Blockchain { this.udapp.startListening(txlistener) } + runOrCallContractMethod (contractName, contractABI, funABI, value, address, params, lookupOnly, logMsg, logCallback, outputCb, callbacksInContext) { + // contractsDetails is used to resolve libraries + txFormat.buildData(contractName, contractABI, {}, false, funABI, params, (error, data) => { + if (!error) { + if (!lookupOnly) { + logCallback(`${logMsg} pending ... `) + } else { + logCallback(`${logMsg}`) + } + if (funABI.type === 'fallback') data.dataHex = value + this.udapp.callFunction(address, data, funABI, callbacksInContext.confirmationCb.bind(callbacksInContext), callbacksInContext.continueCb.bind(callbacksInContext), callbacksInContext.promptCb.bind(callbacksInContext), (error, txResult) => { + if (!error) { + var isVM = this.executionContext.isVM() + if (isVM) { + var vmError = txExecution.checkVMError(txResult) + if (vmError.error) { + logCallback(`${logMsg} errored: ${vmError.message} `) + return + } + } + if (lookupOnly) { + const returnValue = (this.executionContext.isVM() ? txResult.result.execResult.returnValue : ethJSUtil.toBuffer(txResult.result)) + outputCb(returnValue) + } + } else { + logCallback(`${logMsg} errored: ${error} `) + } + }) + } else { + logCallback(`${logMsg} errored: ${error} `) + } + }, (msg) => { + logCallback(msg) + }, (data, runTxCallback) => { + // called for libraries deployment + this.udapp.runTx(data, callbacksInContext.confirmationCb.bind(callbacksInContext), runTxCallback) + }) + } + } module.exports = Blockchain diff --git a/src/app/ui/universal-dapp-ui.js b/src/app/ui/universal-dapp-ui.js index 509bbb68749..a3af0c564c6 100644 --- a/src/app/ui/universal-dapp-ui.js +++ b/src/app/ui/universal-dapp-ui.js @@ -10,7 +10,6 @@ var copyToClipboard = require('./copy-to-clipboard') var css = require('../../universal-dapp-styles') var MultiParamManager = require('./multiParamManager') var remixLib = require('remix-lib') -var txExecution = remixLib.execution.txExecution var txFormat = remixLib.execution.txFormat var TreeView = require('./TreeView') var txCallBacks = require('./sendTxCallbacks') @@ -232,55 +231,20 @@ UniversalDAppUI.prototype.getCallButton = function (args) { } UniversalDAppUI.prototype.runTransaction = function (lookupOnly, args, valArr, inputsValues, outputOverride) { - let self = this const functionName = args.funABI.type === 'function' ? args.funABI.name : `(${args.funABI.type})` const logMsg = `${lookupOnly ? 'call' : 'transact'} to ${args.contractName}.${functionName}` - var value = inputsValues + const callbacksInContext = txCallBacks.getCallBacksWithContext(this, this.executionContext) - const outputCb = (decoded) => { + const outputCb = (returnValue) => { if (outputOverride) { + const decoded = decodeResponseToTreeView(returnValue, args.funABI) outputOverride.innerHTML = '' outputOverride.appendChild(decoded) } } - // contractsDetails is used to resolve libraries - const callbacksInContext = txCallBacks.getCallBacksWithContext(self, self.executionContext) - txFormat.buildData(args.contractName, args.contractABI, {}, false, args.funABI, args.funABI.type !== 'fallback' ? value : '', (error, data) => { - if (!error) { - if (!lookupOnly) { - self.logCallback(`${logMsg} pending ... `) - } else { - self.logCallback(`${logMsg}`) - } - if (args.funABI.type === 'fallback') data.dataHex = value - self.udapp.callFunction(args.address, data, args.funABI, callbacksInContext.confirmationCb.bind(callbacksInContext), callbacksInContext.continueCb.bind(callbacksInContext), callbacksInContext.promptCb.bind(callbacksInContext), (error, txResult) => { - if (!error) { - var isVM = self.executionContext.isVM() - if (isVM) { - var vmError = txExecution.checkVMError(txResult) - if (vmError.error) { - self.logCallback(`${logMsg} errored: ${vmError.message} `) - return - } - } - if (lookupOnly) { - const decoded = decodeResponseToTreeView(self.executionContext.isVM() ? txResult.result.execResult.returnValue : ethJSUtil.toBuffer(txResult.result), args.funABI) - outputCb(decoded) - } - } else { - self.logCallback(`${logMsg} errored: ${error} `) - } - }) - } else { - self.logCallback(`${logMsg} errored: ${error} `) - } - }, (msg) => { - self.logCallback(msg) - }, (data, runTxCallback) => { - // called for libraries deployment - self.udapp.runTx(data, callbacksInContext.confirmationCb.bind(callbacksInContext), runTxCallback) - }) + const params = args.funABI.type !== 'fallback' ? inputsValues : '' + this.blockchain.runOrCallContractMethod(args.contractName, args.contractAbi, args.funABI, inputsValues, args.address, params, lookupOnly, logMsg, this.logCallback, outputCb, callbacksInContext) } module.exports = UniversalDAppUI From 57b2963f568c638290f1eb648990ffb5c6fe96ca Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 31 Dec 2019 10:45:25 -0500 Subject: [PATCH 03/19] remove udapp and executionContext from universal dapp ui --- src/app/tabs/runTab/model/blockchain.js | 4 ++++ src/app/udapp/run-tab.js | 2 +- src/app/ui/universal-dapp-ui.js | 13 +++++-------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/app/tabs/runTab/model/blockchain.js b/src/app/tabs/runTab/model/blockchain.js index 599f9a38b80..3c8f422f537 100644 --- a/src/app/tabs/runTab/model/blockchain.js +++ b/src/app/tabs/runTab/model/blockchain.js @@ -304,6 +304,10 @@ class Blockchain { }) } + context () { + return (this.executionContext.isVM() ? 'memory' : 'blockchain') + } + } module.exports = Blockchain diff --git a/src/app/udapp/run-tab.js b/src/app/udapp/run-tab.js index f9cfc583bcd..24171f6ab8a 100644 --- a/src/app/udapp/run-tab.js +++ b/src/app/udapp/run-tab.js @@ -49,7 +49,7 @@ export class RunTab extends LibraryPlugin { } onActivationInternal () { - this.udappUI = new UniversalDAppUI(this.blockchain, this.udapp, this.logCallback, this.executionContext) + this.udappUI = new UniversalDAppUI(this.blockchain, this.logCallback) this.udapp.resetAPI({ getAddress: (cb) => { cb(null, $('#txorigin').val()) diff --git a/src/app/ui/universal-dapp-ui.js b/src/app/ui/universal-dapp-ui.js index a3af0c564c6..ea892b1e05e 100644 --- a/src/app/ui/universal-dapp-ui.js +++ b/src/app/ui/universal-dapp-ui.js @@ -14,12 +14,10 @@ var txFormat = remixLib.execution.txFormat var TreeView = require('./TreeView') var txCallBacks = require('./sendTxCallbacks') -function UniversalDAppUI (blockchain, udapp, logCallback, executionContext) { +function UniversalDAppUI (blockchain, logCallback) { this.blockchain = blockchain - this.udapp = udapp this.logCallback = logCallback this.compilerData = {contractsDetails: {}} - this.executionContext = executionContext } function decodeResponseToTreeView (response, fnabi) { @@ -52,11 +50,10 @@ UniversalDAppUI.prototype.renderInstance = function (contract, address, contract // basically this has to be called for the "atAddress" (line 393) and when a contract creation succeed // this returns a DOM element UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address, contractName) { - var self = this address = (address.slice(0, 2) === '0x' ? '' : '0x') + address.toString('hex') address = ethJSUtil.toChecksumAddress(address) var instance = yo`
` - var context = self.udapp.context() + const context = this.blockchain.context() var shortAddress = helper.shortenAddress(address) var title = yo` @@ -160,8 +157,8 @@ UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address } setLLIError('') - const fallback = self.udapp.getFallbackInterface(contractABI) - const receive = self.udapp.getReceiveInterface(contractABI) + const fallback = this.udapp.getFallbackInterface(contractABI) + const receive = this.udapp.getReceiveInterface(contractABI) const args = { funABI: fallback || receive, address: address, @@ -203,7 +200,7 @@ UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address else if (fallback) args.funABI = fallback if (!args.funABI) return setLLIError(`Please define a 'Fallback' function to send calldata and a either 'Receive' or payable 'Fallback' to send ethers`) - self.runTransaction(false, args, null, calldataInput.value, null) + this.runTransaction(false, args, null, calldataInput.value, null) } contractActionsWrapper.appendChild(lowLevelInteracions) From 806e250e9830c34e8db190f666d7d490237e8cc7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 31 Dec 2019 11:31:20 -0500 Subject: [PATCH 04/19] move reset and init methods to blockchain class --- src/app/tabs/runTab/model/blockchain.js | 10 ++++ src/app/udapp/run-tab.js | 64 +++++++++++-------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/app/tabs/runTab/model/blockchain.js b/src/app/tabs/runTab/model/blockchain.js index 3c8f422f537..d64aa014788 100644 --- a/src/app/tabs/runTab/model/blockchain.js +++ b/src/app/tabs/runTab/model/blockchain.js @@ -308,6 +308,16 @@ class Blockchain { return (this.executionContext.isVM() ? 'memory' : 'blockchain') } + // NOTE: the config is only needed because exectuionContext.init does + // if config.get('settings/always-use-vm'), we can simplify this later + resetAndInit (config, transactionContext) { + this.udapp.resetAPI(transactionContext) + this.executionContext.init(config) + this.executionContext.stopListenOnLastBlock() + this.executionContext.listenOnLastBlock() + this.udapp.resetEnvironment() + } + } module.exports = Blockchain diff --git a/src/app/udapp/run-tab.js b/src/app/udapp/run-tab.js index 24171f6ab8a..2c1c04e5ffc 100644 --- a/src/app/udapp/run-tab.js +++ b/src/app/udapp/run-tab.js @@ -48,37 +48,6 @@ export class RunTab extends LibraryPlugin { this.networkModule = networkModule } - onActivationInternal () { - this.udappUI = new UniversalDAppUI(this.blockchain, this.logCallback) - this.udapp.resetAPI({ - getAddress: (cb) => { - cb(null, $('#txorigin').val()) - }, - getValue: (cb) => { - try { - const number = document.querySelector('#value').value - const select = document.getElementById('unit') - const index = select.selectedIndex - const selectedUnit = select.querySelectorAll('option')[index].dataset.unit - let unit = 'ether' // default - if (['ether', 'finney', 'gwei', 'wei'].indexOf(selectedUnit) >= 0) { - unit = selectedUnit - } - cb(null, Web3.utils.toWei(number, unit)) - } catch (e) { - cb(e) - } - }, - getGasLimit: (cb) => { - try { - cb(null, '0x' + new ethJSUtil.BN($('#gasLimit').val(), 10).toString(16)) - } catch (e) { - cb(e.message) - } - } - }) - } - renderContainer () { this.container = yo`
` @@ -200,11 +169,34 @@ export class RunTab extends LibraryPlugin { } render () { - this.onActivationInternal() - this.executionContext.init(this.config) - this.executionContext.stopListenOnLastBlock() - this.executionContext.listenOnLastBlock() - this.udapp.resetEnvironment() + this.udappUI = new UniversalDAppUI(this.blockchain, this.logCallback) + this.blockchain.resetAndInit(this.config, { + getAddress: (cb) => { + cb(null, $('#txorigin').val()) + }, + getValue: (cb) => { + try { + const number = document.querySelector('#value').value + const select = document.getElementById('unit') + const index = select.selectedIndex + const selectedUnit = select.querySelectorAll('option')[index].dataset.unit + let unit = 'ether' // default + if (['ether', 'finney', 'gwei', 'wei'].indexOf(selectedUnit) >= 0) { + unit = selectedUnit + } + cb(null, Web3.utils.toWei(number, unit)) + } catch (e) { + cb(e) + } + }, + getGasLimit: (cb) => { + try { + cb(null, '0x' + new ethJSUtil.BN($('#gasLimit').val(), 10).toString(16)) + } catch (e) { + cb(e.message) + } + } + }) this.renderInstanceContainer() this.renderSettings() this.renderDropdown(this.udappUI, this.fileManager, this.compilersArtefacts, this.config, this.editor, this.logCallback) From 04801f56a4271ccbf2c89b868020263634a1bf70 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 31 Dec 2019 11:33:28 -0500 Subject: [PATCH 05/19] remove udapp and execution context from run-tab --- src/app.js | 2 -- src/app/udapp/run-tab.js | 6 ++---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/app.js b/src/app.js index ad89486e314..636e3a179c8 100644 --- a/src/app.js +++ b/src/app.js @@ -300,8 +300,6 @@ Please make a backup of your contracts and start using http://remix.ethereum.org ) const run = new RunTab( blockchain, - udapp, - executionContext, registry.get('config').api, registry.get('filemanager').api, registry.get('editor').api, diff --git a/src/app/udapp/run-tab.js b/src/app/udapp/run-tab.js index 2c1c04e5ffc..7b4df5149f1 100644 --- a/src/app/udapp/run-tab.js +++ b/src/app/udapp/run-tab.js @@ -33,12 +33,10 @@ const profile = { export class RunTab extends LibraryPlugin { - constructor (blockchain, udapp, executionContext, config, fileManager, editor, filePanel, compilersArtefacts, networkModule, mainView) { - super(udapp, profile) + constructor (blockchain, config, fileManager, editor, filePanel, compilersArtefacts, networkModule, mainView) { + super(blockchain.udapp, profile) this.event = new EventManager() this.config = config - this.udapp = udapp - this.executionContext = executionContext this.blockchain = blockchain this.fileManager = fileManager this.editor = editor From 0821c23d381e51573f97e8b0c4e312b3111a2d8f Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 31 Dec 2019 11:38:19 -0500 Subject: [PATCH 06/19] move udapp reference to blockchain class --- src/app.js | 5 +---- src/app/tabs/runTab/model/blockchain.js | 7 +++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/app.js b/src/app.js index 636e3a179c8..e81e07f3f73 100644 --- a/src/app.js +++ b/src/app.js @@ -50,7 +50,6 @@ import { HiddenPanel } from './app/components/hidden-panel' import { VerticalIcons } from './app/components/vertical-icons' import { LandingPage } from './app/ui/landing-page/landing-page' import { MainPanel } from './app/components/main-panel' -import { UniversalDApp } from 'remix-lib' import migrateFileSystem from './migrateFileSystem' @@ -225,9 +224,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org const fileManager = new FileManager(editor) registry.put({api: fileManager, name: 'filemanager'}) - // ----------------- universal dapp: run transaction, listen on transactions, decode events - const udapp = new UniversalDApp(registry.get('config').api, executionContext) - const blockchain = new Blockchain(executionContext, udapp) + const blockchain = new Blockchain(registry.get('config').api, executionContext) // ----------------- compilation metadata generation servive ---------------------------- const compilerMetadataGenerator = new CompilerMetadata(blockchain, fileManager, registry.get('config').api) diff --git a/src/app/tabs/runTab/model/blockchain.js b/src/app/tabs/runTab/model/blockchain.js index d64aa014788..d0eba44cb7d 100644 --- a/src/app/tabs/runTab/model/blockchain.js +++ b/src/app/tabs/runTab/model/blockchain.js @@ -8,12 +8,15 @@ const ethJSUtil = require('ethereumjs-util') const Personal = require('web3-eth-personal') const Web3 = require('web3') +import { UniversalDApp } from 'remix-lib' + class Blockchain { - constructor (executionContext, udapp) { + // NOTE: the config object will need to be refactored out in remix-lib + constructor (config, executionContext) { this.event = new EventManager() this.executionContext = executionContext - this.udapp = udapp + this.udapp = new UniversalDApp(config, this.executionContext) this.networkcallid = 0 this.setupEvents() From a5160fc341439bf0ecbcffaee688f4f190ca249b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 31 Dec 2019 11:46:21 -0500 Subject: [PATCH 07/19] replace executionContext for debugger with blockchain object --- src/app.js | 2 +- src/app/tabs/debugger-tab.js | 6 +++--- src/app/tabs/debugger/debuggerUI.js | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/app.js b/src/app.js index e81e07f3f73..28568a366bc 100644 --- a/src/app.js +++ b/src/app.js @@ -306,7 +306,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org mainview ) const analysis = new AnalysisTab(registry) - const debug = new DebuggerTab(executionContext) + const debug = new DebuggerTab(blockchain) const test = new TestTab( registry.get('filemanager').api, filePanel, diff --git a/src/app/tabs/debugger-tab.js b/src/app/tabs/debugger-tab.js index d86db9aef5a..ff636f8a7c1 100644 --- a/src/app/tabs/debugger-tab.js +++ b/src/app/tabs/debugger-tab.js @@ -21,10 +21,10 @@ const profile = { class DebuggerTab extends ViewPlugin { - constructor (executionContext) { + constructor (blockchain) { super(profile) this.el = null - this.executionContext = executionContext + this.blockchain = blockchain } render () { @@ -34,7 +34,7 @@ class DebuggerTab extends ViewPlugin {
` - this.debuggerUI = new DebuggerUI(this.el.querySelector('#debugger'), this.executionContext) + this.debuggerUI = new DebuggerUI(this.el.querySelector('#debugger'), this.blockchain) return this.el } diff --git a/src/app/tabs/debugger/debuggerUI.js b/src/app/tabs/debugger/debuggerUI.js index d30f32b5b95..5b199e045b8 100644 --- a/src/app/tabs/debugger/debuggerUI.js +++ b/src/app/tabs/debugger/debuggerUI.js @@ -30,9 +30,9 @@ var css = csjs` class DebuggerUI { - constructor (container, executionContext) { + constructor (container, blockchain) { this.registry = globalRegistry - this.executionContext = executionContext + this.blockchain = blockchain this.event = new EventManager() this.isActive = false @@ -105,13 +105,13 @@ class DebuggerUI { getDebugWeb3 () { return new Promise((resolve, reject) => { - this.executionContext.detectNetwork((error, network) => { + this.blockchain.detectNetwork((error, network) => { let web3 if (error || !network) { - web3 = init.web3DebugNode(this.executionContext.web3()) + web3 = init.web3DebugNode(this.blockchain.web3()) } else { const webDebugNode = init.web3DebugNode(network.name) - web3 = !webDebugNode ? this.executionContext.web3() : webDebugNode + web3 = !webDebugNode ? this.blockchain.web3() : webDebugNode } init.extendWeb3(web3) resolve(web3) From 72d97a3f138b526327517284d2e497340f7298cf Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 31 Dec 2019 11:54:47 -0500 Subject: [PATCH 08/19] replace executionContext for network module with blockchain object --- src/app.js | 2 +- src/app/tabs/network-module.js | 18 +++++++++--------- src/app/tabs/runTab/model/blockchain.js | 7 +++++++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/app.js b/src/app.js index 28568a366bc..a1c1b247dc7 100644 --- a/src/app.js +++ b/src/app.js @@ -234,7 +234,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org const {eventsDecoder, txlistener} = makeUdapp(blockchain, compilersArtefacts, (domEl) => mainview.getTerminal().logHtml(domEl)) // ----------------- network service (resolve network id / name) ---------------------------- - const networkModule = new NetworkModule(executionContext) + const networkModule = new NetworkModule(blockchain) // ----------------- convert offset to line/column service ---------------------------- var offsetToLineColumnConverter = new OffsetToLineColumnConverter() registry.put({api: offsetToLineColumnConverter, name: 'offsettolinecolumnconverter'}) diff --git a/src/app/tabs/network-module.js b/src/app/tabs/network-module.js index 4ead25261ed..21740401357 100644 --- a/src/app/tabs/network-module.js +++ b/src/app/tabs/network-module.js @@ -14,11 +14,11 @@ export const profile = { // - methods: ['getNetworkProvider', 'getEndpoint', 'detectNetwork', 'addNetwork', 'removeNetwork'] export class NetworkModule extends Plugin { - constructor (executionContext) { + constructor (blockchain) { super(profile) - this.executionContext = executionContext + this.blockchain = blockchain // TODO: See with remix-lib to make sementic coherent - this.executionContext.event.register('contextChanged', (provider) => { + this.blockchain.event.register('contextChanged', (provider) => { this.emit('providerChanged', provider) }) /* @@ -37,13 +37,13 @@ export class NetworkModule extends Plugin { /** Return the current network provider (web3, vm, injected) */ getNetworkProvider () { - return this.executionContext.getProvider() + return this.blockchain.getProvider() } /** Return the current network */ detectNetwork () { return new Promise((resolve, reject) => { - this.executionContext.detectNetwork((error, network) => { + this.blockchain.detectNetwork((error, network) => { error ? reject(error) : resolve(network) }) }) @@ -51,20 +51,20 @@ export class NetworkModule extends Plugin { /** Return the url only if network provider is 'web3' */ getEndpoint () { - const provider = this.executionContext.getProvider() + const provider = this.blockchain.getProvider() if (provider !== 'web3') { throw new Error('no endpoint: current provider is either injected or vm') } - return this.executionContext.web3().currentProvider.host + return this.blockchain.web3().currentProvider.host } /** Add a custom network to the list of available networks */ addNetwork (customNetwork) { - this.executionContext.addProvider(customNetwork) + this.blockchain.addProvider(customNetwork) } /** Remove a network to the list of availble networks */ removeNetwork (name) { - this.executionContext.removeProvider(name) + this.blockchain.removeProvider(name) } } diff --git a/src/app/tabs/runTab/model/blockchain.js b/src/app/tabs/runTab/model/blockchain.js index d0eba44cb7d..140bd5be8ee 100644 --- a/src/app/tabs/runTab/model/blockchain.js +++ b/src/app/tabs/runTab/model/blockchain.js @@ -321,6 +321,13 @@ class Blockchain { this.udapp.resetEnvironment() } + addNetwork (customNetwork) { + this.executionContext.addProvider(customNetwork) + } + + removeNetwork (name) { + this.executionContext.removeProvider(name) + } } module.exports = Blockchain From d07ff7be1b991c466c569b72bdb01c13e365e4ab Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 31 Dec 2019 11:56:42 -0500 Subject: [PATCH 09/19] move executionContext initialization to blockchain object --- src/app.js | 4 +--- src/app/tabs/runTab/model/blockchain.js | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/app.js b/src/app.js index a1c1b247dc7..428111b8718 100644 --- a/src/app.js +++ b/src/app.js @@ -22,8 +22,6 @@ var toolTip = require('./app/ui/tooltip') var CompilerMetadata = require('./app/files/compiler-metadata') var CompilerImport = require('./app/compiler/compiler-imports') -var executionContext = remixLib.execution.executionContext - const Blockchain = require('./app/tabs/runTab/model/blockchain.js') const PluginManagerComponent = require('./app/components/plugin-manager-component') @@ -224,7 +222,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org const fileManager = new FileManager(editor) registry.put({api: fileManager, name: 'filemanager'}) - const blockchain = new Blockchain(registry.get('config').api, executionContext) + const blockchain = new Blockchain(registry.get('config').api) // ----------------- compilation metadata generation servive ---------------------------- const compilerMetadataGenerator = new CompilerMetadata(blockchain, fileManager, registry.get('config').api) diff --git a/src/app/tabs/runTab/model/blockchain.js b/src/app/tabs/runTab/model/blockchain.js index 140bd5be8ee..e65b9c5d967 100644 --- a/src/app/tabs/runTab/model/blockchain.js +++ b/src/app/tabs/runTab/model/blockchain.js @@ -4,6 +4,7 @@ const txExecution = remixLib.execution.txExecution const typeConversion = remixLib.execution.typeConversion const Txlistener = remixLib.execution.txListener const EventManager = remixLib.EventManager +const executionContext = remixLib.execution.executionContext const ethJSUtil = require('ethereumjs-util') const Personal = require('web3-eth-personal') const Web3 = require('web3') @@ -13,7 +14,7 @@ import { UniversalDApp } from 'remix-lib' class Blockchain { // NOTE: the config object will need to be refactored out in remix-lib - constructor (config, executionContext) { + constructor (config) { this.event = new EventManager() this.executionContext = executionContext this.udapp = new UniversalDApp(config, this.executionContext) From 3d426df64bc8b040ec734aaced1fef1d7206070e Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 31 Dec 2019 12:07:56 -0500 Subject: [PATCH 10/19] move blockchain class to its own folder --- src/app.js | 2 +- src/{app/tabs/runTab/model => blockchain}/blockchain.js | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{app/tabs/runTab/model => blockchain}/blockchain.js (100%) diff --git a/src/app.js b/src/app.js index 428111b8718..6aab808d8c1 100644 --- a/src/app.js +++ b/src/app.js @@ -22,7 +22,7 @@ var toolTip = require('./app/ui/tooltip') var CompilerMetadata = require('./app/files/compiler-metadata') var CompilerImport = require('./app/compiler/compiler-imports') -const Blockchain = require('./app/tabs/runTab/model/blockchain.js') +const Blockchain = require('./blockchain/blockchain.js') const PluginManagerComponent = require('./app/components/plugin-manager-component') const CompilersArtefacts = require('./app/compiler/compiler-artefacts') diff --git a/src/app/tabs/runTab/model/blockchain.js b/src/blockchain/blockchain.js similarity index 100% rename from src/app/tabs/runTab/model/blockchain.js rename to src/blockchain/blockchain.js From 2ac2b64b85a43c9c87467ba3531fbdd3f5bff314 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 31 Dec 2019 12:16:14 -0500 Subject: [PATCH 11/19] move udapp from remix-lib back to remix-ide --- src/blockchain/blockchain.js | 2 +- src/blockchain/txResultHelper.js | 46 ++++ src/blockchain/universalDapp.js | 375 +++++++++++++++++++++++++++++++ 3 files changed, 422 insertions(+), 1 deletion(-) create mode 100644 src/blockchain/txResultHelper.js create mode 100644 src/blockchain/universalDapp.js diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index e65b9c5d967..27d83788647 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -9,7 +9,7 @@ const ethJSUtil = require('ethereumjs-util') const Personal = require('web3-eth-personal') const Web3 = require('web3') -import { UniversalDApp } from 'remix-lib' +const UniversalDApp = require('./universalDapp') class Blockchain { diff --git a/src/blockchain/txResultHelper.js b/src/blockchain/txResultHelper.js new file mode 100644 index 00000000000..f6083240628 --- /dev/null +++ b/src/blockchain/txResultHelper.js @@ -0,0 +1,46 @@ +'use strict' +const { bufferToHex, isHexString } = require('ethereumjs-util') + +function convertToPrefixedHex (input) { + if (input === undefined || input === null || isHexString(input)) { + return input + } else if (Buffer.isBuffer(input)) { + return bufferToHex(input) + } + return '0x' + input.toString(16) +} + +/* + txResult.result can be 3 different things: + - VM call or tx: ethereumjs-vm result object + - Node transaction: object returned from eth.getTransactionReceipt() + - Node call: return value from function call (not an object) + + Also, VM results use BN and Buffers, Node results use hex strings/ints, + So we need to normalize the values to prefixed hex strings +*/ +function resultToRemixTx (txResult) { + const { result, transactionHash } = txResult + const { status, execResult, gasUsed, createdAddress, contractAddress } = result + let returnValue, errorMessage + + if (isHexString(result)) { + returnValue = result + } else if (execResult !== undefined) { + returnValue = execResult.returnValue + errorMessage = execResult.exceptionError + } + + return { + transactionHash, + status, + gasUsed: convertToPrefixedHex(gasUsed), + error: errorMessage, + return: convertToPrefixedHex(returnValue), + createdAddress: convertToPrefixedHex(createdAddress || contractAddress) + } +} + +module.exports = { + resultToRemixTx +} diff --git a/src/blockchain/universalDapp.js b/src/blockchain/universalDapp.js new file mode 100644 index 00000000000..370dedbb54d --- /dev/null +++ b/src/blockchain/universalDapp.js @@ -0,0 +1,375 @@ +const async = require('async') +const { BN, privateToAddress, isValidPrivate, stripHexPrefix, toChecksumAddress } = require('ethereumjs-util') +const crypto = require('crypto') +const { EventEmitter } = require('events') + +const remixLib = require('remix-lib') + +const TxRunner = remixLib.execution.txRunner +const txHelper = remixLib.execution.txHelper +const EventManager = remixLib.EventManager + +const { resultToRemixTx } = require('./txResultHelper') + +class UniversalDApp { + + constructor (config, executionContext) { + this.events = new EventEmitter() + this.event = new EventManager() + // has a default for now for backwards compatability + this.executionContext = executionContext + this.config = config + + this.txRunner = new TxRunner({}, { + config: config, + detectNetwork: (cb) => { + this.executionContext.detectNetwork(cb) + }, + personalMode: () => { + return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false + } + }, this.executionContext) + this.accounts = {} + this.executionContext.event.register('contextChanged', this.resetEnvironment.bind(this)) + } + + // TODO : event should be triggered by Udapp instead of TxListener + /** Listen on New Transaction. (Cannot be done inside constructor because txlistener doesn't exist yet) */ + startListening (txlistener) { + txlistener.event.register('newTransaction', (tx) => { + this.events.emit('newTransaction', tx) + }) + } + + resetEnvironment () { + this.accounts = {} + if (this.executionContext.isVM()) { + this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000') + this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000') + this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000') + this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000') + this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000') + } + // TODO: most params here can be refactored away in txRunner + this.txRunner = new TxRunner(this.accounts, { + // TODO: only used to check value of doNotShowTransactionConfirmationAgain property + config: this.config, + // TODO: to refactor, TxRunner already has access to executionContext + detectNetwork: (cb) => { + this.executionContext.detectNetwork(cb) + }, + personalMode: () => { + return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false + } + }, this.executionContext) + this.txRunner.event.register('transactionBroadcasted', (txhash) => { + this.executionContext.detectNetwork((error, network) => { + if (error || !network) return + this.event.trigger('transactionBroadcasted', [txhash, network.name]) + }) + }) + } + + resetAPI (transactionContextAPI) { + this.transactionContextAPI = transactionContextAPI + } + + /** + * Create a VM Account + * @param {{privateKey: string, balance: string}} newAccount The new account to create + */ + createVMAccount (newAccount) { + const { privateKey, balance } = newAccount + if (this.executionContext.getProvider() !== 'vm') { + throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed') + } + this._addAccount(privateKey, balance) + const privKey = Buffer.from(privateKey, 'hex') + return '0x' + privateToAddress(privKey).toString('hex') + } + + newAccount (password, passwordPromptCb, cb) { + if (!this.executionContext.isVM()) { + if (!this.config.get('settings/personal-mode')) { + return cb('Not running in personal mode') + } + passwordPromptCb((passphrase) => { + this.executionContext.web3().personal.newAccount(passphrase, cb) + }) + } else { + let privateKey + do { + privateKey = crypto.randomBytes(32) + } while (!isValidPrivate(privateKey)) + this._addAccount(privateKey, '0x56BC75E2D63100000') + cb(null, '0x' + privateToAddress(privateKey).toString('hex')) + } + } + + /** Add an account to the list of account (only for Javascript VM) */ + _addAccount (privateKey, balance) { + if (!this.executionContext.isVM()) { + throw new Error('_addAccount() cannot be called in non-VM mode') + } + + if (this.accounts) { + privateKey = Buffer.from(privateKey, 'hex') + const address = privateToAddress(privateKey) + + // FIXME: we don't care about the callback, but we should still make this proper + let stateManager = this.executionContext.vm().stateManager + stateManager.getAccount(address, (error, account) => { + if (error) return console.log(error) + account.balance = balance || '0xf00000000000000001' + stateManager.putAccount(address, account, function cb (error) { + if (error) console.log(error) + }) + }) + + this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } + } + } + + /** Return the list of accounts */ + getAccounts (cb) { + return new Promise((resolve, reject) => { + const provider = this.executionContext.getProvider() + switch (provider) { + case 'vm': { + if (!this.accounts) { + if (cb) cb('No accounts?') + reject('No accounts?') + return + } + if (cb) cb(null, Object.keys(this.accounts)) + resolve(Object.keys(this.accounts)) + } + break + case 'web3': { + if (this.config.get('settings/personal-mode')) { + return this.executionContext.web3().personal.getListAccounts((error, accounts) => { + if (cb) cb(error, accounts) + if (error) return reject(error) + resolve(accounts) + }) + } else { + this.executionContext.web3().eth.getAccounts((error, accounts) => { + if (cb) cb(error, accounts) + if (error) return reject(error) + resolve(accounts) + }) + } + } + break + case 'injected': { + this.executionContext.web3().eth.getAccounts((error, accounts) => { + if (cb) cb(error, accounts) + if (error) return reject(error) + resolve(accounts) + }) + } + } + }) + } + + /** Get the balance of an address */ + getBalance (address, cb) { + address = stripHexPrefix(address) + + if (!this.executionContext.isVM()) { + this.executionContext.web3().eth.getBalance(address, (err, res) => { + if (err) { + cb(err) + } else { + cb(null, res.toString(10)) + } + }) + } else { + if (!this.accounts) { + return cb('No accounts?') + } + + this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => { + if (err) { + cb('Account not found') + } else { + cb(null, new BN(res.balance).toString(10)) + } + }) + } + } + + /** Get the balance of an address, and convert wei to ether */ + getBalanceInEther (address, callback) { + this.getBalance(address, (error, balance) => { + if (error) { + callback(error) + } else { + callback(null, this.executionContext.web3().fromWei(balance, 'ether')) + } + }) + } + + pendingTransactionsCount () { + return Object.keys(this.txRunner.pendingTxs).length + } + + /** + * deploy the given contract + * + * @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). + * @param {Function} callback - callback. + */ + createContract (data, confirmationCb, continueCb, promptCb, callback) { + this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => { + // see universaldapp.js line 660 => 700 to check possible values of txResult (error case) + callback(error, txResult) + }) + } + + /** + * call the current given contract + * + * @param {String} to - address of the contract to call. + * @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). + * @param {Object} funAbi - abi definition of the function to call. + * @param {Function} callback - callback. + */ + callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) { + const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure' + this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, (error, txResult) => { + // see universaldapp.js line 660 => 700 to check possible values of txResult (error case) + callback(error, txResult) + }) + } + + context () { + return (this.executionContext.isVM() ? 'memory' : 'blockchain') + } + + getABI (contract) { + return txHelper.sortAbiFunction(contract.abi) + } + + getFallbackInterface (contractABI) { + return txHelper.getFallbackInterface(contractABI) + } + + getInputs (funABI) { + if (!funABI.inputs) { + return '' + } + return txHelper.inputParametersDeclarationToString(funABI.inputs) + } + + /** + * This function send a tx only to javascript VM or testnet, will return an error for the mainnet + * SHOULD BE TAKEN CAREFULLY! + * + * @param {Object} tx - transaction. + */ + sendTransaction (tx) { + return new Promise((resolve, reject) => { + this.executionContext.detectNetwork((error, network) => { + if (error) return reject(error) + if (network.name === 'Main' && network.id === '1') { + return reject(new Error('It is not allowed to make this action against mainnet')) + } + this.silentRunTx(tx, (error, result) => { + if (error) return reject(error) + try { + resolve(resultToRemixTx(result)) + } catch (e) { + reject(e) + } + }) + }) + }) + } + + /** + * This function send a tx without alerting the user (if mainnet or if gas estimation too high). + * SHOULD BE TAKEN CAREFULLY! + * + * @param {Object} tx - transaction. + * @param {Function} callback - callback. + */ + silentRunTx (tx, cb) { + this.txRunner.rawRun( + tx, + (network, tx, gasEstimation, continueTxExecution, cancelCb) => { continueTxExecution() }, + (error, continueTxExecution, cancelCb) => { if (error) { cb(error) } else { continueTxExecution() } }, + (okCb, cancelCb) => { okCb() }, + cb + ) + } + + runTx (args, confirmationCb, continueCb, promptCb, cb) { + const self = this + async.waterfall([ + function getGasLimit (next) { + if (self.transactionContextAPI.getGasLimit) { + return self.transactionContextAPI.getGasLimit(next) + } + next(null, 3000000) + }, + function queryValue (gasLimit, next) { + if (args.value) { + return next(null, args.value, gasLimit) + } + if (args.useCall || !self.transactionContextAPI.getValue) { + return next(null, 0, gasLimit) + } + self.transactionContextAPI.getValue(function (err, value) { + next(err, value, gasLimit) + }) + }, + function getAccount (value, gasLimit, next) { + if (args.from) { + return next(null, args.from, value, gasLimit) + } + if (self.transactionContextAPI.getAddress) { + return self.transactionContextAPI.getAddress(function (err, address) { + next(err, address, value, gasLimit) + }) + } + self.getAccounts(function (err, accounts) { + let address = accounts[0] + + if (err) return next(err) + if (!address) return next('No accounts available') + if (self.executionContext.isVM() && !self.accounts[address]) { + return next('Invalid account selected') + } + next(null, address, value, gasLimit) + }) + }, + function runTransaction (fromAddress, value, gasLimit, next) { + const tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: value, gasLimit: gasLimit, timestamp: args.data.timestamp } + const payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName, contractABI: args.data.contractABI, linkReferences: args.data.linkReferences } + let timestamp = Date.now() + if (tx.timestamp) { + timestamp = tx.timestamp + } + + self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) + self.txRunner.rawRun(tx, confirmationCb, continueCb, promptCb, + function (error, result) { + let eventName = (tx.useCall ? 'callExecuted' : 'transactionExecuted') + self.event.trigger(eventName, [error, tx.from, tx.to, tx.data, tx.useCall, result, timestamp, payLoad]) + + if (error && (typeof (error) !== 'string')) { + if (error.message) error = error.message + else { + try { error = 'error: ' + JSON.stringify(error) } catch (e) {} + } + } + next(error, result) + } + ) + } + ], cb) + } +} + +module.exports = UniversalDApp From a9e16b82af63eaf905689ffed7450ef81a909bc6 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 1 Jan 2020 11:34:46 -0500 Subject: [PATCH 12/19] listen to new transaction using blockchain abstraction instead of txlistener --- src/app/ui/txLogger.js | 2 +- src/blockchain/blockchain.js | 5 ++++- src/blockchain/universalDapp.js | 8 -------- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/app/ui/txLogger.js b/src/app/ui/txLogger.js index 15c45f69825..bbdc73ed790 100644 --- a/src/app/ui/txLogger.js +++ b/src/app/ui/txLogger.js @@ -164,7 +164,7 @@ class TxLogger { } }) - this.txListener.event.register('newTransaction', (tx, receipt) => { + blockchain.event.register('newTransaction', (tx, receipt) => { log(this, tx, receipt) }) diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index 27d83788647..44d749eb17e 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -266,7 +266,10 @@ class Blockchain { } startListening (txlistener) { - this.udapp.startListening(txlistener) + txlistener.event.register('newTransaction', (tx, receipt) => { + this.event.emit('newTransaction', tx, receipt) + this.udapp.events.emit('newTransaction', tx, receipt) // for plugin backwards compatibility + }) } runOrCallContractMethod (contractName, contractABI, funABI, value, address, params, lookupOnly, logMsg, logCallback, outputCb, callbacksInContext) { diff --git a/src/blockchain/universalDapp.js b/src/blockchain/universalDapp.js index 370dedbb54d..ea288345296 100644 --- a/src/blockchain/universalDapp.js +++ b/src/blockchain/universalDapp.js @@ -33,14 +33,6 @@ class UniversalDApp { this.executionContext.event.register('contextChanged', this.resetEnvironment.bind(this)) } - // TODO : event should be triggered by Udapp instead of TxListener - /** Listen on New Transaction. (Cannot be done inside constructor because txlistener doesn't exist yet) */ - startListening (txlistener) { - txlistener.event.register('newTransaction', (tx) => { - this.events.emit('newTransaction', tx) - }) - } - resetEnvironment () { this.accounts = {} if (this.executionContext.isVM()) { From a827b6bd51333b66672177e0d12b84bfa73cb15d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 1 Jan 2020 12:01:01 -0500 Subject: [PATCH 13/19] Revert "listen to new transaction using blockchain abstraction instead of txlistener" This reverts commit c2d742226608ea6b1635ebd5e5715c2eae2665e2. --- src/app/ui/txLogger.js | 2 +- src/blockchain/blockchain.js | 5 +---- src/blockchain/universalDapp.js | 8 ++++++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/app/ui/txLogger.js b/src/app/ui/txLogger.js index bbdc73ed790..15c45f69825 100644 --- a/src/app/ui/txLogger.js +++ b/src/app/ui/txLogger.js @@ -164,7 +164,7 @@ class TxLogger { } }) - blockchain.event.register('newTransaction', (tx, receipt) => { + this.txListener.event.register('newTransaction', (tx, receipt) => { log(this, tx, receipt) }) diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index 44d749eb17e..27d83788647 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -266,10 +266,7 @@ class Blockchain { } startListening (txlistener) { - txlistener.event.register('newTransaction', (tx, receipt) => { - this.event.emit('newTransaction', tx, receipt) - this.udapp.events.emit('newTransaction', tx, receipt) // for plugin backwards compatibility - }) + this.udapp.startListening(txlistener) } runOrCallContractMethod (contractName, contractABI, funABI, value, address, params, lookupOnly, logMsg, logCallback, outputCb, callbacksInContext) { diff --git a/src/blockchain/universalDapp.js b/src/blockchain/universalDapp.js index ea288345296..370dedbb54d 100644 --- a/src/blockchain/universalDapp.js +++ b/src/blockchain/universalDapp.js @@ -33,6 +33,14 @@ class UniversalDApp { this.executionContext.event.register('contextChanged', this.resetEnvironment.bind(this)) } + // TODO : event should be triggered by Udapp instead of TxListener + /** Listen on New Transaction. (Cannot be done inside constructor because txlistener doesn't exist yet) */ + startListening (txlistener) { + txlistener.event.register('newTransaction', (tx) => { + this.events.emit('newTransaction', tx) + }) + } + resetEnvironment () { this.accounts = {} if (this.executionContext.isVM()) { From 20f39ede7a4ce2dab5c0afa7b8fe4011112cc3a7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 1 Jan 2020 12:41:53 -0500 Subject: [PATCH 14/19] replace web3 from wei with directly using web3 utils --- src/blockchain/universalDapp.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/blockchain/universalDapp.js b/src/blockchain/universalDapp.js index 370dedbb54d..99477a36496 100644 --- a/src/blockchain/universalDapp.js +++ b/src/blockchain/universalDapp.js @@ -8,6 +8,7 @@ const remixLib = require('remix-lib') const TxRunner = remixLib.execution.txRunner const txHelper = remixLib.execution.txHelper const EventManager = remixLib.EventManager +const Web3 = require('web3') const { resultToRemixTx } = require('./txResultHelper') @@ -205,7 +206,8 @@ class UniversalDApp { if (error) { callback(error) } else { - callback(null, this.executionContext.web3().fromWei(balance, 'ether')) + // callback(null, this.executionContext.web3().fromWei(balance, 'ether')) + callback(null, Web3.utils.fromWei(balance.toString(10), 'ether')) } }) } From 18cadabdfc50bf1e7c54c99d9f42c95f1739ca06 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 2 Jan 2020 07:05:33 -0500 Subject: [PATCH 15/19] remove unused methods from udapp --- src/blockchain/universalDapp.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/blockchain/universalDapp.js b/src/blockchain/universalDapp.js index 99477a36496..b08532f8d86 100644 --- a/src/blockchain/universalDapp.js +++ b/src/blockchain/universalDapp.js @@ -249,21 +249,6 @@ class UniversalDApp { return (this.executionContext.isVM() ? 'memory' : 'blockchain') } - getABI (contract) { - return txHelper.sortAbiFunction(contract.abi) - } - - getFallbackInterface (contractABI) { - return txHelper.getFallbackInterface(contractABI) - } - - getInputs (funABI) { - if (!funABI.inputs) { - return '' - } - return txHelper.inputParametersDeclarationToString(funABI.inputs) - } - /** * This function send a tx only to javascript VM or testnet, will return an error for the mainnet * SHOULD BE TAKEN CAREFULLY! From 1732e9e70762bbdabd11c9d6a0f9d46b8b85a72f Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 2 Jan 2020 07:43:56 -0500 Subject: [PATCH 16/19] abstract udapp plugin so it maintains backwards compatibilify with the plugin api --- src/app.js | 3 +++ src/app/udapp/run-tab.js | 4 ++-- src/blockchain/pluginUDapp.js | 35 +++++++++++++++++++++++++++++++++ src/blockchain/universalDapp.js | 1 - 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/blockchain/pluginUDapp.js diff --git a/src/app.js b/src/app.js index 6aab808d8c1..4c3fcbff44b 100644 --- a/src/app.js +++ b/src/app.js @@ -23,6 +23,7 @@ var CompilerMetadata = require('./app/files/compiler-metadata') var CompilerImport = require('./app/compiler/compiler-imports') const Blockchain = require('./blockchain/blockchain.js') +const PluginUDapp = require('./blockchain/pluginUDapp.js') const PluginManagerComponent = require('./app/components/plugin-manager-component') const CompilersArtefacts = require('./app/compiler/compiler-artefacts') @@ -223,6 +224,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org registry.put({api: fileManager, name: 'filemanager'}) const blockchain = new Blockchain(registry.get('config').api) + const pluginUdapp = new PluginUDapp(blockchain) // ----------------- compilation metadata generation servive ---------------------------- const compilerMetadataGenerator = new CompilerMetadata(blockchain, fileManager, registry.get('config').api) @@ -295,6 +297,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org ) const run = new RunTab( blockchain, + pluginUdapp, registry.get('config').api, registry.get('filemanager').api, registry.get('editor').api, diff --git a/src/app/udapp/run-tab.js b/src/app/udapp/run-tab.js index 7b4df5149f1..bf2c89a509e 100644 --- a/src/app/udapp/run-tab.js +++ b/src/app/udapp/run-tab.js @@ -33,8 +33,8 @@ const profile = { export class RunTab extends LibraryPlugin { - constructor (blockchain, config, fileManager, editor, filePanel, compilersArtefacts, networkModule, mainView) { - super(blockchain.udapp, profile) + constructor (blockchain, pluginUDapp, config, fileManager, editor, filePanel, compilersArtefacts, networkModule, mainView) { + super(pluginUDapp, profile) this.event = new EventManager() this.config = config this.blockchain = blockchain diff --git a/src/blockchain/pluginUDapp.js b/src/blockchain/pluginUDapp.js new file mode 100644 index 00000000000..a6ece20ddb6 --- /dev/null +++ b/src/blockchain/pluginUDapp.js @@ -0,0 +1,35 @@ +const { EventEmitter } = require('events') + +class PluginUdapp { + + constructor (blockchain) { + this.blockchain = blockchain + this.events = new EventEmitter() + this.setupEvents() + } + + setupEvents () { + this.blockchain.event.register('newTransaction', (tx, receipt) => { + this.events.trigger('newTransaction', [tx, receipt]) + }) + } + + createVMAccount (newAccount) { + return this.blockchain.udapp.createVMAccount(newAccount) + } + + sendTransaction (tx) { + return this.blockchain.udapp.sendTransaction(tx) + } + + getAccounts (cb) { + return this.blockchain.udapp.getAccounts(cb) + } + + pendingTransactionsCount () { + return this.blockchain.udapp.pendingTransactionsCount() + } + +} + +module.exports = PluginUdapp diff --git a/src/blockchain/universalDapp.js b/src/blockchain/universalDapp.js index b08532f8d86..ea491676d5c 100644 --- a/src/blockchain/universalDapp.js +++ b/src/blockchain/universalDapp.js @@ -6,7 +6,6 @@ const { EventEmitter } = require('events') const remixLib = require('remix-lib') const TxRunner = remixLib.execution.txRunner -const txHelper = remixLib.execution.txHelper const EventManager = remixLib.EventManager const Web3 = require('web3') From 5169cfb515b64a847c79cc4ed7c73ebed8dd2dfb Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 2 Jan 2020 08:41:56 -0500 Subject: [PATCH 17/19] move udapp into blockchain module --- src/app/tabs/runTab/settings.js | 1 + src/blockchain/blockchain.js | 395 ++++++++++++++++++++++++++++---- src/blockchain/universalDapp.js | 361 ----------------------------- 3 files changed, 357 insertions(+), 400 deletions(-) delete mode 100644 src/blockchain/universalDapp.js diff --git a/src/app/tabs/runTab/settings.js b/src/app/tabs/runTab/settings.js index 09375020a37..5017090f7b5 100644 --- a/src/app/tabs/runTab/settings.js +++ b/src/app/tabs/runTab/settings.js @@ -251,6 +251,7 @@ class SettingsUI { newAccount () { this.blockchain.newAccount( + '', (cb) => { modalDialogCustom.promptPassphraseCreation((error, passphrase) => { if (error) { diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index 27d83788647..e52bd77a898 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -2,6 +2,7 @@ const remixLib = require('remix-lib') const txFormat = remixLib.execution.txFormat const txExecution = remixLib.execution.txExecution const typeConversion = remixLib.execution.typeConversion +const TxRunner = remixLib.execution.txRunner const Txlistener = remixLib.execution.txListener const EventManager = remixLib.EventManager const executionContext = remixLib.execution.executionContext @@ -9,7 +10,12 @@ const ethJSUtil = require('ethereumjs-util') const Personal = require('web3-eth-personal') const Web3 = require('web3') -const UniversalDApp = require('./universalDapp') +const async = require('async') +const { BN, privateToAddress, isValidPrivate, stripHexPrefix, toChecksumAddress } = require('ethereumjs-util') +const crypto = require('crypto') +const { EventEmitter } = require('events') + +const { resultToRemixTx } = require('./txResultHelper') class Blockchain { @@ -17,7 +23,21 @@ class Blockchain { constructor (config) { this.event = new EventManager() this.executionContext = executionContext - this.udapp = new UniversalDApp(config, this.executionContext) + + this.events = new EventEmitter() + this.config = config + + this.txRunner = new TxRunner({}, { + config: config, + detectNetwork: (cb) => { + this.executionContext.detectNetwork(cb) + }, + personalMode: () => { + return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false + } + }, this.executionContext) + this.accounts = {} + this.executionContext.event.register('contextChanged', this.resetEnvironment.bind(this)) this.networkcallid = 0 this.setupEvents() @@ -35,18 +55,6 @@ class Blockchain { this.executionContext.event.register('removeProvider', (name) => { this.event.trigger('removeProvider', [name]) }) - - this.udapp.event.register('initiatingTransaction', (timestamp, tx, payLoad) => { - this.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) - }) - - this.udapp.event.register('transactionExecuted', (error, from, to, data, call, txResult, timestamp) => { - this.event.trigger('transactionExecuted', [error, from, to, data, call, txResult, timestamp]) - }) - - this.udapp.event.register('transactionBroadcasted', (txhash, networkName) => { - this.event.trigger('transactionBroadcasted', [txhash, networkName]) - }) } async deployContract (selectedContract, args, contractMetadata, compilerContracts, callbacks, confirmationCb) { @@ -74,7 +82,7 @@ class Blockchain { } runTransaction (data, continueCb, promptCb, confirmationCb, finalCb) { - this.udapp.runTx(data, confirmationCb, continueCb, promptCb, finalCb) + this.runTx(data, confirmationCb, continueCb, promptCb, finalCb) } createContract (selectedContract, data, continueCb, promptCb, confirmationCb, finalCb) { @@ -84,7 +92,7 @@ class Blockchain { data.contractABI = selectedContract.abi } - this.udapp.createContract(data, confirmationCb, continueCb, promptCb, + this._createContract(data, confirmationCb, continueCb, promptCb, (error, txResult) => { if (error) { return finalCb(`creation of ${selectedContract.name} errored: ${error}`) @@ -175,7 +183,7 @@ class Blockchain { } getAccountBalanceForAddress (address, cb) { - return this.udapp.getBalanceInEther(address, cb) + return this.getBalanceInEther(address, cb) } updateNetwork (cb) { @@ -196,14 +204,6 @@ class Blockchain { return this.executionContext.detectNetwork(cb) } - newAccount (passphraseCb, cb) { - return this.udapp.newAccount('', passphraseCb, cb) - } - - getAccounts (cb) { - return this.udapp.getAccounts(cb) - } - isWeb3Provider () { var isVM = this.executionContext.isVM() var isInjected = this.executionContext.getProvider() === 'injected' @@ -220,7 +220,7 @@ class Blockchain { if (isVM) { const personalMsg = ethJSUtil.hashPersonalMessage(Buffer.from(message)) - var privKey = this.udapp.accounts[account].privateKey + var privKey = this.accounts[account].privateKey try { var rsv = ethJSUtil.ecsign(personalMsg, privKey) var signedData = ethJSUtil.toRpcSig(rsv.v, rsv.r, rsv.s) @@ -259,16 +259,12 @@ class Blockchain { getTxListener (opts) { opts.event = { - udapp: this.udapp.event + udapp: this.event } const txlistener = new Txlistener(opts, this.executionContext) return txlistener } - startListening (txlistener) { - this.udapp.startListening(txlistener) - } - runOrCallContractMethod (contractName, contractABI, funABI, value, address, params, lookupOnly, logMsg, logCallback, outputCb, callbacksInContext) { // contractsDetails is used to resolve libraries txFormat.buildData(contractName, contractABI, {}, false, funABI, params, (error, data) => { @@ -279,7 +275,7 @@ class Blockchain { logCallback(`${logMsg}`) } if (funABI.type === 'fallback') data.dataHex = value - this.udapp.callFunction(address, data, funABI, callbacksInContext.confirmationCb.bind(callbacksInContext), callbacksInContext.continueCb.bind(callbacksInContext), callbacksInContext.promptCb.bind(callbacksInContext), (error, txResult) => { + this.callFunction(address, data, funABI, callbacksInContext.confirmationCb.bind(callbacksInContext), callbacksInContext.continueCb.bind(callbacksInContext), callbacksInContext.promptCb.bind(callbacksInContext), (error, txResult) => { if (!error) { var isVM = this.executionContext.isVM() if (isVM) { @@ -304,22 +300,18 @@ class Blockchain { logCallback(msg) }, (data, runTxCallback) => { // called for libraries deployment - this.udapp.runTx(data, callbacksInContext.confirmationCb.bind(callbacksInContext), runTxCallback) + this.runTx(data, callbacksInContext.confirmationCb.bind(callbacksInContext), runTxCallback) }) } - context () { - return (this.executionContext.isVM() ? 'memory' : 'blockchain') - } - // NOTE: the config is only needed because exectuionContext.init does // if config.get('settings/always-use-vm'), we can simplify this later resetAndInit (config, transactionContext) { - this.udapp.resetAPI(transactionContext) + this.resetAPI(transactionContext) this.executionContext.init(config) this.executionContext.stopListenOnLastBlock() this.executionContext.listenOnLastBlock() - this.udapp.resetEnvironment() + this.resetEnvironment() } addNetwork (customNetwork) { @@ -329,6 +321,331 @@ class Blockchain { removeNetwork (name) { this.executionContext.removeProvider(name) } + + // TODO : event should be triggered by Udapp instead of TxListener + /** Listen on New Transaction. (Cannot be done inside constructor because txlistener doesn't exist yet) */ + startListening (txlistener) { + txlistener.event.register('newTransaction', (tx) => { + this.events.emit('newTransaction', tx) + }) + } + + resetEnvironment () { + this.accounts = {} + if (this.executionContext.isVM()) { + this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000') + this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000') + this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000') + this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000') + this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000') + } + // TODO: most params here can be refactored away in txRunner + this.txRunner = new TxRunner(this.accounts, { + // TODO: only used to check value of doNotShowTransactionConfirmationAgain property + config: this.config, + // TODO: to refactor, TxRunner already has access to executionContext + detectNetwork: (cb) => { + this.executionContext.detectNetwork(cb) + }, + personalMode: () => { + return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false + } + }, this.executionContext) + this.txRunner.event.register('transactionBroadcasted', (txhash) => { + this.executionContext.detectNetwork((error, network) => { + if (error || !network) return + this.event.trigger('transactionBroadcasted', [txhash, network.name]) + }) + }) + } + + resetAPI (transactionContextAPI) { + this.transactionContextAPI = transactionContextAPI + } + + /** + * Create a VM Account + * @param {{privateKey: string, balance: string}} newAccount The new account to create + */ + createVMAccount (newAccount) { + const { privateKey, balance } = newAccount + if (this.executionContext.getProvider() !== 'vm') { + throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed') + } + this._addAccount(privateKey, balance) + const privKey = Buffer.from(privateKey, 'hex') + return '0x' + privateToAddress(privKey).toString('hex') + } + + newAccount (_password, passwordPromptCb, cb) { + if (!this.executionContext.isVM()) { + if (!this.config.get('settings/personal-mode')) { + return cb('Not running in personal mode') + } + passwordPromptCb((passphrase) => { + this.executionContext.web3().personal.newAccount(passphrase, cb) + }) + } else { + let privateKey + do { + privateKey = crypto.randomBytes(32) + } while (!isValidPrivate(privateKey)) + this._addAccount(privateKey, '0x56BC75E2D63100000') + cb(null, '0x' + privateToAddress(privateKey).toString('hex')) + } + } + + /** Add an account to the list of account (only for Javascript VM) */ + _addAccount (privateKey, balance) { + if (!this.executionContext.isVM()) { + throw new Error('_addAccount() cannot be called in non-VM mode') + } + + if (this.accounts) { + privateKey = Buffer.from(privateKey, 'hex') + const address = privateToAddress(privateKey) + + // FIXME: we don't care about the callback, but we should still make this proper + let stateManager = this.executionContext.vm().stateManager + stateManager.getAccount(address, (error, account) => { + if (error) return console.log(error) + account.balance = balance || '0xf00000000000000001' + stateManager.putAccount(address, account, function cb (error) { + if (error) console.log(error) + }) + }) + + this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } + } + } + + /** Return the list of accounts */ + getAccounts (cb) { + return new Promise((resolve, reject) => { + const provider = this.executionContext.getProvider() + switch (provider) { + case 'vm': { + if (!this.accounts) { + if (cb) cb('No accounts?') + reject('No accounts?') + return + } + if (cb) cb(null, Object.keys(this.accounts)) + resolve(Object.keys(this.accounts)) + } + break + case 'web3': { + if (this.config.get('settings/personal-mode')) { + return this.executionContext.web3().personal.getListAccounts((error, accounts) => { + if (cb) cb(error, accounts) + if (error) return reject(error) + resolve(accounts) + }) + } else { + this.executionContext.web3().eth.getAccounts((error, accounts) => { + if (cb) cb(error, accounts) + if (error) return reject(error) + resolve(accounts) + }) + } + } + break + case 'injected': { + this.executionContext.web3().eth.getAccounts((error, accounts) => { + if (cb) cb(error, accounts) + if (error) return reject(error) + resolve(accounts) + }) + } + } + }) + } + + /** Get the balance of an address */ + getBalance (address, cb) { + address = stripHexPrefix(address) + + if (!this.executionContext.isVM()) { + this.executionContext.web3().eth.getBalance(address, (err, res) => { + if (err) { + cb(err) + } else { + cb(null, res.toString(10)) + } + }) + } else { + if (!this.accounts) { + return cb('No accounts?') + } + + this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => { + if (err) { + cb('Account not found') + } else { + cb(null, new BN(res.balance).toString(10)) + } + }) + } + } + + /** Get the balance of an address, and convert wei to ether */ + getBalanceInEther (address, callback) { + this.getBalance(address, (error, balance) => { + if (error) { + callback(error) + } else { + // callback(null, this.executionContext.web3().fromWei(balance, 'ether')) + callback(null, Web3.utils.fromWei(balance.toString(10), 'ether')) + } + }) + } + + pendingTransactionsCount () { + return Object.keys(this.txRunner.pendingTxs).length + } + + /** + * deploy the given contract + * + * @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). + * @param {Function} callback - callback. + */ + _createContract (data, confirmationCb, continueCb, promptCb, callback) { + this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => { + // see universaldapp.js line 660 => 700 to check possible values of txResult (error case) + callback(error, txResult) + }) + } + + /** + * call the current given contract + * + * @param {String} to - address of the contract to call. + * @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). + * @param {Object} funAbi - abi definition of the function to call. + * @param {Function} callback - callback. + */ + callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) { + const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure' + this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, (error, txResult) => { + // see universaldapp.js line 660 => 700 to check possible values of txResult (error case) + callback(error, txResult) + }) + } + + context () { + return (this.executionContext.isVM() ? 'memory' : 'blockchain') + } + + /** + * This function send a tx only to javascript VM or testnet, will return an error for the mainnet + * SHOULD BE TAKEN CAREFULLY! + * + * @param {Object} tx - transaction. + */ + sendTransaction (tx) { + return new Promise((resolve, reject) => { + this.executionContext.detectNetwork((error, network) => { + if (error) return reject(error) + if (network.name === 'Main' && network.id === '1') { + return reject(new Error('It is not allowed to make this action against mainnet')) + } + this.silentRunTx(tx, (error, result) => { + if (error) return reject(error) + try { + resolve(resultToRemixTx(result)) + } catch (e) { + reject(e) + } + }) + }) + }) + } + + /** + * This function send a tx without alerting the user (if mainnet or if gas estimation too high). + * SHOULD BE TAKEN CAREFULLY! + * + * @param {Object} tx - transaction. + * @param {Function} callback - callback. + */ + silentRunTx (tx, cb) { + this.txRunner.rawRun( + tx, + (network, tx, gasEstimation, continueTxExecution, cancelCb) => { continueTxExecution() }, + (error, continueTxExecution, cancelCb) => { if (error) { cb(error) } else { continueTxExecution() } }, + (okCb, cancelCb) => { okCb() }, + cb + ) + } + + runTx (args, confirmationCb, continueCb, promptCb, cb) { + const self = this + async.waterfall([ + function getGasLimit (next) { + if (self.transactionContextAPI.getGasLimit) { + return self.transactionContextAPI.getGasLimit(next) + } + next(null, 3000000) + }, + function queryValue (gasLimit, next) { + if (args.value) { + return next(null, args.value, gasLimit) + } + if (args.useCall || !self.transactionContextAPI.getValue) { + return next(null, 0, gasLimit) + } + self.transactionContextAPI.getValue(function (err, value) { + next(err, value, gasLimit) + }) + }, + function getAccount (value, gasLimit, next) { + if (args.from) { + return next(null, args.from, value, gasLimit) + } + if (self.transactionContextAPI.getAddress) { + return self.transactionContextAPI.getAddress(function (err, address) { + next(err, address, value, gasLimit) + }) + } + self.getAccounts(function (err, accounts) { + let address = accounts[0] + + if (err) return next(err) + if (!address) return next('No accounts available') + if (self.executionContext.isVM() && !self.accounts[address]) { + return next('Invalid account selected') + } + next(null, address, value, gasLimit) + }) + }, + function runTransaction (fromAddress, value, gasLimit, next) { + const tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: value, gasLimit: gasLimit, timestamp: args.data.timestamp } + const payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName, contractABI: args.data.contractABI, linkReferences: args.data.linkReferences } + let timestamp = Date.now() + if (tx.timestamp) { + timestamp = tx.timestamp + } + + self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) + self.txRunner.rawRun(tx, confirmationCb, continueCb, promptCb, + function (error, result) { + let eventName = (tx.useCall ? 'callExecuted' : 'transactionExecuted') + self.event.trigger(eventName, [error, tx.from, tx.to, tx.data, tx.useCall, result, timestamp, payLoad]) + + if (error && (typeof (error) !== 'string')) { + if (error.message) error = error.message + else { + try { error = 'error: ' + JSON.stringify(error) } catch (e) {} + } + } + next(error, result) + } + ) + } + ], cb) + } + } module.exports = Blockchain diff --git a/src/blockchain/universalDapp.js b/src/blockchain/universalDapp.js deleted file mode 100644 index ea491676d5c..00000000000 --- a/src/blockchain/universalDapp.js +++ /dev/null @@ -1,361 +0,0 @@ -const async = require('async') -const { BN, privateToAddress, isValidPrivate, stripHexPrefix, toChecksumAddress } = require('ethereumjs-util') -const crypto = require('crypto') -const { EventEmitter } = require('events') - -const remixLib = require('remix-lib') - -const TxRunner = remixLib.execution.txRunner -const EventManager = remixLib.EventManager -const Web3 = require('web3') - -const { resultToRemixTx } = require('./txResultHelper') - -class UniversalDApp { - - constructor (config, executionContext) { - this.events = new EventEmitter() - this.event = new EventManager() - // has a default for now for backwards compatability - this.executionContext = executionContext - this.config = config - - this.txRunner = new TxRunner({}, { - config: config, - detectNetwork: (cb) => { - this.executionContext.detectNetwork(cb) - }, - personalMode: () => { - return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false - } - }, this.executionContext) - this.accounts = {} - this.executionContext.event.register('contextChanged', this.resetEnvironment.bind(this)) - } - - // TODO : event should be triggered by Udapp instead of TxListener - /** Listen on New Transaction. (Cannot be done inside constructor because txlistener doesn't exist yet) */ - startListening (txlistener) { - txlistener.event.register('newTransaction', (tx) => { - this.events.emit('newTransaction', tx) - }) - } - - resetEnvironment () { - this.accounts = {} - if (this.executionContext.isVM()) { - this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000') - this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000') - this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000') - this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000') - this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000') - } - // TODO: most params here can be refactored away in txRunner - this.txRunner = new TxRunner(this.accounts, { - // TODO: only used to check value of doNotShowTransactionConfirmationAgain property - config: this.config, - // TODO: to refactor, TxRunner already has access to executionContext - detectNetwork: (cb) => { - this.executionContext.detectNetwork(cb) - }, - personalMode: () => { - return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false - } - }, this.executionContext) - this.txRunner.event.register('transactionBroadcasted', (txhash) => { - this.executionContext.detectNetwork((error, network) => { - if (error || !network) return - this.event.trigger('transactionBroadcasted', [txhash, network.name]) - }) - }) - } - - resetAPI (transactionContextAPI) { - this.transactionContextAPI = transactionContextAPI - } - - /** - * Create a VM Account - * @param {{privateKey: string, balance: string}} newAccount The new account to create - */ - createVMAccount (newAccount) { - const { privateKey, balance } = newAccount - if (this.executionContext.getProvider() !== 'vm') { - throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed') - } - this._addAccount(privateKey, balance) - const privKey = Buffer.from(privateKey, 'hex') - return '0x' + privateToAddress(privKey).toString('hex') - } - - newAccount (password, passwordPromptCb, cb) { - if (!this.executionContext.isVM()) { - if (!this.config.get('settings/personal-mode')) { - return cb('Not running in personal mode') - } - passwordPromptCb((passphrase) => { - this.executionContext.web3().personal.newAccount(passphrase, cb) - }) - } else { - let privateKey - do { - privateKey = crypto.randomBytes(32) - } while (!isValidPrivate(privateKey)) - this._addAccount(privateKey, '0x56BC75E2D63100000') - cb(null, '0x' + privateToAddress(privateKey).toString('hex')) - } - } - - /** Add an account to the list of account (only for Javascript VM) */ - _addAccount (privateKey, balance) { - if (!this.executionContext.isVM()) { - throw new Error('_addAccount() cannot be called in non-VM mode') - } - - if (this.accounts) { - privateKey = Buffer.from(privateKey, 'hex') - const address = privateToAddress(privateKey) - - // FIXME: we don't care about the callback, but we should still make this proper - let stateManager = this.executionContext.vm().stateManager - stateManager.getAccount(address, (error, account) => { - if (error) return console.log(error) - account.balance = balance || '0xf00000000000000001' - stateManager.putAccount(address, account, function cb (error) { - if (error) console.log(error) - }) - }) - - this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } - } - } - - /** Return the list of accounts */ - getAccounts (cb) { - return new Promise((resolve, reject) => { - const provider = this.executionContext.getProvider() - switch (provider) { - case 'vm': { - if (!this.accounts) { - if (cb) cb('No accounts?') - reject('No accounts?') - return - } - if (cb) cb(null, Object.keys(this.accounts)) - resolve(Object.keys(this.accounts)) - } - break - case 'web3': { - if (this.config.get('settings/personal-mode')) { - return this.executionContext.web3().personal.getListAccounts((error, accounts) => { - if (cb) cb(error, accounts) - if (error) return reject(error) - resolve(accounts) - }) - } else { - this.executionContext.web3().eth.getAccounts((error, accounts) => { - if (cb) cb(error, accounts) - if (error) return reject(error) - resolve(accounts) - }) - } - } - break - case 'injected': { - this.executionContext.web3().eth.getAccounts((error, accounts) => { - if (cb) cb(error, accounts) - if (error) return reject(error) - resolve(accounts) - }) - } - } - }) - } - - /** Get the balance of an address */ - getBalance (address, cb) { - address = stripHexPrefix(address) - - if (!this.executionContext.isVM()) { - this.executionContext.web3().eth.getBalance(address, (err, res) => { - if (err) { - cb(err) - } else { - cb(null, res.toString(10)) - } - }) - } else { - if (!this.accounts) { - return cb('No accounts?') - } - - this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => { - if (err) { - cb('Account not found') - } else { - cb(null, new BN(res.balance).toString(10)) - } - }) - } - } - - /** Get the balance of an address, and convert wei to ether */ - getBalanceInEther (address, callback) { - this.getBalance(address, (error, balance) => { - if (error) { - callback(error) - } else { - // callback(null, this.executionContext.web3().fromWei(balance, 'ether')) - callback(null, Web3.utils.fromWei(balance.toString(10), 'ether')) - } - }) - } - - pendingTransactionsCount () { - return Object.keys(this.txRunner.pendingTxs).length - } - - /** - * deploy the given contract - * - * @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). - * @param {Function} callback - callback. - */ - createContract (data, confirmationCb, continueCb, promptCb, callback) { - this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => { - // see universaldapp.js line 660 => 700 to check possible values of txResult (error case) - callback(error, txResult) - }) - } - - /** - * call the current given contract - * - * @param {String} to - address of the contract to call. - * @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). - * @param {Object} funAbi - abi definition of the function to call. - * @param {Function} callback - callback. - */ - callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) { - const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure' - this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, (error, txResult) => { - // see universaldapp.js line 660 => 700 to check possible values of txResult (error case) - callback(error, txResult) - }) - } - - context () { - return (this.executionContext.isVM() ? 'memory' : 'blockchain') - } - - /** - * This function send a tx only to javascript VM or testnet, will return an error for the mainnet - * SHOULD BE TAKEN CAREFULLY! - * - * @param {Object} tx - transaction. - */ - sendTransaction (tx) { - return new Promise((resolve, reject) => { - this.executionContext.detectNetwork((error, network) => { - if (error) return reject(error) - if (network.name === 'Main' && network.id === '1') { - return reject(new Error('It is not allowed to make this action against mainnet')) - } - this.silentRunTx(tx, (error, result) => { - if (error) return reject(error) - try { - resolve(resultToRemixTx(result)) - } catch (e) { - reject(e) - } - }) - }) - }) - } - - /** - * This function send a tx without alerting the user (if mainnet or if gas estimation too high). - * SHOULD BE TAKEN CAREFULLY! - * - * @param {Object} tx - transaction. - * @param {Function} callback - callback. - */ - silentRunTx (tx, cb) { - this.txRunner.rawRun( - tx, - (network, tx, gasEstimation, continueTxExecution, cancelCb) => { continueTxExecution() }, - (error, continueTxExecution, cancelCb) => { if (error) { cb(error) } else { continueTxExecution() } }, - (okCb, cancelCb) => { okCb() }, - cb - ) - } - - runTx (args, confirmationCb, continueCb, promptCb, cb) { - const self = this - async.waterfall([ - function getGasLimit (next) { - if (self.transactionContextAPI.getGasLimit) { - return self.transactionContextAPI.getGasLimit(next) - } - next(null, 3000000) - }, - function queryValue (gasLimit, next) { - if (args.value) { - return next(null, args.value, gasLimit) - } - if (args.useCall || !self.transactionContextAPI.getValue) { - return next(null, 0, gasLimit) - } - self.transactionContextAPI.getValue(function (err, value) { - next(err, value, gasLimit) - }) - }, - function getAccount (value, gasLimit, next) { - if (args.from) { - return next(null, args.from, value, gasLimit) - } - if (self.transactionContextAPI.getAddress) { - return self.transactionContextAPI.getAddress(function (err, address) { - next(err, address, value, gasLimit) - }) - } - self.getAccounts(function (err, accounts) { - let address = accounts[0] - - if (err) return next(err) - if (!address) return next('No accounts available') - if (self.executionContext.isVM() && !self.accounts[address]) { - return next('Invalid account selected') - } - next(null, address, value, gasLimit) - }) - }, - function runTransaction (fromAddress, value, gasLimit, next) { - const tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: value, gasLimit: gasLimit, timestamp: args.data.timestamp } - const payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName, contractABI: args.data.contractABI, linkReferences: args.data.linkReferences } - let timestamp = Date.now() - if (tx.timestamp) { - timestamp = tx.timestamp - } - - self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) - self.txRunner.rawRun(tx, confirmationCb, continueCb, promptCb, - function (error, result) { - let eventName = (tx.useCall ? 'callExecuted' : 'transactionExecuted') - self.event.trigger(eventName, [error, tx.from, tx.to, tx.data, tx.useCall, result, timestamp, payLoad]) - - if (error && (typeof (error) !== 'string')) { - if (error.message) error = error.message - else { - try { error = 'error: ' + JSON.stringify(error) } catch (e) {} - } - } - next(error, result) - } - ) - } - ], cb) - } -} - -module.exports = UniversalDApp From ed22d29da0680fe132564ecd3baec0c11afae308 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 2 Jan 2020 08:47:42 -0500 Subject: [PATCH 18/19] update blockchain module syntax --- src/blockchain/blockchain.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index e52bd77a898..c1fdb334f34 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -60,7 +60,7 @@ class Blockchain { async deployContract (selectedContract, args, contractMetadata, compilerContracts, callbacks, confirmationCb) { const { continueCb, promptCb, statusCb, finalCb } = callbacks - var constructor = selectedContract.getConstructorInterface() + const constructor = selectedContract.getConstructorInterface() if (!contractMetadata || (contractMetadata && contractMetadata.autoDeployLib)) { return txFormat.buildData(selectedContract.name, selectedContract.object, compilerContracts, true, constructor, args, (error, data) => { if (error) return statusCb(`creation of ${selectedContract.name} errored: ` + error) @@ -97,9 +97,9 @@ class Blockchain { if (error) { return finalCb(`creation of ${selectedContract.name} errored: ${error}`) } - var isVM = this.executionContext.isVM() + const isVM = this.executionContext.isVM() if (isVM) { - var vmError = txExecution.checkVMError(txResult) + const vmError = txExecution.checkVMError(txResult) if (vmError.error) { return finalCb(vmError.message) } @@ -107,7 +107,7 @@ class Blockchain { if (txResult.result.status === false || txResult.result.status === '0x0') { return finalCb(`creation of ${selectedContract.name} errored: transaction execution failed`) } - var address = isVM ? txResult.result.createdAddress : txResult.result.contractAddress + const address = isVM ? txResult.result.createdAddress : txResult.result.contractAddress finalCb(null, selectedContract, address) } ) @@ -115,12 +115,12 @@ class Blockchain { determineGasPrice (cb) { this.getGasPrice((error, gasPrice) => { - var warnMessage = ' Please fix this issue before sending any transaction. ' + const warnMessage = ' Please fix this issue before sending any transaction. ' if (error) { return cb('Unable to retrieve the current network gas price.' + warnMessage + error) } try { - var gasPriceValue = this.fromWei(gasPrice, false, 'gwei') + const gasPriceValue = this.fromWei(gasPrice, false, 'gwei') cb(null, gasPriceValue) } catch (e) { cb(warnMessage + e.message, null, false) @@ -153,7 +153,7 @@ class Blockchain { // TODO: this try catch feels like an anti pattern, can/should be // removed, but for now keeping the original logic try { - var fee = this.calculateFee(tx.gas, gasPrice) + const fee = this.calculateFee(tx.gas, gasPrice) txFeeText = ' ' + this.fromWei(fee, false, 'ether') + ' Ether' priceStatus = true } catch (e) { @@ -205,8 +205,8 @@ class Blockchain { } isWeb3Provider () { - var isVM = this.executionContext.isVM() - var isInjected = this.executionContext.getProvider() === 'injected' + const isVM = this.executionContext.isVM() + const isInjected = this.executionContext.getProvider() === 'injected' return (!isVM && !isInjected) } @@ -215,15 +215,15 @@ class Blockchain { } signMessage (message, account, passphrase, cb) { - var isVM = this.executionContext.isVM() - var isInjected = this.executionContext.getProvider() === 'injected' + const isVM = this.executionContext.isVM() + const isInjected = this.executionContext.getProvider() === 'injected' if (isVM) { const personalMsg = ethJSUtil.hashPersonalMessage(Buffer.from(message)) - var privKey = this.accounts[account].privateKey + const privKey = this.accounts[account].privateKey try { - var rsv = ethJSUtil.ecsign(personalMsg, privKey) - var signedData = ethJSUtil.toRpcSig(rsv.v, rsv.r, rsv.s) + const rsv = ethJSUtil.ecsign(personalMsg, privKey) + const signedData = ethJSUtil.toRpcSig(rsv.v, rsv.r, rsv.s) cb(null, '0x' + personalMsg.toString('hex'), signedData) } catch (e) { cb(e.message) @@ -244,7 +244,7 @@ class Blockchain { const hashedMsg = Web3.utils.sha3(message) try { - var personal = new Personal(this.executionContext.web3().currentProvider) + const personal = new Personal(this.executionContext.web3().currentProvider) personal.sign(hashedMsg, account, passphrase, (error, signedData) => { cb(error.message, hashedMsg, signedData) }) @@ -277,9 +277,9 @@ class Blockchain { if (funABI.type === 'fallback') data.dataHex = value this.callFunction(address, data, funABI, callbacksInContext.confirmationCb.bind(callbacksInContext), callbacksInContext.continueCb.bind(callbacksInContext), callbacksInContext.promptCb.bind(callbacksInContext), (error, txResult) => { if (!error) { - var isVM = this.executionContext.isVM() + const isVM = this.executionContext.isVM() if (isVM) { - var vmError = txExecution.checkVMError(txResult) + const vmError = txExecution.checkVMError(txResult) if (vmError.error) { logCallback(`${logMsg} errored: ${vmError.message} `) return @@ -410,7 +410,7 @@ class Blockchain { stateManager.getAccount(address, (error, account) => { if (error) return console.log(error) account.balance = balance || '0xf00000000000000001' - stateManager.putAccount(address, account, function cb (error) { + stateManager.putAccount(address, account, (error) => { if (error) console.log(error) }) }) From 180081366397ea5d1416899d33cc8a2dd979d4b4 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 5 Feb 2020 14:06:46 +0100 Subject: [PATCH 19/19] fix missing ref --- src/app/ui/universal-dapp-ui.js | 12 +++++++----- src/blockchain/blockchain.js | 14 ++++++++++++++ src/blockchain/pluginUDapp.js | 8 ++++---- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/app/ui/universal-dapp-ui.js b/src/app/ui/universal-dapp-ui.js index ea892b1e05e..030e9a76239 100644 --- a/src/app/ui/universal-dapp-ui.js +++ b/src/app/ui/universal-dapp-ui.js @@ -11,6 +11,7 @@ var css = require('../../universal-dapp-styles') var MultiParamManager = require('./multiParamManager') var remixLib = require('remix-lib') var txFormat = remixLib.execution.txFormat +const txHelper = remixLib.execution.txHelper var TreeView = require('./TreeView') var txCallBacks = require('./sendTxCallbacks') @@ -41,7 +42,7 @@ UniversalDAppUI.prototype.renderInstance = function (contract, address, contract if (noInstances) { noInstances.parentNode.removeChild(noInstances) } - var abi = this.udapp.getABI(contract) + const abi = txHelper.sortAbiFunction(contract.abi) return this.renderInstanceFromABI(abi, address, contractName) } @@ -50,6 +51,7 @@ UniversalDAppUI.prototype.renderInstance = function (contract, address, contract // basically this has to be called for the "atAddress" (line 393) and when a contract creation succeed // this returns a DOM element UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address, contractName) { + let self = this address = (address.slice(0, 2) === '0x' ? '' : '0x') + address.toString('hex') address = ethJSUtil.toChecksumAddress(address) var instance = yo`
` @@ -157,8 +159,8 @@ UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address } setLLIError('') - const fallback = this.udapp.getFallbackInterface(contractABI) - const receive = this.udapp.getReceiveInterface(contractABI) + const fallback = self.blockchain.getFallbackInterface(contractABI) + const receive = self.blockchain.getReceiveInterface(contractABI) const args = { funABI: fallback || receive, address: address, @@ -200,7 +202,7 @@ UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address else if (fallback) args.funABI = fallback if (!args.funABI) return setLLIError(`Please define a 'Fallback' function to send calldata and a either 'Receive' or payable 'Fallback' to send ethers`) - this.runTransaction(false, args, null, calldataInput.value, null) + self.runTransaction(false, args, null, calldataInput.value, null) } contractActionsWrapper.appendChild(lowLevelInteracions) @@ -218,7 +220,7 @@ UniversalDAppUI.prototype.getCallButton = function (args) { lookupOnly, args.funABI, (valArray, inputsValues) => self.runTransaction(lookupOnly, args, valArray, inputsValues, outputOverride), - self.udapp.getInputs(args.funABI) + self.blockchain.getInputs(args.funABI) ) const contractActionsContainer = yo`
${multiParamManager.render()}
` diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index c1fdb334f34..88c3e06dfed 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -4,6 +4,7 @@ const txExecution = remixLib.execution.txExecution const typeConversion = remixLib.execution.typeConversion const TxRunner = remixLib.execution.txRunner const Txlistener = remixLib.execution.txListener +const txHelper = remixLib.execution.txHelper const EventManager = remixLib.EventManager const executionContext = remixLib.execution.executionContext const ethJSUtil = require('ethereumjs-util') @@ -132,6 +133,19 @@ class Blockchain { return this.executionContext.web3().eth.getGasPrice(cb) } + getFallbackInterface (contractABI) { + return txHelper.getFallbackInterface(contractABI) + } + + getReceiveInterface (contractABI) { + return txHelper.getReceiveInterface(contractABI) + } + + getInputs (funABI) { + if (!funABI.inputs) return '' + return txHelper.inputParametersDeclarationToString(funABI.inputs) + } + fromWei (value, doTypeConversion, unit) { if (doTypeConversion) { return Web3.utils.fromWei(typeConversion.toInt(value), unit || 'ether') diff --git a/src/blockchain/pluginUDapp.js b/src/blockchain/pluginUDapp.js index a6ece20ddb6..dec4b51d1d6 100644 --- a/src/blockchain/pluginUDapp.js +++ b/src/blockchain/pluginUDapp.js @@ -15,19 +15,19 @@ class PluginUdapp { } createVMAccount (newAccount) { - return this.blockchain.udapp.createVMAccount(newAccount) + return this.blockchain.createVMAccount(newAccount) } sendTransaction (tx) { - return this.blockchain.udapp.sendTransaction(tx) + return this.blockchain.sendTransaction(tx) } getAccounts (cb) { - return this.blockchain.udapp.getAccounts(cb) + return this.blockchain.getAccounts(cb) } pendingTransactionsCount () { - return this.blockchain.udapp.pendingTransactionsCount() + return this.blockchain.pendingTransactionsCount() } }