Code coverage report for ethereum.js/lib/web3/allevents.js

Statements: 91.89% (34 / 37)      Branches: 62.5% (5 / 8)      Functions: 87.5% (7 / 8)      Lines: 91.89% (34 / 37)      Ignored: none     

All files » ethereum.js/lib/web3/ » allevents.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                            1 1 1 1 1 1   1 25 25     1 1 1   1 2         1 1   1     1 2 2   2 2 2     2         2 2     1 1 1 1     1 25 25     1    
/*
    This file is part of ethereum.js.
 
    ethereum.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.
 
    ethereum.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 ethereum.js.  If not, see <http://www.gnu.org/licenses/>.
*/
/** 
 * @file allevents.js
 * @author Marek Kotewicz <marek@ethdev.com>
 * @date 2014
 */
 
var sha3 = require('../utils/sha3');
var SolidityEvent = require('./event');
var formatters = require('./formatters');
var utils = require('../utils/utils');
var Filter = require('./filter');
var watches = require('./watches');
 
var AllSolidityEvents = function (json, address) {
    this._json = json;
    this._address = address;
};
 
AllSolidityEvents.prototype.encode = function (options) {
    options = options || {};
    var result = {};
 
    ['fromBlock', 'toBlock'].filter(function (f) {
        return options[f] !== undefined;
    }).forEach(function (f) {
        result[f] = formatters.inputBlockNumberFormatter(options[f]);
    });
 
    result.topics = [null, null, null, null, null]; // match all topics
    result.address = this._address;
 
    return result;
};
 
AllSolidityEvents.prototype.decode = function (data) {
    data.data = data.data || '';
    data.topics = data.topics || [];
 
    var eventTopic = data.topics[0].slice(2);
    var match = this._json.filter(function (j) {
        return eventTopic === sha3(utils.transformToFullName(j));
    })[0];
 
    Iif (!match) { // cannot find matching event?
        console.warn('cannot find event for log');
        return data;
    }
 
    var event = new SolidityEvent(match, this._address);
    return event.decode(data);
};
 
AllSolidityEvents.prototype.execute = function (options, callback) {
    var o = this.encode(options);
    var formatter = this.decode.bind(this);
    return new Filter(o, watches.eth(), formatter, callback);
};
 
AllSolidityEvents.prototype.attachToContract = function (contract) {
    var execute = this.execute.bind(this);
    contract.allEvents = execute;
};
 
module.exports = AllSolidityEvents;