From 38b8de713878c952d1241de11c51f658bc9dcb42 Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 8 Aug 2017 11:06:22 +0200 Subject: [PATCH] remove unneeded code --- src/app/renderer.js | 323 +----------------- src/universal-dapp.js | 776 +++++++++--------------------------------- 2 files changed, 175 insertions(+), 924 deletions(-) diff --git a/src/app/renderer.js b/src/app/renderer.js index db39e6ae5e..8a4ca99e7d 100644 --- a/src/app/renderer.js +++ b/src/app/renderer.js @@ -3,73 +3,18 @@ var $ = require('jquery') var utils = require('./utils') -var helper = require('../lib/helper.js') -// -------------- styling ---------------------- -var csjs = require('csjs-inject') -var styleGuide = require('./style-guide') -var styles = styleGuide() - -var css = csjs` - .col2 { - width: 70%; - float: left; - } - .col1 extends ${styles.titleL} { - width: 30%; - float: left; - } - .toggleText { - text-decoration: underline; - margin-left: 2px; - font-size: .9em; - } - .toggle { - font-size: 1.1em; - color: ${styles.colors.blue}; - margin: 1em; - cursor: pointer; - font-weight: 400; - display: flex; - align-items: center; - } - .toggle:hover { - opacity: .8; - } -` -// ---------------------------------------------- - -function Renderer (appAPI, compilerEvent) { +/** + * After refactor, the renderer is only used to render error/warning + * TODO: This don't need to be an object anymore. Simplify and just export the renderError function. + * + */ +function Renderer (appAPI) { this.appAPI = appAPI - var self = this - compilerEvent.register('compilationFinished', this, function (success, data, source) { - $('#output').empty() - if (success) { - self.contracts(data, source) - $('#header #menu .envView').css('color', '') - } else { - // envView is the `Contract` tab, as a refactor the entire envView should have his own module - $('#header #menu .envView').css('color', '#FF8B8B') - } - - // NOTE: still need to display as there might be warnings - if (data['error']) { - self.error(data['error']) - } - if (data['errors']) { - data['errors'].forEach(function (err) { - self.error(err) - }) - } - }) - setInterval(() => { updateAccountBalances(self, appAPI) }, 1000) -} - -Renderer.prototype.clear = function () { - $('#output').empty() } Renderer.prototype.error = function (message, container, options) { + if (container === undefined) return var self = this var opt = options || {} if (!opt.type) { @@ -81,10 +26,7 @@ Renderer.prototype.error = function (message, container, options) { } else { $pre = $(opt.useSpan ? '' : '
').text(message)
   }
