Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file getNetworkType.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @date 2017
*/
"use strict";
var _ = require('underscore');
var getNetworkType = function (callback) {
var _this = this, id;
return this.net.getId()
.then(function (givenId) {
id = givenId;
return _this.getBlock(0);
})
.then(function (genesis) {
var returnValue = 'private';
if (genesis.hash === '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3' &&
id === 1) {
returnValue = 'main';
}
if (genesis.hash === '0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303' &&
id === 2) {
returnValue = 'morden';
}
if (genesis.hash === '0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d' &&
id === 3) {
returnValue = 'ropsten';
}
if (genesis.hash === '0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177' &&
id === 4) {
returnValue = 'rinkeby';
}
if (genesis.hash === '0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a' &&
id === 5) {
returnValue = 'goerli';
}
if (genesis.hash === '0xa3c565fc15c7478862d50ccd6561e3c06b24cc509bf388941c25ea985ce32cb9' &&
id === 42) {
returnValue = 'kovan';
}
if (_.isFunction(callback)) {
callback(null, returnValue);
}
return returnValue;
})
.catch(function (err) {
if (_.isFunction(callback)) {
callback(err);
}
else {
throw err;
}
});
};
module.exports = getNetworkType;
+591
View File
@@ -0,0 +1,591 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file index.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @date 2017
*/
"use strict";
var _ = require('underscore');
var core = require('web3-core');
var helpers = require('web3-core-helpers');
var Subscriptions = require('web3-core-subscriptions').subscriptions;
var Method = require('web3-core-method');
var utils = require('web3-utils');
var Net = require('web3-net');
var ENS = require('web3-eth-ens');
var Personal = require('web3-eth-personal');
var BaseContract = require('web3-eth-contract');
var Iban = require('web3-eth-iban');
var Accounts = require('web3-eth-accounts');
var abi = require('web3-eth-abi');
var getNetworkType = require('./getNetworkType.js');
var formatter = helpers.formatters;
var blockCall = function (args) {
return (_.isString(args[0]) && 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';
};
var uncleCall = function (args) {
return (_.isString(args[0]) && 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';
};
var uncleCountCall = function (args) {
return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber';
};
var Eth = function Eth() {
var _this = this;
// sets _requestmanager
core.packageInit(this, arguments);
// overwrite package setRequestManager
var setRequestManager = this.setRequestManager;
this.setRequestManager = function (manager) {
setRequestManager(manager);
_this.net.setRequestManager(manager);
_this.personal.setRequestManager(manager);
_this.accounts.setRequestManager(manager);
_this.Contract._requestManager = _this._requestManager;
_this.Contract.currentProvider = _this._provider;
return true;
};
// overwrite setProvider
var setProvider = this.setProvider;
this.setProvider = function () {
setProvider.apply(_this, arguments);
_this.setRequestManager(_this._requestManager);
// Set detectedAddress/lastSyncCheck back to null because the provider could be connected to a different chain now
_this.ens._detectedAddress = null;
_this.ens._lastSyncCheck = null;
};
var handleRevert = false;
var defaultAccount = null;
var defaultBlock = 'latest';
var transactionBlockTimeout = 50;
var transactionConfirmationBlocks = 24;
var transactionPollingTimeout = 750;
var maxListenersWarningThreshold = 100;
var defaultChain, defaultHardfork, defaultCommon;
Object.defineProperty(this, 'handleRevert', {
get: function () {
return handleRevert;
},
set: function (val) {
handleRevert = val;
// also set on the Contract object
_this.Contract.handleRevert = handleRevert;
// update handleRevert
methods.forEach(function (method) {
method.handleRevert = handleRevert;
});
},
enumerable: true
});
Object.defineProperty(this, 'defaultCommon', {
get: function () {
return defaultCommon;
},
set: function (val) {
defaultCommon = val;
// also set on the Contract object
_this.Contract.defaultCommon = defaultCommon;
// update defaultBlock
methods.forEach(function (method) {
method.defaultCommon = defaultCommon;
});
},
enumerable: true
});
Object.defineProperty(this, 'defaultHardfork', {
get: function () {
return defaultHardfork;
},
set: function (val) {
defaultHardfork = val;
// also set on the Contract object
_this.Contract.defaultHardfork = defaultHardfork;
// update defaultBlock
methods.forEach(function (method) {
method.defaultHardfork = defaultHardfork;
});
},
enumerable: true
});
Object.defineProperty(this, 'defaultChain', {
get: function () {
return defaultChain;
},
set: function (val) {
defaultChain = val;
// also set on the Contract object
_this.Contract.defaultChain = defaultChain;
// update defaultBlock
methods.forEach(function (method) {
method.defaultChain = defaultChain;
});
},
enumerable: true
});
Object.defineProperty(this, 'transactionPollingTimeout', {
get: function () {
return transactionPollingTimeout;
},
set: function (val) {
transactionPollingTimeout = val;
// also set on the Contract object
_this.Contract.transactionPollingTimeout = transactionPollingTimeout;
// update defaultBlock
methods.forEach(function (method) {
method.transactionPollingTimeout = transactionPollingTimeout;
});
},
enumerable: true
});
Object.defineProperty(this, 'transactionConfirmationBlocks', {
get: function () {
return transactionConfirmationBlocks;
},
set: function (val) {
transactionConfirmationBlocks = val;
// also set on the Contract object
_this.Contract.transactionConfirmationBlocks = transactionConfirmationBlocks;
// update defaultBlock
methods.forEach(function (method) {
method.transactionConfirmationBlocks = transactionConfirmationBlocks;
});
},
enumerable: true
});
Object.defineProperty(this, 'transactionBlockTimeout', {
get: function () {
return transactionBlockTimeout;
},
set: function (val) {
transactionBlockTimeout = val;
// also set on the Contract object
_this.Contract.transactionBlockTimeout = transactionBlockTimeout;
// update defaultBlock
methods.forEach(function (method) {
method.transactionBlockTimeout = transactionBlockTimeout;
});
},
enumerable: true
});
Object.defineProperty(this, 'defaultAccount', {
get: function () {
return defaultAccount;
},
set: function (val) {
if (val) {
defaultAccount = utils.toChecksumAddress(formatter.inputAddressFormatter(val));
}
// also set on the Contract object
_this.Contract.defaultAccount = defaultAccount;
_this.personal.defaultAccount = defaultAccount;
// update defaultBlock
methods.forEach(function (method) {
method.defaultAccount = defaultAccount;
});
return val;
},
enumerable: true
});
Object.defineProperty(this, 'defaultBlock', {
get: function () {
return defaultBlock;
},
set: function (val) {
defaultBlock = val;
// also set on the Contract object
_this.Contract.defaultBlock = defaultBlock;
_this.personal.defaultBlock = defaultBlock;
// update defaultBlock
methods.forEach(function (method) {
method.defaultBlock = defaultBlock;
});
return val;
},
enumerable: true
});
Object.defineProperty(this, 'maxListenersWarningThreshold', {
get: function () {
return maxListenersWarningThreshold;
},
set: function (val) {
if (_this.currentProvider && _this.currentProvider.setMaxListeners) {
maxListenersWarningThreshold = val;
_this.currentProvider.setMaxListeners(val);
}
},
enumerable: true
});
this.clearSubscriptions = _this._requestManager.clearSubscriptions.bind(_this._requestManager);
// add net
this.net = new Net(this);
// add chain detection
this.net.getNetworkType = getNetworkType.bind(this);
// add accounts
this.accounts = new Accounts(this);
// add personal
this.personal = new Personal(this);
this.personal.defaultAccount = this.defaultAccount;
// set warnings threshold
this.maxListenersWarningThreshold = maxListenersWarningThreshold;
// create a proxy Contract type for this instance, as a Contract's provider
// is stored as a class member rather than an instance variable. If we do
// not create this proxy type, changing the provider in one instance of
// web3-eth would subsequently change the provider for _all_ contract
// instances!
var self = this;
var Contract = function Contract() {
BaseContract.apply(this, arguments);
// when Eth.setProvider is called, call packageInit
// on all contract instances instantiated via this Eth
// instances. This will update the currentProvider for
// the contract instances
var _this = this;
var setProvider = self.setProvider;
self.setProvider = function () {
setProvider.apply(self, arguments);
core.packageInit(_this, [self]);
};
};
Contract.setProvider = function () {
BaseContract.setProvider.apply(this, arguments);
};
// make our proxy Contract inherit from web3-eth-contract so that it has all
// the right functionality and so that instanceof and friends work properly
Contract.prototype = Object.create(BaseContract.prototype);
Contract.prototype.constructor = Contract;
// add contract
this.Contract = Contract;
this.Contract.defaultAccount = this.defaultAccount;
this.Contract.defaultBlock = this.defaultBlock;
this.Contract.transactionBlockTimeout = this.transactionBlockTimeout;
this.Contract.transactionConfirmationBlocks = this.transactionConfirmationBlocks;
this.Contract.transactionPollingTimeout = this.transactionPollingTimeout;
this.Contract.handleRevert = this.handleRevert;
this.Contract._requestManager = this._requestManager;
this.Contract._ethAccounts = this.accounts;
this.Contract.currentProvider = this._requestManager.provider;
// add IBAN
this.Iban = Iban;
// add ABI
this.abi = abi;
// add ENS
this.ens = new ENS(this);
var methods = [
new Method({
name: 'getNodeInfo',
call: 'web3_clientVersion'
}),
new Method({
name: 'getProtocolVersion',
call: 'eth_protocolVersion',
params: 0
}),
new Method({
name: 'getCoinbase',
call: 'eth_coinbase',
params: 0
}),
new Method({
name: 'isMining',
call: 'eth_mining',
params: 0
}),
new Method({
name: 'getHashrate',
call: 'eth_hashrate',
params: 0,
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'isSyncing',
call: 'eth_syncing',
params: 0,
outputFormatter: formatter.outputSyncingFormatter
}),
new Method({
name: 'getGasPrice',
call: 'eth_gasPrice',
params: 0,
outputFormatter: formatter.outputBigNumberFormatter
}),
new Method({
name: 'getAccounts',
call: 'eth_accounts',
params: 0,
outputFormatter: utils.toChecksumAddress
}),
new Method({
name: 'getBlockNumber',
call: 'eth_blockNumber',
params: 0,
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'getBalance',
call: 'eth_getBalance',
params: 2,
inputFormatter: [formatter.inputAddressFormatter, formatter.inputDefaultBlockNumberFormatter],
outputFormatter: formatter.outputBigNumberFormatter
}),
new Method({
name: 'getStorageAt',
call: 'eth_getStorageAt',
params: 3,
inputFormatter: [formatter.inputAddressFormatter, utils.numberToHex, formatter.inputDefaultBlockNumberFormatter]
}),
new Method({
name: 'getCode',
call: 'eth_getCode',
params: 2,
inputFormatter: [formatter.inputAddressFormatter, formatter.inputDefaultBlockNumberFormatter]
}),
new Method({
name: 'getBlock',
call: blockCall,
params: 2,
inputFormatter: [formatter.inputBlockNumberFormatter, function (val) { return !!val; }],
outputFormatter: formatter.outputBlockFormatter
}),
new Method({
name: 'getUncle',
call: uncleCall,
params: 2,
inputFormatter: [formatter.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatter.outputBlockFormatter,
}),
new Method({
name: 'getBlockTransactionCount',
call: getBlockTransactionCountCall,
params: 1,
inputFormatter: [formatter.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'getBlockUncleCount',
call: uncleCountCall,
params: 1,
inputFormatter: [formatter.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'getTransaction',
call: 'eth_getTransactionByHash',
params: 1,
inputFormatter: [null],
outputFormatter: formatter.outputTransactionFormatter
}),
new Method({
name: 'getTransactionFromBlock',
call: transactionFromBlockCall,
params: 2,
inputFormatter: [formatter.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatter.outputTransactionFormatter
}),
new Method({
name: 'getTransactionReceipt',
call: 'eth_getTransactionReceipt',
params: 1,
inputFormatter: [null],
outputFormatter: formatter.outputTransactionReceiptFormatter
}),
new Method({
name: 'getTransactionCount',
call: 'eth_getTransactionCount',
params: 2,
inputFormatter: [formatter.inputAddressFormatter, formatter.inputDefaultBlockNumberFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'sendSignedTransaction',
call: 'eth_sendRawTransaction',
params: 1,
inputFormatter: [null],
abiCoder: abi
}),
new Method({
name: 'signTransaction',
call: 'eth_signTransaction',
params: 1,
inputFormatter: [formatter.inputTransactionFormatter]
}),
new Method({
name: 'sendTransaction',
call: 'eth_sendTransaction',
params: 1,
inputFormatter: [formatter.inputTransactionFormatter],
abiCoder: abi
}),
new Method({
name: 'sign',
call: 'eth_sign',
params: 2,
inputFormatter: [formatter.inputSignFormatter, formatter.inputAddressFormatter],
transformPayload: function (payload) {
payload.params.reverse();
return payload;
}
}),
new Method({
name: 'call',
call: 'eth_call',
params: 2,
inputFormatter: [formatter.inputCallFormatter, formatter.inputDefaultBlockNumberFormatter],
abiCoder: abi
}),
new Method({
name: 'estimateGas',
call: 'eth_estimateGas',
params: 1,
inputFormatter: [formatter.inputCallFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'submitWork',
call: 'eth_submitWork',
params: 3
}),
new Method({
name: 'getWork',
call: 'eth_getWork',
params: 0
}),
new Method({
name: 'getPastLogs',
call: 'eth_getLogs',
params: 1,
inputFormatter: [formatter.inputLogFormatter],
outputFormatter: formatter.outputLogFormatter
}),
new Method({
name: 'getChainId',
call: 'eth_chainId',
params: 0,
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'requestAccounts',
call: 'eth_requestAccounts',
params: 0,
outputFormatter: utils.toChecksumAddress
}),
new Method({
name: 'getProof',
call: 'eth_getProof',
params: 3,
inputFormatter: [formatter.inputAddressFormatter, formatter.inputStorageKeysFormatter, formatter.inputDefaultBlockNumberFormatter],
outputFormatter: formatter.outputProofFormatter
}),
new Method({
name: 'getPendingTransactions',
call: 'eth_pendingTransactions',
params: 0,
outputFormatter: formatter.outputTransactionFormatter
}),
// subscriptions
new Subscriptions({
name: 'subscribe',
type: 'eth',
subscriptions: {
'newBlockHeaders': {
// TODO rename on RPC side?
subscriptionName: 'newHeads',
params: 0,
outputFormatter: formatter.outputBlockFormatter
},
'pendingTransactions': {
subscriptionName: 'newPendingTransactions',
params: 0
},
'logs': {
params: 1,
inputFormatter: [formatter.inputLogFormatter],
outputFormatter: formatter.outputLogFormatter,
// DUBLICATE, also in web3-eth-contract
subscriptionHandler: function (output) {
if (output.removed) {
this.emit('changed', output);
}
else {
this.emit('data', output);
}
if (_.isFunction(this.callback)) {
this.callback(null, output, this);
}
}
},
'syncing': {
params: 0,
outputFormatter: formatter.outputSyncingFormatter,
subscriptionHandler: function (output) {
var _this = this;
// fire TRUE at start
if (this._isSyncing !== true) {
this._isSyncing = true;
this.emit('changed', _this._isSyncing);
if (_.isFunction(this.callback)) {
this.callback(null, _this._isSyncing, this);
}
setTimeout(function () {
_this.emit('data', output);
if (_.isFunction(_this.callback)) {
_this.callback(null, output, _this);
}
}, 0);
// fire sync status
}
else {
this.emit('data', output);
if (_.isFunction(_this.callback)) {
this.callback(null, output, this);
}
// wait for some time before fireing the FALSE
clearTimeout(this._isSyncingTimeout);
this._isSyncingTimeout = setTimeout(function () {
if (output.currentBlock > output.highestBlock - 200) {
_this._isSyncing = false;
_this.emit('changed', _this._isSyncing);
if (_.isFunction(_this.callback)) {
_this.callback(null, _this._isSyncing, _this);
}
}
}, 500);
}
}
}
}
})
];
methods.forEach(function (method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager, _this.accounts); // second param is the eth.accounts module (necessary for signing transactions locally)
method.defaultBlock = _this.defaultBlock;
method.defaultAccount = _this.defaultAccount;
method.transactionBlockTimeout = _this.transactionBlockTimeout;
method.transactionConfirmationBlocks = _this.transactionConfirmationBlocks;
method.transactionPollingTimeout = _this.transactionPollingTimeout;
method.handleRevert = _this.handleRevert;
});
};
// Adds the static givenProvider and providers property to the Eth module
core.addProviders(Eth);
module.exports = Eth;
+64
View File
@@ -0,0 +1,64 @@
{
"_from": "web3-eth@1.3.0",
"_id": "web3-eth@1.3.0",
"_inBundle": false,
"_integrity": "sha512-/bzJcxXPM9EM18JM5kO2JjZ3nEqVo3HxqU93aWAEgJNqaP/Lltmufl2GpvIB2Hvj+FXAjAXquxUdQ2/xP7BzHQ==",
"_location": "/web3-eth",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "web3-eth@1.3.0",
"name": "web3-eth",
"escapedName": "web3-eth",
"rawSpec": "1.3.0",
"saveSpec": null,
"fetchSpec": "1.3.0"
},
"_requiredBy": [
"/web3"
],
"_resolved": "https://registry.npmjs.org/web3-eth/-/web3-eth-1.3.0.tgz",
"_shasum": "898e5f5a8827f9bc6844e267a52eb388916a6771",
"_spec": "web3-eth@1.3.0",
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\web3",
"bundleDependencies": false,
"dependencies": {
"underscore": "1.9.1",
"web3-core": "1.3.0",
"web3-core-helpers": "1.3.0",
"web3-core-method": "1.3.0",
"web3-core-subscriptions": "1.3.0",
"web3-eth-abi": "1.3.0",
"web3-eth-accounts": "1.3.0",
"web3-eth-contract": "1.3.0",
"web3-eth-ens": "1.3.0",
"web3-eth-iban": "1.3.0",
"web3-eth-personal": "1.3.0",
"web3-net": "1.3.0",
"web3-utils": "1.3.0"
},
"deprecated": false,
"description": "Web3 module to interact with the Ethereum blockchain and smart contracts.",
"devDependencies": {
"dtslint": "^3.4.1",
"typescript": "^3.9.5"
},
"engines": {
"node": ">=8.0.0"
},
"gitHead": "a88944fff1f62b2b6f00e3de52446d24d9e92cc6",
"license": "LGPL-3.0",
"main": "lib/index.js",
"name": "web3-eth",
"repository": {
"type": "git",
"url": "https://github.com/ethereum/web3.js/tree/1.x/packages/web3-eth"
},
"scripts": {
"dtslint": "dtslint --localTs ../../node_modules/typescript/lib types",
"tsc": "tsc -b tsconfig.json"
},
"types": "types/index.d.ts",
"version": "1.3.0"
}
+82
View File
@@ -0,0 +1,82 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file getNetworkType.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @date 2017
*/
"use strict";
var _ = require('underscore');
var getNetworkType = function (callback) {
var _this = this,
id;
return this.net.getId()
.then(function (givenId) {
id = givenId;
return _this.getBlock(0);
})
.then(function (genesis) {
var returnValue = 'private';
if (genesis.hash === '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3' &&
id === 1) {
returnValue = 'main';
}
if (genesis.hash === '0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303' &&
id === 2) {
returnValue = 'morden';
}
if (genesis.hash === '0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d' &&
id === 3) {
returnValue = 'ropsten';
}
if (genesis.hash === '0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177' &&
id === 4) {
returnValue = 'rinkeby';
}
if (genesis.hash === '0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a' &&
id === 5) {
returnValue = 'goerli';
}
if (genesis.hash === '0xa3c565fc15c7478862d50ccd6561e3c06b24cc509bf388941c25ea985ce32cb9' &&
id === 42) {
returnValue = 'kovan';
}
if (_.isFunction(callback)) {
callback(null, returnValue);
}
return returnValue;
})
.catch(function (err) {
if (_.isFunction(callback)) {
callback(err);
} else {
throw err;
}
});
};
module.exports = getNetworkType;
+661
View File
@@ -0,0 +1,661 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file index.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @date 2017
*/
"use strict";
var _ = require('underscore');
var core = require('web3-core');
var helpers = require('web3-core-helpers');
var Subscriptions = require('web3-core-subscriptions').subscriptions;
var Method = require('web3-core-method');
var utils = require('web3-utils');
var Net = require('web3-net');
var ENS = require('web3-eth-ens');
var Personal = require('web3-eth-personal');
var BaseContract = require('web3-eth-contract');
var Iban = require('web3-eth-iban');
var Accounts = require('web3-eth-accounts');
var abi = require('web3-eth-abi');
var getNetworkType = require('./getNetworkType.js');
var formatter = helpers.formatters;
var blockCall = function (args) {
return (_.isString(args[0]) && 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';
};
var uncleCall = function (args) {
return (_.isString(args[0]) && 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';
};
var uncleCountCall = function (args) {
return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber';
};
var Eth = function Eth() {
var _this = this;
// sets _requestmanager
core.packageInit(this, arguments);
// overwrite package setRequestManager
var setRequestManager = this.setRequestManager;
this.setRequestManager = function (manager) {
setRequestManager(manager);
_this.net.setRequestManager(manager);
_this.personal.setRequestManager(manager);
_this.accounts.setRequestManager(manager);
_this.Contract._requestManager = _this._requestManager;
_this.Contract.currentProvider = _this._provider;
return true;
};
// overwrite setProvider
var setProvider = this.setProvider;
this.setProvider = function () {
setProvider.apply(_this, arguments);
_this.setRequestManager(_this._requestManager);
// Set detectedAddress/lastSyncCheck back to null because the provider could be connected to a different chain now
_this.ens._detectedAddress = null;
_this.ens._lastSyncCheck = null;
};
var handleRevert = false;
var defaultAccount = null;
var defaultBlock = 'latest';
var transactionBlockTimeout = 50;
var transactionConfirmationBlocks = 24;
var transactionPollingTimeout = 750;
var maxListenersWarningThreshold = 100;
var defaultChain, defaultHardfork, defaultCommon;
Object.defineProperty(this, 'handleRevert', {
get: function () {
return handleRevert;
},
set: function (val) {
handleRevert = val;
// also set on the Contract object
_this.Contract.handleRevert = handleRevert;
// update handleRevert
methods.forEach(function(method) {
method.handleRevert = handleRevert;
});
},
enumerable: true
});
Object.defineProperty(this, 'defaultCommon', {
get: function () {
return defaultCommon;
},
set: function (val) {
defaultCommon = val;
// also set on the Contract object
_this.Contract.defaultCommon = defaultCommon;
// update defaultBlock
methods.forEach(function(method) {
method.defaultCommon = defaultCommon;
});
},
enumerable: true
});
Object.defineProperty(this, 'defaultHardfork', {
get: function () {
return defaultHardfork;
},
set: function (val) {
defaultHardfork = val;
// also set on the Contract object
_this.Contract.defaultHardfork = defaultHardfork;
// update defaultBlock
methods.forEach(function(method) {
method.defaultHardfork = defaultHardfork;
});
},
enumerable: true
});
Object.defineProperty(this, 'defaultChain', {
get: function () {
return defaultChain;
},
set: function (val) {
defaultChain = val;
// also set on the Contract object
_this.Contract.defaultChain = defaultChain;
// update defaultBlock
methods.forEach(function(method) {
method.defaultChain = defaultChain;
});
},
enumerable: true
});
Object.defineProperty(this, 'transactionPollingTimeout', {
get: function () {
return transactionPollingTimeout;
},
set: function (val) {
transactionPollingTimeout = val;
// also set on the Contract object
_this.Contract.transactionPollingTimeout = transactionPollingTimeout;
// update defaultBlock
methods.forEach(function(method) {
method.transactionPollingTimeout = transactionPollingTimeout;
});
},
enumerable: true
});
Object.defineProperty(this, 'transactionConfirmationBlocks', {
get: function () {
return transactionConfirmationBlocks;
},
set: function (val) {
transactionConfirmationBlocks = val;
// also set on the Contract object
_this.Contract.transactionConfirmationBlocks = transactionConfirmationBlocks;
// update defaultBlock
methods.forEach(function(method) {
method.transactionConfirmationBlocks = transactionConfirmationBlocks;
});
},
enumerable: true
});
Object.defineProperty(this, 'transactionBlockTimeout', {
get: function () {
return transactionBlockTimeout;
},
set: function (val) {
transactionBlockTimeout = val;
// also set on the Contract object
_this.Contract.transactionBlockTimeout = transactionBlockTimeout;
// update defaultBlock
methods.forEach(function(method) {
method.transactionBlockTimeout = transactionBlockTimeout;
});
},
enumerable: true
});
Object.defineProperty(this, 'defaultAccount', {
get: function () {
return defaultAccount;
},
set: function (val) {
if(val) {
defaultAccount = utils.toChecksumAddress(formatter.inputAddressFormatter(val));
}
// also set on the Contract object
_this.Contract.defaultAccount = defaultAccount;
_this.personal.defaultAccount = defaultAccount;
// update defaultBlock
methods.forEach(function(method) {
method.defaultAccount = defaultAccount;
});
return val;
},
enumerable: true
});
Object.defineProperty(this, 'defaultBlock', {
get: function () {
return defaultBlock;
},
set: function (val) {
defaultBlock = val;
// also set on the Contract object
_this.Contract.defaultBlock = defaultBlock;
_this.personal.defaultBlock = defaultBlock;
// update defaultBlock
methods.forEach(function(method) {
method.defaultBlock = defaultBlock;
});
return val;
},
enumerable: true
});
Object.defineProperty(this, 'maxListenersWarningThreshold', {
get: function () {
return maxListenersWarningThreshold;
},
set: function (val) {
if (_this.currentProvider && _this.currentProvider.setMaxListeners){
maxListenersWarningThreshold = val;
_this.currentProvider.setMaxListeners(val);
}
},
enumerable: true
});
this.clearSubscriptions = _this._requestManager.clearSubscriptions.bind(_this._requestManager);
// add net
this.net = new Net(this);
// add chain detection
this.net.getNetworkType = getNetworkType.bind(this);
// add accounts
this.accounts = new Accounts(this);
// add personal
this.personal = new Personal(this);
this.personal.defaultAccount = this.defaultAccount;
// set warnings threshold
this.maxListenersWarningThreshold = maxListenersWarningThreshold;
// create a proxy Contract type for this instance, as a Contract's provider
// is stored as a class member rather than an instance variable. If we do
// not create this proxy type, changing the provider in one instance of
// web3-eth would subsequently change the provider for _all_ contract
// instances!
var self = this;
var Contract = function Contract() {
BaseContract.apply(this, arguments);
// when Eth.setProvider is called, call packageInit
// on all contract instances instantiated via this Eth
// instances. This will update the currentProvider for
// the contract instances
var _this = this;
var setProvider = self.setProvider;
self.setProvider = function() {
setProvider.apply(self, arguments);
core.packageInit(_this, [self]);
};
};
Contract.setProvider = function() {
BaseContract.setProvider.apply(this, arguments);
};
// make our proxy Contract inherit from web3-eth-contract so that it has all
// the right functionality and so that instanceof and friends work properly
Contract.prototype = Object.create(BaseContract.prototype);
Contract.prototype.constructor = Contract;
// add contract
this.Contract = Contract;
this.Contract.defaultAccount = this.defaultAccount;
this.Contract.defaultBlock = this.defaultBlock;
this.Contract.transactionBlockTimeout = this.transactionBlockTimeout;
this.Contract.transactionConfirmationBlocks = this.transactionConfirmationBlocks;
this.Contract.transactionPollingTimeout = this.transactionPollingTimeout;
this.Contract.handleRevert = this.handleRevert;
this.Contract._requestManager = this._requestManager;
this.Contract._ethAccounts = this.accounts;
this.Contract.currentProvider = this._requestManager.provider;
// add IBAN
this.Iban = Iban;
// add ABI
this.abi = abi;
// add ENS
this.ens = new ENS(this);
var methods = [
new Method({
name: 'getNodeInfo',
call: 'web3_clientVersion'
}),
new Method({
name: 'getProtocolVersion',
call: 'eth_protocolVersion',
params: 0
}),
new Method({
name: 'getCoinbase',
call: 'eth_coinbase',
params: 0
}),
new Method({
name: 'isMining',
call: 'eth_mining',
params: 0
}),
new Method({
name: 'getHashrate',
call: 'eth_hashrate',
params: 0,
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'isSyncing',
call: 'eth_syncing',
params: 0,
outputFormatter: formatter.outputSyncingFormatter
}),
new Method({
name: 'getGasPrice',
call: 'eth_gasPrice',
params: 0,
outputFormatter: formatter.outputBigNumberFormatter
}),
new Method({
name: 'getAccounts',
call: 'eth_accounts',
params: 0,
outputFormatter: utils.toChecksumAddress
}),
new Method({
name: 'getBlockNumber',
call: 'eth_blockNumber',
params: 0,
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'getBalance',
call: 'eth_getBalance',
params: 2,
inputFormatter: [formatter.inputAddressFormatter, formatter.inputDefaultBlockNumberFormatter],
outputFormatter: formatter.outputBigNumberFormatter
}),
new Method({
name: 'getStorageAt',
call: 'eth_getStorageAt',
params: 3,
inputFormatter: [formatter.inputAddressFormatter, utils.numberToHex, formatter.inputDefaultBlockNumberFormatter]
}),
new Method({
name: 'getCode',
call: 'eth_getCode',
params: 2,
inputFormatter: [formatter.inputAddressFormatter, formatter.inputDefaultBlockNumberFormatter]
}),
new Method({
name: 'getBlock',
call: blockCall,
params: 2,
inputFormatter: [formatter.inputBlockNumberFormatter, function (val) { return !!val; }],
outputFormatter: formatter.outputBlockFormatter
}),
new Method({
name: 'getUncle',
call: uncleCall,
params: 2,
inputFormatter: [formatter.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatter.outputBlockFormatter,
}),
new Method({
name: 'getBlockTransactionCount',
call: getBlockTransactionCountCall,
params: 1,
inputFormatter: [formatter.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'getBlockUncleCount',
call: uncleCountCall,
params: 1,
inputFormatter: [formatter.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'getTransaction',
call: 'eth_getTransactionByHash',
params: 1,
inputFormatter: [null],
outputFormatter: formatter.outputTransactionFormatter
}),
new Method({
name: 'getTransactionFromBlock',
call: transactionFromBlockCall,
params: 2,
inputFormatter: [formatter.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatter.outputTransactionFormatter
}),
new Method({
name: 'getTransactionReceipt',
call: 'eth_getTransactionReceipt',
params: 1,
inputFormatter: [null],
outputFormatter: formatter.outputTransactionReceiptFormatter
}),
new Method({
name: 'getTransactionCount',
call: 'eth_getTransactionCount',
params: 2,
inputFormatter: [formatter.inputAddressFormatter, formatter.inputDefaultBlockNumberFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'sendSignedTransaction',
call: 'eth_sendRawTransaction',
params: 1,
inputFormatter: [null],
abiCoder: abi
}),
new Method({
name: 'signTransaction',
call: 'eth_signTransaction',
params: 1,
inputFormatter: [formatter.inputTransactionFormatter]
}),
new Method({
name: 'sendTransaction',
call: 'eth_sendTransaction',
params: 1,
inputFormatter: [formatter.inputTransactionFormatter],
abiCoder: abi
}),
new Method({
name: 'sign',
call: 'eth_sign',
params: 2,
inputFormatter: [formatter.inputSignFormatter, formatter.inputAddressFormatter],
transformPayload: function (payload) {
payload.params.reverse();
return payload;
}
}),
new Method({
name: 'call',
call: 'eth_call',
params: 2,
inputFormatter: [formatter.inputCallFormatter, formatter.inputDefaultBlockNumberFormatter],
abiCoder: abi
}),
new Method({
name: 'estimateGas',
call: 'eth_estimateGas',
params: 1,
inputFormatter: [formatter.inputCallFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'submitWork',
call: 'eth_submitWork',
params: 3
}),
new Method({
name: 'getWork',
call: 'eth_getWork',
params: 0
}),
new Method({
name: 'getPastLogs',
call: 'eth_getLogs',
params: 1,
inputFormatter: [formatter.inputLogFormatter],
outputFormatter: formatter.outputLogFormatter
}),
new Method({
name: 'getChainId',
call: 'eth_chainId',
params: 0,
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'requestAccounts',
call: 'eth_requestAccounts',
params: 0,
outputFormatter: utils.toChecksumAddress
}),
new Method({
name: 'getProof',
call: 'eth_getProof',
params: 3,
inputFormatter: [formatter.inputAddressFormatter, formatter.inputStorageKeysFormatter, formatter.inputDefaultBlockNumberFormatter],
outputFormatter: formatter.outputProofFormatter
}),
new Method({
name: 'getPendingTransactions',
call: 'eth_pendingTransactions',
params: 0,
outputFormatter: formatter.outputTransactionFormatter
}),
// subscriptions
new Subscriptions({
name: 'subscribe',
type: 'eth',
subscriptions: {
'newBlockHeaders': {
// TODO rename on RPC side?
subscriptionName: 'newHeads', // replace subscription with this name
params: 0,
outputFormatter: formatter.outputBlockFormatter
},
'pendingTransactions': {
subscriptionName: 'newPendingTransactions', // replace subscription with this name
params: 0
},
'logs': {
params: 1,
inputFormatter: [formatter.inputLogFormatter],
outputFormatter: formatter.outputLogFormatter,
// DUBLICATE, also in web3-eth-contract
subscriptionHandler: function (output) {
if(output.removed) {
this.emit('changed', output);
} else {
this.emit('data', output);
}
if (_.isFunction(this.callback)) {
this.callback(null, output, this);
}
}
},
'syncing': {
params: 0,
outputFormatter: formatter.outputSyncingFormatter,
subscriptionHandler: function (output) {
var _this = this;
// fire TRUE at start
if(this._isSyncing !== true) {
this._isSyncing = true;
this.emit('changed', _this._isSyncing);
if (_.isFunction(this.callback)) {
this.callback(null, _this._isSyncing, this);
}
setTimeout(function () {
_this.emit('data', output);
if (_.isFunction(_this.callback)) {
_this.callback(null, output, _this);
}
}, 0);
// fire sync status
} else {
this.emit('data', output);
if (_.isFunction(_this.callback)) {
this.callback(null, output, this);
}
// wait for some time before fireing the FALSE
clearTimeout(this._isSyncingTimeout);
this._isSyncingTimeout = setTimeout(function () {
if(output.currentBlock > output.highestBlock - 200) {
_this._isSyncing = false;
_this.emit('changed', _this._isSyncing);
if (_.isFunction(_this.callback)) {
_this.callback(null, _this._isSyncing, _this);
}
}
}, 500);
}
}
}
}
})
];
methods.forEach(function(method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager, _this.accounts); // second param is the eth.accounts module (necessary for signing transactions locally)
method.defaultBlock = _this.defaultBlock;
method.defaultAccount = _this.defaultAccount;
method.transactionBlockTimeout = _this.transactionBlockTimeout;
method.transactionConfirmationBlocks = _this.transactionConfirmationBlocks;
method.transactionPollingTimeout = _this.transactionPollingTimeout;
method.handleRevert = _this.handleRevert;
});
};
// Adds the static givenProvider and providers property to the Eth module
core.addProviders(Eth);
module.exports = Eth;
+9
View File
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./lib"
},
"include": [
"./src"
]
}
+17
View File
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"target": "es6",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noEmit": true,
"allowSyntheticDefaultImports": false,
"baseUrl": ".",
"paths": {
"web3-eth": ["."]
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false,
"no-import-default-of-export-equals": false,
"file-name-casing": [true, "kebab-case"],
"whitespace": false,
"no-unnecessary-class": false,
"unified-signatures": false
}
}