diff --git a/CHANGELOG.md b/CHANGELOG.md index 44bf5d49a06..abddafac36b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ Released with 1.0.0-beta.37 code base. ### Fixed +- Fix allow `0` as a valid `fromBlock` or `toBlock` filter param (#1100) - Fix randomHex returning inconsistent string lengths (#1490) - Fix make isBN minification safe (#1777) - Fix incorrect references to BigNumber in utils.fromWei and utils.toWei error messages (#2468) diff --git a/packages/web3-core-helpers/src/formatters.js b/packages/web3-core-helpers/src/formatters.js index 06ce76c30ef..068d5e6d6c9 100644 --- a/packages/web3-core-helpers/src/formatters.js +++ b/packages/web3-core-helpers/src/formatters.js @@ -279,10 +279,10 @@ var inputLogFormatter = function(options) { return utils.fromUtf8(value); }; - if (options.fromBlock) + if (options.fromBlock || options.fromBlock === 0) options.fromBlock = inputBlockNumberFormatter(options.fromBlock); - if (options.toBlock) + if (options.toBlock || options.toBlock === 0) options.toBlock = inputBlockNumberFormatter(options.toBlock); diff --git a/test/formatters.inputLogFormatter.js b/test/formatters.inputLogFormatter.js new file mode 100644 index 00000000000..eee62490e39 --- /dev/null +++ b/test/formatters.inputLogFormatter.js @@ -0,0 +1,108 @@ +var chai = require('chai'); +var assert = chai.assert; +var formatters = require('../packages/web3-core-helpers/src/formatters.js'); + +describe('InputLogFormatterTest', function() { + + it('call inputLogFormatter with a valid log', function() { + var log = { + fromBlock: 'earliest', + toBlock: 'latest', + topics: ['0x0'], + address: '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078' + }; + + assert.deepEqual(formatters.inputLogFormatter(log), { + fromBlock: 'earliest', + toBlock: 'latest', + topics: ['0x0'], + address: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078' + }); + }); + + it('call inputLogFormatter with numerical from/to blocks', function() { + var log = { + fromBlock: 1, + toBlock: 2, + topics: ['0x0'], + address: '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078' + }; + + assert.deepEqual(formatters.inputLogFormatter(log), { + fromBlock: '0x1', + toBlock: '0x2', + topics: ['0x0'], + address: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078' + }); + }); + + it('call inputLogFormatter with zero valued from/to blocks', function() { + var log = { + fromBlock: 0, + toBlock: 0, + topics: ['0x0'], + address: '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078' + }; + + assert.deepEqual(formatters.inputLogFormatter(log), { + fromBlock: '0x0', + toBlock: '0x0', + topics: ['0x0'], + address: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078' + }); + }); + + it('call inputLogFormatter with a array of addresses in the log', function() { + var log = { + fromBlock: 'earliest', + toBlock: 'latest', + topics: ['0x0'], + address: [ + '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078', + '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078' + ] + }; + + assert.deepEqual(formatters.inputLogFormatter(log), { + fromBlock: 'earliest', + toBlock: 'latest', + topics: ['0x0'], + address: [ + '0x03c9a938ff7f54090d0d99e2c6f80380510ea078', + '0x03c9a938ff7f54090d0d99e2c6f80380510ea078' + ] + }); + }); + + it('call inputLogFormatter with an topic item of null', function() { + var log = { + fromBlock: 'earliest', + toBlock: 'latest', + topics: [null], + address: '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078' + }; + + assert.deepEqual(formatters.inputLogFormatter(log), { + fromBlock: 'earliest', + toBlock: 'latest', + topics: [null], + address: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078' + }); + }); + + it('call inputLogFormatter with an topic item that does not start with "0x"', function() { + var log = { + fromBlock: 'earliest', + toBlock: 'latest', + topics: ['00'], + address: '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078' + }; + + assert.deepEqual(formatters.inputLogFormatter(log), { + fromBlock: 'earliest', + toBlock: 'latest', + topics: ['0x3030'], + address: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078' + }); + }); +});