From 825ca33c2d506fe0f0905dd1e504f92205a112a0 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 31 Dec 2018 12:36:03 -0500 Subject: [PATCH] remove need for self in dropdown logic --- src/app/execution/confirmDialog.js | 1 + src/app/files/compiler-metadata.js | 1 + src/app/tabs/run-tab.js | 11 +++- src/app/tabs/runTab/contractDropdown.js | 8 ++- src/app/tabs/runTab/model/dropdownlogic.js | 73 ++++++++++++---------- 5 files changed, 57 insertions(+), 37 deletions(-) diff --git a/src/app/execution/confirmDialog.js b/src/app/execution/confirmDialog.js index c8921ac3a8..c139cbf6c1 100644 --- a/src/app/execution/confirmDialog.js +++ b/src/app/execution/confirmDialog.js @@ -16,6 +16,7 @@ var css = csjs` } ` +// TODO: self is not actually used and can be removed function confirmDialog (tx, amount, gasEstimation, self, newGasPriceCb, initialParamsCb) { var onGasPriceChange = function () { var gasPrice = el.querySelector('#gasprice').value diff --git a/src/app/files/compiler-metadata.js b/src/app/files/compiler-metadata.js index 3790651c3a..59959aae9e 100644 --- a/src/app/files/compiler-metadata.js +++ b/src/app/files/compiler-metadata.js @@ -74,6 +74,7 @@ class CompilerMetadata { return metadata } + // TODO: is only called by dropdownLogic and can be moved there deployMetadataOf (contractName, callback) { var self = this var provider = self._opts.fileManager.currentFileProvider() diff --git a/src/app/tabs/run-tab.js b/src/app/tabs/run-tab.js index d3b90c34db..e9c4539c51 100644 --- a/src/app/tabs/run-tab.js +++ b/src/app/tabs/run-tab.js @@ -142,7 +142,16 @@ function runTab (opts, localRegistry) { this.event.trigger('clearInstance', []) }) - var dropdownLogic = new DropdownLogic(self) + var dropdownLogic = new DropdownLogic( + this.parentSelf._deps.fileManager, + this.parentSelf._deps.pluginManager, + this.parentSelf._deps.compilersArtefacts, + this.parentSelf._deps.compiler, + this.parentSelf._deps.config, + this.parentSelf._deps.editor, + this.parentSelf._deps.udapp, + this.parentSelf._deps.filePanel + ) var contractDropdownUI = new ContractDropdownUI(dropdownLogic, self) contractDropdownUI.event.register('clearInstance', () => { diff --git a/src/app/tabs/runTab/contractDropdown.js b/src/app/tabs/runTab/contractDropdown.js index 1c612fdc05..588db7c77d 100644 --- a/src/app/tabs/runTab/contractDropdown.js +++ b/src/app/tabs/runTab/contractDropdown.js @@ -142,6 +142,10 @@ class ContractDropdownUI { modalDialogCustom.promptPassphrase(null, 'Personal mode is enabled. Please provide passphrase of account', '', okCb, cancelCb) } + var statusCb = (msg) => { + return this.parentSelf._deps.logCallback(msg) + } + var finalCb = (error, contractObject, address) => { this.event.trigger('clearInstance') @@ -159,7 +163,7 @@ class ContractDropdownUI { { label: 'Force Send', fn: () => { - this.dropdownLogic.forceSend(selectedContract, args, continueCb, promptCb, modalDialogCustom, confirmDialog, finalCb) + this.dropdownLogic.forceSend(selectedContract, args, continueCb, promptCb, modalDialogCustom, confirmDialog, statusCb, finalCb) }}, { label: 'Cancel', fn: () => { @@ -167,7 +171,7 @@ class ContractDropdownUI { } }) } - this.dropdownLogic.forceSend(selectedContract, args, continueCb, promptCb, modalDialogCustom, confirmDialog, finalCb) + this.dropdownLogic.forceSend(selectedContract, args, continueCb, promptCb, modalDialogCustom, confirmDialog, statusCb, finalCb) } loadFromAddress () { diff --git a/src/app/tabs/runTab/model/dropdownlogic.js b/src/app/tabs/runTab/model/dropdownlogic.js index e1e023eb1a..d096777a66 100644 --- a/src/app/tabs/runTab/model/dropdownlogic.js +++ b/src/app/tabs/runTab/model/dropdownlogic.js @@ -9,32 +9,39 @@ var CompilerAbstract = require('../../../compiler/compiler-abstract') var EventManager = remixLib.EventManager class DropdownLogic { - constructor (parentSelf) { - this.parentSelf = parentSelf + constructor (fileManager, pluginManager, compilersArtefacts, compiler, config, editor, udapp, filePanel) { + this.pluginManager = pluginManager + this.compilersArtefacts = compilersArtefacts + this.compiler = compiler + this.config = config + this.editor = editor + this.udapp = udapp + this.filePanel = filePanel + this.event = new EventManager() this.listenToCompilationEvents() - this.parentSelf._deps.fileManager.event.register('currentFileChanged', (currentFile) => { + fileManager.event.register('currentFileChanged', (currentFile) => { this.event.trigger('currentFileChanged', [currentFile]) }) } listenToCompilationEvents () { - this.parentSelf._deps.pluginManager.event.register('sendCompilationResult', (file, source, languageVersion, data) => { + this.pluginManager.event.register('sendCompilationResult', (file, source, languageVersion, data) => { // TODO check whether the tab is configured let compiler = new CompilerAbstract(languageVersion, data) - this.parentSelf._deps.compilersArtefacts[languageVersion] = compiler - this.parentSelf._deps.compilersArtefacts['__last'] = compiler + this.compilersArtefacts[languageVersion] = compiler + this.compilersArtefacts['__last'] = compiler this.event.trigger('newlyCompiled', [true, data, source, compiler, languageVersion]) }) - this.parentSelf._deps.compiler.event.register('compilationFinished', (success, data, source) => { + this.compiler.event.register('compilationFinished', (success, data, source) => { var name = 'solidity' let compiler = new CompilerAbstract(name, data) - this.parentSelf._deps.compilersArtefacts[name] = compiler - this.parentSelf._deps.compilersArtefacts['__last'] = compiler - this.event.trigger('newlyCompiled', [success, data, source, this.parentSelf._deps.compiler, name]) + this.compilersArtefacts[name] = compiler + this.compilersArtefacts['__last'] = compiler + this.event.trigger('newlyCompiled', [success, data, source, this.compiler, name]) }) } @@ -45,11 +52,11 @@ class DropdownLogic { if (/[a-f]/.test(address) && /[A-F]/.test(address) && !ethJSUtil.isValidChecksumAddress(address)) { return cb('Invalid checksum address.') } - if (/.(.abi)$/.exec(this.parentSelf._deps.config.get('currentFile'))) { + if (/.(.abi)$/.exec(this.config.get('currentFile'))) { confirmCb(() => { var abi try { - abi = JSON.parse(this.parentSelf._deps.editor.currentContent()) + abi = JSON.parse(this.editor.currentContent()) } catch (e) { return cb('Failed to parse the current file as JSON ABI.') } @@ -70,7 +77,7 @@ class DropdownLogic { getSelectedContract (contractName, compilerAtributeName) { if (!contractName) return null - var compiler = this.parentSelf._deps.compilersArtefacts[compilerAtributeName] + var compiler = this.compilersArtefacts[compilerAtributeName] if (!compiler) return null var contract = compiler.getContract(contractName) @@ -185,7 +192,7 @@ class DropdownLogic { }) } - this.parentSelf._deps.udapp.createContract(data, confirmationCb, continueCb, promptCb, + this.udapp.createContract(data, confirmationCb, continueCb, promptCb, (error, txResult) => { if (error) { return finalCb(`creation of ${selectedContract.name} errored: ${error}`) @@ -212,7 +219,7 @@ class DropdownLogic { return continueTxExecution(null) } var amount = this.fromWei(tx.value, true, 'ether') - var content = confirmDialog(tx, amount, gasEstimation, this.parentSelf, + var content = confirmDialog(tx, amount, gasEstimation, null, (gasPrice, cb) => { let txFeeText, priceStatus // TODO: this try catch feels like an anti pattern, can/should be @@ -245,7 +252,7 @@ class DropdownLogic { modalDialog('Confirm transaction', content, { label: 'Confirm', fn: () => { - this.parentSelf._deps.config.setUnpersistedProperty('doNotShowTransactionConfirmationAgain', content.querySelector('input#confirmsetting').checked) + this.config.setUnpersistedProperty('doNotShowTransactionConfirmationAgain', content.querySelector('input#confirmsetting').checked) // TODO: check if this is check is still valid given the refactor if (!content.gasPriceStatus) { cancelCb('Given gas price is not correct') @@ -262,34 +269,32 @@ class DropdownLogic { ) } - this.parentSelf._deps.udapp.runTx(data, confirmationCb, promptCb, finalCb) + this.udapp.runTx(data, confirmationCb, promptCb, finalCb) } - forceSend (selectedContract, args, continueCb, promptCb, modalDialog, confirmDialog, cb) { + forceSend (selectedContract, args, continueCb, promptCb, modalDialog, confirmDialog, statusCb, cb) { var constructor = selectedContract.getConstructorInterface() - this.parentSelf._deps.filePanel.compilerMetadata().deployMetadataOf(selectedContract.name, (error, contractMetadata) => { - if (error) return this.parentSelf._deps.logCallback(`creation of ${selectedContract.name} errored: ` + error) + // TODO: deployMetadataOf can be moved here + this.filePanel.compilerMetadata().deployMetadataOf(selectedContract.name, (error, contractMetadata) => { + if (error) return statusCb(`creation of ${selectedContract.name} errored: ` + error) if (!contractMetadata || (contractMetadata && contractMetadata.autoDeployLib)) { - txFormat.buildData(selectedContract.name, selectedContract.object, selectedContract.compiler.getContracts(), true, constructor, args, (error, data) => { - if (error) return this.parentSelf._deps.logCallback(`creation of ${selectedContract.name} errored: ` + error) + return txFormat.buildData(selectedContract.name, selectedContract.object, selectedContract.compiler.getContracts(), true, constructor, args, (error, data) => { + if (error) return statusCb(`creation of ${selectedContract.name} errored: ` + error) - this.parentSelf._deps.logCallback(`creation of ${selectedContract.name} pending...`) + statusCb(`creation of ${selectedContract.name} pending...`) this.createContract(selectedContract, data, continueCb, promptCb, modalDialog, confirmDialog, cb) - }, (msg) => { - this.parentSelf._deps.logCallback(msg) - }, (data, runTxCallback) => { + }, statusCb, (data, runTxCallback) => { // called for libraries deployment this.runTransaction(data, promptCb, modalDialog, confirmDialog, runTxCallback) }) - } else { - if (Object.keys(selectedContract.bytecodeLinkReferences).length) this.parentSelf._deps.logCallback(`linking ${JSON.stringify(selectedContract.bytecodeLinkReferences, null, '\t')} using ${JSON.stringify(contractMetadata.linkReferences, null, '\t')}`) - txFormat.encodeConstructorCallAndLinkLibraries(selectedContract.object, args, constructor, contractMetadata.linkReferences, selectedContract.bytecodeLinkReferences, (error, data) => { - if (error) return this.parentSelf._deps.logCallback(`creation of ${selectedContract.name} errored: ` + error) - - this.parentSelf._deps.logCallback(`creation of ${selectedContract.name} pending...`) - this.createContract(selectedContract, data, continueCb, promptCb, modalDialog, confirmDialog, cb) - }) } + if (Object.keys(selectedContract.bytecodeLinkReferences).length) statusCb(`linking ${JSON.stringify(selectedContract.bytecodeLinkReferences, null, '\t')} using ${JSON.stringify(contractMetadata.linkReferences, null, '\t')}`) + txFormat.encodeConstructorCallAndLinkLibraries(selectedContract.object, args, constructor, contractMetadata.linkReferences, selectedContract.bytecodeLinkReferences, (error, data) => { + if (error) return statusCb(`creation of ${selectedContract.name} errored: ` + error) + + statusCb(`creation of ${selectedContract.name} pending...`) + this.createContract(selectedContract, data, continueCb, promptCb, modalDialog, confirmDialog, cb) + }) }) }