-
-
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
Feature: better historical event API #463
Comments
You should be able to just use the In v5, contracts have a simpler interface too. Coming soon. :) |
I can confirm that getting past events with |
For a quick note about the upcoming v5, I've already added a method to Contract, which can be used as: I'll be putting it up soon for feedback. :) |
@ricmoo I tried just using let contractEnsName = '0x123....123';
let topic = ethers.utils.id("nameRegistered(bytes32,address,uint256)");
let filter = {
address: contractEnsName,
fromBlock: 7400416,
toBlock: 'latest',
topics: [ topic ]
}
provider.getLogs(filter).then((result) => {
console.log(result);
}) a few questions come up:
Thank you I'm still confused by both answers above because they don't provide actual examples. The reason why I want to migrate off web3.js - is because they have been inconsistent with their stability (like if you switch from npm to yarn - things will break) and I really loved ethers.js from day1(it was yesterday) because you allow doing things like nonce overwriting in the tx construction, non-hardcoded 50 blocks wait to throw and bunch of other cool stuff. We still need truffle team to convince to generate ethers.js-type of readable abi type, so we can just copy/paste from it. |
So, for a few examples (which I agree, the documentation could certainly use more of): let abi = [
"event Transfer(bytes32 indexed node, address owner)"
];
let ens = new Contract("0x314159265dD8dbb310642f98f50C066173C1259b", abi, provider)
// Get the filter event
filter = ens.filters.Transfer()
// {
// address: "0x314159265dD8dbb310642f98f50C066173C1259b",
// topics: [ "0xd4735d920b0f87494915f556dd9b54c8f309026070caea5c737245152564d266" ]
// }
// Oryou could pass in a parameter to the above call to filter by node, sine it is indexed,
// let filter = ens.filters.Transfer(ethers.utils.namehash("ricmoo.firefly.eth"));
// Now you can specify fromBlock and toBlock (you may pass in promises; no need to await)
filter.fromBlock = provider.getBlockNumber().then((b) => b - 10000);
filter.toBlock = "latest";
// And query:
provider.getLogs(filter).then((logs) => {
console.log(logs);
});
// Of course, you can get all events, just by not including topics (again, you can use promises):
let filterAll = {
address: provider.getNetwork().then((n) => n.ensAddress),
fromBlock: provider.getBlockNumber().then((b) => b - 10000),
toBlock: "latest"
}
// Or if you want an event listener, you can do something like:
contract.on("Transfer", function(node, owner, eventObject) {
console.log(node, owner, eventObject);
});
// You could also use a filter object:
// contract.on(ens.filters.Transfer(), function(node, owner, eventObject) { ... }
// Or listen for all events:
contract.on("*", function(eventObject) {
// If the event matches anything in the ABI, it will add the parsed parameters for you,
// otherwise, you only get the raw data and topics.
console.log(eventObject);
}); I will focus on getting more examples together in the next round of documentation, since I'll be sprucing it up for v5, but hopefully that will get you started. Here is an overview on the eventObject too, which is quite useful: https://docs.ethers.io/ethers.js/html/api-contract.html#event-emitter Let me know if you have any more questions. |
@ricmoo const ethers = require('ethers')
const fs = require('fs')
let abi = [
"event Transfer(address indexed from, address indexed to, uint tokens)"
];
let infuraProvider = new ethers.providers.InfuraProvider('mainnet');
let ens = new ethers.Contract("0xB8c77482e45F1F44dE1745F52C74426C631bDD52", abi, infuraProvider)
filter = ens.filters.Transfer()
filter.fromBlock = infuraProvider.getBlockNumber().then((b) => b - 10000);
filter.toBlock = "latest";
infuraProvider.getLogs(filter).then((logs) => {
logs.forEach((log) => {
const data = ens.interface.parseLog(log)
fs.appendFileSync('addressesBNB.csv', data.values.to +'\n')
})
}) is there a way to do |
I think that is as good as you are going to get for now. In v5, you will be able to do: ens.queryFilter(ens.filters.Transfer(), infuraProvider.getBlockNumber().then((b) => b - 10000), "latest") which should be coming out soon, as a public beta. :) |
@ricmoo I was able to record a video and tell the world about ethers.js |
The The dry-run can be done using the new Thanks! :) |
This works very well inn in v5, thanks Ricmoo! For those still on v4, here is another simple example: const topicTransfer= ethers.utils.id("Transfer(address,address,uint256)") //This is the interface for your event
console.log("topic ID for your event", topicTransfer);
const web3 = await getWeb3(); // Your provider
const logs = await web3.getLogs({
fromBlock: 0,
address: "0xC841dCc53D20560c77F0C6d858383568CDf96182", // Address of contract
toBlock: 'latest',
topics: [topicTransfer]
}).catch(() => [])
console.log("logs qued", logs); |
The transfer Event on the Smart Contract:
The above code is using web3. How can I replicate this simple code with ethers. Getting an array of the 'Transfer' event, so that I can get the |
If you are using v5 (which I recommend) this has been made much easier.
|
@ricmoo is it posible to filter by multiple addresses like in web3? |
Right now there isn't a way to get the union directly from the rpc. Maybe you can try parallelizing both requests. const fromLogsPromise = contract.queryFilter(...);
const toLogsPromise = contract.queryFilter(...);
const [fromLogs, toLogs] = await Promise.all([fromLogsPromise, toLogsPromise]);
Rpc doesn't include timestamp in the logs. So you have to call @kimiro34 Is it possible for you to deploy this contract to any public testnet, so that we can try to reproduce the problem? |
when I use contract.filter.event(), it's have an error: myContract.filter.myEvent() is not the function. How can I handle this issue? please help me |
I really like the library, but I spent an hour trying to figure out how to get past events, and when I did, it was painfully slow with
provider.resetEventsBlock(fromBlock)
Please let me know how I can have the same speed as web3.getPastEvents
Thank you
The text was updated successfully, but these errors were encountered: