Skip to content
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

Handle tx revert in useTransactor #907

Merged
merged 4 commits into from
Aug 15, 2024

Conversation

MattPereira
Copy link
Contributor

@MattPereira MattPereira commented Aug 13, 2024

Video Demo

https://www.loom.com/share/2727672272374fbba1c4afcc33b8d9a3?sid=d22eb180-f1de-4629-8256-413802920376

Issue

  • The useTransactor hook displays a success notification even if the transaction reverts, which is confusing for users.
  • When using tanstack's useMutation, the onSuccess will trigger even when the tx reverts

Proposal

  • Only show success notification when tx receipt status is success
  • Throw a "Transaction reverted" error if tx receipt status is not "success"
    • so that useMutation's onError is triggered
    • so that user sees a "Transaction reverted" notification
    • so developer that is relying on transaction successfully executing doesnt get wrecked 🫠

Example Resources

@MattPereira MattPereira changed the title Fix use transactor Handle tx revert in useTransactor Aug 13, 2024
@MattPereira MattPereira marked this pull request as ready for review August 14, 2024 21:30
Copy link
Collaborator

@technophile-04 technophile-04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of throwing hardcoded error string "Transaction reverted" , I tried digging in if viem/wagmi gives us nice error. Although wagmi docs mentions that if transaction revert it throws an error

Wagmi docs :

Screenshot 2024-08-15 at 7 44 48 PM

https://wagmi.sh/core/api/actions/waitForTransactionReceipt#waitfortransactionreceipt

But lol here they mentioned that docs are lying and they only hint you with status. So hardcoding "Transaction reverted" makes sense 🙌

Also tysm @MattPereira for great PR description, video and example!! The example was superhelpful to dig in more on error!!!

@technophile-04 technophile-04 merged commit b6d73d2 into scaffold-eth:main Aug 15, 2024
1 check passed
@MattPereira
Copy link
Contributor Author

MattPereira commented Aug 15, 2024

yeah I was hoping to figure out a way to offer the link to etherscan so dev / user could investigate why the tx reverted but only using notification.error doesn't throw an error, and using both throw new Erorr and a notification.error will show 2 notifications 🤔

@technophile-04
Copy link
Collaborator

and using both throw new Erorr and a notification.error will show 2 notifications 🤔

Yes so basically the main purpose useTransactor is that it shows UI notification of current transaction. So yeah whenever you use useTransactor it shows both sucess and error notification if it fails. We throw error outside from useTransactor callback function for people not to show again UI notification(since useTransactor already does it) but to execute some internal business logic if they want to execute if transaction fails.

Just merged #910 which points to blockexplorer if transaction is reverted 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants