web3.js/lib/web3/event.js

174 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-01-29 15:05:43 +01:00
/*
This file is part of ethereum.js.
2015-01-29 12:35:21 +01:00
2015-01-29 15:05:43 +01:00
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/>.
*/
2015-04-20 22:48:01 +02:00
/**
* @file event.js
* @author Marek Kotewicz <marek@ethdev.com>
2015-01-29 15:05:43 +01:00
* @date 2014
*/
2015-01-29 12:35:21 +01:00
2015-03-08 18:18:52 +01:00
var utils = require('../utils/utils');
2015-04-20 21:59:35 +02:00
var coder = require('../solidity/coder');
var web3 = require('../web3');
2015-04-20 22:48:01 +02:00
/**
* This prototype should be used to create event filters
*/
2015-04-20 21:59:35 +02:00
var SolidityEvent = function (json, address) {
this._params = json.inputs;
this._name = json.name;
this._address = address;
};
2015-04-20 22:48:01 +02:00
/**
* Should be used to get filtered param types
*
* @method types
* @param {Bool} decide if returned typed should be indexed
* @return {Array} array of types
*/
2015-04-20 21:59:35 +02:00
SolidityEvent.prototype.types = function (indexed) {
return this._params.filter(function (i) {
return i.indexed === indexed;
}).map(function (i) {
return i.type;
});
};
2015-04-20 22:48:01 +02:00
/**
* Should be used to get event display name
*
* @method displayName
* @return {String} event display name
*/
2015-04-20 21:59:35 +02:00
SolidityEvent.prototype.displayName = function () {
return utils.extractDisplayName(this._name);
};
2015-04-20 22:48:01 +02:00
/**
* Should be used to get event type name
*
* @method typeName
* @return {String} event type name
*/
2015-04-20 21:59:35 +02:00
SolidityEvent.prototype.typeName = function () {
return utils.extractTypeName(this._name);
};
2015-04-20 22:48:01 +02:00
/**
* Should be used to get event signature
*
* @method signature
* @return {String} event signature
*/
2015-04-20 21:59:35 +02:00
SolidityEvent.prototype.signature = function () {
return web3.sha3(web3.fromAscii(this._name)).slice(2);
};
2015-04-20 22:48:01 +02:00
/**
* Should be used to encode indexed params and options to one final object
*
* @method encode
* @param {Object} indexed
* @param {Object} options
* @return {Object} everything combined together and encoded
*/
2015-04-20 21:59:35 +02:00
SolidityEvent.prototype.encode = function (indexed, options) {
indexed = indexed || {};
options = options || {};
options.address = this._address;
options.topics = options.topics || [];
options.topics.push('0x' + this.signature());
var indexedTopics = this._params.filter(function (i) {
return i.indexed === true;
}).map(function (i) {
var value = indexed[i.name];
if (value !== undefined) {
return '0x' + coder.encodeParam(i.type, value);
}
return null;
});
options.topics = options.topics.concat(indexedTopics);
return options;
};
2015-04-20 22:48:01 +02:00
/**
* Should be used to decode indexed params and options
*
* @method decode
* @param {Object} data
* @return {Object} result object with decoded indexed && not indexed params
*/
2015-04-20 21:59:35 +02:00
SolidityEvent.prototype.decode = function (data) {
var result = {
event: this.displayName(),
number: data.number,
hash: data.hash,
args: {}
};
data.data = data.data || '';
var indexedData = data.topics.slice(1).map(function (topics) { return topics.slice(2); }).join("");
var indexedParams = coder.decodeParams(this.types(true), indexedData);
var notIndexedData = data.data.slice(2);
var notIndexedParams = coder.decodeParams(this.types(false), notIndexedData);
result.args = this._params.reduce(function (acc, current) {
acc[current.name] = current.indexed ? indexedParams.shift() : notIndexedParams.shift();
return acc;
}, {});
return result;
};
2015-04-20 22:48:01 +02:00
/**
* Should be used to create new filter object from event
*
* @method execute
* @param {Object} indexed
* @param {Object} options
* @return {Object} filter object
*/
2015-04-20 21:59:35 +02:00
SolidityEvent.prototype.execute = function (indexed, options) {
var o = this.encode(indexed, options);
var formatter = this.decode.bind(this);
return web3.eth.filter(o, undefined, undefined, formatter);
};
2015-04-20 22:48:01 +02:00
/**
* Should be used to attach event to contract object
*
* @method attachToContract
* @param {Contract}
*/
2015-04-20 21:59:35 +02:00
SolidityEvent.prototype.attachToContract = function (contract) {
var execute = this.execute.bind(this);
var displayName = this.displayName();
if (!contract[displayName]) {
contract[displayName] = execute;
}
contract[displayName][this.typeName()] = this.execute.bind(this, contract);
};
2015-02-03 16:16:38 +01:00
2015-04-20 22:06:49 +02:00
module.exports = SolidityEvent;
2015-01-29 12:35:21 +01:00