move treeview ui out of the udapp logic

pull/1/head
Iuri Matias 6 years ago committed by yann300
parent 0cf33086bc
commit 6de3a31d7c
  1. 172
      src/universal-dapp-ui.js
  2. 68
      src/universal-dapp.js

@ -3,22 +3,43 @@
var $ = require('jquery') var $ = require('jquery')
var yo = require('yo-yo') var yo = require('yo-yo')
var ethJSUtil = require('ethereumjs-util')
var BN = ethJSUtil.BN
var helper = require('./lib/helper') var helper = require('./lib/helper')
var copyToClipboard = require('./app/ui/copy-to-clipboard') var copyToClipboard = require('./app/ui/copy-to-clipboard')
var css = require('./universal-dapp-styles') var css = require('./universal-dapp-styles')
var MultiParamManager = require('./multiParamManager') var MultiParamManager = require('./multiParamManager')
var remixLib = require('remix-lib') var remixLib = require('remix-lib')
var typeConversion = remixLib.execution.typeConversion var typeConversion = remixLib.execution.typeConversion
var txExecution = remixLib.execution.txExecution
var txFormat = remixLib.execution.txFormat
var executionContext = require('./execution-context') var executionContext = require('./execution-context')
var modalDialog = require('./app/ui/modaldialog') var modalDialog = require('./app/ui/modaldialog')
var confirmDialog = require('./app/execution/confirmDialog') var confirmDialog = require('./app/execution/confirmDialog')
var TreeView = require('./app/ui/TreeView')
function UniversalDAppUI (udapp, opts = {}) { function UniversalDAppUI (udapp, opts = {}) {
this.udapp = udapp this.udapp = udapp
} }
function decodeResponseToTreeView (response, fnabi) {
var treeView = new TreeView({
extractData: (item, parent, key) => {
var ret = {}
if (BN.isBN(item)) {
ret.self = item.toString(10)
ret.children = []
} else {
ret = treeView.extractDataDefault(item, parent, key)
}
return ret
}
})
return treeView.render(txFormat.decodeResponse(response, fnabi))
}
UniversalDAppUI.prototype.renderInstance = function (contract, address, contractName) { UniversalDAppUI.prototype.renderInstance = function (contract, address, contractName) {
var noInstances = document.querySelector('[class^="noInstancesText"]') var noInstances = document.querySelector('[class^="noInstancesText"]')
if (noInstances) { if (noInstances) {
@ -97,67 +118,110 @@ UniversalDAppUI.prototype.getCallButton = function (args) {
var outputOverride = yo`<div class=${css.value}></div>` // show return value var outputOverride = yo`<div class=${css.value}></div>` // show return value
function clickButton (valArr, inputsValues) { function clickButton (valArr, inputsValues) {
self.udapp.call(true, args, inputsValues, lookupOnly, var logMsg
if (!args.funABI.constant) {
logMsg = `transact to ${args.contractName}.${(args.funABI.name) ? args.funABI.name : '(fallback)'}`
} else {
logMsg = `call to ${args.contractName}.${(args.funABI.name) ? args.funABI.name : '(fallback)'}`
}
(network, tx, gasEstimation, continueTxExecution, cancelCb) => { var value = inputsValues
if (network.name !== 'Main') {
return continueTxExecution(null) var confirmationCb = (network, tx, gasEstimation, continueTxExecution, cancelCb) => {
} if (network.name !== 'Main') {
var amount = executionContext.web3().fromWei(typeConversion.toInt(tx.value), 'ether') return continueTxExecution(null)
var content = confirmDialog(tx, amount, gasEstimation, self, }
(gasPrice, cb) => { var amount = executionContext.web3().fromWei(typeConversion.toInt(tx.value), 'ether')
let txFeeText, priceStatus var content = confirmDialog(tx, amount, gasEstimation, self.udapp,
// TODO: this try catch feels like an anti pattern, can/should be (gasPrice, cb) => {
// removed, but for now keeping the original logic 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 = executionContext.web3().toBigNumber(tx.gas).mul(executionContext.web3().toBigNumber(executionContext.web3().toWei(gasPrice.toString(10), 'gwei')))
txFeeText = ' ' + executionContext.web3().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) => {
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 { try {
var fee = executionContext.web3().toBigNumber(tx.gas).mul(executionContext.web3().toBigNumber(executionContext.web3().toWei(gasPrice.toString(10), 'gwei'))) var gasPriceValue = executionContext.web3().fromWei(gasPrice.toString(10), 'gwei')
txFeeText = ' ' + executionContext.web3().fromWei(fee.toString(10), 'ether') + ' Ether' cb(null, gasPriceValue)
priceStatus = true
} catch (e) { } catch (e) {
txFeeText = ' Please fix this issue before sending any transaction. ' + e.message cb(warnMessage + e.message, null, false)
priceStatus = false
} }
cb(txFeeText, priceStatus) })
}, }
(cb) => { )
executionContext.web3().eth.getGasPrice((error, gasPrice) => { modalDialog('Confirm transaction', content,
var warnMessage = ' Please fix this issue before sending any transaction. ' { label: 'Confirm',
if (error) { fn: () => {
return cb('Unable to retrieve the current network gas price.' + warnMessage + error) self.udapp._deps.config.setUnpersistedProperty('doNotShowTransactionConfirmationAgain', content.querySelector('input#confirmsetting').checked)
} // TODO: check if this is check is still valid given the refactor
try { if (!content.gasPriceStatus) {
var gasPriceValue = executionContext.web3().fromWei(gasPrice.toString(10), 'gwei') cancelCb('Given gas price is not correct')
cb(null, gasPriceValue) } else {
} catch (e) { var gasPrice = executionContext.web3().toWei(content.querySelector('#gasprice').value, 'gwei')
cb(warnMessage + e.message, null, false) continueTxExecution(gasPrice)
} }
}) }}, {
} label: 'Cancel',
)
modalDialog('Confirm transaction', content,
{ label: 'Confirm',
fn: () => { fn: () => {
self._deps.config.setUnpersistedProperty('doNotShowTransactionConfirmationAgain', content.querySelector('input#confirmsetting').checked) return cancelCb('Transaction canceled by user.')
// 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 = executionContext.web3().toWei(content.querySelector('#gasprice').value, 'gwei')
continueTxExecution(gasPrice)
}
}}, {
label: 'Cancel',
fn: () => {
return cancelCb('Transaction canceled by user.')
}
})
},
(decoded) => { var outputCb = (decoded) => {
outputOverride.innerHTML = '' outputOverride.innerHTML = ''
outputOverride.appendChild(decoded) outputOverride.appendChild(decoded)
}
// contractsDetails is used to resolve libraries
txFormat.buildData(args.contractName, args.contractAbi, self.udapp.data.contractsDetails, false, args.funABI, args.funABI.type !== 'fallback' ? value : '', (error, data) => {
if (!error) {
if (!args.funABI.constant) {
self.udapp._deps.logCallback(`${logMsg} pending ... `)
} else {
self.udapp._deps.logCallback(`${logMsg}`)
}
if (args.funABI.type === 'fallback') data.dataHex = value
self.udapp.callFunction(args.address, data, args.funABI, confirmationCb, (error, txResult) => {
if (!error) {
var isVM = executionContext.isVM()
if (isVM) {
var vmError = txExecution.checkVMError(txResult)
if (vmError.error) {
self.udapp._deps.logCallback(`${logMsg} errored: ${vmError.message} `)
return
}
}
if (lookupOnly) {
var decoded = decodeResponseToTreeView(executionContext.isVM() ? txResult.result.vm.return : ethJSUtil.toBuffer(txResult.result), args.funABI)
outputCb(decoded)
}
} else {
self.udapp._deps.logCallback(`${logMsg} errored: ${error} `)
}
})
} else {
self.udapp._deps.logCallback(`${logMsg} errored: ${error} `)
} }
) }, (msg) => {
self.udapp._deps.logCallback(msg)
}, (data, runTxCallback) => {
// called for libraries deployment
self.udapp.runTx(data, confirmationCb, runTxCallback)
})
} }
var multiParamManager = new MultiParamManager(lookupOnly, args.funABI, (valArray, inputsValues, domEl) => { var multiParamManager = new MultiParamManager(lookupOnly, args.funABI, (valArray, inputsValues, domEl) => {

@ -9,31 +9,12 @@ var remixLib = require('remix-lib')
var EventManager = require('./lib/events') var EventManager = require('./lib/events')
var crypto = require('crypto') var crypto = require('crypto')
var TxRunner = remixLib.execution.txRunner var TxRunner = remixLib.execution.txRunner
var txExecution = remixLib.execution.txExecution
var txFormat = remixLib.execution.txFormat
var txHelper = remixLib.execution.txHelper var txHelper = remixLib.execution.txHelper
var executionContext = require('./execution-context') var executionContext = require('./execution-context')
var globalRegistry = require('./global/registry') var globalRegistry = require('./global/registry')
var modalCustom = require('./app/ui/modal-dialog-custom') var modalCustom = require('./app/ui/modal-dialog-custom')
var modalDialog = require('./app/ui/modaldialog') var modalDialog = require('./app/ui/modaldialog')
var TreeView = require('./app/ui/TreeView')
function decodeResponseToTreeView (response, fnabi) {
var treeView = new TreeView({
extractData: (item, parent, key) => {
var ret = {}
if (BN.isBN(item)) {
ret.self = item.toString(10)
ret.children = []
} else {
ret = treeView.extractDataDefault(item, parent, key)
}
return ret
}
})
return treeView.render(txFormat.decodeResponse(response, fnabi))
}
function UniversalDApp () { function UniversalDApp () {
this.event = new EventManager() this.event = new EventManager()
@ -202,55 +183,6 @@ UniversalDApp.prototype.pendingTransactionsCount = function () {
return Object.keys(this.txRunner.pendingTxs).length return Object.keys(this.txRunner.pendingTxs).length
} }
UniversalDApp.prototype.call = function (isUserAction, args, value, lookupOnly, confirmationCb, outputCb) {
const self = this
var logMsg
if (isUserAction) {
if (!args.funABI.constant) {
logMsg = `transact to ${args.contractName}.${(args.funABI.name) ? args.funABI.name : '(fallback)'}`
} else {
logMsg = `call to ${args.contractName}.${(args.funABI.name) ? args.funABI.name : '(fallback)'}`
}
}
txFormat.buildData(args.contractName, args.contractAbi, self._deps.compilersartefacts['__last'].getData().contracts, false, args.funABI, args.funABI.type !== 'fallback' ? value : '', (error, data) => {
if (!error) {
if (isUserAction) {
if (!args.funABI.constant) {
self._deps.logCallback(`${logMsg} pending ... `)
} else {
self._deps.logCallback(`${logMsg}`)
}
}
if (args.funABI.type === 'fallback') data.dataHex = value
self.callFunction(args.address, data, args.funABI, confirmationCb, (error, txResult) => {
if (!error) {
var isVM = executionContext.isVM()
if (isVM) {
var vmError = txExecution.checkVMError(txResult)
if (vmError.error) {
self._deps.logCallback(`${logMsg} errored: ${vmError.message} `)
return
}
}
if (lookupOnly) {
var decoded = decodeResponseToTreeView(executionContext.isVM() ? txResult.result.vm.return : ethJSUtil.toBuffer(txResult.result), args.funABI)
outputCb(decoded)
}
} else {
self._deps.logCallback(`${logMsg} errored: ${error} `)
}
})
} else {
self._deps.logCallback(`${logMsg} errored: ${error} `)
}
}, (msg) => {
self._deps.logCallback(msg)
}, (data, runTxCallback) => {
// called for libraries deployment
self.runTx(data, confirmationCb, runTxCallback)
})
}
/** /**
* deploy the given contract * deploy the given contract
* *

Loading…
Cancel
Save