From 58aeb89e830df652da751c68b1fe8bf58d7f8a89 Mon Sep 17 00:00:00 2001 From: Fabian Vogelsteller Date: Wed, 9 Aug 2017 15:50:12 +0200 Subject: [PATCH] fixes #432 --- packages/web3-core-helpers/src/config.js | 65 -- packages/web3-core-helpers/src/formatters.js | 10 +- packages/web3-core-helpers/src/index.js | 4 +- packages/web3-core-method/src/index.js | 8 +- packages/web3-eth-contract/src/index.js | 47 +- packages/web3-eth-personal/src/index.js | 169 +++-- packages/web3-eth/src/index.js | 713 +++++++++---------- packages/web3-net/src/index.js | 22 +- packages/web3-shh/src/index.js | 21 +- test/eth.defaultAccount.js | 53 ++ test/eth.defaultBlock.js | 13 + test/eth.getBlockTransactionCount.js | 2 +- test/method.buildCall.js | 8 +- 13 files changed, 573 insertions(+), 562 deletions(-) delete mode 100644 packages/web3-core-helpers/src/config.js create mode 100644 test/eth.defaultAccount.js diff --git a/packages/web3-core-helpers/src/config.js b/packages/web3-core-helpers/src/config.js deleted file mode 100644 index 5a99c171..00000000 --- a/packages/web3-core-helpers/src/config.js +++ /dev/null @@ -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 . - */ -/** - * @file index.js - * @author Marek Kotewicz - * @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 -}; - diff --git a/packages/web3-core-helpers/src/formatters.js b/packages/web3-core-helpers/src/formatters.js index 61375153..be580a4d 100644 --- a/packages/web3-core-helpers/src/formatters.js +++ b/packages/web3-core-helpers/src/formatters.js @@ -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!'); diff --git a/packages/web3-core-helpers/src/index.js b/packages/web3-core-helpers/src/index.js index a26a0e03..b0f9a4db 100644 --- a/packages/web3-core-helpers/src/index.js +++ b/packages/web3-core-helpers/src/index.js @@ -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 }; diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index d8ec9229..5e7ced4f 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -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]; }); }; diff --git a/packages/web3-eth-contract/src/index.js b/packages/web3-eth-contract/src/index.js index 7a49896a..21526633 100644 --- a/packages/web3-eth-contract/src/index.js +++ b/packages/web3-eth-contract/src/index.js @@ -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 }; diff --git a/packages/web3-eth-personal/src/index.js b/packages/web3-eth-personal/src/index.js index 48ce341a..9da7c95a 100644 --- a/packages/web3-eth-personal/src/index.js +++ b/packages/web3-eth-personal/src/index.js @@ -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; diff --git a/packages/web3-eth/src/index.js b/packages/web3-eth/src/index.js index 8558e03e..6e55595b 100644 --- a/packages/web3-eth/src/index.js +++ b/packages/web3-eth/src/index.js @@ -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; diff --git a/packages/web3-net/src/index.js b/packages/web3-net/src/index.js index 168c0e9e..ebf1b456 100755 --- a/packages/web3-net/src/index.js +++ b/packages/web3-net/src/index.js @@ -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; diff --git a/packages/web3-shh/src/index.js b/packages/web3-shh/src/index.js index 594f3b5b..e3646983 100644 --- a/packages/web3-shh/src/index.js +++ b/packages/web3-shh/src/index.js @@ -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; diff --git a/test/eth.defaultAccount.js b/test/eth.defaultAccount.js new file mode 100644 index 00000000..1ae9d9c0 --- /dev/null +++ b/test/eth.defaultAccount.js @@ -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); + + }); + }); +}); + diff --git a/test/eth.defaultBlock.js b/test/eth.defaultBlock.js index bf963958..b8dec964 100644 --- a/test/eth.defaultBlock.js +++ b/test/eth.defaultBlock.js @@ -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); }); }); }); diff --git a/test/eth.getBlockTransactionCount.js b/test/eth.getBlockTransactionCount.js index 83460dcb..e443250d 100644 --- a/test/eth.getBlockTransactionCount.js +++ b/test/eth.getBlockTransactionCount.js @@ -10,7 +10,7 @@ var tests = [{ formattedResult: 11, call: 'eth_getBlockTransactionCountByHash' },{ - args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], + args: ['0x47D33b27Bb249a2DBab4C0612BF9CaF4C1950855'], formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], result: '0xb', formattedResult: 11, diff --git a/test/method.buildCall.js b/test/method.buildCall.js index 3dc46363..7475cdf8 100644 --- a/test/method.buildCall.js +++ b/test/method.buildCall.js @@ -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();