Skip to content

Commit

Permalink
finished pastEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Aug 15, 2016
1 parent fdb55f7 commit 446c03d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
104 changes: 63 additions & 41 deletions lib/web3/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

var utils = require('../utils/utils');
var eventifiedPromise = require('./eventifiedPromise.js');
var Method = require('./method.js');
var coder = require('../solidity/coder');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
Expand Down Expand Up @@ -191,43 +192,50 @@ Contract.prototype._encodeEventABI = function (event, options) {
var filter = options.filter || {},
result = {};


['fromBlock', 'toBlock'].filter(function (f) {
return options[f] !== undefined;
}).forEach(function (f) {
result[f] = formatters.inputBlockNumberFormatter(options[f]);
});

result.topics = [];
// use given topics
if(utils.isArray(options.topics)) {
result.topics = options.topics;

// add event signature
if (event && !event.anonymous && event.name !== 'ALLEVENTS') {
result.topics.push(event.signature);
}
// create topics based on filter
} else {

// add event topics (indexed arguments)
if (event.name !== 'ALLEVENTS') {
var indexedTopics = event.inputs.filter(function (i) {
return i.indexed === true;
}).map(function (i) {
var value = filter[i.name];
if (!value) {
return null;
}
result.topics = [];

if (utils.isArray(value)) {
return value.map(function (v) {
return '0x' + coder.encodeParam(i.type, v);
});
}
return '0x' + coder.encodeParam(i.type, value);
});
// add event signature
if (event && !event.anonymous && event.name !== 'ALLEVENTS') {
result.topics.push(event.signature);
}

result.topics = result.topics.concat(indexedTopics);
}
// add event topics (indexed arguments)
if (event.name !== 'ALLEVENTS') {
var indexedTopics = event.inputs.filter(function (i) {
return i.indexed === true;
}).map(function (i) {
var value = filter[i.name];
if (!value) {
return null;
}

if(!result.topics.length)
delete result.topics;
if (utils.isArray(value)) {
return value.map(function (v) {
return '0x' + coder.encodeParam(i.type, v);
});
}
return '0x' + coder.encodeParam(i.type, value);
});

result.topics = result.topics.concat(indexedTopics);
}

if(!result.topics.length)
delete result.topics;
}

result.address = this.address;

Expand All @@ -243,6 +251,7 @@ Contract.prototype._encodeEventABI = function (event, options) {
*/
Contract.prototype._decodeEventABI = function (data) {
var event = this;

data.data = data.data || '';
data.topics = data.topics || [];
var result = formatters.outputLogFormatter(data);
Expand Down Expand Up @@ -527,24 +536,27 @@ Contract.prototype.encodeABI = function encodeABI(options){
* @param {String} _generateEventOptions
* @param {Object} event
* @param {Object} options
* @param {Function} callback
* @return {Object} the event options object
*/
Contract.prototype._generateEventOptions = function(event, options, func) {
var args = Array.prototype.slice.call(arguments),
event = (event.toLowerCase() === 'allevents') ? {
name: 'ALLEVENTS',
jsonInterface: this.jsonInterface
} : this.jsonInterface.find(function (json) {
return (json.type === 'event' && json.name === event);
});
Contract.prototype._generateEventOptions = function(event, options, callback) {
var args = Array.prototype.slice.call(arguments);

// get the callback
if (utils.isFunction(args[args.length - 1])) {
callback = args[args.length - 1];
callback = args.pop();
}

// get the options
options = (utils.isObject(args[args.length - 1])) ? args[args.length - 1] : options;
options = (utils.isObject(args[args.length - 1])) ? args.pop() : {};

event = (utils.isString(args[0])) ? event : 'allevents';
event = (event.toLowerCase() === 'allevents') ? {
name: 'ALLEVENTS',
jsonInterface: this.jsonInterface
} : this.jsonInterface.find(function (json) {
return (json.type === 'event' && json.name === event);
});

if (!event) {
throw new Error('Event "' + event.name + '" doesn\'t exist in this contract.');
Expand Down Expand Up @@ -596,7 +608,7 @@ Contract.prototype.once = function(event, options, callback) {
* @return {Object} the event subscription
*/
Contract.prototype.on = function(event, options, callback){
var subOptions = this._generateEventOptions(event ,options, callback);
var subOptions = this._generateEventOptions.apply(this, arguments);


// prevent the event "newListener" and "removeListener" from being overwritten
Expand Down Expand Up @@ -631,11 +643,21 @@ Contract.prototype.on = function(event, options, callback){
* @return {Object} the promievent
*/
Contract.prototype.pastEvents = function(event, options, callback){
var subOptions = this._generateEventOptions(event ,options, callback);
var subOptions = this._generateEventOptions.apply(this, arguments);

var getPastLogs = new Method({
name: 'getPastLogs',
call: 'eth_getLogs',
params: 1,
inputFormatter: [formatters.inputLogFormatter],
outputFormatter: this._decodeEventABI.bind(subOptions.event)
});
getPastLogs.setRequestManager(this._web3._requestManager);
var call = getPastLogs.buildCall();

console.log(subOptions.params, this._decodeEventABI.bind(subOptions.event));
getPastLogs = null;

return this._web3.eth.getPastLogs(subOptions.params, subOptions.callback);
return call(subOptions.params, subOptions.callback);
};


Expand Down Expand Up @@ -684,7 +706,7 @@ Contract.prototype._executeMethod = function _executeMethod(type){
options = (utils.isObject(args[args.length - 1]))
? args.pop()
: {};

options.from = options.from || this._parent.options.from;
options.data = this.encodeABI();
// TODO remove once we switched everywhere to gasLimit
Expand Down
6 changes: 3 additions & 3 deletions lib/web3/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Method.prototype.extractCallback = function (args) {

/**
* Should be called to check if the number of arguments is correct
*
*
* @method validateArgs
* @param {Array} arguments
* @throws {Error} if it is not
Expand All @@ -75,7 +75,7 @@ Method.prototype.validateArgs = function (args) {

/**
* Should be called to format input args of method
*
*
* @method formatInput
* @param {Array}
* @return {Array}
Expand Down Expand Up @@ -137,7 +137,7 @@ Method.prototype.attachToObject = function (obj) {
obj[name[0]] = obj[name[0]] || {};
obj[name[0]][name[1]] = func;
} else {
obj[name[0]] = func;
obj[name[0]] = func;
}
};

Expand Down
11 changes: 8 additions & 3 deletions lib/web3/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ Subscription.prototype.subscribe = function() {
if(!err) {
logs.forEach(function(log){
var output = _this._formatOutput(log);
_this.callback(null, output);
_this.callback(null, output, _this);
_this.emit('data', output);
});
} else {
_this.callback(err);
_this.callback(err, null, _this);
_this.emit('error', err);
}
});
Expand All @@ -208,6 +208,11 @@ Subscription.prototype.subscribe = function() {

// call callback on notifications
_this.options.requestManager.addSubscription(_this.id, payload.params[0] ,'eth', function(err, result) {

// TODO remove once its fixed in geth
if(utils.isArray(result))
result = result[0];

var output = _this._formatOutput(result);

_this.callback(err, output, _this);
Expand Down Expand Up @@ -238,7 +243,7 @@ Subscription.prototype.subscribe = function() {

});
} else {
_this.callback(err);
_this.callback(err, null, _this);
}
});

Expand Down

0 comments on commit 446c03d

Please sign in to comment.