diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ea1d4c9f95..af5e311c338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -394,6 +394,10 @@ Released with 1.0.0-beta.37 code base. - Changed Geth Docker verision from `stable` to `1.10.3` in `e2e.geth.instamine.sh` and `scripts/e2e.geth.automine.sh` (#4154) -## [Unreleased] - ## [1.4.1] + +### Removes + +- Removing the underscore package + +## [Unreleased] \ No newline at end of file diff --git a/package.json b/package.json index 78ef53a4a32..e80c45af7c7 100644 --- a/package.json +++ b/package.json @@ -101,7 +101,6 @@ "@types/bignumber.js": "^4.0.2", "@types/bn.js": "^4.11.6", "@types/node": "^12.12.68", - "@types/underscore": "^1.10.24", "babel-loader": "^8.1.0", "bignumber.js": "^9.0.1", "bn.js": "^4.11.9", @@ -130,7 +129,6 @@ "sandboxed-module": "^2.0.4", "ts-node": "^9.0.0", "typescript": "^3.9.7", - "underscore": "1.12.1", "wait-port": "^0.2.9", "webpack": "^4.44.2", "webpack-cli": "^3.3.12" diff --git a/packages/web3-bzz/package.json b/packages/web3-bzz/package.json index 05c60637f52..88019ef614e 100644 --- a/packages/web3-bzz/package.json +++ b/packages/web3-bzz/package.json @@ -17,8 +17,7 @@ "dependencies": { "@types/node": "^12.12.6", "got": "9.6.0", - "swarm-js": "^0.1.40", - "underscore": "1.12.1" + "swarm-js": "^0.1.40" }, "devDependencies": { "dtslint": "^3.4.1", diff --git a/packages/web3-bzz/src/index.js b/packages/web3-bzz/src/index.js index 7f87381f838..b81e7f67433 100644 --- a/packages/web3-bzz/src/index.js +++ b/packages/web3-bzz/src/index.js @@ -22,7 +22,6 @@ "use strict"; -var _ = require('underscore'); var swarm = require("swarm-js"); @@ -52,7 +51,7 @@ if (typeof ethereum !== 'undefined' && ethereum.bzz) { Bzz.prototype.setProvider = function(provider) { // is ethereum provider - if(_.isObject(provider) && _.isString(provider.bzz)) { + if(!!provider && typeof provider === 'object' && typeof provider.bzz === 'string') { provider = provider.bzz; // is no string, set default } @@ -61,7 +60,7 @@ Bzz.prototype.setProvider = function(provider) { // } - if(_.isString(provider)) { + if(typeof provider === 'string') { this.currentProvider = provider; } else { this.currentProvider = null; diff --git a/packages/web3-core-helpers/package.json b/packages/web3-core-helpers/package.json index ae9d76010fb..7e930fba356 100644 --- a/packages/web3-core-helpers/package.json +++ b/packages/web3-core-helpers/package.json @@ -14,7 +14,6 @@ }, "main": "lib/index.js", "dependencies": { - "underscore": "1.12.1", "web3-eth-iban": "1.4.0", "web3-utils": "1.4.0" }, diff --git a/packages/web3-core-helpers/src/formatters.js b/packages/web3-core-helpers/src/formatters.js index 6f9af04de0d..2de56c11105 100644 --- a/packages/web3-core-helpers/src/formatters.js +++ b/packages/web3-core-helpers/src/formatters.js @@ -23,7 +23,6 @@ "use strict"; -var _ = require('underscore'); var utils = require('web3-utils'); var Iban = require('web3-eth-iban'); @@ -121,7 +120,7 @@ var inputBlockNumberFormatter = function (blockNumber) { return '0x0'; } - return (utils.isHexStrict(blockNumber)) ? ((_.isString(blockNumber)) ? blockNumber.toLowerCase() : blockNumber) : utils.numberToHex(blockNumber); + return (utils.isHexStrict(blockNumber)) ? ((typeof blockNumber === 'string') ? blockNumber.toLowerCase() : blockNumber) : utils.numberToHex(blockNumber); }; /** @@ -201,10 +200,10 @@ var inputTransactionFormatter = function (options) { options = _txInputFormatter(options); // check from, only if not number, or object - if (!_.isNumber(options.from) && !_.isObject(options.from)) { + if (!(typeof options.from === 'number') && !(!!options.from && typeof options.from === 'object')) { options.from = options.from || (this ? this.defaultAccount : null); - if (!options.from && !_.isNumber(options.from)) { + if (!options.from && !(typeof options.from === 'number')) { throw new Error('The send transactions "from" field must be defined!'); } @@ -274,7 +273,7 @@ var outputTransactionReceiptFormatter = function (receipt) { receipt.cumulativeGasUsed = utils.hexToNumber(receipt.cumulativeGasUsed); receipt.gasUsed = utils.hexToNumber(receipt.gasUsed); - if (_.isArray(receipt.logs)) { + if (Array.isArray(receipt.logs)) { receipt.logs = receipt.logs.map(outputLogFormatter); } @@ -311,9 +310,9 @@ var outputBlockFormatter = function (block) { if (block.totalDifficulty) block.totalDifficulty = outputBigNumberFormatter(block.totalDifficulty); - if (_.isArray(block.transactions)) { + if (Array.isArray(block.transactions)) { block.transactions.forEach(function (item) { - if (!_.isString(item)) + if (!(typeof item === 'string')) return outputTransactionFormatter(item); }); } @@ -358,13 +357,13 @@ var inputLogFormatter = function (options) { // make sure topics, get converted to hex options.topics = options.topics || []; options.topics = options.topics.map(function (topic) { - return (_.isArray(topic)) ? topic.map(toTopic) : toTopic(topic); + return (Array.isArray(topic)) ? topic.map(toTopic) : toTopic(topic); }); toTopic = null; if (options.address) { - options.address = (_.isArray(options.address)) ? options.address.map(function (addr) { + options.address = (Array.isArray(options.address)) ? options.address.map(function (addr) { return inputAddressFormatter(addr); }) : inputAddressFormatter(options.address); } @@ -424,7 +423,7 @@ var inputPostFormatter = function (post) { post.priority = utils.numberToHex(post.priority); // fallback - if (!_.isArray(post.topics)) { + if (!Array.isArray(post.topics)) { post.topics = post.topics ? [post.topics] : []; } diff --git a/packages/web3-core-method/package.json b/packages/web3-core-method/package.json index 20fa6b70f72..45f2f34a0f4 100644 --- a/packages/web3-core-method/package.json +++ b/packages/web3-core-method/package.json @@ -15,7 +15,6 @@ "main": "lib/index.js", "dependencies": { "@ethersproject/transactions": "^5.0.0-beta.135", - "underscore": "1.12.1", "web3-core-helpers": "1.4.0", "web3-core-promievent": "1.4.0", "web3-core-subscriptions": "1.4.0", diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index 63ce5acbc00..7335adbb7be 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -23,7 +23,6 @@ 'use strict'; -var _ = require('underscore'); var errors = require('web3-core-helpers').errors; var formatters = require('web3-core-helpers').formatters; var utils = require('web3-utils'); @@ -102,7 +101,7 @@ Method.prototype.attachToObject = function (obj) { * @return {String} name of jsonrpc method */ Method.prototype.getCall = function (args) { - return _.isFunction(this.call) ? this.call(args) : this.call; + return typeof this.call === 'function' ? this.call(args) : this.call; }; /** @@ -113,7 +112,7 @@ Method.prototype.getCall = function (args) { * @return {Function|Null} callback, if exists */ Method.prototype.extractCallback = function (args) { - if (_.isFunction(args[args.length - 1])) { + if (typeof (args[args.length - 1]) === 'function') { return args.pop(); // modify the args array! } }; @@ -161,7 +160,7 @@ Method.prototype.formatInput = function (args) { Method.prototype.formatOutput = function (result) { var _this = this; - if (_.isArray(result)) { + if (Array.isArray(result)) { return result.map(function (res) { return _this.outputFormatter && res ? _this.outputFormatter(res) : res; }); @@ -180,6 +179,7 @@ Method.prototype.formatOutput = function (result) { Method.prototype.toPayload = function (args) { var call = this.getCall(args); var callback = this.extractCallback(args); + var params = this.formatInput(args); this.validateArgs(params); @@ -206,8 +206,8 @@ Method.prototype._confirmTransaction = function (defer, result, payload) { intervalId = null, lastBlock = null, receiptJSON = '', - gasProvided = (_.isObject(payload.params[0]) && payload.params[0].gas) ? payload.params[0].gas : null, - isContractDeployment = _.isObject(payload.params[0]) && + gasProvided = ((!!payload.params[0] && typeof payload.params[0] === 'object') && payload.params[0].gas) ? payload.params[0].gas : null, + isContractDeployment = (!!payload.params[0] && typeof payload.params[0] === 'object') && payload.params[0].data && payload.params[0].from && !payload.params[0].to, @@ -258,7 +258,7 @@ Method.prototype._confirmTransaction = function (defer, result, payload) { ]; // attach methods to this._ethereumCall var _ethereumCall = {}; - _.each(_ethereumCalls, function (mthd) { + _ethereumCalls.forEach(mthd => { mthd.attachToObject(_ethereumCall); mthd.requestManager = method.requestManager; // assign rather than call setRequestManager() }); @@ -591,11 +591,11 @@ var getWallet = function (from, accounts) { var wallet = null; // is index given - if (_.isNumber(from)) { + if (typeof from === 'number') { wallet = accounts.wallet[from]; // is account given - } else if (_.isObject(from) && from.address && from.privateKey) { + } else if (!!from && typeof from === 'object' && from.address && from.privateKey) { wallet = from; // search in wallet for address @@ -689,10 +689,10 @@ Method.prototype.buildCall = function () { // SENDS the SIGNED SIGNATURE var sendSignedTx = function (sign) { - var signedPayload = _.extend({}, payload, { + var signedPayload = { ... payload, method: 'eth_sendRawTransaction', params: [sign.rawTransaction] - }); + }; method.requestManager.send(signedPayload, sendTxCallback); }; @@ -706,29 +706,30 @@ Method.prototype.buildCall = function () { // ETH_SENDTRANSACTION if (payload.method === 'eth_sendTransaction') { var tx = payload.params[0]; - wallet = getWallet((_.isObject(tx)) ? tx.from : null, method.accounts); + wallet = getWallet((!!tx && typeof tx === 'object') ? tx.from : null, method.accounts); // If wallet was found, sign tx, and send using sendRawTransaction if (wallet && wallet.privateKey) { - var txOptions = _.omit(tx, 'from'); + var tx = JSON.parse(JSON.stringify(tx)); + delete tx.from; - if (method.defaultChain && !txOptions.chain) { - txOptions.chain = method.defaultChain; + if (method.defaultChain && !tx.chain) { + tx.chain = method.defaultChain; } - if (method.defaultHardfork && !txOptions.hardfork) { - txOptions.hardfork = method.defaultHardfork; + if (method.defaultHardfork && !tx.hardfork) { + tx.hardfork = method.defaultHardfork; } - if (method.defaultCommon && !txOptions.common) { - txOptions.common = method.defaultCommon; + if (method.defaultCommon && !tx.common) { + tx.common = method.defaultCommon; } - method.accounts.signTransaction(txOptions, wallet.privateKey) + method.accounts.signTransaction(tx, wallet.privateKey) .then(sendSignedTx) .catch(function (err) { - if (_.isFunction(defer.eventEmitter.listeners) && defer.eventEmitter.listeners('error').length) { + if (typeof defer.eventEmitter.listeners === 'function' && defer.eventEmitter.listeners('error').length) { try { defer.eventEmitter.emit('error', err); } catch (err) { @@ -769,7 +770,7 @@ Method.prototype.buildCall = function () { }; // Send the actual transaction - if (isSendTx && _.isObject(payload.params[0]) && typeof payload.params[0].gasPrice === 'undefined') { + if (isSendTx && !!payload.params[0] && typeof payload.params[0] === 'object' && typeof payload.params[0].gasPrice === 'undefined') { var getGasPrice = (new Method({ name: 'getGasPrice', @@ -866,7 +867,7 @@ Method.prototype.getRevertReason = function (txOptions, blockNumber) { * @returns {Boolean} */ Method.prototype.isRevertReasonString = function (data) { - return _.isString(data) && ((data.length - 2) / 2) % 32 === 4 && data.substring(0, 10) === '0x08c379a0'; + return typeof data === 'string' && ((data.length - 2) / 2) % 32 === 4 && data.substring(0, 10) === '0x08c379a0'; }; /** diff --git a/packages/web3-core-requestmanager/package.json b/packages/web3-core-requestmanager/package.json index 4ab22c6d26e..c959a00d544 100644 --- a/packages/web3-core-requestmanager/package.json +++ b/packages/web3-core-requestmanager/package.json @@ -12,7 +12,6 @@ "compile": "tsc -b tsconfig.json" }, "dependencies": { - "underscore": "1.12.1", "util": "^0.12.0", "web3-core-helpers": "1.4.0", "web3-providers-http": "1.4.0", diff --git a/packages/web3-core-requestmanager/src/index.js b/packages/web3-core-requestmanager/src/index.js index 4e472fc8f41..31062ec61d6 100644 --- a/packages/web3-core-requestmanager/src/index.js +++ b/packages/web3-core-requestmanager/src/index.js @@ -21,7 +21,6 @@ const { callbackify } = require('util'); -var _ = require('underscore'); var errors = require('web3-core-helpers').errors; var Jsonrpc = require('./jsonrpc.js'); var BatchManager = require('./batch.js'); @@ -207,7 +206,7 @@ RequestManager.prototype.sendBatch = function (data, callback) { return callback(err); } - if (!_.isArray(results)) { + if (!Array.isArray(results)) { return callback(errors.InvalidResponse(results)); } diff --git a/packages/web3-core-subscriptions/package.json b/packages/web3-core-subscriptions/package.json index e8bba135a6d..b4eca96df5f 100644 --- a/packages/web3-core-subscriptions/package.json +++ b/packages/web3-core-subscriptions/package.json @@ -15,7 +15,6 @@ "main": "lib/index.js", "dependencies": { "eventemitter3": "4.0.4", - "underscore": "1.12.1", "web3-core-helpers": "1.4.0" }, "devDependencies": { diff --git a/packages/web3-core-subscriptions/src/subscription.js b/packages/web3-core-subscriptions/src/subscription.js index 9303b98401d..b0b134d7997 100644 --- a/packages/web3-core-subscriptions/src/subscription.js +++ b/packages/web3-core-subscriptions/src/subscription.js @@ -22,16 +22,19 @@ "use strict"; -var _ = require('underscore'); var errors = require('web3-core-helpers').errors; var EventEmitter = require('eventemitter3'); var formatters = require('web3-core-helpers').formatters; +function identity(value) { //used to return the exact same replica of the value given to it as the argument + return value; +} + function Subscription(options) { EventEmitter.call(this); this.id = null; - this.callback = _.identity; + this.callback = identity; this.arguments = null; this.lastBlock = null; // "from" block tracker for backfilling events on reconnection @@ -56,7 +59,7 @@ Subscription.prototype.constructor = Subscription; */ Subscription.prototype._extractCallback = function (args) { - if (_.isFunction(args[args.length - 1])) { + if (typeof args[args.length - 1] === 'function') { return args.pop(); // modify the args array! } }; @@ -136,7 +139,7 @@ Subscription.prototype._formatOutput = function (result) { */ Subscription.prototype._toPayload = function (args) { var params = []; - this.callback = this._extractCallback(args) || _.identity; + this.callback = this._extractCallback(args) || identity; if (!this.subscriptionMethod) { this.subscriptionMethod = args.shift(); @@ -226,7 +229,7 @@ Subscription.prototype.subscribe = function() { // Re-subscription only: continue fetching from the last block we received. // a dropped connection may have resulted in gaps in the logs... - if (this.lastBlock && _.isObject(this.options.params)){ + if (this.lastBlock && !!this.options.params && typeof this.options.params === 'object'){ payload.params[1] = this.options.params; payload.params[1].fromBlock = formatters.inputBlockNumberFormatter(this.lastBlock + 1); } @@ -240,7 +243,7 @@ Subscription.prototype.subscribe = function() { this.options.params = payload.params[1]; // get past logs, if fromBlock is available - if(payload.params[0] === 'logs' && _.isObject(payload.params[1]) && payload.params[1].hasOwnProperty('fromBlock') && isFinite(payload.params[1].fromBlock)) { + if(payload.params[0] === 'logs' && !!payload.params[1] && typeof payload.params[1] === 'object' && payload.params[1].hasOwnProperty('fromBlock') && isFinite(payload.params[1].fromBlock)) { // send the subscription request // copy the params to avoid race-condition with deletion below this block @@ -283,7 +286,7 @@ Subscription.prototype.subscribe = function() { // call callback on notifications _this.options.requestManager.addSubscription(_this, function(error, result) { if (!error) { - if (!_.isArray(result)) { + if (!Array.isArray(result)) { result = [result]; } @@ -291,9 +294,9 @@ Subscription.prototype.subscribe = function() { var output = _this._formatOutput(resultItem); // Track current block (for gaps introduced by dropped connections) - _this.lastBlock = _.isObject(output) ? output.blockNumber : null; + _this.lastBlock = !!output && typeof output === 'object' ? output.blockNumber : null; - if (_.isFunction(_this.options.subscription.subscriptionHandler)) { + if (typeof _this.options.subscription.subscriptionHandler === 'function' ) { return _this.options.subscription.subscriptionHandler.call(_this, output); } else { _this.emit('data', output); diff --git a/packages/web3-eth-abi/package.json b/packages/web3-eth-abi/package.json index 9f1687beed2..ada7d27e6ff 100644 --- a/packages/web3-eth-abi/package.json +++ b/packages/web3-eth-abi/package.json @@ -15,7 +15,6 @@ "main": "lib/index.js", "dependencies": { "@ethersproject/abi": "5.0.7", - "underscore": "1.12.1", "web3-utils": "1.4.0" }, "devDependencies": { diff --git a/packages/web3-eth-abi/src/index.js b/packages/web3-eth-abi/src/index.js index 34801939265..63728ac567c 100644 --- a/packages/web3-eth-abi/src/index.js +++ b/packages/web3-eth-abi/src/index.js @@ -22,13 +22,12 @@ */ var Buffer = require('buffer').Buffer; -var _ = require('underscore'); var utils = require('web3-utils'); var EthersAbiCoder = require('@ethersproject/abi').AbiCoder; var ParamType = require('@ethersproject/abi').ParamType; var ethersAbiCoder = new EthersAbiCoder(function (type, value) { - if (type.match(/^u?int/) && !_.isArray(value) && (!_.isObject(value) || value.constructor.name !== 'BN')) { + if (type.match(/^u?int/) && !Array.isArray(value) && (!(!!value && typeof value === 'object') || value.constructor.name !== 'BN')) { return value.toString(); } return value; @@ -52,7 +51,7 @@ var ABICoder = function () { * @return {String} encoded function name */ ABICoder.prototype.encodeFunctionSignature = function (functionName) { - if (_.isObject(functionName)) { + if (typeof functionName === 'function' || typeof functionName === 'object' && functionName) { functionName = utils._jsonInterfaceMethodToString(functionName); } @@ -67,7 +66,7 @@ ABICoder.prototype.encodeFunctionSignature = function (functionName) { * @return {String} encoded function name */ ABICoder.prototype.encodeEventSignature = function (functionName) { - if (_.isObject(functionName)) { + if (typeof functionName === 'function' || typeof functionName === 'object' && functionName) { functionName = utils._jsonInterfaceMethodToString(functionName); } @@ -367,7 +366,7 @@ ABICoder.prototype.decodeParametersWith = function (outputs, bytes, loose) { returnValue[i] = decodedValue; - if (_.isObject(output) && output.name) { + if ((typeof output === 'function' || !!output && typeof output === 'object') && output.name) { returnValue[output.name] = decodedValue; } @@ -388,7 +387,7 @@ ABICoder.prototype.decodeParametersWith = function (outputs, bytes, loose) { */ ABICoder.prototype.decodeLog = function (inputs, data, topics) { var _this = this; - topics = _.isArray(topics) ? topics : [topics]; + topics = Array.isArray(topics) ? topics : [topics]; data = data || ''; diff --git a/packages/web3-eth-accounts/package.json b/packages/web3-eth-accounts/package.json index dd8550d5390..879ccc5e607 100644 --- a/packages/web3-eth-accounts/package.json +++ b/packages/web3-eth-accounts/package.json @@ -20,7 +20,6 @@ "eth-lib": "0.2.8", "ethereumjs-util": "^7.0.10", "scrypt-js": "^3.0.1", - "underscore": "1.12.1", "uuid": "3.3.2", "web3-core": "1.4.0", "web3-core-helpers": "1.4.0", diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 8a2124b33f1..0a21ef48af1 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -22,7 +22,6 @@ 'use strict'; -var _ = require('underscore'); var core = require('web3-core'); var Method = require('web3-core-method'); var Account = require('eth-lib/lib/account'); @@ -37,7 +36,7 @@ var Common = require('@ethereumjs/common').default; var isNot = function(value) { - return (_.isUndefined(value) || _.isNull(value)); + return (typeof value === 'undefined') || value === null; }; var Accounts = function Accounts() { @@ -85,7 +84,7 @@ var Accounts = function Accounts() { ]; // attach methods to this._ethereumCall this._ethereumCall = {}; - _.each(_ethereumCall, function(method) { + _ethereumCall.forEach( (method) => { method.attachToObject(_this._ethereumCall); method.setRequestManager(_this._requestManager); }); @@ -154,7 +153,7 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca } try { - var transaction = helpers.formatters.inputCallFormatter(_.clone(tx)); + var transaction = helpers.formatters.inputCallFormatter(Object.assign({},tx)); transaction.data = transaction.data || '0x'; transaction.value = transaction.value || '0x'; transaction.gasLimit = transaction.gasLimit || transaction.gas; @@ -255,7 +254,7 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca if (isNot(args[0]) || isNot(args[1]) || isNot(args[2]) || isNot(args[3])) { throw new Error('One of the values "chainId", "networkId", "gasPrice", or "nonce" couldn\'t be fetched: ' + JSON.stringify(args)); } - return signed(_.extend(tx, {chainId: args[0], gasPrice: args[1], nonce: args[2], networkId: args[3]})); + return signed({ ...tx, chainId: args[0], gasPrice: args[1], nonce: args[2], networkId: args[3]}); }); }; @@ -334,7 +333,7 @@ Accounts.prototype.recover = function recover(message, signature, preFixed) { var args = [].slice.apply(arguments); - if (_.isObject(message)) { + if (!!message && typeof message === 'object') { return this.recover(message.messageHash, Account.encodeSignature([message.v, message.r, message.s]), true); } @@ -344,7 +343,7 @@ Accounts.prototype.recover = function recover(message, signature, preFixed) { if (args.length >= 4) { preFixed = args.slice(-1)[0]; - preFixed = _.isBoolean(preFixed) ? !!preFixed : false; + preFixed = typeof preFixed === 'boolean' ? !!preFixed : false; return this.recover(message, Account.encodeSignature(args.slice(1, 4)), preFixed); // v, r, s } @@ -355,11 +354,11 @@ Accounts.prototype.recover = function recover(message, signature, preFixed) { Accounts.prototype.decrypt = function(v3Keystore, password, nonStrict) { /* jshint maxcomplexity: 10 */ - if (!_.isString(password)) { + if (!(typeof password === 'string')) { throw new Error('No password given.'); } - var json = (_.isObject(v3Keystore)) ? v3Keystore : JSON.parse(nonStrict ? v3Keystore.toLowerCase() : v3Keystore); + var json = (!!v3Keystore && typeof v3Keystore === 'object') ? v3Keystore : JSON.parse(nonStrict ? v3Keystore.toLowerCase() : v3Keystore); if (json.version !== 3) { throw new Error('Not a valid V3 wallet'); @@ -468,7 +467,7 @@ function Wallet(accounts) { Wallet.prototype._findSafeIndex = function(pointer) { pointer = pointer || 0; - if (_.has(this, pointer)) { + if (this.hasOwnProperty(pointer)) { return this._findSafeIndex(pointer + 1); } else { return pointer; @@ -497,7 +496,7 @@ Wallet.prototype.create = function(numberOfAccounts, entropy) { Wallet.prototype.add = function(account) { - if (_.isString(account)) { + if (typeof account === 'string') { account = this._accounts.privateKeyToAccount(account); } if (!this[account.address]) { diff --git a/packages/web3-eth-contract/package.json b/packages/web3-eth-contract/package.json index 2f3eaead241..8c48546789b 100644 --- a/packages/web3-eth-contract/package.json +++ b/packages/web3-eth-contract/package.json @@ -15,7 +15,6 @@ "main": "lib/index.js", "dependencies": { "@types/bn.js": "^4.11.5", - "underscore": "1.12.1", "web3-core": "1.4.0", "web3-core-helpers": "1.4.0", "web3-core-method": "1.4.0", diff --git a/packages/web3-eth-contract/src/index.js b/packages/web3-eth-contract/src/index.js index fd46f9be6c5..e597eef7b39 100644 --- a/packages/web3-eth-contract/src/index.js +++ b/packages/web3-eth-contract/src/index.js @@ -30,8 +30,6 @@ "use strict"; - -var _ = require('underscore'); var core = require('web3-core'); var Method = require('web3-core-method'); var utils = require('web3-utils'); @@ -78,11 +76,11 @@ var Contract = function Contract(jsonInterface, address, options) { this.options = {}; var lastArg = args[args.length - 1]; - if(_.isObject(lastArg) && !_.isArray(lastArg)) { + if(!!lastArg && typeof lastArg === 'object' && !Array.isArray(lastArg)) { options = lastArg; - this.options = _.extend(this.options, this._getOrSetDefaultOptions(options)); - if(_.isObject(address)) { + this.options = { ...this.options, ...this._getOrSetDefaultOptions(options)}; + if(!!address && typeof address === 'object') { address = null; } } @@ -327,7 +325,7 @@ Contract.setProvider = function(provider, accounts) { * @return {Function} the callback */ Contract.prototype._getCallback = function getCallback(args) { - if (args && _.isFunction(args[args.length - 1])) { + if (args && !!args[args.length- 1 ] && typeof args[args.length - 1] === 'function') { return args.pop(); // modify the args array! } }; @@ -391,7 +389,7 @@ Contract.prototype._encodeEventABI = function (event, options) { }); // use given topics - if(_.isArray(options.topics)) { + if(Array.isArray(options.topics)) { result.topics = options.topics; // create topics based on filter @@ -417,7 +415,7 @@ Contract.prototype._encodeEventABI = function (event, options) { // TODO: https://github.com/ethereum/web3.js/issues/344 // TODO: deal properly with components - if (_.isArray(value)) { + if (Array.isArray(value)) { return value.map(function (v) { return abi.encodeParameter(i.type, v); }); @@ -517,7 +515,7 @@ Contract.prototype._encodeMethodABI = function _encodeMethodABI() { return ((methodSignature === 'constructor' && json.type === methodSignature) || ((json.signature === methodSignature || json.signature === methodSignature.replace('0x','') || json.name === methodSignature) && json.type === 'function')); }).map(function (json) { - var inputLength = (_.isArray(json.inputs)) ? json.inputs.length : 0; + var inputLength = (Array.isArray(json.inputs)) ? json.inputs.length : 0; if (inputLength !== args.length) { throw new Error('The number of arguments is not matching the methods required number. You need to pass '+ inputLength +' arguments.'); @@ -526,7 +524,7 @@ Contract.prototype._encodeMethodABI = function _encodeMethodABI() { if (json.type === 'function') { signature = json.signature; } - return _.isArray(json.inputs) ? json.inputs : []; + return Array.isArray(json.inputs) ? json.inputs : []; }).map(function (inputs) { return abi.encodeParameters(inputs, args).replace('0x',''); })[0] || ''; @@ -606,7 +604,7 @@ Contract.prototype.deploy = function(options, callback){ throw errors.ContractMissingDeployDataError(); } - var constructor = _.find(this.options.jsonInterface, function (method) { + var constructor = this.options.jsonInterface.find((method) => { return (method.type === 'constructor'); }) || {}; constructor.signature = 'constructor'; @@ -636,9 +634,9 @@ Contract.prototype._generateEventOptions = function() { var callback = this._getCallback(args); // get the options - var options = (_.isObject(args[args.length - 1])) ? args.pop() : {}; + var options = (!!args[args.length - 1] && typeof args[args.length - 1]) === 'object' ? args.pop() : {}; - var eventName = (_.isString(args[0])) ? args[0] : 'allevents'; + var eventName = (typeof args[0] === 'string') ? args[0] : 'allevents'; var event = (eventName.toLowerCase() === 'allevents') ? { name: 'ALLEVENTS', jsonInterface: this.options.jsonInterface @@ -698,7 +696,7 @@ Contract.prototype.once = function(event, options, callback) { // don't return as once shouldn't provide "on" this._on(event, options, function (err, res, sub) { sub.unsubscribe(); - if(_.isFunction(callback)){ + if(typeof callback === 'function'){ callback(err, res, sub); } }); @@ -745,7 +743,7 @@ Contract.prototype._on = function(){ this.emit('data', output); } - if (_.isFunction(this.callback)) { + if (typeof this.callback === 'function') { this.callback(null, output, this); } } @@ -845,11 +843,11 @@ Contract.prototype._processExecuteArguments = function _processExecuteArguments( processedArgs.callback = this._parent._getCallback(args); // get block number to use for call - if(processedArgs.type === 'call' && args[args.length - 1] !== true && (_.isString(args[args.length - 1]) || isFinite(args[args.length - 1]))) + if(processedArgs.type === 'call' && args[args.length - 1] !== true && (typeof args[args.length - 1] === 'string' || isFinite(args[args.length - 1]))) processedArgs.defaultBlock = args.pop(); // get the options - processedArgs.options = (_.isObject(args[args.length - 1])) ? args.pop() : {}; + processedArgs.options = (!!args[args.length - 1] && typeof args[args.length - 1]) === 'object' ? args.pop() : {}; // get the generateRequest argument for batch requests processedArgs.generateRequest = (args[args.length - 1] === true)? args.pop() : false; @@ -951,7 +949,7 @@ Contract.prototype._executeMethod = function _executeMethod(){ return utils._fireError(errors.ContractNoFromAddressDefinedError(), defer.eventEmitter, defer.reject, args.callback); } - if (_.isBoolean(this._method.payable) && !this._method.payable && args.options.value && args.options.value > 0) { + if (typeof this._method.payable === 'boolean' && !this._method.payable && args.options.value && args.options.value > 0) { return utils._fireError(new Error('Can not send value to non-payable contract method or constructor'), defer.eventEmitter, defer.reject, args.callback); } @@ -959,10 +957,10 @@ Contract.prototype._executeMethod = function _executeMethod(){ // make sure receipt logs are decoded var extraFormatters = { receiptFormatter: function (receipt) { - if (_.isArray(receipt.logs)) { + if (Array.isArray(receipt.logs)) { // decode logs - var events = _.map(receipt.logs, function(log) { + var events = receipt.logs.map((log) => { return _this._parent._decodeEventABI.call({ name: 'ALLEVENTS', jsonInterface: _this._parent.options.jsonInterface diff --git a/packages/web3-eth-ens/package.json b/packages/web3-eth-ens/package.json index 52898af3403..fe8794d9885 100644 --- a/packages/web3-eth-ens/package.json +++ b/packages/web3-eth-ens/package.json @@ -16,7 +16,6 @@ "dependencies": { "content-hash": "^2.5.2", "eth-ens-namehash": "2.0.8", - "underscore": "1.12.1", "web3-core": "1.4.0", "web3-core-helpers": "1.4.0", "web3-core-promievent": "1.4.0", diff --git a/packages/web3-eth-ens/src/ENS.js b/packages/web3-eth-ens/src/ENS.js index 604df14329d..7223445f92d 100644 --- a/packages/web3-eth-ens/src/ENS.js +++ b/packages/web3-eth-ens/src/ENS.js @@ -20,7 +20,6 @@ "use strict"; -var _ = require('underscore'); var config = require('./config'); var formatters = require('web3-core-helpers').formatters; var utils = require('web3-utils'); @@ -92,7 +91,7 @@ ENS.prototype.supportsInterface = function (name, interfaceId, callback) { return resolver.methods.supportsInterface(interfaceId).call(callback); }).catch(function(error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -452,7 +451,7 @@ ENS.prototype.setContenthash = function (name, hash, txConfig, callback) { } catch(err){ var error = new Error('Could not encode ' + hash + '. See docs for supported hash protocols.'); - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; diff --git a/packages/web3-eth-ens/src/contracts/Registry.js b/packages/web3-eth-ens/src/contracts/Registry.js index d0119e09d75..eee8ac4e468 100644 --- a/packages/web3-eth-ens/src/contracts/Registry.js +++ b/packages/web3-eth-ens/src/contracts/Registry.js @@ -20,7 +20,6 @@ "use strict"; -var _ = require('underscore'); var Contract = require('web3-eth-contract'); var namehash = require('eth-ens-namehash'); var PromiEvent = require('web3-core-promievent'); @@ -84,7 +83,7 @@ Registry.prototype.getOwner = function (name, callback) { this.contract.then(function (contract) { return contract.methods.owner(namehash.hash(name)).call(); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -93,7 +92,7 @@ Registry.prototype.getOwner = function (name, callback) { promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -124,7 +123,7 @@ Registry.prototype.setOwner = function (name, address, txConfig, callback) { this.contract.then(function (contract) { return contract.methods.setOwner(namehash.hash(name), formatters.inputAddressFormatter(address)).send(txConfig); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -133,7 +132,7 @@ Registry.prototype.setOwner = function (name, address, txConfig, callback) { promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -162,7 +161,7 @@ Registry.prototype.getTTL = function (name, callback) { this.contract.then(function (contract) { return contract.methods.ttl(namehash.hash(name)).call(); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -171,7 +170,7 @@ Registry.prototype.getTTL = function (name, callback) { promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -202,7 +201,7 @@ Registry.prototype.setTTL = function (name, ttl, txConfig, callback) { this.contract.then(function (contract) { return contract.methods.setTTL(namehash.hash(name), ttl).send(txConfig); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -211,7 +210,7 @@ Registry.prototype.setTTL = function (name, ttl, txConfig, callback) { promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -251,7 +250,7 @@ Registry.prototype.setSubnodeOwner = function (name, label, address, txConfig, c formatters.inputAddressFormatter(address) ).send(txConfig); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -260,7 +259,7 @@ Registry.prototype.setSubnodeOwner = function (name, label, address, txConfig, c promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -298,7 +297,7 @@ Registry.prototype.setRecord = function (name, owner, resolver, ttl, txConfig, c ttl ).send(txConfig); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -307,7 +306,7 @@ Registry.prototype.setRecord = function (name, owner, resolver, ttl, txConfig, c promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -351,7 +350,7 @@ Registry.prototype.setSubnodeRecord = function (name, label, owner, resolver, tt ttl ).send(txConfig); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -360,7 +359,7 @@ Registry.prototype.setSubnodeRecord = function (name, label, owner, resolver, tt promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -391,7 +390,7 @@ Registry.prototype.setApprovalForAll = function (operator, approved, txConfig, c this.contract.then(function (contract) { return contract.methods.setApprovalForAll(formatters.inputAddressFormatter(operator), approved).send(txConfig); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -400,7 +399,7 @@ Registry.prototype.setApprovalForAll = function (operator, approved, txConfig, c promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -433,7 +432,7 @@ Registry.prototype.isApprovedForAll = function (owner, operator, callback) { formatters.inputAddressFormatter(operator) ).call(); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -442,7 +441,7 @@ Registry.prototype.isApprovedForAll = function (owner, operator, callback) { promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -471,7 +470,7 @@ Registry.prototype.recordExists = function (name, callback) { this.contract.then(function (contract) { return contract.methods.recordExists(namehash.hash(name)).call(); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -480,7 +479,7 @@ Registry.prototype.recordExists = function (name, callback) { promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -531,7 +530,7 @@ Registry.prototype.getResolver = function (name, callback) { var contract = new Contract(RESOLVER_ABI, address); contract.setProvider(self.ens.eth.currentProvider); - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the contract to the first argument to be backward compatible and to have the required consistency callback(contract, contract); @@ -540,7 +539,7 @@ Registry.prototype.getResolver = function (name, callback) { return contract; }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -569,7 +568,7 @@ Registry.prototype.setResolver = function (name, address, txConfig, callback) { this.contract.then(function (contract) { return contract.methods.setResolver(namehash.hash(name), formatters.inputAddressFormatter(address)).send(txConfig); }).then(function (receipt) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the first argument to be backward compatible and to have the required consistency callback(receipt, receipt); @@ -578,7 +577,7 @@ Registry.prototype.setResolver = function (name, address, txConfig, callback) { promiEvent.resolve(receipt); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; diff --git a/packages/web3-eth-ens/src/lib/ResolverMethodHandler.js b/packages/web3-eth-ens/src/lib/ResolverMethodHandler.js index a07d8e89fe4..04e6b703a50 100644 --- a/packages/web3-eth-ens/src/lib/ResolverMethodHandler.js +++ b/packages/web3-eth-ens/src/lib/ResolverMethodHandler.js @@ -23,7 +23,6 @@ var PromiEvent = require('web3-core-promievent'); var namehash = require('eth-ens-namehash'); var errors = require('web3-core-helpers').errors; -var _ = require('underscore'); var interfaceIds = require('../config').interfaceIds; /** @@ -78,7 +77,7 @@ ResolverMethodHandler.prototype.call = function (callback) { await self.parent.checkInterfaceSupport(resolver, self.methodName); self.parent.handleCall(promiEvent, resolver.methods[self.methodName], preparedArguments, outputFormatter, callback); }).catch(function(error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -107,7 +106,7 @@ ResolverMethodHandler.prototype.send = function (sendOptions, callback) { await self.parent.checkInterfaceSupport(resolver, self.methodName); self.parent.handleSend(promiEvent, resolver.methods[self.methodName], preparedArguments, sendOptions, callback); }).catch(function(error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -135,7 +134,7 @@ ResolverMethodHandler.prototype.handleCall = function (promiEvent, method, prepa result = outputFormatter(result); } - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the second argument to be backwards compatible and to have the required consistency callback(result, result); @@ -144,7 +143,7 @@ ResolverMethodHandler.prototype.handleCall = function (promiEvent, method, prepa promiEvent.resolve(result); }).catch(function (error) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; @@ -184,7 +183,7 @@ ResolverMethodHandler.prototype.handleSend = function (promiEvent, method, prepa promiEvent.eventEmitter.emit('receipt', receipt); promiEvent.resolve(receipt); - if (_.isFunction(callback)) { + if (typeof callback === 'function') { // It's required to pass the receipt to the second argument to be backwards compatible and to have the required consistency callback(receipt, receipt); } @@ -192,7 +191,7 @@ ResolverMethodHandler.prototype.handleSend = function (promiEvent, method, prepa .on('error', function (error) { promiEvent.eventEmitter.emit('error', error); - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, null); return; diff --git a/packages/web3-eth/package.json b/packages/web3-eth/package.json index 1e0c3da17c8..6f3c5942504 100644 --- a/packages/web3-eth/package.json +++ b/packages/web3-eth/package.json @@ -14,7 +14,6 @@ }, "main": "lib/index.js", "dependencies": { - "underscore": "1.12.1", "web3-core": "1.4.0", "web3-core-helpers": "1.4.0", "web3-core-method": "1.4.0", diff --git a/packages/web3-eth/src/getNetworkType.js b/packages/web3-eth/src/getNetworkType.js index 3be3a2027e6..5f9b9b31e57 100644 --- a/packages/web3-eth/src/getNetworkType.js +++ b/packages/web3-eth/src/getNetworkType.js @@ -22,8 +22,6 @@ "use strict"; -var _ = require('underscore'); - var getNetworkType = function (callback) { var _this = this, id; @@ -64,14 +62,14 @@ var getNetworkType = function (callback) { returnValue = 'kovan'; } - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(null, returnValue); } return returnValue; }) .catch(function (err) { - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(err); } else { throw err; diff --git a/packages/web3-eth/src/index.js b/packages/web3-eth/src/index.js index 0bd9f42fe81..33c9a93e20a 100644 --- a/packages/web3-eth/src/index.js +++ b/packages/web3-eth/src/index.js @@ -22,7 +22,6 @@ "use strict"; -var _ = require('underscore'); var core = require('web3-core'); var helpers = require('web3-core-helpers'); var Subscriptions = require('web3-core-subscriptions').subscriptions; @@ -42,23 +41,23 @@ var formatter = helpers.formatters; var blockCall = function (args) { - return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber"; + return (typeof args[0] === 'string' && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber"; }; var transactionFromBlockCall = function (args) { - return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex'; + return (typeof args[0] === 'string' && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex'; }; var uncleCall = function (args) { - return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex'; + return (typeof args[0] === 'string' && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex'; }; var getBlockTransactionCountCall = function (args) { - return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getBlockTransactionCountByHash' : 'eth_getBlockTransactionCountByNumber'; + return (typeof args[0] === 'string' && args[0].indexOf('0x') === 0) ? 'eth_getBlockTransactionCountByHash' : 'eth_getBlockTransactionCountByNumber'; }; var uncleCountCall = function (args) { - return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber'; + return (typeof args[0] === 'string' && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber'; }; @@ -588,7 +587,7 @@ var Eth = function Eth() { this.emit('data', output); } - if (_.isFunction(this.callback)) { + if (typeof this.callback === 'function') { this.callback(null, output, this); } } @@ -604,14 +603,14 @@ var Eth = function Eth() { this._isSyncing = true; this.emit('changed', _this._isSyncing); - if (_.isFunction(this.callback)) { + if (typeof this.callback === 'function') { this.callback(null, _this._isSyncing, this); } setTimeout(function () { _this.emit('data', output); - if (_.isFunction(_this.callback)) { + if (typeof _this.callback === 'function') { _this.callback(null, output, _this); } }, 0); @@ -619,7 +618,7 @@ var Eth = function Eth() { // fire sync status } else { this.emit('data', output); - if (_.isFunction(_this.callback)) { + if ( typeof _this.callback === 'function') { this.callback(null, output, this); } @@ -630,7 +629,7 @@ var Eth = function Eth() { _this._isSyncing = false; _this.emit('changed', _this._isSyncing); - if (_.isFunction(_this.callback)) { + if ( typeof _this.callback === 'function') { _this.callback(null, _this._isSyncing, _this); } } diff --git a/packages/web3-providers-ipc/package.json b/packages/web3-providers-ipc/package.json index 8fc55494498..b9b5645679f 100644 --- a/packages/web3-providers-ipc/package.json +++ b/packages/web3-providers-ipc/package.json @@ -15,7 +15,6 @@ "main": "lib/index.js", "dependencies": { "oboe": "2.1.5", - "underscore": "1.12.1", "web3-core-helpers": "1.4.0" }, "devDependencies": { diff --git a/packages/web3-providers-ipc/src/index.js b/packages/web3-providers-ipc/src/index.js index e0fcdeefb70..8e3b9c76393 100644 --- a/packages/web3-providers-ipc/src/index.js +++ b/packages/web3-providers-ipc/src/index.js @@ -22,7 +22,6 @@ "use strict"; -var _ = require('underscore'); var errors = require('web3-core-helpers').errors; var oboe = require('oboe'); @@ -45,7 +44,7 @@ var IpcProvider = function IpcProvider(path, net) { var id = null; // get the id which matches the returned id - if(_.isArray(result)) { + if(Array.isArray(result)) { result.forEach(function(load){ if(_this.responseCallbacks[load.id]) id = load.id; @@ -57,7 +56,7 @@ var IpcProvider = function IpcProvider(path, net) { // notification if(!id && result.method.indexOf('_subscription') !== -1) { _this.notificationCallbacks.forEach(function(callback){ - if(_.isFunction(callback)) + if(typeof callback === 'function') callback(result); }); diff --git a/packages/web3-providers-ws/package.json b/packages/web3-providers-ws/package.json index 5795b961820..bb03d30c064 100644 --- a/packages/web3-providers-ws/package.json +++ b/packages/web3-providers-ws/package.json @@ -15,7 +15,6 @@ "main": "lib/index.js", "dependencies": { "eventemitter3": "4.0.4", - "underscore": "1.12.1", "web3-core-helpers": "1.4.0", "websocket": "^1.0.32" }, diff --git a/packages/web3-utils/package.json b/packages/web3-utils/package.json index 97298143f50..28b3495830f 100644 --- a/packages/web3-utils/package.json +++ b/packages/web3-utils/package.json @@ -20,7 +20,6 @@ "ethjs-unit": "0.1.6", "number-to-bn": "1.7.0", "randombytes": "^2.1.0", - "underscore": "1.12.1", "utf8": "3.0.0" }, "devDependencies": { diff --git a/packages/web3-utils/src/index.js b/packages/web3-utils/src/index.js index 215a83f78ac..1eec4b76c3d 100644 --- a/packages/web3-utils/src/index.js +++ b/packages/web3-utils/src/index.js @@ -21,8 +21,6 @@ * @date 2017 */ - -var _ = require('underscore'); var ethjsUnit = require('ethjs-unit'); var utils = require('./utils.js'); var soliditySha3 = require('./soliditySha3.js'); @@ -45,28 +43,28 @@ var _fireError = function (error, emitter, reject, callback, optionalData) { /*jshint maxcomplexity: 10 */ // add data if given - if(_.isObject(error) && !(error instanceof Error) && error.data) { - if(_.isObject(error.data) || _.isArray(error.data)) { + if(!!error && typeof error === 'object' && !(error instanceof Error) && error.data) { + if(!!error.data && typeof error.data === 'object' || Array.isArray(error.data)) { error.data = JSON.stringify(error.data, null, 2); } error = error.message +"\n"+ error.data; } - if(_.isString(error)) { + if(typeof error === 'string') { error = new Error(error); } - if (_.isFunction(callback)) { + if (typeof callback === 'function') { callback(error, optionalData); } - if (_.isFunction(reject)) { + if (typeof reject === 'function') { // suppress uncatched error if an error listener is present // OR suppress uncatched error if an callback listener is present if ( emitter && - (_.isFunction(emitter.listeners) && - emitter.listeners('error').length) || _.isFunction(callback) + (typeof emitter.listeners === 'function' && + emitter.listeners('error').length) || typeof callback === 'function' ) { emitter.catch(function(){}); } @@ -76,7 +74,7 @@ var _fireError = function (error, emitter, reject, callback, optionalData) { }, 1); } - if(emitter && _.isFunction(emitter.emit)) { + if(emitter && typeof emitter.emit === 'function') { // emit later, to be able to return emitter setTimeout(function () { emitter.emit('error', error, optionalData); @@ -95,7 +93,7 @@ var _fireError = function (error, emitter, reject, callback, optionalData) { * @return {String} full function/event name */ var _jsonInterfaceMethodToString = function (json) { - if (_.isObject(json) && json.name && json.name.indexOf('(') !== -1) { + if (!!json && typeof json === 'object' && json.name && json.name.indexOf('(') !== -1) { return json.name; } @@ -126,7 +124,7 @@ var _flattenTypes = function(includeTuple, puts) if (arrayBracket >= 0) { suffix = param.type.substring(arrayBracket); } var result = _flattenTypes(includeTuple, param.components); // console.log("result should have things: " + result) - if(_.isArray(result) && includeTuple) { + if(Array.isArray(result) && includeTuple) { // console.log("include tuple word, and its an array. joining...: " + result.types) types.push('tuple(' + result.join(',') + ')' + suffix); } @@ -244,7 +242,7 @@ var getUnitValue = function (unit) { var fromWei = function(number, unit) { unit = getUnitValue(unit); - if(!utils.isBN(number) && !_.isString(number)) { + if(!utils.isBN(number) && !(typeof number === 'string')) { throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.'); } @@ -276,7 +274,7 @@ var fromWei = function(number, unit) { var toWei = function(number, unit) { unit = getUnitValue(unit); - if(!utils.isBN(number) && !_.isString(number)) { + if(!utils.isBN(number) && !(typeof number === 'string')) { throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.'); } @@ -379,7 +377,6 @@ module.exports = { // extractDisplayName: extractDisplayName, // extractTypeName: extractTypeName, randomHex: randomHex, - _: _, BN: utils.BN, isBN: utils.isBN, isBigNumber: utils.isBigNumber, diff --git a/packages/web3-utils/src/soliditySha3.js b/packages/web3-utils/src/soliditySha3.js index 055f3e30f9e..3366180c446 100644 --- a/packages/web3-utils/src/soliditySha3.js +++ b/packages/web3-utils/src/soliditySha3.js @@ -20,7 +20,6 @@ * @date 2017 */ -var _ = require('underscore'); var BN = require('bn.js'); var utils = require('./utils.js'); @@ -172,7 +171,7 @@ var _solidityPack = function (type, value, arraySize) { var _processSolidityEncodePackedArgs = function (arg) { /*jshint maxcomplexity:false */ - if(_.isArray(arg)) { + if(Array.isArray(arg)) { throw new Error('Autodetection of array types is not supported.'); } @@ -180,7 +179,7 @@ var _processSolidityEncodePackedArgs = function (arg) { var hexArg, arraySize; // if type is given - if (_.isObject(arg) && (arg.hasOwnProperty('v') || arg.hasOwnProperty('t') || arg.hasOwnProperty('value') || arg.hasOwnProperty('type'))) { + if (!!arg && typeof arg === 'object' && (arg.hasOwnProperty('v') || arg.hasOwnProperty('t') || arg.hasOwnProperty('value') || arg.hasOwnProperty('type'))) { type = arg.hasOwnProperty('t') ? arg.t : arg.type; value = arg.hasOwnProperty('v') ? arg.v : arg.value; @@ -200,7 +199,7 @@ var _processSolidityEncodePackedArgs = function (arg) { } // get the array size - if(_.isArray(value)) { + if(Array.isArray(value)) { arraySize = _parseTypeNArray(type); if(arraySize && value.length !== arraySize) { throw new Error(type +' is not matching the given array '+ JSON.stringify(value)); @@ -210,7 +209,7 @@ var _processSolidityEncodePackedArgs = function (arg) { } - if (_.isArray(value)) { + if (Array.isArray(value)) { hexArg = value.map(function (val) { return _solidityPack(type, val, arraySize).toString('hex').replace('0x',''); }); @@ -233,7 +232,7 @@ var soliditySha3 = function () { var args = Array.prototype.slice.call(arguments); - var hexArgs = _.map(args, _processSolidityEncodePackedArgs); + var hexArgs = args.map(_processSolidityEncodePackedArgs); // console.log(args, hexArgs); // console.log('0x'+ hexArgs.join('')); @@ -248,7 +247,7 @@ var soliditySha3 = function () { * @return {Object} the sha3 */ var soliditySha3Raw = function () { - return utils.sha3Raw('0x'+ _.map(Array.prototype.slice.call(arguments), _processSolidityEncodePackedArgs).join('')); + return utils.sha3Raw('0x'+ Array.prototype.slice.call(arguments).map(_processSolidityEncodePackedArgs).join('')); }; /** @@ -262,7 +261,7 @@ var encodePacked = function () { var args = Array.prototype.slice.call(arguments); - var hexArgs = _.map(args, _processSolidityEncodePackedArgs); + var hexArgs = args.map(_processSolidityEncodePackedArgs); return '0x'+ hexArgs.join('').toLowerCase(); }; diff --git a/packages/web3-utils/src/utils.js b/packages/web3-utils/src/utils.js index 63afa1c7dee..4e71b160f52 100644 --- a/packages/web3-utils/src/utils.js +++ b/packages/web3-utils/src/utils.js @@ -20,7 +20,6 @@ * @date 2017 */ -var _ = require('underscore'); var BN = require('bn.js'); var numberToBN = require('number-to-bn'); var utf8 = require('utf8'); @@ -232,7 +231,7 @@ var hexToNumber = function (value) { return value; } - if (_.isString(value) && !isHexStrict(value)) { + if (typeof value === 'string' && !isHexStrict(value)) { throw new Error('Given value "'+value+'" is not a valid hex string.'); } @@ -249,7 +248,7 @@ var hexToNumber = function (value) { var hexToNumberString = function (value) { if (!value) return value; - if (_.isString(value) && !isHexStrict(value)) { + if (typeof value === 'string' && !isHexStrict(value)) { throw new Error('Given value "'+value+'" is not a valid hex string.'); } @@ -265,7 +264,7 @@ var hexToNumberString = function (value) { * @return {String} */ var numberToHex = function (value) { - if (_.isNull(value) || _.isUndefined(value)) { + if ((value === null || value === undefined)) { return value; } @@ -339,7 +338,7 @@ var toHex = function (value, returnType) { return returnType ? 'address' : '0x'+ value.toLowerCase().replace(/^0x/i,''); } - if (_.isBoolean(value)) { + if (typeof value === 'boolean' ) { return returnType ? 'bool' : value ? '0x01' : '0x00'; } @@ -347,12 +346,12 @@ var toHex = function (value, returnType) { return '0x' + value.toString('hex'); } - if (_.isObject(value) && !isBigNumber(value) && !isBN(value)) { + if (typeof value === 'object' && !!value && !isBigNumber(value) && !isBN(value)) { return returnType ? 'string' : utf8ToHex(JSON.stringify(value)); } // if its a negative number, pass it through numberToHex - if (_.isString(value)) { + if (typeof value === 'string') { if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) { return returnType ? 'int256' : numberToHex(value); } else if(value.indexOf('0x') === 0 || value.indexOf('0X') === 0) { @@ -374,7 +373,7 @@ var toHex = function (value, returnType) { * @returns {Boolean} */ var isHexStrict = function (hex) { - return ((_.isString(hex) || _.isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(hex)); + return ((typeof hex === 'string' || typeof hex === 'number') && /^(-)?0x[0-9a-f]*$/i.test(hex)); }; /** @@ -385,7 +384,7 @@ var isHexStrict = function (hex) { * @returns {Boolean} */ var isHex = function (hex) { - return ((_.isString(hex) || _.isNumber(hex)) && /^(-0x|0x)?[0-9a-f]*$/i.test(hex)); + return ((typeof hex === 'string' || typeof hex === 'number') && /^(-0x|0x)?[0-9a-f]*$/i.test(hex)); }; /** diff --git a/test/abi.decodeParameter.js b/test/abi.decodeParameter.js index e79fab2c927..6c09e2f2ed8 100644 --- a/test/abi.decodeParameter.js +++ b/test/abi.decodeParameter.js @@ -1,4 +1,3 @@ -var _ = require('underscore'); var chai = require('chai'); var assert = chai.assert; var coder = require('../packages/web3-eth-abi'); @@ -349,10 +348,10 @@ describe('lib/solidity/coder', function () { var result = coder.decodeParameters(t.types, t.values); var resultArray = []; - _.each(result, function (res, key) { - if(_.isFinite(key)) - resultArray.push(res); - }); + Object.keys(result).map(key => { + if(isFinite(key)) resultArray.push(result[key]) + }); + diff --git a/test/eth.accounts.signTransaction.js b/test/eth.accounts.signTransaction.js index 2f6fe3cf1f0..d805392702d 100644 --- a/test/eth.accounts.signTransaction.js +++ b/test/eth.accounts.signTransaction.js @@ -2,7 +2,6 @@ var FakeHttpProvider = require('./helpers/FakeIpcProvider'); var Web3 = require('../packages/web3'); var Accounts = require("./../packages/web3-eth-accounts"); var chai = require('chai'); -var _ = require('underscore'); var assert = chai.assert; var common = { @@ -35,7 +34,7 @@ var accessList = [ } ]; -var clone = function (object) { return object ? _.clone(object) : []; }; +var clone = function (object) { return object ? Object.assign({},object) : []; }; var tests = [ { diff --git a/test/helpers/test.method.js b/test/helpers/test.method.js index 3afa2958e5c..25817c679f5 100644 --- a/test/helpers/test.method.js +++ b/test/helpers/test.method.js @@ -1,4 +1,3 @@ -var _ = require('underscore'); var chai = require('chai'); var assert = chai.assert; var FakeIpcProvider = require('./FakeIpcProvider'); @@ -30,7 +29,7 @@ var useLocalWallet = function (test, provider, web3) { var runTests = function (obj, method, tests) { var objName; - if(_.isArray(obj)) { + if(Array.isArray(obj)) { objName = obj.join('.'); } else { objName = obj; @@ -98,7 +97,7 @@ var runTests = function (obj, method, tests) { if(test.error) { if (obj) { - if(_.isArray(obj)) { + if(Array.isArray(obj)) { w3 = web3[obj[0]][obj[1]]; } else { w3 = web3[obj]; @@ -126,7 +125,7 @@ var runTests = function (obj, method, tests) { } else { if (obj) { - if(_.isArray(obj)) { + if(Array.isArray(obj)) { w3 = web3[obj[0]][obj[1]]; } else { w3 = web3[obj]; @@ -204,7 +203,7 @@ var runTests = function (obj, method, tests) { if(test.error) { if (obj) { - if(_.isArray(obj)) { + if(Array.isArray(obj)) { w3 = web3[obj[0]][obj[1]]; } else { w3 = web3[obj]; @@ -233,7 +232,7 @@ var runTests = function (obj, method, tests) { // when if (obj) { - if(_.isArray(obj)) { + if(Array.isArray(obj)) { w3 = web3[obj[0]][obj[1]]; } else { w3 = web3[obj]; diff --git a/test/utils.encodePacked.js b/test/utils.encodePacked.js index a56f13d9609..d7bd208c45b 100644 --- a/test/utils.encodePacked.js +++ b/test/utils.encodePacked.js @@ -1,4 +1,3 @@ -var _ = require('underscore'); var BN = require('bn.js'); var BigNumber = require('bignumber.js'); var chai = require('chai'); @@ -183,7 +182,7 @@ describe('web3.encodePacked', function () { test.values.forEach(function (value) { it('should hash "'+ JSON.stringify(value) +'" into "'+ test.expected +'"', function() { - if(value.error || _.isArray(value)) { + if(value.error || Array.isArray(value)) { assert.throws(utils.encodePacked.bind(null, value)); } else { assert.deepEqual(utils.encodePacked(value), test.expected); diff --git a/test/utils.soliditySha3.js b/test/utils.soliditySha3.js index c40b1eb1fa0..9150eceeae8 100644 --- a/test/utils.soliditySha3.js +++ b/test/utils.soliditySha3.js @@ -1,4 +1,3 @@ -var _ = require('underscore'); var BN = require('bn.js'); var BigNumber = require('bignumber.js'); var chai = require('chai'); @@ -183,7 +182,7 @@ describe('web3.soliditySha3', function () { test.values.forEach(function (value) { it('should hash "'+ JSON.stringify(value) +'" into "'+ test.expected +'"', function() { - if(value.error || _.isArray(value)) { + if(value.error || Array.isArray(value)) { assert.throws(utils.soliditySha3.bind(null, value)); } else { assert.deepEqual(utils.soliditySha3(value), test.expected);