This commit is contained in:
Fabian Vogelsteller
2017-08-09 15:50:12 +02:00
parent 4ec489dacb
commit 58aeb89e83
13 changed files with 573 additions and 562 deletions
-65
View File
@@ -1,65 +0,0 @@
/*
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 Marek Kotewicz <marek@parity.io>
* @date 2015
*/
"use strict";
// var ETH_UNITS = [
// 'wei',
// 'kwei',
// 'Mwei',
// 'Gwei',
// 'szabo',
// 'finney',
// 'femtoether',
// 'picoether',
// 'nanoether',
// 'microether',
// 'milliether',
// 'nano',
// 'micro',
// 'milli',
// 'ether',
// 'grand',
// 'Mether',
// 'Gether',
// 'Tether',
// 'Pether',
// 'Eether',
// 'Zether',
// 'Yether',
// 'Nether',
// 'Dether',
// 'Vether',
// 'Uether'
// ];
module.exports = {
// ETH_PADDING: 32,
// ETH_SIGNATURE_LENGTH: 4,
// ETH_UNITS: ETH_UNITS,
// ETH_POLLING_TIMEOUT: 1000/2,
defaultBlock: 'latest',
defaultAccount: null
};
+4 -6
View File
@@ -28,8 +28,6 @@ var _ = require('underscore');
var utils = require('web3-utils');
var Iban = require('web3-eth-iban');
var config = require('./config');
/**
* Should the format output to a big number
*
@@ -46,8 +44,8 @@ var isPredefinedBlockNumber = function (blockNumber) {
};
var inputDefaultBlockNumberFormatter = function (blockNumber) {
if (blockNumber === undefined || blockNumber === null) {
return config.defaultBlock;
if (this && (blockNumber === undefined || blockNumber === null)) {
return this.defaultBlock;
}
if (blockNumber === 'genesis' || blockNumber === 'earliest') {
return '0x0';
@@ -73,7 +71,7 @@ var inputBlockNumberFormatter = function (blockNumber) {
*/
var inputCallFormatter = function (options){
var from = options.from || config.defaultAccount;
var from = options.from || (this ? this.defaultAccount : null);
if (from) {
options.from = inputAddressFormatter(from);
@@ -108,7 +106,7 @@ var inputTransactionFormatter = function (options) {
// check from, only if not number, or object
if (!_.isNumber(options.from) && !_.isObject(options.from)) {
options.from = options.from || config.defaultAccount;
options.from = options.from || (this ? this.defaultAccount : null);
if (!options.from && !_.isNumber(options.from)) {
throw new Error('The send transactions "from" field must be defined!');
+1 -3
View File
@@ -24,11 +24,9 @@
var errors = require('./errors');
var formatters = require('./formatters');
var config = require('./config');
module.exports = {
errors: errors,
formatters: formatters,
config: config
formatters: formatters
};
+7 -1
View File
@@ -47,9 +47,12 @@ var Method = function Method(options) {
this.transformPayload = options.transformPayload;
this.requestManager = options.requestManager;
// reference to eth.accounts
this.accounts = options.accounts;
this.defaultBlock = options.defaultBlock || 'latest';
this.defaultAccount = options.defaultAccount || null;
};
Method.prototype.setRequestManager = function (requestManager, accounts) {
@@ -146,12 +149,15 @@ Method.prototype.validateArgs = function (args) {
* @return {Array}
*/
Method.prototype.formatInput = function (args) {
var _this = this;
if (!this.inputFormatter) {
return args;
}
return this.inputFormatter.map(function (formatter, index) {
return formatter ? formatter(args[index]) : args[index];
// bind this for defaultBlock, and defaultAccount
return formatter ? formatter.call(_this, args[index]) : args[index];
});
};
+43 -4
View File
@@ -76,20 +76,20 @@ var Contract = function Contract(jsonInterface, address, options) {
name: 'estimateGas',
call: 'eth_estimateGas',
params: 1,
inputFormatter: [formatters.inputCallFormatter],
inputFormatter: [formatters.inputCallFormatter.bind(this)],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'call',
call: 'eth_call',
params: 2,
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
inputFormatter: [formatters.inputCallFormatter.bind(this), formatters.inputDefaultBlockNumberFormatter.bind(this)]
}),
new Method({
name: 'sendTransaction',
call: 'eth_sendTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
inputFormatter: [formatters.inputTransactionFormatter.bind(this)]
})
];
// attach methods to this._ethereumCall
@@ -193,6 +193,45 @@ var Contract = function Contract(jsonInterface, address, options) {
enumerable: true
});
// get default account from the Class
var defaultAccount = Contract.defaultAccount;
var defaultBlock = Contract.defaultBlock || 'latest';
Object.defineProperty(this, 'defaultAccount', {
get: function () {
return defaultAccount;
},
set: function (val) {
if(val) {
defaultAccount = utils.toChecksumAddress(formatters.inputAddressFormatter(val));
}
// update defaultBlock
_ethereumCall.forEach(function(method) {
method.defaultAccount = defaultAccount;
});
return val;
},
enumerable: true
});
Object.defineProperty(this, 'defaultBlock', {
get: function () {
return defaultBlock;
},
set: function (val) {
defaultBlock = val;
// update defaultBlock
_ethereumCall.forEach(function(method) {
method.defaultBlock = defaultBlock;
});
return val;
},
enumerable: true
});
// properties
this.methods = {};
this.events = {};
@@ -752,7 +791,7 @@ Contract.prototype._executeMethod = function _executeMethod(){
if(args.generateRequest) {
var payload = {
params: [formatters.inputCallFormatter(args.options), formatters.inputDefaultBlockNumberFormatter(args.defaultBlock)],
params: [formatters.inputCallFormatter.call(this._parent, args.options), formatters.inputDefaultBlockNumberFormatter.call(this._parent, args.defaultBlock)],
callback: args.callback
};
+94 -75
View File
@@ -36,89 +36,108 @@ var Personal = function Personal() {
// sets _requestmanager
core.packageInit(this, arguments);
this.net = new Net(this.currentProvider);
methods().forEach(function(method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager);
var defaultAccount = null;
var defaultBlock = 'latest';
Object.defineProperty(this, 'defaultAccount', {
get: function () {
return defaultAccount;
},
set: function (val) {
if(val) {
defaultAccount = utils.toChecksumAddress(formatters.inputAddressFormatter(val));
}
// 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;
// update defaultBlock
methods.forEach(function(method) {
method.defaultBlock = defaultBlock;
});
return val;
},
enumerable: true
});
this.net = new Net(this.currentProvider);
var methods = [
new Method({
name: 'getAccounts',
call: 'personal_listAccounts',
params: 0,
outputFormatter: utils.toChecksumAddress
}),
new Method({
name: 'newAccount',
call: 'personal_newAccount',
params: 1,
inputFormatter: [null],
outputFormatter: utils.toChecksumAddress
}),
new Method({
name: 'unlockAccount',
call: 'personal_unlockAccount',
params: 3,
inputFormatter: [formatters.inputAddressFormatter, null, null]
}),
new Method({
name: 'lockAccount',
call: 'personal_lockAccount',
params: 1,
inputFormatter: [formatters.inputAddressFormatter]
}),
new Method({
name: 'importRawKey',
call: 'personal_importRawKey',
params: 2
}),
new Method({
name: 'sendTransaction',
call: 'personal_sendTransaction',
params: 2,
inputFormatter: [formatters.inputTransactionFormatter, null]
}),
new Method({
name: 'sign',
call: 'personal_sign',
params: 3,
inputFormatter: [formatters.inputSignFormatter, formatters.inputAddressFormatter, null]
}),
new Method({
name: 'ecRecover',
call: 'personal_ecRecover',
params: 2,
inputFormatter: [formatters.inputSignFormatter, null]
})
];
methods.forEach(function(method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager);
method.defaultBlock = _this.defaultBlock;
method.defaultAccount = _this.defaultAccount;
});
};
core.addProviders(Personal);
var methods = function () {
var getAccounts = new Method({
name: 'getAccounts',
call: 'personal_listAccounts',
params: 0,
outputFormatter: utils.toChecksumAddress
});
var newAccount = new Method({
name: 'newAccount',
call: 'personal_newAccount',
params: 1,
inputFormatter: [null],
outputFormatter: utils.toChecksumAddress
});
var unlockAccount = new Method({
name: 'unlockAccount',
call: 'personal_unlockAccount',
params: 3,
inputFormatter: [formatters.inputAddressFormatter, null, null]
});
var lockAccount = new Method({
name: 'lockAccount',
call: 'personal_lockAccount',
params: 1,
inputFormatter: [formatters.inputAddressFormatter]
});
var importRawKey = new Method({
name: 'importRawKey',
call: 'personal_importRawKey',
params: 2
});
var sendTransaction = new Method({
name: 'sendTransaction',
call: 'personal_sendTransaction',
params: 2,
inputFormatter: [formatters.inputTransactionFormatter, null]
});
var sign = new Method({
name: 'sign',
call: 'personal_sign',
params: 3,
inputFormatter: [formatters.inputSignFormatter, formatters.inputAddressFormatter, null]
});
var ecRecover = new Method({
name: 'ecRecover',
call: 'personal_ecRecover',
params: 2,
inputFormatter: [formatters.inputSignFormatter, null]
});
return [
getAccounts,
newAccount,
unlockAccount,
sendTransaction,
lockAccount,
importRawKey,
sign,
ecRecover
];
};
module.exports = Personal;
+336 -377
View File
@@ -80,6 +80,53 @@ var Eth = function Eth() {
_this.Contract.setProvider(_this.currentProvider, _this.accounts);
};
var defaultAccount = null;
var defaultBlock = 'latest';
Object.defineProperty(this, 'defaultAccount', {
get: function () {
return defaultAccount;
},
set: function (val) {
if(val) {
defaultAccount = utils.toChecksumAddress(formatters.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
});
this.clearSubscriptions = _this._requestManager.clearSubscriptions;
// add net
@@ -92,9 +139,12 @@ var Eth = function Eth() {
// add personal
this.personal = new Personal(this.currentProvider);
this.personal.defaultAccount = this.defaultAccount;
// add contract
this.Contract = Contract;
this.Contract.defaultAccount = this.defaultAccount;
this.Contract.defaultBlock = this.defaultBlock;
this.Contract.setProvider(this.currentProvider, this.accounts);
// add IBAN
@@ -103,9 +153,294 @@ var Eth = function Eth() {
// add ABI
this.abi = abi;
methods().forEach(function(method) {
var methods = [
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: formatters.outputSyncingFormatter
}),
new Method({
name: 'getGasPrice',
call: 'eth_gasPrice',
params: 0,
outputFormatter: formatters.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: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: formatters.outputBigNumberFormatter
}),
new Method({
name: 'getStorageAt',
call: 'eth_getStorageAt',
params: 3,
inputFormatter: [formatters.inputAddressFormatter, utils.numberToHex, formatters.inputDefaultBlockNumberFormatter]
}),
new Method({
name: 'getCode',
call: 'eth_getCode',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter]
}),
new Method({
name: 'getBlock',
call: blockCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }],
outputFormatter: formatters.outputBlockFormatter
}),
new Method({
name: 'getUncle',
call: uncleCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatters.outputBlockFormatter,
}),
new Method({
name: 'getBlockTransactionCount',
call: getBlockTransactionCountCall,
params: 1,
inputFormatter: [formatters.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'getBlockUncleCount',
call: uncleCountCall,
params: 1,
inputFormatter: [formatters.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'getTransaction',
call: 'eth_getTransactionByHash',
params: 1,
inputFormatter: [null],
outputFormatter: formatters.outputTransactionFormatter
}),
new Method({
name: 'getTransactionFromBlock',
call: transactionFromBlockCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatters.outputTransactionFormatter
}),
new Method({
name: 'getTransactionReceipt',
call: 'eth_getTransactionReceipt',
params: 1,
inputFormatter: [null],
outputFormatter: formatters.outputTransactionReceiptFormatter
}),
new Method({
name: 'getTransactionCount',
call: 'eth_getTransactionCount',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'sendSignedTransaction',
call: 'eth_sendRawTransaction',
params: 1,
inputFormatter: [null]
}),
new Method({
name: 'signTransaction',
call: 'eth_signTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
}),
new Method({
name: 'sendTransaction',
call: 'eth_sendTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
}),
new Method({
name: 'sign',
call: 'eth_sign',
params: 2,
inputFormatter: [formatters.inputSignFormatter, formatters.inputAddressFormatter],
transformPayload: function (payload) {
payload.params.reverse();
return payload;
}
}),
new Method({
name: 'call',
call: 'eth_call',
params: 2,
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
}),
new Method({
name: 'estimateGas',
call: 'eth_estimateGas',
params: 1,
inputFormatter: [formatters.inputCallFormatter],
outputFormatter: utils.hexToNumber
}),
new Method({
name: 'getCompilers',
call: 'eth_getCompilers',
params: 0
}),
new Method({
name: 'compile.solidity',
call: 'eth_compileSolidity',
params: 1
}),
new Method({
name: 'compile.lll',
call: 'eth_compileLLL',
params: 1
}),
new Method({
name: 'compile.serpent',
call: 'eth_compileSerpent',
params: 1
}),
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: [formatters.inputLogFormatter],
outputFormatter: formatters.outputLogFormatter
}),
// subscriptions
new Subscriptions({
name: 'subscribe',
type: 'eth',
subscriptions: {
'newBlockHeaders': {
// TODO rename on RPC side?
subscriptionName: 'newHeads', // replace subscription with this name
params: 0,
outputFormatter: formatters.outputBlockFormatter
},
'pendingTransactions': {
subscriptionName: 'newPendingTransactions', // replace subscription with this name
params: 0
},
'logs': {
params: 1,
inputFormatter: [formatters.inputLogFormatter],
outputFormatter: formatters.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: formatters.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 means is eth.accounts (necessary for wallet signing)
method.defaultBlock = _this.defaultBlock;
method.defaultAccount = _this.defaultAccount;
});
};
@@ -113,381 +448,5 @@ var Eth = function Eth() {
core.addProviders(Eth);
Object.defineProperty(Eth.prototype, 'defaultBlock', {
get: function () {
return helpers.config.defaultBlock;
},
set: function (val) {
helpers.config.defaultBlock = val;
return val;
},
enumerable: true
});
Object.defineProperty(Eth.prototype, 'defaultAccount', {
get: function () {
return helpers.config.defaultAccount;
},
set: function (val) {
helpers.config.defaultAccount = val;
return val;
},
enumerable: true
});
var methods = function () {
var getVersion = new Method({
name: 'getProtocolVersion',
call: 'eth_protocolVersion',
params: 0
});
var getCoinbase = new Method({
name: 'getCoinbase',
call: 'eth_coinbase',
params: 0
});
var getMining = new Method({
name: 'isMining',
call: 'eth_mining',
params: 0
});
var getHashrate = new Method({
name: 'getHashrate',
call: 'eth_hashrate',
params: 0,
outputFormatter: utils.hexToNumber
});
var isSyncing = new Method({
name: 'isSyncing',
call: 'eth_syncing',
params: 0,
outputFormatter: formatters.outputSyncingFormatter
});
var getGasPrice = new Method({
name: 'getGasPrice',
call: 'eth_gasPrice',
params: 0,
outputFormatter: formatters.outputBigNumberFormatter
});
var getAccounts = new Method({
name: 'getAccounts',
call: 'eth_accounts',
params: 0,
outputFormatter: utils.toChecksumAddress
});
var getBlockNumber = new Method({
name: 'getBlockNumber',
call: 'eth_blockNumber',
params: 0,
outputFormatter: utils.hexToNumber
});
var getBalance = new Method({
name: 'getBalance',
call: 'eth_getBalance',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: formatters.outputBigNumberFormatter
});
var getStorageAt = new Method({
name: 'getStorageAt',
call: 'eth_getStorageAt',
params: 3,
inputFormatter: [formatters.inputAddressFormatter, utils.numberToHex, formatters.inputDefaultBlockNumberFormatter]
});
var getCode = new Method({
name: 'getCode',
call: 'eth_getCode',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter]
});
var getBlock = new Method({
name: 'getBlock',
call: blockCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }],
outputFormatter: formatters.outputBlockFormatter
});
var getUncle = new Method({
name: 'getUncle',
call: uncleCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatters.outputBlockFormatter,
});
var getBlockTransactionCount = new Method({
name: 'getBlockTransactionCount',
call: getBlockTransactionCountCall,
params: 1,
inputFormatter: [formatters.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
});
var getBlockUncleCount = new Method({
name: 'getBlockUncleCount',
call: uncleCountCall,
params: 1,
inputFormatter: [formatters.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
});
var getTransaction = new Method({
name: 'getTransaction',
call: 'eth_getTransactionByHash',
params: 1,
inputFormatter: [null],
outputFormatter: formatters.outputTransactionFormatter
});
var getTransactionFromBlock = new Method({
name: 'getTransactionFromBlock',
call: transactionFromBlockCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatters.outputTransactionFormatter
});
var getTransactionReceipt = new Method({
name: 'getTransactionReceipt',
call: 'eth_getTransactionReceipt',
params: 1,
inputFormatter: [null],
outputFormatter: formatters.outputTransactionReceiptFormatter
});
var getTransactionCount = new Method({
name: 'getTransactionCount',
call: 'eth_getTransactionCount',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: utils.hexToNumber
});
var sendSignedTransaction = new Method({
name: 'sendSignedTransaction',
call: 'eth_sendRawTransaction',
params: 1,
inputFormatter: [null]
});
var signTransaction = new Method({
name: 'signTransaction',
call: 'eth_signTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
var sendTransaction = new Method({
name: 'sendTransaction',
call: 'eth_sendTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
var sign = new Method({
name: 'sign',
call: 'eth_sign',
params: 2,
inputFormatter: [formatters.inputSignFormatter, formatters.inputAddressFormatter],
transformPayload: function (payload) {
payload.params.reverse();
return payload;
}
});
var call = new Method({
name: 'call',
call: 'eth_call',
params: 2,
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
});
var estimateGas = new Method({
name: 'estimateGas',
call: 'eth_estimateGas',
params: 1,
inputFormatter: [formatters.inputCallFormatter],
outputFormatter: utils.hexToNumber
});
var getCompilers = new Method({
name: 'getCompilers',
call: 'eth_getCompilers',
params: 0
});
var compileSolidity = new Method({
name: 'compile.solidity',
call: 'eth_compileSolidity',
params: 1
});
var compileLLL = new Method({
name: 'compile.lll',
call: 'eth_compileLLL',
params: 1
});
var compileSerpent = new Method({
name: 'compile.serpent',
call: 'eth_compileSerpent',
params: 1
});
var submitWork = new Method({
name: 'submitWork',
call: 'eth_submitWork',
params: 3
});
var getWork = new Method({
name: 'getWork',
call: 'eth_getWork',
params: 0
});
var getPastLogs = new Method({
name: 'getPastLogs',
call: 'eth_getLogs',
params: 1,
inputFormatter: [formatters.inputLogFormatter],
outputFormatter: formatters.outputLogFormatter
});
// subscriptions
var subscribe = new Subscriptions({
name: 'subscribe',
type: 'eth',
subscriptions: {
'newBlockHeaders': {
// TODO rename on RPC side?
subscriptionName: 'newHeads', // replace subscription with this name
params: 0,
outputFormatter: formatters.outputBlockFormatter
},
'pendingTransactions': {
subscriptionName: 'newPendingTransactions', // replace subscription with this name
params: 0
},
'logs': {
params: 1,
inputFormatter: [formatters.inputLogFormatter],
outputFormatter: formatters.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: formatters.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);
}
}
}
}
});
return [
getVersion,
getCoinbase,
getMining,
getHashrate,
isSyncing,
getGasPrice,
getAccounts,
getBlockNumber,
getBalance,
getStorageAt,
getCode,
getBlock,
getUncle,
getCompilers,
getBlockTransactionCount,
getBlockUncleCount,
getTransaction,
getTransactionFromBlock,
getTransactionReceipt,
getTransactionCount,
call,
estimateGas,
sendSignedTransaction,
signTransaction,
sendTransaction,
sign,
compileSolidity,
compileLLL,
compileSerpent,
submitWork,
getWork,
getPastLogs,
subscribe
];
};
module.exports = Eth;
+8 -14
View File
@@ -34,19 +34,7 @@ var Net = function () {
core.packageInit(this, arguments);
methods().forEach(function(method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager);
});
};
core.addProviders(Net);
var methods = function () {
return [
[
new Method({
name: 'getId',
call: 'net_version',
@@ -64,9 +52,15 @@ var methods = function () {
params: 0,
outputFormatter: utils.hexToNumber
})
];
].forEach(function(method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager);
});
};
core.addProviders(Net);
module.exports = Net;
+9 -12
View File
@@ -44,20 +44,10 @@ var Shh = function Shh() {
this.clearSubscriptions = _this._requestManager.clearSubscriptions;
methods().forEach(function(method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager);
});
this.net = new Net(this.currentProvider);
};
core.addProviders(Shh);
var methods = function () {
return [
[
new Subscriptions({
name: 'subscribe',
type: 'shh',
@@ -178,9 +168,16 @@ var methods = function () {
params: 1,
inputFormatter: [null]
})
];
].forEach(function(method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager);
});
};
core.addProviders(Shh);
module.exports = Shh;
+53
View File
@@ -0,0 +1,53 @@
var chai = require('chai');
var assert = chai.assert;
var Eth = require('../packages/web3-eth');
var Web3 = require('../src/index.js');
var eth = new Eth();
var setValue = '0x47D33b27Bb249a2DBab4C0612BF9CaF4C1950855';
describe('web3.eth', function () {
describe('defaultAccount', function () {
it('should check if defaultAccount is set to proper value', function () {
assert.equal(eth.defaultAccount, null);
assert.equal(eth.personal.defaultAccount, null);
assert.equal(eth.Contract.defaultAccount, null);
assert.equal(eth.getCode.method.defaultAccount, null);
});
it('should set defaultAccount for all sub packages is set to proper value, if Eth package is changed', function () {
eth.defaultAccount = setValue;
assert.equal(eth.defaultAccount, setValue);
assert.equal(eth.personal.defaultAccount, setValue);
assert.equal(eth.Contract.defaultAccount, setValue);
assert.equal(eth.getCode.method.defaultAccount, setValue);
});
it('should fail if address is invalid, wich is to be set to defaultAccount', function () {
assert.throws(function(){ eth.defaultAccount = '0x17F33b27Bb249a2DBab4C0612BF9CaF4C1950855'; });
});
it('should have different values for two Eth instances', function () {
var eth1 = new Eth();
eth1.defaultAccount = setValue;
assert.equal(eth1.defaultAccount, setValue);
var eth2 = new Eth();
assert.equal(eth2.defaultAccount, null);
});
it('should have different values for two Web3 instances', function () {
var web31 = new Web3();
web31.eth.defaultAccount = setValue;
assert.equal(web31.eth.defaultAccount, setValue);
var web32 = new Web3();
assert.equal(web32.eth.defaultAccount, null);
});
});
});
+13
View File
@@ -4,10 +4,23 @@ var Eth = require('../packages/web3-eth');
var eth = new Eth();
var setValue = 123;
describe('web3.eth', function () {
describe('defaultBlock', function () {
it('should check if defaultBlock is set to proper value', function () {
assert.equal(eth.defaultBlock, 'latest');
assert.equal(eth.personal.defaultBlock, 'latest');
assert.equal(eth.Contract.defaultBlock, 'latest');
assert.equal(eth.getCode.method.defaultBlock, 'latest');
});
it('should set defaultBlock for all sub packages is set to proper value, if Eth package is changed', function () {
eth.defaultBlock = setValue;
assert.equal(eth.defaultBlock, setValue);
assert.equal(eth.personal.defaultBlock, setValue);
assert.equal(eth.Contract.defaultBlock, setValue);
assert.equal(eth.getCode.method.defaultBlock, setValue);
});
});
});
+1 -1
View File
@@ -10,7 +10,7 @@ var tests = [{
formattedResult: 11,
call: 'eth_getBlockTransactionCountByHash'
},{
args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'],
args: ['0x47D33b27Bb249a2DBab4C0612BF9CaF4C1950855'],
formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'],
result: '0xb',
formattedResult: 11,
+4 -4
View File
@@ -17,7 +17,7 @@ describe('lib/web3/method', function () {
name: 'call',
call: 'eth_call',
params: 2,
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter.bind({defaultBlock: 'latest'})]
});
method.setRequestManager(eth._requestManager);
@@ -31,7 +31,7 @@ describe('lib/web3/method', function () {
from: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae',
to: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae',
data: '0xa123456'
},"latest"]);
}, "latest"]);
});
provider.injectResult('0x1234567453543456321456321'); // tx hash
@@ -54,7 +54,7 @@ describe('lib/web3/method', function () {
name: 'call',
call: 'eth_call',
params: 2,
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter.bind({defaultBlock: 'latest'})]
});
method.setRequestManager(eth._requestManager);
@@ -264,7 +264,7 @@ describe('lib/web3/method', function () {
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
method.setRequestManager(eth._requestManager, eth);
method.setRequestManager(eth._requestManager); // second parameter accounts
// generate send function
var send = method.buildCall();