Merge pull request #244 from ethereum/allevents

all events filter
This commit is contained in:
Fabian Vogelsteller 2015-06-24 14:55:49 +02:00
commit ef15fc1886
9 changed files with 1233 additions and 837 deletions

828
dist/web3-light.js vendored

File diff suppressed because it is too large Load Diff

149
dist/web3-light.js.map vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

830
dist/web3.js vendored

File diff suppressed because it is too large Load Diff

14
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

12
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

81
lib/web3/allevents.js Normal file
View File

@ -0,0 +1,81 @@
/*
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];
if (!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;

View File

@ -25,6 +25,7 @@ var utils = require('../utils/utils');
var coder = require('../solidity/coder');
var SolidityEvent = require('./event');
var SolidityFunction = require('./function');
var AllEvents = require('./allevents');
/**
* Should be called to encode constructor params
@ -70,9 +71,14 @@ var addFunctionsToContract = function (contract, abi) {
* @param {Array} abi
*/
var addEventsToContract = function (contract, abi) {
abi.filter(function (json) {
var events = abi.filter(function (json) {
return json.type === 'event';
}).map(function (json) {
});
var All = new AllEvents(events, contract.address);
All.attachToContract(contract);
events.map(function (json) {
return new SolidityEvent(json, contract.address);
}).forEach(function (e) {
e.attachToContract(contract);

View File

@ -196,6 +196,78 @@ describe('web3.eth.contract', function () {
});
});
it('should create all event filter', function (done) {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset(); // reset different polls
var signature = 'Changed(address,uint256,uint256,uint256)';
var step = 0;
provider.injectValidation(function (payload) {
if (step === 0) {
step = 1;
provider.injectResult('0x3');
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_newFilter');
assert.deepEqual(payload.params[0], {
topics: [
null,
null,
null,
null,
null
],
address: '0x1234567890123456789012345678901234567890'
});
} else if (step === 1) {
step = 2;
provider.injectResult([{
address: address,
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]);
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_getFilterLogs');
} else if (step === 2 && utils.isArray(payload)) {
provider.injectBatchResults([[{
address: address,
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]]);
var r = payload.filter(function (p) {
return p.jsonrpc === '2.0' && p.method === 'eth_getFilterChanges' && p.params[0] === '0x3';
});
assert.equal(r.length > 0, true);
}
});
var contract = web3.eth.contract(desc).at(address);
var res = 0;
var event = contract.allEvents();
event.watch(function(err, result) {
assert.equal(result.args.from, address);
assert.equal(result.args.amount, 1);
assert.equal(result.args.t1, 1);
assert.equal(result.args.t2, 8);
res++;
if (res === 2) {
done();
}
});
});
it('should call constant function', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);