refactor contract dropdown logic

pull/1/head
Iuri Matias 5 years ago
parent 3e47a365b0
commit 29457b71e6
  1. 11
      src/app/tabs/runTab/contractDropdown.js
  2. 139
      src/app/tabs/runTab/model/dropdownlogic.js

@ -109,7 +109,8 @@ class ContractDropdownUI {
const selectedContract = this.getSelectedContract() const selectedContract = this.getSelectedContract()
const clickCallback = (valArray, inputsValues) => { const clickCallback = (valArray, inputsValues) => {
this.createInstance(inputsValues) var selectedContract = this.getSelectedContract()
this.createInstance(selectedContract, inputsValues)
} }
const createConstructorInstance = new MultiParamManager( const createConstructorInstance = new MultiParamManager(
0, 0,
@ -130,9 +131,7 @@ class ContractDropdownUI {
return this.dropdownLogic.getSelectedContract(contractName, compilerAtributeName) return this.dropdownLogic.getSelectedContract(contractName, compilerAtributeName)
} }
createInstance (args) { createInstance (selectedContract, args) {
var selectedContract = this.getSelectedContract()
if (selectedContract.bytecodeObject.length === 0) { if (selectedContract.bytecodeObject.length === 0) {
return modalDialogCustom.alert('This contract may be abstract, not implement an abstract parent\'s methods completely or not invoke an inherited contract\'s constructor correctly.') return modalDialogCustom.alert('This contract may be abstract, not implement an abstract parent\'s methods completely or not invoke an inherited contract\'s constructor correctly.')
} }
@ -184,7 +183,7 @@ class ContractDropdownUI {
{ {
label: 'Force Send', label: 'Force Send',
fn: () => { fn: () => {
this.dropdownLogic.forceSend(selectedContract, args, continueCb, promptCb, modalDialog, confirmDialog, statusCb, finalCb) this.dropdownLogic.deployContract(selectedContract, args, {continueCb, promptCb, statusCb, finalCb}, {modalDialog, confirmDialog})
}}, { }}, {
label: 'Cancel', label: 'Cancel',
fn: () => { fn: () => {
@ -192,7 +191,7 @@ class ContractDropdownUI {
} }
}) })
} }
this.dropdownLogic.forceSend(selectedContract, args, continueCb, promptCb, modalDialog, confirmDialog, statusCb, finalCb) this.dropdownLogic.deployContract(selectedContract, args, {continueCb, promptCb, statusCb, finalCb}, {modalDialog, confirmDialog})
} }
loadFromAddress () { loadFromAddress () {

@ -136,62 +136,7 @@ class DropdownLogic {
data.contractABI = selectedContract.abi data.contractABI = selectedContract.abi
} }
var confirmationCb = (network, tx, gasEstimation, continueTxExecution, cancelCb) => { const confirmationCb = this.getConfirmationCb(modalDialog, confirmDialog)
if (network.name !== 'Main') {
return continueTxExecution(null)
}
var amount = Web3.utils.fromWei(typeConversion.toInt(tx.value), 'ether')
// TODO: there is still a UI dependency to remove here, it's still too coupled at this point to remove easily
var content = confirmDialog(tx, amount, gasEstimation, this.recorder,
(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 = Web3.utils.toBN(tx.gas).mul(Web3.utils.toBN(Web3.utils.toWei(gasPrice.toString(10), 'gwei')))
txFeeText = ' ' + Web3.utils.fromWei(fee.toString(10), 'ether') + ' Ether'
priceStatus = true
} catch (e) {
txFeeText = ' Please fix this issue before sending any transaction. ' + e.message
priceStatus = false
}
cb(txFeeText, priceStatus)
},
(cb) => {
this.executionContext.web3().eth.getGasPrice((error, gasPrice) => {
var 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 = Web3.utils.fromWei(gasPrice.toString(10), 'gwei')
cb(null, gasPriceValue)
} catch (e) {
cb(warnMessage + e.message, null, false)
}
})
}
)
modalDialog('Confirm transaction', content,
{ label: 'Confirm',
fn: () => {
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')
} else {
var gasPrice = Web3.utils.toWei(content.querySelector('#gasprice').value, 'gwei')
continueTxExecution(gasPrice)
}
}}, {
label: 'Cancel',
fn: () => {
return cancelCb('Transaction canceled by user.')
}
})
}
this.udapp.createContract(data, confirmationCb, continueCb, promptCb, this.udapp.createContract(data, confirmationCb, continueCb, promptCb,
(error, txResult) => { (error, txResult) => {
if (error) { if (error) {
@ -213,42 +158,44 @@ class DropdownLogic {
) )
} }
runTransaction (data, continueCb, promptCb, modalDialog, confirmDialog, finalCb) { determineGasFees(gasPrice, cb) {
var confirmationCb = (network, tx, gasEstimation, continueTxExecution, cancelCb) => { 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)
}
determineGasPrice(cb) {
this.getGasPrice((error, gasPrice) => {
var 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')
cb(null, gasPriceValue)
} catch (e) {
cb(warnMessage + e.message, null, false)
}
})
}
getConfirmationCb(modalDialog, confirmDialog) {
const confirmationCb = (network, tx, gasEstimation, continueTxExecution, cancelCb) => {
if (network.name !== 'Main') { if (network.name !== 'Main') {
return continueTxExecution(null) return continueTxExecution(null)
} }
var amount = this.fromWei(tx.value, true, 'ether') const amount = this.fromWei(tx.value, true, 'ether')
var content = confirmDialog(tx, amount, gasEstimation, null, const content = confirmDialog(tx, amount, gasEstimation, null, this.determineGasFees, this.determineGasPrice)
(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)
},
(cb) => {
this.getGasPrice((error, gasPrice) => {
var 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')
cb(null, gasPriceValue)
} catch (e) {
cb(warnMessage + e.message, null, false)
}
})
}
)
modalDialog('Confirm transaction', content, modalDialog('Confirm transaction', content,
{ label: 'Confirm', { label: 'Confirm',
fn: () => { fn: () => {
@ -269,10 +216,18 @@ class DropdownLogic {
) )
} }
return confirmationCb
}
runTransaction (data, continueCb, promptCb, modalDialog, confirmDialog, finalCb) {
const confirmationCb = this.getConfirmationCb(modalDialog, confirmDialog)
this.udapp.runTx(data, confirmationCb, continueCb, promptCb, finalCb) this.udapp.runTx(data, confirmationCb, continueCb, promptCb, finalCb)
} }
async forceSend (selectedContract, args, continueCb, promptCb, modalDialog, confirmDialog, statusCb, cb) { async deploContract (selectedContract, args, callbacks, dialogs) {
const {continueCb, promptCb, statusCb, finalCb} = callbacks
const {modalDialog, confirmDialog} = dialogs
var constructor = selectedContract.getConstructorInterface() var constructor = selectedContract.getConstructorInterface()
// TODO: deployMetadataOf can be moved here // TODO: deployMetadataOf can be moved here
let contractMetadata let contractMetadata
@ -286,7 +241,7 @@ class DropdownLogic {
if (error) return statusCb(`creation of ${selectedContract.name} errored: ` + error) if (error) return statusCb(`creation of ${selectedContract.name} errored: ` + error)
statusCb(`creation of ${selectedContract.name} pending...`) statusCb(`creation of ${selectedContract.name} pending...`)
this.createContract(selectedContract, data, continueCb, promptCb, modalDialog, confirmDialog, cb) this.createContract(selectedContract, data, continueCb, promptCb, modalDialog, confirmDialog, finalCb)
}, statusCb, (data, runTxCallback) => { }, statusCb, (data, runTxCallback) => {
// called for libraries deployment // called for libraries deployment
this.runTransaction(data, continueCb, promptCb, modalDialog, confirmDialog, runTxCallback) this.runTransaction(data, continueCb, promptCb, modalDialog, confirmDialog, runTxCallback)
@ -297,7 +252,7 @@ class DropdownLogic {
if (error) return statusCb(`creation of ${selectedContract.name} errored: ` + error) if (error) return statusCb(`creation of ${selectedContract.name} errored: ` + error)
statusCb(`creation of ${selectedContract.name} pending...`) statusCb(`creation of ${selectedContract.name} pending...`)
this.createContract(selectedContract, data, continueCb, promptCb, modalDialog, confirmDialog, cb) this.createContract(selectedContract, data, continueCb, promptCb, modalDialog, confirmDialog, finalCb)
}) })
} }

Loading…
Cancel
Save