diff --git a/lib/web3/eth.js b/lib/web3/eth.js index 3800dc2..42ece6d 100644 --- a/lib/web3/eth.js +++ b/lib/web3/eth.js @@ -14,10 +14,10 @@ You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** @file eth.js - * @authors: - * Marek Kotewicz - * Fabian Vogelsteller +/** + * @file eth.js + * @author Marek Kotewicz + * @author Fabian Vogelsteller * @date 2015 */ @@ -77,7 +77,7 @@ var methods = [{ name: 'getBalance', call: 'eth_getBalance', addDefaultblock: 2, - outputFormatter: formatters.convertToBigNumber + outputFormatter: formatters.inputNumberFormatter }, { name: 'getStorage', call: 'eth_getStorage', @@ -95,12 +95,12 @@ var methods = [{ name: 'getBlock', call: blockCall, outputFormatter: formatters.outputBlockFormatter, - inputFormatter: [utils.toHex, function(param){ return (!param) ? false : true; }] + inputFormatter: formatters.inputBlockFormatter }, { name: 'getUncle', call: uncleCall, outputFormatter: formatters.outputBlockFormatter, - inputFormatter: [utils.toHex, utils.toHex, function(param){ return (!param) ? false : true; }] + inputFormatter: formatters.inputUncleFormatter }, { name: 'getCompilers', call: 'eth_getCompilers' @@ -176,7 +176,7 @@ var methods = [{ var properties = [ { name: 'coinbase', getter: 'eth_coinbase'}, { name: 'mining', getter: 'eth_mining'}, - { name: 'gasPrice', getter: 'eth_gasPrice', outputFormatter: formatters.convertToBigNumber}, + { name: 'gasPrice', getter: 'eth_gasPrice', outputFormatter: formatters.inputNumberFormatter}, { name: 'accounts', getter: 'eth_accounts' }, { name: 'blockNumber', getter: 'eth_blockNumber', outputFormatter: utils.toDecimal}, @@ -186,7 +186,6 @@ var properties = [ { name: 'number', getter: 'eth_number', newProperty: 'eth.blockNumber'} ]; - module.exports = { methods: methods, properties: properties diff --git a/lib/web3/formatters.js b/lib/web3/formatters.js index 0c3d1d6..0966255 100644 --- a/lib/web3/formatters.js +++ b/lib/web3/formatters.js @@ -14,10 +14,10 @@ You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** @file formatters.js - * @authors: - * Marek Kotewicz - * Fabian Vogelsteller +/** + * @file formatters.js + * @author Marek Kotewicz + * @author Fabian Vogelsteller * @date 2015 */ @@ -26,12 +26,13 @@ var utils = require('../utils/utils'); /** * Should the input to a big number * - * @method convertToBigNumber + * @method inputNumberFormatter * @param {String|Number|BigNumber} * @returns {BigNumber} object */ -var convertToBigNumber = function (value) { - return utils.toBigNumber(value); +var inputNumberFormatter = function (args) { + args[0] = utils.toBigNumber(args[0]); + return args; }; /** @@ -41,7 +42,8 @@ var convertToBigNumber = function (value) { * @param {Object} transaction options * @returns object */ -var inputTransactionFormatter = function (options){ +var inputTransactionFormatter = function (args){ + var options = args[0]; // make code -> data if (options.code) { @@ -53,7 +55,7 @@ var inputTransactionFormatter = function (options){ options[key] = utils.fromDecimal(options[key]); }); - return options; + return args; }; /** @@ -77,7 +79,8 @@ var outputTransactionFormatter = function (tx){ * @param {Object} transaction options * @returns object */ -var inputCallFormatter = function (options){ +var inputCallFormatter = function (args){ + var options = args[0]; // make code -> data if (options.code) { @@ -85,9 +88,21 @@ var inputCallFormatter = function (options){ delete options.code; } - return options; + return args; }; +var inputBlockFormatter = function (args) { + args[0] = utils.toHex(args[0]); + args[1] = !!args[1]; + return args; +}; + +var inputUncleFormatter = function (args) { + args[0] = utils.toHex(args[0]); + args[1] = utils.toHex(args[1]); + args[2] = !!args[2]; + return args; +}; /** * Formats the output of a block to its proper values @@ -96,7 +111,7 @@ var inputCallFormatter = function (options){ * @param {Object} block object * @returns {Object} block object */ -var outputBlockFormatter = function(block){ +var outputBlockFormatter = function(block) { // transform to number block.gasLimit = utils.toDecimal(block.gasLimit); @@ -142,7 +157,8 @@ var outputLogFormatter = function(log){ * @param {Object} transaction object * @returns {Object} */ -var inputPostFormatter = function(post){ +var inputPostFormatter = function(args){ + var post = args[0]; post.payload = utils.toHex(post.payload); post.ttl = utils.fromDecimal(post.ttl); @@ -157,7 +173,7 @@ var inputPostFormatter = function(post){ return utils.fromAscii(topic); }); - return post; + return args; }; /** @@ -191,13 +207,15 @@ var outputPostFormatter = function(post){ }; module.exports = { - convertToBigNumber: convertToBigNumber, + inputNumberFormatter: inputNumberFormatter, inputTransactionFormatter: inputTransactionFormatter, - outputTransactionFormatter: outputTransactionFormatter, inputCallFormatter: inputCallFormatter, + inputPostFormatter: inputPostFormatter, + inputBlockFormatter: inputBlockFormatter, + inputUncleFormatter: inputUncleFormatter, + outputTransactionFormatter: outputTransactionFormatter, outputBlockFormatter: outputBlockFormatter, outputLogFormatter: outputLogFormatter, - inputPostFormatter: inputPostFormatter, outputPostFormatter: outputPostFormatter }; diff --git a/test/formatters.inputPostFormatter.js b/test/formatters.inputPostFormatter.js index 0995d6d..3b5c372 100644 --- a/test/formatters.inputPostFormatter.js +++ b/test/formatters.inputPostFormatter.js @@ -1,28 +1,29 @@ var chai = require('chai'); -var formatters = require('../lib/web3/formatters.js'); var assert = chai.assert; +var formatters = require('../lib/web3/formatters.js'); describe('formatters', function () { describe('inputPostFormatter', function () { it('should return the correct value', function () { // input as strings and numbers - assert.deepEqual(formatters.inputPostFormatter({ + assert.deepEqual(formatters.inputPostFormatter([{ from: '0x00000', to: '0x00000', payload: {test: 'test'}, ttl: 200, priority: 1000, topics: ['hello','mytopics'] - }), { + }]), [{ from: '0x00000', to: '0x00000', payload: '0x7b2274657374223a2274657374227d', ttl: '0xc8', priority: '0x3e8', topics: ['0x68656c6c6f','0x6d79746f70696373'] - }); + }]); }); }); }); + diff --git a/test/formatters.inputTransactionFormatter.js b/test/formatters.inputTransactionFormatter.js index 5ff0526..685dc49 100644 --- a/test/formatters.inputTransactionFormatter.js +++ b/test/formatters.inputTransactionFormatter.js @@ -1,4 +1,5 @@ -var assert = require('assert'); +var chai = require('chai'); +var assert = chai.assert; var formatters = require('../lib/web3/formatters.js'); var BigNumber = require('bignumber.js'); @@ -6,21 +7,21 @@ describe('formatters', function () { describe('inputTransactionFormatter', function () { it('should return the correct value', function () { - assert.deepEqual(formatters.inputTransactionFormatter({ + assert.deepEqual(formatters.inputTransactionFormatter([{ data: '0x34234kjh23kj4234', value: new BigNumber(100), from: '0x00000', to: '0x00000', gas: 1000, gasPrice: new BigNumber(1000), - }), { + }]), [{ data: '0x34234kjh23kj4234', value: '0x64', from: '0x00000', to: '0x00000', gas: '0x3e8', gasPrice: '0x3e8', - }); + }]); }); }); }); diff --git a/test/requestmanager.js b/test/requestmanager.js index 5b01956..162f205 100644 --- a/test/requestmanager.js +++ b/test/requestmanager.js @@ -4,6 +4,7 @@ var RequestManager = require('../lib/web3/requestmanager'); var FakeHttpProvider = require('./FakeHttpProvider'); // TODO: handling errors! +// TODO: validation of params! describe('requestmanager', function () { describe('send', function () {