-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Contract events: filter indexed. #204
Comments
#So from what I've found this is the correct answer I think:
If we want to filter by both sender and recipient we would add Is it possible to update the documentation for this perhaps? I turned this into a semi-generic function. Need to add errors on contract, wallet or eventName null.
|
A simpler method is coming in v4 (the TypeScript branch) which should address this. I’m just updat Ng the documentation n now and it should be available this week. :) |
Cool cant wait to check it out! |
One more note regarding the |
This now works in the v4 ( let filterFromMe = contract.filters.Transfer(myAddress, null);
let filterToMe = contract.filters.Transfer(null, myAddress);
let handleEvent = function(from, to) {
};
contract.on(filterFromMe, handleEvent);
contract.on(filterToMe, handleEvent); A future (backwards compatible) feature will be coming to create |
@ricmoo is there also the possibility of browsing for events through the provider prototype? The documentation isn't really clear there. This might seem like an unnecessary feature but to me it's necessary when browsing for events when the contract address is unknown or we have multiple contract addresses to fetch events from with the same signature (eg getting ALL ERC20 transfer events from ALL tokens) something similar to:
Where we swap the 'eth' for provider prototype perhaps? I'll try to come up with something as for some reason I'm getting errors with web3 when ethers is already installed. Weird, I didn't know web3 used ethers for ABI Encoding? -> web3/web3.js#1946 |
Sorry, my bad it's already there. It just took me a while to realise I have to encode manually and can not use the ABI since no contract address is known (results in ENS error) Edit: Or use the Interface I just found out about !
Regarding hashing the addresses, it works but you're probably right (as always!). Do they get fit automatically like function params in solidity or do I have to add the padding manually here? |
For clarity, does the |
Yes, only indexed parameters are placed in topics by the EVM. It would certainly be possible though, for me to do client side filtering on non-indexed parameters. I would need to think more about that, since it would need some way of communicating that the filter is not saving bandwidth... interesting idea though, thanks! :) |
Is it possible to apply the the v4 filters to let filterFromMe = contract.filters.Transfer(myAddress, null);
const logs = await provider.getLogs(filterFromMe);
... decode Or perhaps I need to use the existing filter like const filter = {
address: contract.address,
fromBlock: 0,
topics: [contract.interface.events.Transfer.topic]
}; And somehow manipulate the Thanks :) |
That will totally work, and is how it is handled internally: let filterFromMe = contract.filters.Transfer(myAddress, null);
filterFromMe.fromBlock = 0;
let logs = await provider.getLogs(filterFromMe); More advanced filters are coming soon, which will allow: // Check from is either myAddress OR otherAddress
let filterFromMe = contract.filters.Transfer(OR(myAddress, otherAddress), null); I'm just trying to figure out the best way to add |
Thanks @ricmoo. I tried it and it works fine. I think there is a slight issue with the typings on export declare type EventFilter = {
address?: string;
topics?: Array<string>;
}; Perhaps instead it should be export declare type EventFilter = {
address?: string;
topics?: Array<string>;
fromBlock?: BlockTag;
toBlock?: BLockTag
}; |
@ricmoo did you end up implementing the more advanced |
Hi,
I find the documentation on events a little confusing. How do I find events filtered by event type and an indexed parameter?
Web3 has a really simple and good syntax to do this with .watch() :
As I see it there are two methods to retrieve contract events with ethersjs.
contract.on_Eventname_().then()
As far as I can tell there is no way to add a filter object to this method like we can do with getLogs()?
I assume just because we use the contract object this doesn't necessarily mean it binds to wallet.address and looks only for events in which wallet.address is indexed?
And provider.getLogs :
Is this the correct syntax for retrieving all events with a topic that has user addresses indexed?
For example can we get all ERC20 transfer events originating from an address like follows? :
const event = this.contract.interface.events.Transfer
event.topics[1] = wallet.address
provider.getLogs({
fromBlock: 1,
toBlock: 'latest',
address: 0x.... (token contract address) ,
topics: event.topics
})
What do we put at 'topics'?
Since event.topics[0] is the signature we should keep that one
Should we replace event.topics[1] (which would be the 'from' address in Transfer event) with the address we want to index by?
What's the correct approach?
Perhaps a more unified approach comparable to the web3 one is interesting ?
Thanks in advance.
The text was updated successfully, but these errors were encountered: