From a3946e67bfb8ba4e2d0fe9082581c7ba3586594b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 26 Dec 2019 10:56:39 -0500 Subject: [PATCH] move common fromWei/toWei/determineGasFee methods into blockchain module --- src/app/tabs/runTab/contractDropdown.js | 6 ++-- src/app/tabs/runTab/model/blockchain.js | 27 ++++++++++++++++ src/app/tabs/runTab/model/dropdownlogic.js | 36 ---------------------- src/app/tabs/runTab/model/recorder.js | 36 ---------------------- src/app/tabs/runTab/recorder.js | 6 ++-- 5 files changed, 33 insertions(+), 78 deletions(-) diff --git a/src/app/tabs/runTab/contractDropdown.js b/src/app/tabs/runTab/contractDropdown.js index fa727bbaed..0d5ae4fe3b 100644 --- a/src/app/tabs/runTab/contractDropdown.js +++ b/src/app/tabs/runTab/contractDropdown.js @@ -209,8 +209,8 @@ class ContractDropdownUI { if (network.name !== 'Main') { return continueTxExecution(null) } - const amount = this.dropdownLogic.fromWei(tx.value, true, 'ether') - const content = confirmDialog(tx, amount, gasEstimation, null, this.dropdownLogic.determineGasFees(tx), this.blockchain.determineGasPrice) + const amount = this.blockchain.fromWei(tx.value, true, 'ether') + const content = confirmDialog(tx, amount, gasEstimation, null, this.blockchain.determineGasFees(tx), this.blockchain.determineGasPrice) modalDialog('Confirm transaction', content, { label: 'Confirm', @@ -220,7 +220,7 @@ class ContractDropdownUI { if (!content.gasPriceStatus) { cancelCb('Given gas price is not correct') } else { - var gasPrice = this.dropdownLogic.toWei(content.querySelector('#gasprice').value, 'gwei') + var gasPrice = this.blockchain.toWei(content.querySelector('#gasprice').value, 'gwei') continueTxExecution(gasPrice) } }}, { diff --git a/src/app/tabs/runTab/model/blockchain.js b/src/app/tabs/runTab/model/blockchain.js index ddbb72a661..f651016b68 100644 --- a/src/app/tabs/runTab/model/blockchain.js +++ b/src/app/tabs/runTab/model/blockchain.js @@ -93,6 +93,33 @@ class Blockchain { return Web3.utils.fromWei(value.toString(10), unit || 'ether') } + toWei (value, unit) { + return Web3.utils.toWei(value, unit || 'gwei') + } + + calculateFee (gas, gasPrice, unit) { + return Web3.utils.toBN(gas).mul(Web3.utils.toBN(Web3.utils.toWei(gasPrice.toString(10), unit || 'gwei'))) + } + + determineGasFees (tx) { + const determineGasFeesCb = (gasPrice, cb) => { + let txFeeText, priceStatus + // 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) + txFeeText = ' ' + this.fromWei(fee, false, 'ether') + ' Ether' + priceStatus = true + } catch (e) { + txFeeText = ' Please fix this issue before sending any transaction. ' + e.message + priceStatus = false + } + cb(txFeeText, priceStatus) + } + + return determineGasFeesCb + } + } module.exports = Blockchain diff --git a/src/app/tabs/runTab/model/dropdownlogic.js b/src/app/tabs/runTab/model/dropdownlogic.js index 573245d223..78497907fa 100644 --- a/src/app/tabs/runTab/model/dropdownlogic.js +++ b/src/app/tabs/runTab/model/dropdownlogic.js @@ -1,10 +1,8 @@ var ethJSUtil = require('ethereumjs-util') var remixLib = require('remix-lib') var txHelper = remixLib.execution.txHelper -var typeConversion = remixLib.execution.typeConversion var CompilerAbstract = require('../../../compiler/compiler-abstract') var EventManager = remixLib.EventManager -var Web3 = require('web3') class DropdownLogic { constructor (compilersArtefacts, config, editor, runView) { @@ -96,40 +94,6 @@ class DropdownLogic { } } - fromWei (value, doTypeConversion, unit) { - if (doTypeConversion) { - return Web3.utils.fromWei(typeConversion.toInt(value), unit || 'ether') - } - return Web3.utils.fromWei(value.toString(10), unit || 'ether') - } - - toWei (value, unit) { - return Web3.utils.toWei(value, unit || 'gwei') - } - - calculateFee (gas, gasPrice, unit) { - return Web3.utils.toBN(gas).mul(Web3.utils.toBN(Web3.utils.toWei(gasPrice.toString(10), unit || 'gwei'))) - } - - determineGasFees (tx) { - const determineGasFeesCb = (gasPrice, cb) => { - let txFeeText, priceStatus - // 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) - txFeeText = ' ' + this.fromWei(fee, false, 'ether') + ' Ether' - priceStatus = true - } catch (e) { - txFeeText = ' Please fix this issue before sending any transaction. ' + e.message - priceStatus = false - } - cb(txFeeText, priceStatus) - } - - return determineGasFeesCb - } - getCompilerContracts () { return this.compilersArtefacts['__last'].getData().contracts } diff --git a/src/app/tabs/runTab/model/recorder.js b/src/app/tabs/runTab/model/recorder.js index 73c15a15a7..7e3c48028b 100644 --- a/src/app/tabs/runTab/model/recorder.js +++ b/src/app/tabs/runTab/model/recorder.js @@ -4,9 +4,7 @@ var remixLib = require('remix-lib') var EventManager = remixLib.EventManager var format = remixLib.execution.txFormat var txHelper = remixLib.execution.txHelper -var typeConversion = remixLib.execution.typeConversion var helper = require('../../../../lib/helper.js') -var Web3 = require('web3') /** * Record transaction as long as the user create them. @@ -288,40 +286,6 @@ class Recorder { return address } - fromWei (value, doTypeConversion, unit) { - if (doTypeConversion) { - return Web3.utils.fromWei(typeConversion.toInt(value), unit || 'ether') - } - return Web3.utils.fromWei(value.toString(10), unit || 'ether') - } - - toWei (value, unit) { - return Web3.utils.toWei(value, unit || 'gwei') - } - - calculateFee (gas, gasPrice, unit) { - return Web3.utils.toBN(gas).mul(Web3.utils.toBN(Web3.utils.toWei(gasPrice.toString(10), unit || 'gwei'))) - } - - determineGasFees (tx) { - const determineGasFeesCb = (gasPrice, cb) => { - let txFeeText, priceStatus - // 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) - txFeeText = ' ' + this.fromWei(fee, false, 'ether') + ' Ether' - priceStatus = true - } catch (e) { - txFeeText = ' Please fix this issue before sending any transaction. ' + e.message - priceStatus = false - } - cb(txFeeText, priceStatus) - } - - return determineGasFeesCb - } - runScenario (continueCb, promptCb, alertCb, confirmationCb, logCallBack, cb) { var currentFile = this.config.get('currentFile') this.fileManager.fileProviderOf(currentFile).get(currentFile, (error, json) => { diff --git a/src/app/tabs/runTab/recorder.js b/src/app/tabs/runTab/recorder.js index cbef6f1d71..5c7231b6cc 100644 --- a/src/app/tabs/runTab/recorder.js +++ b/src/app/tabs/runTab/recorder.js @@ -81,8 +81,8 @@ class RecorderUI { if (network.name !== 'Main') { return continueTxExecution(null) } - const amount = this.recorder.fromWei(tx.value, true, 'ether') - const content = confirmDialog(tx, amount, gasEstimation, null, this.recorder.determineGasFees(tx), this.blockchain.determineGasPrice) + const amount = this.blockchain.fromWei(tx.value, true, 'ether') + const content = confirmDialog(tx, amount, gasEstimation, null, this.blockchain.determineGasFees(tx), this.blockchain.determineGasPrice) modalDialog('Confirm transaction', content, { label: 'Confirm', @@ -92,7 +92,7 @@ class RecorderUI { if (!content.gasPriceStatus) { cancelCb('Given gas price is not correct') } else { - var gasPrice = this.recorder.toWei(content.querySelector('#gasprice').value, 'gwei') + var gasPrice = this.blockchain.toWei(content.querySelector('#gasprice').value, 'gwei') continueTxExecution(gasPrice) } }}, {