DePayFi / web3-mock

🤡 JavaScript library to mock web3 responses either by emulating web3 wallets or web3 RPC requests.
https://depay.com
MIT License
87 stars 20 forks source link

Is there a way to trigger contract "sending" or "receipt" events #11

Closed megamattron closed 2 years ago

megamattron commented 2 years ago

I have mocked a transaction to a contract which seems to be working well, but I'm not sure how to trigger events like "sending" or "receipt". For example, in our app code something like:

        contract.methods.contractMethod(param).send()
            .on('sending', function () {
                // how to trigger this
            })
            .once('receipt', function (receipt) {
                // or this
            })

Thank you!

10xSebastian commented 2 years ago

Hi Matt 👋

Usually those are only abstractions of actual transaction confirmations (simple javascript loops and intervals wrapped in promises and/or callbacks).

You need to confirm the mocked transaction in order to trigger those: see here

It actually also required a small patch to make it work with web3js (which I've added in v11.2.0).

I've also added a test dedicated to web3js contract interactions that can serve as an example in this case: https://github.com/DePayFi/web3-mock/blob/main/tests/units/web3wallet/web3js-contracts.spec.js

Let me know if there is anything else.

megamattron commented 2 years ago

Thank you for the help! When I call confirm on my mocked transaction it seems to open an alert in the web UI shown in Cypress, is this what the 11.2 patch was supposed to fix? Perhaps I'm not correctly on the latest version?

10xSebastian commented 2 years ago

There is one thing I've noticed with web3js that transactions are not sent immediately. Hence in the example test I added a 1 second timeout before confirming it: see https://github.com/DePayFi/web3-mock/blob/main/tests/units/web3wallet/web3js-contracts.spec.js#L57

web3-mock does not trigger any alert dialogs, so maybe you're catching errors somewhere in your code and trigger an alert there?

Any errors in the cypress console?

10xSebastian commented 2 years ago

Otherwise you can also add a console.log('SENDING worked') as first thing in your on('sending') and a console.log('RECEIPT worked') as first thing in your once('receipt') callback in your code to see if you even get there.

10xSebastian commented 2 years ago

I think this has been solved – right?

Let me know if otherwise, or if you have any further feedback/questions @megamattron

megamattron commented 2 years ago

Yes, thank you. We didn't end up using this particular feature but the other fixes were super helpful, thanks again!