eth.filter.watch() works as documented.
However, I cannot get eth.filter.get() to work at all.
filter = web3.eth.filter();
filter.get(function(error, result){ if (!error) console.log(JSON.stringify(result)); }); // does not work
filter.options.fromBlock = 0;
filter.get(function(error, result){ if (!error) console.log(JSON.stringify(result)); }); // does not work
filter.options.toBlock = 'latest';
filter.get(function(error, result){ if (!error) console.log(JSON.stringify(result)); }); // does not work
filter.options.toBlock = 387229;
filter.get(function(error, result){ if (!error) console.log(JSON.stringify(result)); }); // does not work
changing the options later on has no effect. We should probably change it to _options
could you try printing the error?
Still can't get it to work:
> var filter = web3.eth.filter({fromBlock: 0, toBlock: 'latest'});
undefined
> filter.get(function(error, result){ if (!error) console.log(result.transactionHash); });
undefined
{
callbacks: [],
filterId: "0x0",
getLogsCallbacks: [],
implementation: {
getLogs: function(),
newFilter: function(),
poll: function(),
uninstallFilter: function()
},
options: {
address: undefined,
fromBlock: "0x0",
to: undefined,
toBlock: "latest",
topics: []
},
pollFilters: [],
formatter: function(log),
get: function(callback),
stopWatching: function(),
watch: function(callback)
}
I just tested with the latest web3 and i get filter... though not when asking from block 0, as the retrieval takes longer as the timeout of the socket..
(we are already having a faster way to retrieve logs in the pipeline.. in go)
Try setting the fromBlock to 350000, and see if you then get anything.
Works for me
var filter = web3.eth.filter({fromBlock: 350000, toBlock: 'latest'});
undefined
> filter.get(function(error, result){ console.log(error, result); });
null [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
{
callbacks: [],
filterId: "0x0",
getLogsCallbacks: [],
implementation: {
getLogs: function(),
newFilter: function(),
poll: function(),
uninstallFilter: function()
},
options: {
address: undefined,
fromBlock: "0x55730",
to: undefined,
toBlock: "latest",
topics: []
},
pollFilters: [],
formatter: function(log),
get: function(callback),
stopWatching: function(),
watch: function(callback)
}
>
Okay - I can get that to work, but I can't get it to work on a specific address.
Here is the log I want to find:
> eth.getTransactionReceipt(tx)
{
blockHash: "0x4281fcb117ed37ddc176b8867478958564b75eef6c7e9c507c0d0e923c4f7d83",
blockNumber: 392110,
contractAddress: null,
cumulativeGasUsed: 252081,
gasUsed: 252081,
logs: [{
address: "0xa076155806214a73f37f5fcd8025036d92f6a3fb",
blockHash: "0x4281fcb117ed37ddc176b8867478958564b75eef6c7e9c507c0d0e923c4f7d83",
blockNumber: 392110,
data: "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000014546869732069732074686520636f6e74656e742e000000000000000000000000",
logIndex: 0,
topics: [],
transactionHash: "0xc89803426f69870e94d2ad7ac8db7ba45b9a6fd6ba95e3c9f19217245bbddbac",
transactionIndex: 0
}],
transactionHash: "0xc89803426f69870e94d2ad7ac8db7ba45b9a6fd6ba95e3c9f19217245bbddbac",
transactionIndex: 0
}
Here is what I do to find it:
> filter = etherbin.allEvents({fromBlock: 390000, toBlock: 'latest'})
{
callbacks: [],
filterId: "0x1",
getLogsCallbacks: [],
implementation: {
getLogs: function(),
newFilter: function(),
poll: function(),
uninstallFilter: function()
},
options: {
address: "0xa076155806214a73f37f5fcd8025036d92f6a3fb",
fromBlock: "0x5f370",
to: undefined,
toBlock: "latest",
topics: []
},
pollFilters: [],
formatter: function(),
get: function(callback),
stopWatching: function(),
watch: function(callback)
> filter.get(function(error, result){ if (!error) console.log(error, result); });
{
callbacks: [],
filterId: "0x1",
getLogsCallbacks: [],
implementation: {
getLogs: function(),
newFilter: function(),
poll: function(),
uninstallFilter: function()
},
options: {
address: "0xa076155806214a73f37f5fcd8025036d92f6a3fb",
fromBlock: "0x5f370",
to: undefined,
toBlock: "latest",
topics: []
},
pollFilters: [],
formatter: function(),
get: function(callback),
stopWatching: function(),
watch: function(callback)
}
Okay if I do this it works:
filter = eth.filter({fromBlock: 390000, toBlock: 'latest', address: '0xa076155806214a73f37f5fcd8025036d92f6a3fb'})
But if I do
filter = etherbin.allEvents({fromBlock: 390000, toBlock: 'latest'})
It does not.
The difference between the filters that get created is that eth.filter() creates a filter with
formatter: function(log),
Whereas etherbin.allEvents() creates
formatter: function(),
Could you point me to code lines, i can't find it.
Those formatter lines are just from the returned filter object. I don't know where in the code it is set.
it is an anonymous event right?
@debris could you take a look?
Yes it is anonymous.
So there are two important points where it got lost:
AllSolidityEvents.prototype.execute = function (options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
if(arguments.length === 1)
options = null;
}
var o = this.encode(options);
var formatter = this.decode.bind(this);
return new Filter(this._web3, o, watches.eth(), formatter, callback);
};
Decode
AllSolidityEvents.prototype.decode = function (data) {
data.data = data.data || '';
data.topics = data.topics || [];
var eventTopic = data.topics[0].slice(2);
// i assume it gets lost here
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(this._web3, match, this._address);
return event.decode(data);
};
Could you please run the above example in the browser, where you loaded web3 in?
It could be that go's otto VM doesn't support console.warn, which would tell you that it didn't found an event..
But i assume its a problem that we don't support anonymous events here..
I'll try it tomorrow.
+1 on this issue
can't filter for one address (or a list of addresses)
The issue I'm finding is that the filter.get() only works when you initialize the filter _before_ the transactions that create the logs. It only finds logs that are generated after that point.
Although this may be the _intended_ behavior, It's very counterintuitive. The method is get(). Its just a read from the blockchain. it should be able to do a global data lookup.
Maybe it's a feature request, but I believe something is needed that can get _any_ log/event especially if I can provide an index/topic parameter. Looking though the entire chain should not be expensive because its an index.
var foo = eth.filter({"fromBlock":"0x0","toBlock":"pending","address":"0x308c1fe555c7f6599594f2C7B1402A4F91E49211","topics":[]})
var bar = foo.get()
And I'm frozen solid, but if I run a parity node and use geth attach on it, and it runs fine. What gives?
@kingcocomango fromBlock:0 hardly works beacuse of the depth, if you are aware of a block from which to search, it is advisible to set that as your fromBlock.
Also, to use the filter succesfully:
eth.filter("fromBlock":"0x0","toBlock":"pending","address":"0x308c1fe555c7f6599594f2C7B1402A4F91E49211","topics":[]}).get(function(e,r){console.log(e,r)});
To filter by topic, you need to set the 1st topic to the signature of the event
topics[0] = web3.sha3('MyEvent(uint256,uint256,bytes)')
Resulting in:
eth.filter("fromBlock":"0x0","toBlock":"pending","address":"0x308c1fe555c7f6599594f2C7B1402A4F91E49211","topics":[ web3.sha3('MyEvent(uint256,uint256,bytes)') ]}).get(function(e,r){console.log(e,r)});
*Note: Full data type needs to be indicated in the topic name e.g (uint == uint256)
Please reopen if thats still problematic in web3.js 1.0.0
Most helpful comment
The issue I'm finding is that the
filter.get()only works when you initialize the filter _before_ the transactions that create the logs. It only finds logs that are generated after that point.Although this may be the _intended_ behavior, It's very counterintuitive. The method is
get(). Its just a read from the blockchain. it should be able to do a global data lookup.Maybe it's a feature request, but I believe something is needed that can get _any_ log/event especially if I can provide an index/topic parameter. Looking though the entire chain should not be expensive because its an index.