Commit 3ef70425 authored by Iuri Matias's avatar Iuri Matias

remove unnecessary if elses

parent fda09db1
'use strict'
/**
* Crawl the given AST through the function walk(ast, callback)
*/
function AstWalker () {} // eslint-disable-line
/**
* visit all the AST nodes
*
* @param {Object} ast - AST node
* @param {Object or Function} callback - if (Function) the function will be called for every node.
* - if (Object) callback[<Node Type>] will be called for
* every node of type <Node Type>. callback["*"] will be called for all other nodes.
* in each case, if the callback returns false it does not descend into children.
* If no callback for the current type, children are visited.
*/
AstWalker.prototype.walk = function (ast, callback) {
if (callback instanceof Function) {
callback = {'*': callback}
}
if (!('*' in callback)) {
callback['*'] = function () { return true }
}
const nodes = ast.nodes || (ast.body && ast.body.statements) || ast.declarations
if(ast.body && ast.initializationExpression) // 'for' loop handling
nodes.push(ast.initializationExpression)
if (manageCallBack(ast, callback) && nodes && nodes.length > 0) {
for (let k in nodes) {
const child = nodes[k]
this.walk(child, callback)
}
}
}
/**
* walk the given @astList
*
* @param {Object} sourcesList - sources list (containing root AST node)
* @param {Function} - callback used by AstWalker to compute response
*/
AstWalker.prototype.walkAstList = function (sourcesList, callback) {
const walker = new AstWalker()
for (let k in sourcesList) {
walker.walk(sourcesList[k].ast, callback)
}
}
function manageCallBack (node, callback) {
if (node.nodeType in callback) {
return callback[node.nodeType](node)
}
return callback['*'](node)
}
module.exports = AstWalker
...@@ -88,10 +88,9 @@ function extractSourceMap (self, codeManager, address, contracts) { ...@@ -88,10 +88,9 @@ function extractSourceMap (self, codeManager, address, contracts) {
const sourceMap = getSourceMap(address, result.bytecode, contracts) const sourceMap = getSourceMap(address, result.bytecode, contracts)
if (sourceMap) { if (sourceMap) {
if (!helper.isContractCreation(address)) self.sourceMapByAddress[address] = sourceMap if (!helper.isContractCreation(address)) self.sourceMapByAddress[address] = sourceMap
resolve(sourceMap) return resolve(sourceMap)
} else {
reject('no sourcemap associated with the code ' + address)
} }
reject('no sourcemap associated with the code ' + address)
}).catch(reject) }).catch(reject)
}) })
} }
......
...@@ -94,12 +94,8 @@ SourceMappingDecoder.prototype.convertOffsetToLineColumn = function (sourceLocat ...@@ -94,12 +94,8 @@ SourceMappingDecoder.prototype.convertOffsetToLineColumn = function (sourceLocat
start: convertFromCharPosition(sourceLocation.start, lineBreakPositions), start: convertFromCharPosition(sourceLocation.start, lineBreakPositions),
end: convertFromCharPosition(sourceLocation.start + sourceLocation.length, lineBreakPositions) end: convertFromCharPosition(sourceLocation.start + sourceLocation.length, lineBreakPositions)
} }
} else {
return {
start: null,
end: null
}
} }
return {start: null, end: null}
} }
/** /**
...@@ -119,10 +115,7 @@ function convertFromCharPosition (pos, lineBreakPositions) { ...@@ -119,10 +115,7 @@ function convertFromCharPosition (pos, lineBreakPositions) {
} }
const beginColumn = line === 0 ? 0 : (lineBreakPositions[line - 1] + 1) const beginColumn = line === 0 ? 0 : (lineBreakPositions[line - 1] + 1)
const column = pos - beginColumn const column = pos - beginColumn
return { return {line, column}
line: line,
column: column
}
} }
function sourceLocationFromAstNode (astNode) { function sourceLocationFromAstNode (astNode) {
......
...@@ -27,10 +27,8 @@ function Storage (prefix) { ...@@ -27,10 +27,8 @@ function Storage (prefix) {
this.remove = function (name) { this.remove = function (name) {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
window.localStorage.removeItem(prefix + name) window.localStorage.removeItem(prefix + name)
return true
} else {
return true
} }
return true
} }
this.rename = function (originalName, newName) { this.rename = function (originalName, newName) {
...@@ -46,9 +44,8 @@ function Storage (prefix) { ...@@ -46,9 +44,8 @@ function Storage (prefix) {
// NOTE: this is a workaround for some browsers // NOTE: this is a workaround for some browsers
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined }) return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined })
} else {
return []
} }
return []
} }
this.keys = function () { this.keys = function () {
......
...@@ -91,17 +91,16 @@ module.exports = class UniversalDApp { ...@@ -91,17 +91,16 @@ module.exports = class UniversalDApp {
if (!this.config.get('settings/personal-mode')) { if (!this.config.get('settings/personal-mode')) {
return cb('Not running in personal mode') return cb('Not running in personal mode')
} }
passwordPromptCb((passphrase) => { return passwordPromptCb((passphrase) => {
this.executionContext.web3().personal.newAccount(passphrase, cb) this.executionContext.web3().personal.newAccount(passphrase, cb)
}) })
} else {
let privateKey
do {
privateKey = crypto.randomBytes(32)
} while (!isValidPrivate(privateKey))
this._addAccount(privateKey, '0x56BC75E2D63100000')
cb(null, '0x' + privateToAddress(privateKey).toString('hex'))
} }
let privateKey
do {
privateKey = crypto.randomBytes(32)
} while (!isValidPrivate(privateKey))
this._addAccount(privateKey, '0x56BC75E2D63100000')
cb(null, '0x' + privateToAddress(privateKey).toString('hex'))
} }
/** Add an account to the list of account (only for Javascript VM) */ /** Add an account to the list of account (only for Javascript VM) */
...@@ -110,22 +109,23 @@ module.exports = class UniversalDApp { ...@@ -110,22 +109,23 @@ module.exports = class UniversalDApp {
throw new Error('_addAccount() cannot be called in non-VM mode') throw new Error('_addAccount() cannot be called in non-VM mode')
} }
if (this.accounts) { if (!this.accounts) {
privateKey = Buffer.from(privateKey, 'hex') return
const address = privateToAddress(privateKey) }
privateKey = Buffer.from(privateKey, 'hex')
// FIXME: we don't care about the callback, but we should still make this proper const address = privateToAddress(privateKey)
let stateManager = this.executionContext.vm().stateManager
stateManager.getAccount(address, (error, account) => { // FIXME: we don't care about the callback, but we should still make this proper
if (error) return console.log(error) let stateManager = this.executionContext.vm().stateManager
account.balance = balance || '0xf00000000000000001' stateManager.getAccount(address, (error, account) => {
stateManager.putAccount(address, account, function cb (error) { if (error) return console.log(error)
if (error) console.log(error) account.balance = balance || '0xf00000000000000001'
}) stateManager.putAccount(address, account, function cb(error) {
if (error) console.log(error)
}) })
})
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
}
} }
/** Return the list of accounts */ /** Return the list of accounts */
...@@ -171,40 +171,36 @@ module.exports = class UniversalDApp { ...@@ -171,40 +171,36 @@ module.exports = class UniversalDApp {
} }
/** Get the balance of an address */ /** Get the balance of an address */
getBalance (address, cb) { getBalance(address, cb) {
address = stripHexPrefix(address) address = stripHexPrefix(address)
if (!this.executionContext.isVM()) { if (!this.executionContext.isVM()) {
this.executionContext.web3().eth.getBalance(address, (err, res) => { return this.executionContext.web3().eth.getBalance(address, (err, res) => {
if (err) {
cb(err)
} else {
cb(null, res.toString(10))
}
})
} else {
if (!this.accounts) {
return cb('No accounts?')
}
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => {
if (err) { if (err) {
cb('Account not found') return cb(err)
} else {
cb(null, new BN(res.balance).toString(10))
} }
cb(null, res.toString(10))
}) })
} }
if (!this.accounts) {
return cb('No accounts?')
}
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => {
if (err) {
return cb('Account not found')
}
cb(null, new BN(res.balance).toString(10))
})
} }
/** Get the balance of an address, and convert wei to ether */ /** Get the balance of an address, and convert wei to ether */
getBalanceInEther (address, callback) { getBalanceInEther (address, callback) {
this.getBalance(address, (error, balance) => { this.getBalance(address, (error, balance) => {
if (error) { if (error) {
callback(error) return callback(error)
} else {
callback(null, this.executionContext.web3().utils.fromWei(balance, 'ether'))
} }
callback(null, this.executionContext.web3().utils.fromWei(balance, 'ether'))
}) })
} }
...@@ -219,10 +215,7 @@ module.exports = class UniversalDApp { ...@@ -219,10 +215,7 @@ module.exports = class UniversalDApp {
* @param {Function} callback - callback. * @param {Function} callback - callback.
*/ */
createContract (data, confirmationCb, continueCb, promptCb, callback) { createContract (data, confirmationCb, continueCb, promptCb, callback) {
this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => { this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, callback)
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})
} }
/** /**
...@@ -235,10 +228,7 @@ module.exports = class UniversalDApp { ...@@ -235,10 +228,7 @@ module.exports = class UniversalDApp {
*/ */
callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) { callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) {
const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure' const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure'
this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, (error, txResult) => { this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, callback)
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})
} }
/** /**
...@@ -249,10 +239,7 @@ module.exports = class UniversalDApp { ...@@ -249,10 +239,7 @@ module.exports = class UniversalDApp {
* @param {Function} callback - callback. * @param {Function} callback - callback.
*/ */
sendRawTransaction (to, data, confirmationCb, continueCb, promptCb, callback) { sendRawTransaction (to, data, confirmationCb, continueCb, promptCb, callback) {
this.runTx({to, data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => { this.runTx({to, data, useCall: false}, confirmationCb, continueCb, promptCb, callback)
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})
} }
context () { context () {
......
...@@ -19,10 +19,9 @@ Web3Providers.prototype.addProvider = function (type, obj) { ...@@ -19,10 +19,9 @@ Web3Providers.prototype.addProvider = function (type, obj) {
Web3Providers.prototype.get = function (type, cb) { Web3Providers.prototype.get = function (type, cb) {
if (this.modes[type]) { if (this.modes[type]) {
cb(null, this.modes[type]) return cb(null, this.modes[type])
} else {
cb('error: this provider has not been setup (' + type + ')', null)
} }
cb('error: this provider has not been setup (' + type + ')', null)
} }
Web3Providers.prototype.addWeb3 = function (type, web3) { Web3Providers.prototype.addWeb3 = function (type, web3) {
......
...@@ -211,10 +211,9 @@ web3VmProvider.prototype.traceTransaction = function (txHash, options, cb) { ...@@ -211,10 +211,9 @@ web3VmProvider.prototype.traceTransaction = function (txHash, options, cb) {
cb(null, this.vmTraces[txHash]) cb(null, this.vmTraces[txHash])
} }
return this.vmTraces[txHash] return this.vmTraces[txHash]
} else { }
if (cb) { if (cb) {
cb('unable to retrieve traces ' + txHash, null) cb('unable to retrieve traces ' + txHash, null)
}
} }
} }
...@@ -232,9 +231,8 @@ web3VmProvider.prototype.storageRangeAt = function (blockNumber, txIndex, addres ...@@ -232,9 +231,8 @@ web3VmProvider.prototype.storageRangeAt = function (blockNumber, txIndex, addres
storage: JSON.parse(JSON.stringify(storage)), storage: JSON.parse(JSON.stringify(storage)),
nextKey: null nextKey: null
}) })
} else {
cb('unable to retrieve storage ' + txIndex + ' ' + address)
} }
cb('unable to retrieve storage ' + txIndex + ' ' + address)
} }
web3VmProvider.prototype.getBlockNumber = function (cb) { cb(null, 'vm provider') } web3VmProvider.prototype.getBlockNumber = function (cb) { cb(null, 'vm provider') }
...@@ -245,10 +243,9 @@ web3VmProvider.prototype.getTransaction = function (txHash, cb) { ...@@ -245,10 +243,9 @@ web3VmProvider.prototype.getTransaction = function (txHash, cb) {
cb(null, this.txs[txHash]) cb(null, this.txs[txHash])
} }
return this.txs[txHash] return this.txs[txHash]
} else { }
if (cb) { if (cb) {
cb('unable to retrieve tx ' + txHash, null) cb('unable to retrieve tx ' + txHash, null)
}
} }
} }
...@@ -259,10 +256,9 @@ web3VmProvider.prototype.getTransactionReceipt = function (txHash, cb) { ...@@ -259,10 +256,9 @@ web3VmProvider.prototype.getTransactionReceipt = function (txHash, cb) {
cb(null, this.txsReceipt[txHash]) cb(null, this.txsReceipt[txHash])
} }
return this.txsReceipt[txHash] return this.txsReceipt[txHash]
} else { }
if (cb) { if (cb) {
cb('unable to retrieve txReceipt ' + txHash, null) cb('unable to retrieve txReceipt ' + txHash, null)
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment