diff --git a/src/app.js b/src/app.js index 76629e0e0e..e0991efe5f 100644 --- a/src/app.js +++ b/src/app.js @@ -40,8 +40,9 @@ var run = function () { loadFiles(files); }; - if (filesToLoad !== null) + if (filesToLoad !== null) { loadFiles(filesToLoad); + } // ------------------ query params (hash) ---------------- @@ -133,8 +134,9 @@ var run = function () { 'To which other browser-solidity instance do you want to copy over all files?', 'https://ethereum.github.io/browser-solidity/' ); - if (target === null) + if (target === null) { return; + } var files = editor.packageFiles(); var iframe = $('', {src: target, style: 'display:none;', load: function () { this.contentWindow.postMessage(['loadFiles', files], '*'); diff --git a/src/app/compiler.js b/src/app/compiler.js index cf0abfcf2c..4272c2fb48 100644 --- a/src/app/compiler.js +++ b/src/app/compiler.js @@ -24,10 +24,13 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles editor.setCacheFileContent(''); return; } - if (input === previousInput) + if (input === previousInput) { return; + } previousInput = input; - if (compileTimeout) window.clearTimeout(compileTimeout); + if (compileTimeout) { + window.clearTimeout(compileTimeout); + } compileTimeout = window.setTimeout(compile, 300); } @@ -112,24 +115,30 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles if (data['error'] !== undefined) { renderer.error(data['error']); - if (utils.errortype(data['error']) !== 'warning') noFatalErrors = false; + if (utils.errortype(data['error']) !== 'warning') { + noFatalErrors = false; + } } if (data['errors'] !== undefined) { data['errors'].forEach(function (err) { renderer.error(err); - if (utils.errortype(err) !== 'warning') noFatalErrors = false; + if (utils.errortype(err) !== 'warning') { + noFatalErrors = false; + } }); } - if (missingInputs !== undefined && missingInputs.length > 0) + if (missingInputs !== undefined && missingInputs.length > 0) { compile(missingInputs); - else if (noFatalErrors && !hidingRHP()) + } else if (noFatalErrors && !hidingRHP()) { renderer.contracts(data, editor.getValue()); + } } this.initializeWorker = function (version, setVersionText) { - if (worker !== null) + if (worker !== null) { worker.terminate(); + } worker = new Worker('worker.js'); worker.addEventListener('message', function (msg) { var data = msg.data; @@ -154,8 +163,7 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles function gatherImports (files, importHints, cb) { importHints = importHints || []; - if (!compilerAcceptsMultipleFiles) - { + if (!compilerAcceptsMultipleFiles) { cb(files[editor.getCacheFile()]); return; } @@ -166,12 +174,15 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles reloop = false; for (var fileName in files) { var match; - while (match = importRegex.exec(files[fileName])) + while (match = importRegex.exec(files[fileName])) { importHints.push(match[1]); + } } while (importHints.length > 0) { var m = importHints.pop(); - if (m in files) continue; + if (m in files) { + continue; + } if (editor.hasFile(m)) { files[m] = window.localStorage[utils.fileKey(m)]; reloop = true; @@ -183,15 +194,14 @@ function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles reloop = true; } else if (githubMatch = /^(https?:\/\/)?(www.)?github.com\/([^\/]*\/[^\/]*)\/(.*)/.exec(m)) { handleGithubCall(githubMatch[3], githubMatch[4], function (result) { - if ('content' in result) - { + if ('content' in result) { var content = Base64.decode(result.content); cachedRemoteFiles[m] = content; files[m] = content; gatherImports(files, importHints, cb); - } - else + } else { cb(null, 'Unable to import "' + m + '"'); + } }).fail(function () { cb(null, 'Unable to import "' + m + '"'); }); diff --git a/src/app/editor.js b/src/app/editor.js index fb02c1784d..57808c2443 100644 --- a/src/app/editor.js +++ b/src/app/editor.js @@ -7,8 +7,9 @@ function Editor (loadingFromGist) { this.newFile = function () { untitledCount = ''; - while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) + while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) { untitledCount = (untitledCount - 0) + 1; + } SOL_CACHE_FILE = SOL_CACHE_UNTITLED + untitledCount; sessions[SOL_CACHE_FILE] = null; this.setCacheFileContent(''); @@ -120,8 +121,9 @@ function Editor (loadingFromGist) { if (!files.length || window.localStorage['sol-cache']) { if (loadingFromGist) return; // Backwards-compatibility - while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) + while (window.localStorage[SOL_CACHE_UNTITLED + untitledCount]) { untitledCount = (untitledCount - 0) + 1; + } SOL_CACHE_FILE = SOL_CACHE_UNTITLED + untitledCount; window.localStorage[SOL_CACHE_FILE] = window.localStorage['sol-cache'] || BALLOT_EXAMPLE; window.localStorage.removeItem('sol-cache'); diff --git a/src/app/gist-handler.js b/src/app/gist-handler.js index 048c8f726d..fe21092e73 100644 --- a/src/app/gist-handler.js +++ b/src/app/gist-handler.js @@ -15,7 +15,9 @@ function handleLoad (cb) { gistId = params['gist']; loadingFromGist = !!gistId; } - if (loadingFromGist) cb(gistId); + if (loadingFromGist) { + cb(gistId); + } } return loadingFromGist; } diff --git a/src/app/query-params.js b/src/app/query-params.js index c39021d034..025b45ee98 100644 --- a/src/app/query-params.js +++ b/src/app/query-params.js @@ -11,7 +11,9 @@ function getQueryParams () { var parts = qs.split('&'); for (var x in parts) { var keyValue = parts[x].split('='); - if (keyValue[0] !== '') params[keyValue[0]] = keyValue[1]; + if (keyValue[0] !== '') { + params[keyValue[0]] = keyValue[1]; + } } return params; } diff --git a/src/app/renderer.js b/src/app/renderer.js index be2a356604..3d061207df 100644 --- a/src/app/renderer.js +++ b/src/app/renderer.js @@ -25,18 +25,20 @@ function Renderer (editor, compiler, updateFiles) { function setProviderFromEndpoint () { var endpoint = $web3endpoint.val(); - if (endpoint === 'ipc') + if (endpoint === 'ipc') { web3.setProvider(new web3.providers.IpcProvider()); - else + } else { web3.setProvider(new web3.providers.HttpProvider(endpoint)); + } } var $vmToggle = $('#vm'); var $web3Toggle = $('#web3'); var $web3endpoint = $('#web3Endpoint'); - if (web3.providers && web3.currentProvider instanceof web3.providers.IpcProvider) + if (web3.providers && web3.currentProvider instanceof web3.providers.IpcProvider) { $web3endpoint.val('ipc'); + } $vmToggle.get(0).checked = true; @@ -44,7 +46,9 @@ function Renderer (editor, compiler, updateFiles) { $web3Toggle.on('change', executionContextChange); $web3endpoint.on('change', function () { setProviderFromEndpoint(); - if (executionContext === 'web3') compiler.compile(); + if (executionContext === 'web3') { + compiler.compile(); + } }); })(); @@ -126,13 +130,16 @@ function Renderer (editor, compiler, updateFiles) { var $txOrigin = $('#txorigin'); function renderAccounts (err, accounts) { - if (err) + if (err) { renderError(err.message); + } if (accounts && accounts[0]) { $txOrigin.empty(); for (var a in accounts) { $txOrigin.append($('').val(accounts[a]).text(accounts[a])); } $txOrigin.val(accounts[0]); - } else $txOrigin.val('unknown'); + } else { + $txOrigin.val('unknown'); + } } dapp.getAccounts(renderAccounts); @@ -173,17 +180,18 @@ function Renderer (editor, compiler, updateFiles) { details.append($('
').text(funHashes)); details.append($('Gas Estimates')); details.append($('').text(formatGasEstimates(contract.gasEstimates))); - if (contract.runtimeBytecode && contract.runtimeBytecode.length > 0) + if (contract.runtimeBytecode && contract.runtimeBytecode.length > 0) { details.append(tableRow('Runtime Bytecode', contract.runtimeBytecode)); - if (contract.assembly !== null) - { + } + if (contract.assembly !== null) { details.append($('Assembly')); var assembly = $('').text(formatAssemblyText(contract.assembly, '', source)); details.append(assembly); } button.click(function () { detailsOpen[contractName] = !detailsOpen[contractName]; details.toggle(); }); - if (detailsOpen[contractName]) + if (detailsOpen[contractName]) { details.show(); + } return $('').append(button).append(details); }; @@ -191,38 +199,46 @@ function Renderer (editor, compiler, updateFiles) { var gasToText = function (g) { return g === null ? 'unknown' : g; } var text = ''; var fun; - if ('creation' in data) + if ('creation' in data) { text += 'Creation: ' + gasToText(data.creation[0]) + ' + ' + gasToText(data.creation[1]) + '\n'; + } text += 'External:\n'; - for (fun in data.external) + for (fun in data.external) { text += ' ' + fun + ': ' + gasToText(data.external[fun]) + '\n'; + } text += 'Internal:\n'; - for (fun in data.internal) + for (fun in data.internal) { text += ' ' + fun + ': ' + gasToText(data.internal[fun]) + '\n'; + } return text; }; var formatAssemblyText = function (asm, prefix, source) { - if (typeof asm === typeof '' || asm === null || asm === undefined) + 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) + if (item.begin !== undefined && item.end !== undefined) { src = source.slice(item.begin, item.end).replace('\n', '\\n', 'g'); - if (src.length > 30) + } + if (src.length > 30) { src = src.slice(0, 30) + '...'; - if (item.name !== 'tag') + } + if (item.name !== 'tag') { text += ' '; + } text += prefix + item.name + ' ' + v + '\t\t\t' + src + '\n'; }); text += prefix + '.data\n'; - if (asm['.data']) + if (asm['.data']) { $.each(asm['.data'], function (i, item) { text += ' ' + prefix + '' + i + ':\n'; text += formatAssemblyText(item, prefix + ' ', source); }); + } return text; }; @@ -259,11 +275,12 @@ function Renderer (editor, compiler, updateFiles) { function getConstructorInterface (abi) { var funABI = { 'name': '', 'inputs': [], 'type': 'constructor', 'outputs': [] }; - for (var i = 0; i < abi.length; i++) + for (var i = 0; i < abi.length; i++) { if (abi[i].type === 'constructor') { funABI.inputs = abi[i].inputs || []; break; } + } return funABI; } diff --git a/src/app/storage-handler.js b/src/app/storage-handler.js index c9a75e2a57..1c7caeff7f 100644 --- a/src/app/storage-handler.js +++ b/src/app/storage-handler.js @@ -4,7 +4,9 @@ function StorageHandler (updateFiles) { this.sync = function () { - if (typeof chrome === 'undefined' || !chrome || !chrome.storage || !chrome.storage.sync) return; + if (typeof chrome === 'undefined' || !chrome || !chrome.storage || !chrome.storage.sync) { + return; + } var obj = {}; var done = false; @@ -23,16 +25,20 @@ function StorageHandler (updateFiles) { obj[key] = localStorage[key]; } done++; - if (done >= count) chrome.storage.sync.set(obj, function () { - console.log('updated cloud files with: ', obj, this, arguments); - }) + if (done >= count) { + chrome.storage.sync.set(obj, function () { + console.log('updated cloud files with: ', obj, this, arguments); + }) + } }) } for (var y in window.localStorage) { console.log('checking', y); obj[y] = window.localStorage.getItem(y); - if (y.indexOf(utils.getCacheFilePrefix()) !== 0) continue; + if (y.indexOf(utils.getCacheFilePrefix()) !== 0) { + continue; + } count++; check(y); } diff --git a/src/universal-dapp.js b/src/universal-dapp.js index ac4bbefba7..f3104aa9b9 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -50,7 +50,9 @@ UniversalDApp.prototype.getAccounts = function (cb) { if (!this.vm) { this.web3.eth.getAccounts(cb); } else { - if (!this.accounts) return cb('No accounts?'); + if (!this.accounts) { + return cb('No accounts?'); + } cb(null, Object.keys(this.accounts)); } @@ -68,7 +70,9 @@ UniversalDApp.prototype.getBalance = function (address, cb) { } }); } else { - if (!this.accounts) return cb('No accounts?'); + if (!this.accounts) { + return cb('No accounts?'); + } this.vm.stateManager.getAccountBalance(new Buffer(address, 'hex'), function (err, res) { if (err) { @@ -113,9 +117,11 @@ UniversalDApp.prototype.render = function () { }; UniversalDApp.prototype.getContractByName = function (contractName) { - for (var c in this.contracts) - if (this.contracts[c].name === contractName) + for (var c in this.contracts) { + if (this.contracts[c].name === contractName) { return this.contracts[c]; + } + } return null; }; @@ -162,11 +168,17 @@ UniversalDApp.prototype.getCreateInterface = function ($container, contract) { UniversalDApp.prototype.getInstanceInterface = function (contract, address, $target) { var self = this; var abi = JSON.parse(contract.interface).sort(function (a, b) { - if (a.name > b.name) return -1; - else return 1; + if (a.name > b.name) { + return -1; + } else { + return 1; + } }).sort(function (a, b) { - if (a.constant === true) return -1; - else return 1; + if (a.constant === true) { + return -1; + } else { + return 1; + } }); var web3contract = this.web3.eth.contract(abi); var funABI = this.getConstructorInterface(abi); @@ -189,8 +201,9 @@ UniversalDApp.prototype.getInstanceInterface = function (contract, address, $tar var $events = $(''); var parseLogs = function (err, response) { - if (err) + if (err) { return; + } var $event = $(''); @@ -209,7 +222,9 @@ UniversalDApp.prototype.getInstanceInterface = function (contract, address, $tar var eventABI = {}; $.each(abi, function (i, funABI) { - if (funABI.type !== 'event') return; + if (funABI.type !== 'event') { + return; + } var hash = ethJSABI.eventID(funABI.name, funABI.inputs.map(function (item) { return item.type; })); eventABI[hash.toString('hex')] = { event: funABI.name, inputs: funABI.inputs }; @@ -252,7 +267,9 @@ UniversalDApp.prototype.getInstanceInterface = function (contract, address, $tar })); $.each(abi, function (i, funABI) { - if (funABI.type !== 'function') return; + if (funABI.type !== 'function') { + return; + } // @todo getData cannot be used with overloaded functions $instance.append(self.getCallButton({ abi: funABI, @@ -286,11 +303,12 @@ UniversalDApp.prototype.getInstanceInterface = function (contract, address, $tar UniversalDApp.prototype.getConstructorInterface = function (abi) { var funABI = { 'name': '', 'inputs': [], 'type': 'constructor', 'outputs': [] }; - for (var i = 0; i < abi.length; i++) + for (var i = 0; i < abi.length; i++) { if (abi[i].type === 'constructor') { funABI.inputs = abi[i].inputs || []; break; } + } return funABI; }; @@ -303,7 +321,9 @@ UniversalDApp.prototype.getCallButton = function (args) { var inputs = ''; $.each(args.abi.inputs, function (i, inp) { - if (inputs !== '') inputs += ', '; + if (inputs !== '') { + inputs += ', '; + } inputs += inp.type + ' ' + inp.name; }); var inputField = $('').attr('placeholder', inputs).attr('title', inputs); @@ -363,10 +383,11 @@ UniversalDApp.prototype.getCallButton = function (args) { var handleCallButtonClick = function (ev, $result) { if (!$result) { $result = getOutput(); - if (lookupOnly && !inputs.length) + if (lookupOnly && !inputs.length) { $outputOverride.empty().append($result); - else + } else { outputSpan.append($result); + } } var funArgs = ''; @@ -385,29 +406,34 @@ UniversalDApp.prototype.getCallButton = function (args) { return; } } - if (data.slice(0, 9) === 'undefined') + if (data.slice(0, 9) === 'undefined') { data = data.slice(9); - if (data.slice(0, 2) === '0x') data = data.slice(2); + } + if (data.slice(0, 2) === '0x') { + data = data.slice(2); + } replaceOutput($result, $('Waiting for transaction to be mined...')); if (isConstructor) { if (args.bytecode.indexOf('_') >= 0) { replaceOutput($result, $('Deploying and linking required libraries...')); - if (self.options.vm) + if (self.options.vm) { self.linkBytecode(args.contractName, function (err, bytecode) { - if (err) + if (err) { replaceOutput($result, $('').text('Error deploying required libraries: ' + err)); - else { + } else { args.bytecode = bytecode; handleCallButtonClick(ev, $result); } }); - else + } else { replaceOutput($result, $('Contract needs to be linked to a library, this is only supported in the JavaScript VM for now.')); + } return; - } else + } else { data = args.bytecode + data; + } } self.runTx(data, args, function (err, result) { @@ -462,13 +488,16 @@ UniversalDApp.prototype.getCallButton = function (args) { function testResult (err, address) { if (!err && !address) { setTimeout(function () { tryTillResponse(txhash, done); }, 500); - } else done(err, address); + } else { + done(err, address); + } } } tryTillResponse(result, function (err, result) { - if (err) replaceOutput($result, $('').text(err).addClass('error')); - else if (isConstructor) { + if (err) { + replaceOutput($result, $('').text(err).addClass('error')); + } else if (isConstructor) { $result.html(''); args.appendFunctions(result.contractAddress); } else { @@ -503,41 +532,52 @@ UniversalDApp.prototype.getCallButton = function (args) { UniversalDApp.prototype.linkBytecode = function (contractName, cb) { var bytecode = this.getContractByName(contractName).bytecode; - if (bytecode.indexOf('_') < 0) + if (bytecode.indexOf('_') < 0) { return cb(null, bytecode); + } var m = bytecode.match(/__([^_]{1,36})__/); - if (!m) + if (!m) { return cb('Invalid bytecode format.'); + } var libraryName = m[1]; - if (!this.getContractByName(libraryName)) + if (!this.getContractByName(libraryName)) { return cb('Library ' + libraryName + ' not found.'); + } var self = this; this.deployLibrary(libraryName, function (err, address) { - if (err) return cb(err); + if (err) { + return cb(err); + } var libLabel = '__' + libraryName + Array(39 - libraryName.length).join('_'); var hexAddress = address.toString('hex'); - if (hexAddress.slice(0, 2) === '0x') hexAddress = hexAddress.slice(2); + if (hexAddress.slice(0, 2) === '0x') { + hexAddress = hexAddress.slice(2); + } hexAddress = Array(40 - hexAddress.length + 1).join('0') + hexAddress; - while (bytecode.indexOf(libLabel) >= 0) + while (bytecode.indexOf(libLabel) >= 0) { bytecode = bytecode.replace(libLabel, hexAddress); + } self.getContractByName(contractName).bytecode = bytecode; self.linkBytecode(contractName, cb); }); }; UniversalDApp.prototype.deployLibrary = function (contractName, cb) { - if (this.getContractByName(contractName).address) + if (this.getContractByName(contractName).address) { return cb(null, this.getContractByName(contractName).address); + } var self = this; var bytecode = this.getContractByName(contractName).bytecode; - if (bytecode.indexOf('_') >= 0) + if (bytecode.indexOf('_') >= 0) { this.linkBytecode(contractName, function (err, bytecode) { if (err) cb(err); else self.deployLibrary(contractName, cb); }); - else { + } else { this.runTx(bytecode, { abi: { constant: false }, bytecode: bytecode }, function (err, result) { - if (err) return cb(err); + if (err) { + return cb(err); + } self.getContractByName(contractName).address = result.createdAddress; cb(err, result.createdAddress); }); @@ -554,8 +594,9 @@ UniversalDApp.prototype.runTx = function (data, args, cb) { var to = args.address; var constant = args.abi.constant; var isConstructor = args.bytecode !== undefined; - if (data.slice(0, 2) !== '0x') + if (data.slice(0, 2) !== '0x') { data = '0x' + data; + } var gas = self.options.getGas ? self.options.getGas : 1000000; @@ -582,8 +623,11 @@ UniversalDApp.prototype.runTx = function (data, args, cb) { } else { this.web3.eth.estimateGas(tx, function (err, resp) { tx.gas = resp; - if (!err) self.web3.eth.sendTransaction(tx, cb); - else cb(err, resp); + if (!err) { + self.web3.eth.sendTransaction(tx, cb); + } else { + cb(err, resp); + } }); } } else { diff --git a/src/web3-adapter.js b/src/web3-adapter.js index 21055a0936..9b55eb18cc 100644 --- a/src/web3-adapter.js +++ b/src/web3-adapter.js @@ -3,9 +3,10 @@ var Web3 = require('web3'); -if (typeof web3 !== 'undefined') +if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); -else +} else { web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); +} module.exports = web3;