-  var $error = $('
').prepend($pre) - if (container === undefined) { - container = $('#output') - } + var $error = $('
').prepend($pre) container.append($error) var err = message.match(/^([^:]*):([0-9]*):(([0-9]*):)? /) if (err) { @@ -110,253 +52,4 @@ Renderer.prototype.error = function (message, container, options) { }) } -Renderer.prototype.contracts = function (data, source) { - var self = this - var retrieveMetadataHash = function (bytecode) { - var match = /a165627a7a72305820([0-9a-f]{64})0029$/.exec(bytecode) - if (match) { - return match[1] - } - } - - var udappContracts = [] - for (var contractName in data.contracts) { - var contract = data.contracts[contractName] - udappContracts.push({ - name: contractName, - interface: contract['interface'], - bytecode: contract.bytecode, - metadata: contract.metadata, - metadataHash: contract.bytecode && retrieveMetadataHash(contract.bytecode) - }) - } - - var tableRowItems = function (first, second, cls) { - second.get(0).classList.add(styles.textBoxL) // replace
 styling with textBoxL
-    return $('
') - .addClass(cls) - .append($(`
`).append(first)) - .append($(`
`).append(second)) - } - - var tableRow = function (description, data) { - return tableRowItems( - $('').text(description), - $(``).val(data)) - } - - var preRow = function (description, data) { - return tableRowItems( - $('').text(description), - $('
').text(data)
-    )
-  }
-
-  var formatAssemblyText = function (asm, prefix, source) {
-    if (typeof asm === typeof '' || asm === null || asm === undefined) {
-      return prefix + asm + '\n'
-    }
-    var text = prefix + '.code\n'
-    $.each(asm['.code'], function (i, item) {
-      var v = item.value === undefined ? '' : item.value
-      var src = ''
-      if (item.begin !== undefined && item.end !== undefined) {
-        src = source.slice(item.begin, item.end).replace('\n', '\\n', 'g')
-      }
-      if (src.length > 30) {
-        src = src.slice(0, 30) + '...'
-      }
-      if (item.name !== 'tag') {
-        text += '  '
-      }
-      text += prefix + item.name + ' ' + v + '\t\t\t' + src + '\n'
-    })
-    text += prefix + '.data\n'
-    if (asm['.data']) {
-      $.each(asm['.data'], function (i, item) {
-        text += '  ' + prefix + '' + i + ':\n'
-        text += formatAssemblyText(item, prefix + '    ', source)
-      })
-    }
-    return text
-  }
-
-  var getConstructorInterface = function (abi) {
-    var funABI = { 'name': '', 'inputs': [], 'type': 'constructor', 'outputs': [] }
-    for (var i = 0; i < abi.length; i++) {
-      if (abi[i].type === 'constructor') {
-        funABI.inputs = abi[i].inputs || []
-        break
-      }
-    }
-    return funABI
-  }
-
-  var gethDeploy = function (contractName, jsonInterface, bytecode) {
-    var code = ''
-    var funABI = getConstructorInterface(JSON.parse(jsonInterface))
-
-    funABI.inputs.forEach(function (inp) {
-      code += 'var ' + inp.name + ' = /* var of type ' + inp.type + ' here */ ;\n'
-    })
-
-    contractName = contractName.replace(/[:./]/g, '_')
-    code += 'var ' + contractName + 'Contract = web3.eth.contract(' + jsonInterface.replace('\n', '') + ');' +
-      '\nvar ' + contractName + ' = ' + contractName + 'Contract.new('
-
-    funABI.inputs.forEach(function (inp) {
-      code += '\n   ' + inp.name + ','
-    })
-
-    code += '\n   {' +
-      '\n     from: web3.eth.accounts[0], ' +
-      "\n     data: '0x" + bytecode + "', " +
-      "\n     gas: '" + self.appAPI.currentblockGasLimit() + "'" +
-      '\n   }, function (e, contract){' +
-      '\n    console.log(e, contract);' +
-      "\n    if (typeof contract.address !== 'undefined') {" +
-      "\n         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);" +
-      '\n    }' +
-      '\n })'
-
-    return code
-  }
-
-  var formatGasEstimates = function (data) {
-    // FIXME: the whole gasEstimates object should be nil instead
-    if (data.creation === undefined && data.external === undefined && data.internal === undefined) {
-      return
-    }
-
-    var gasToText = function (g) {
-      return g === null ? 'unknown' : g
-    }
-
-    var text = ''
-    var fun
-    if ('creation' in data) {
-      text += 'Creation: ' + gasToText(data.creation[0]) + ' + ' + gasToText(data.creation[1]) + '\n'
-    }
-
-    if ('external' in data) {
-      text += 'External:\n'
-      for (fun in data.external) {
-        text += '  ' + fun + ': ' + gasToText(data.external[fun]) + '\n'
-      }
-    }
-
-    if ('internal' in data) {
-      text += 'Internal:\n'
-      for (fun in data.internal) {
-        text += '  ' + fun + ': ' + gasToText(data.internal[fun]) + '\n'
-      }
-    }
-    return text
-  }
-
-  var detailsOpen = {}
-  var getDetails = function (contract, source, contractName) {
-    var button = $(`
Contract details (bytecode, interface etc.)
`) - var details = $('
') - - if (contract.bytecode) { - details.append(preRow('Bytecode', contract.bytecode)) - } - - details.append(preRow('Interface', contract['interface'])) - - if (contract.bytecode) { - details.append(preRow('Web3 deploy', gethDeploy(contractName.toLowerCase(), contract['interface'], contract.bytecode), 'deploy')) - - // check if there's a metadata hash appended - var metadataHash = retrieveMetadataHash(contract.bytecode) - if (metadataHash) { - details.append(preRow('Metadata location', 'bzzr://' + metadataHash)) - } - } - - if (contract.metadata) { - details.append(preRow('Metadata', contract.metadata)) - } - - var funHashes = '' - for (var fun in contract.functionHashes) { - funHashes += contract.functionHashes[fun] + ' ' + fun + '\n' - } - if (funHashes.length > 0) { - details.append(preRow('Functions', funHashes)) - } - - var gasEstimates = formatGasEstimates(contract.gasEstimates) - if (gasEstimates) { - details.append(preRow('Gas Estimates', gasEstimates)) - } - - if (contract.runtimeBytecode && contract.runtimeBytecode.length > 0) { - details.append(tableRow('Runtime Bytecode', contract.runtimeBytecode)) - } - - if (contract.opcodes !== undefined && contract.opcodes !== '') { - details.append(tableRow('Opcodes', contract.opcodes)) - } - - if (contract.assembly !== null) { - details.append(preRow('Assembly', formatAssemblyText(contract.assembly, '', source))) - } - - button.click(function () { - detailsOpen[contractName] = !detailsOpen[contractName] - details.toggle() - }) - if (detailsOpen[contractName]) { - details.show() - } - return $('
').append(button).append(details) - } - - var renderOutputModifier = function (contractName, $contractOutput) { - var contract = data.contracts[contractName] - var ctrSource = self.appAPI.currentCompiledSourceCode() - if (ctrSource) { - $contractOutput.append(getDetails(contract, ctrSource, contractName)) - } - return $contractOutput - } - - this.appAPI.resetDapp(udappContracts, renderOutputModifier) - - var $contractOutput = this.appAPI.renderDapp() - - var $txOrigin = $('#txorigin') - - this.appAPI.getAccounts(function (err, accounts) { - if (err) { - self.error(err.message) - } - if (accounts && accounts[0]) { - $txOrigin.empty() - for (var a in accounts) { $txOrigin.append($('