Skip to content

Commit

Permalink
remove underscore (#4069)
Browse files Browse the repository at this point in the history
* removed some of the underscore methods in web3-core-method

* removed underscore from bzz

* adding subscriptions

* fixing up test cases

* changing variable names

* removed underscore from formatters.js

* removed underscore from request manager and abi

* removing underscore in the rest of the web3 packages

* fixing exports

* fixing failing testcases

* removing underscore from tests

* addressing feedback

* removing unwanted code from transaction

* removing underscore from remaining packages

* updating change log

* addressing feedback

* adding strict equality

* efficient short circuiting

* fixing test case
  • Loading branch information
Alex authored Jul 9, 2021
1 parent 3fa960d commit 9b7540d
Show file tree
Hide file tree
Showing 37 changed files with 173 additions and 204 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions packages/web3-bzz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions packages/web3-bzz/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

"use strict";

var _ = require('underscore');
var swarm = require("swarm-js");


Expand Down Expand Up @@ -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
}
Expand All @@ -61,7 +60,7 @@ Bzz.prototype.setProvider = function(provider) {
// }


if(_.isString(provider)) {
if(typeof provider === 'string') {
this.currentProvider = provider;
} else {
this.currentProvider = null;
Expand Down
1 change: 0 additions & 1 deletion packages/web3-core-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
},
"main": "lib/index.js",
"dependencies": {
"underscore": "1.12.1",
"web3-eth-iban": "1.4.0",
"web3-utils": "1.4.0"
},
Expand Down
19 changes: 9 additions & 10 deletions packages/web3-core-helpers/src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

"use strict";

var _ = require('underscore');
var utils = require('web3-utils');
var Iban = require('web3-eth-iban');

Expand Down Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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!');
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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] : [];
}

Expand Down
1 change: 0 additions & 1 deletion packages/web3-core-method/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
47 changes: 24 additions & 23 deletions packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
};

/**
Expand All @@ -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!
}
};
Expand Down Expand Up @@ -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;
});
Expand All @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -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()
});
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
};
Expand All @@ -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) {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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';
};

/**
Expand Down
1 change: 0 additions & 1 deletion packages/web3-core-requestmanager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions packages/web3-core-requestmanager/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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));
}

Expand Down
1 change: 0 additions & 1 deletion packages/web3-core-subscriptions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"main": "lib/index.js",
"dependencies": {
"eventemitter3": "4.0.4",
"underscore": "1.12.1",
"web3-core-helpers": "1.4.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 9b7540d

Please sign in to comment.