Unable to catch events with provider when using Ganache-Core for test #1912
Replies: 19 comments
-
What if you configure the Ganache does blockchain things very synchronously, so the block number may already be up to date when the provider fetched the block number to compare against? |
Beta Was this translation helpful? Give feedback.
-
Also, |
Beta Was this translation helpful? Give feedback.
-
I have done this. Thank you! |
Beta Was this translation helpful? Give feedback.
-
I did this, I also made the provider listen to the block event in the beforeAll hook, but nothing logs still. Is there a way to force the provider to emit an event? I have practically been on this for days |
Beta Was this translation helpful? Give feedback.
-
You could call Oh!! I think I see a problem, maybe. You aren't waiting for the transaction to be mined... I'm not sure about Ganache, but this would be a problem in non-Ganache test cases. The events are only emitted once mined. Can you add |
Beta Was this translation helpful? Give feedback.
-
Sure, I did that already as well, I have also done it again, still nothing. Although when I debug the test, I see that the 'block' event is registered in the |
Beta Was this translation helpful? Give feedback.
-
Oh! I think this may be an issue with how polling is done, which is only on block change. As an experiment, can you add the following (in addition to the above async):
Does the test case exit? Because it should hang, as long as there are non-daemon intervals running... |
Beta Was this translation helpful? Give feedback.
-
I literarily have some tears in my eyes now... adding the last function worked! However, I added it to the top of the test case before it did.
|
Beta Was this translation helpful? Give feedback.
-
Ok... I will address this somehow.... I need to think of a better way to do this. The problem is that in a PoA network, which is what Ganache basically is, there are no blocks mined if there is nothing happening. And since ethers only triggers an event processing loop when a block happens, this never happens in a non-chatty PoA network. Is there a way to make ganache mine empty blocks periodically? This should also "help" the situation, but isn't a real fix. I'll think about this for a bit too... |
Beta Was this translation helpful? Give feedback.
-
Yeah, it requires some thinking to figure out. I appreciate your help thus far Richard. Thank you! I will also look into this issue and come up with suggestions as they come to mind. |
Beta Was this translation helpful? Give feedback.
-
In the polling section of the documentation, it says this This may make sense to lower for PoA networks or when polling a local node. ref (https://docs.ethers.io/ethers.js/html/api-providers.html?highlight=polling#properties) do you think it might just be normal to have a smaller polling time since it a local node recommendation? |
Beta Was this translation helpful? Give feedback.
-
Ganache does offer this feature with the If you don't want to do this, you can also manually mine a block by sending |
Beta Was this translation helpful? Give feedback.
-
I have the same problem. Neither Setup
Test
It works fine when run against a Docker Ganache CLI instance. |
Beta Was this translation helpful? Give feedback.
-
Is this still an issue? Or should I close it? |
Beta Was this translation helpful? Give feedback.
-
I have the same problem |
Beta Was this translation helpful? Give feedback.
-
Same over here. Callback doesn't run with const signer2 = await hre.ethers.provider.getSigner(2);
const receiver = signer2.getAddress();
const amount = hre.ethers.utils.parseEther('1120');
const Contract = await hre.ethers.getContractFactory("Token");
const contract = await Contract.deploy();
await contract.deployed();
function cb(from, to, amount) {
console.log(from, to, amount);
};
await contract.transfer(receiver, { value: amount });
console.log(hre.ethers.utils.formatEther(await signer2.getBalance()));
contract.on("Transfer", cb); |
Beta Was this translation helpful? Give feedback.
-
The way it('test case', async () => {
const contract = ...; // declare
contract.on("Transfer", cb); // 0ms
await contract.transfer(...args); // 100ms
}) // contract variable scope is finished, it is cleared As you can see your test case runs for such a small time, then moves to another test case. If you want to make that work, you might try waiting until the polling interval (also shortening the polling interval). await new Promise(res => setTimeout(res, 4000)); // waits for 4 secs However, to test events, I'd suggest to instead use an alternate method that doesn't need you to wait. it('testcase', async() => {
const tx = await contract.transfer(...args); // 100ms
const rc = await tx.wait(); // 0ms, as tx is already confirmed
const event = rc.events.find(event => event.event === 'Transfer');
const [from, to, value] = event.args;
console.log(from, to, value);
}) |
Beta Was this translation helpful? Give feedback.
-
Perfect explanation and workaround! Thanks @zemse |
Beta Was this translation helpful? Give feedback.
-
I am facing a problem with listening to events on a contract when using ganache-cli locally. Can anyone help ? I have the listener code running on a local node
And the event is generated from a truffle test. truffle test --show-events shows the event being generated, but the I am running |
Beta Was this translation helpful? Give feedback.
-
I am writing a test to listen to events on the blockchain, most especially the 'block' event. My tools are Ethers.js and Ganache-core. Using the
provider.on(event, callback)
function does not work. here is a copy of my code and what I was testing:Could there be a better way to do this? Kindly recommend.
Beta Was this translation helpful? Give feedback.
All reactions