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

Swap failed events: only reject committed txs with code !== 0 #3301

Merged
merged 4 commits into from
Jun 6, 2024

Conversation

jonator
Copy link
Member

@jonator jonator commented Jun 6, 2024

Before, we logged events if the broadcast failed (could be network error or signature rejection). Now, the promise that rejects that causes the error to be logged should largely come from failed txs on chain. Other lower priority errors, like random network errors or signature rejections, should be ignored.

Copy link

vercel bot commented Jun 6, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
osmosis-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 6, 2024 2:03pm
4 Ignored Deployments
Name Status Preview Comments Updated (UTC)
osmosis-frontend-datadog ⬜️ Ignored (Inspect) Visit Preview Jun 6, 2024 2:03pm
osmosis-frontend-dev ⬜️ Ignored (Inspect) Visit Preview Jun 6, 2024 2:03pm
osmosis-frontend-edgenet ⬜️ Ignored (Inspect) Jun 6, 2024 2:03pm
osmosis-testnet ⬜️ Ignored (Inspect) Visit Preview Jun 6, 2024 2:03pm

@jonator jonator requested a review from p0mvn June 6, 2024 00:43
Copy link
Contributor

coderabbitai bot commented Jun 6, 2024

Walkthrough

Recent updates to the codebase have enhanced error handling in the SwapTool component and the useSwap hook. In the SwapTool component, specific error message checks were removed, and a general log statement was added for rejected signatures. The useSwap hook now incorporates more detailed error objects for better error handling and logging, improving the robustness and clarity of error management in swap operations.

Changes

File Change Summary
packages/web/components/swap-tool/... Modified error handling logic in the SwapTool component, removing specific error message checks.
packages/web/hooks/use-swap.tsx Enhanced error handling in the useSwap hook by incorporating error objects and improving logging.

Poem

In the code, where swaps do dance,
Errors caught at every chance.
Logs now clearer, shining bright,
Guiding us through the darkest night.
With each fix, our code's delight,
Swapping safe, both day and night.
🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5ea2c22 and 2c73f13.

Files selected for processing (2)
  • packages/web/components/swap-tool/index.tsx (1 hunks)
  • packages/web/hooks/use-swap.tsx (5 hunks)
Additional context used
Biome
packages/web/components/swap-tool/index.tsx

[error] 363-386: Enforce to have the onClick mouse event with the onKeyUp, the onKeyDown, or the onKeyPress keyboard event. (lint/a11y/useKeyWithClickEvents)

Actions triggered using mouse events should have corresponding keyboard events to account for keyboard-only navigation.


[error] 391-410: Enforce to have the onClick mouse event with the onKeyUp, the onKeyDown, or the onKeyPress keyboard event. (lint/a11y/useKeyWithClickEvents)

Actions triggered using mouse events should have corresponding keyboard events to account for keyboard-only navigation.


[error] 977-978: Avoid redundant Boolean call (lint/complexity/noExtraBooleanCast)

It is not necessary to use Boolean call when a value will already be coerced to a boolean.
Unsafe fix: Remove redundant Boolean call


[error] 387-387: Provide an explicit type prop for the button element. (lint/a11y/useButtonType)

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset


[error] 596-617: Provide an explicit type prop for the button element. (lint/a11y/useButtonType)

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset


[error] 759-771: Provide an explicit type prop for the button element. (lint/a11y/useButtonType)

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

packages/web/hooks/use-swap.tsx

[error] 200-200: Avoid redundant Boolean call (lint/complexity/noExtraBooleanCast)

It is not necessary to use Boolean call when a value will already be coerced to a boolean.
Unsafe fix: Remove redundant Boolean call


[error] 516-516: Avoid redundant Boolean call (lint/complexity/noExtraBooleanCast)

It is not necessary to use Boolean call when a value will already be coerced to a boolean.
Unsafe fix: Remove redundant Boolean call

Additional comments not posted (2)
packages/web/hooks/use-swap.tsx (2)

272-281: Refactor error handling for clarity and maintainability.
[REFACTOR_SUGGESTion]

- if (!maxSlippage) return reject(new Error("Max slippage is not defined."));
- if (!inAmountInput.amount) return reject(new Error("Input amount is not specified."));
- if (!account) return reject(new Error("Account information is missing."));
- if (!swapAssets.fromAsset) return reject(new Error("From asset is not specified."));
- if (!swapAssets.toAsset) return reject(new Error("To asset is not specified."));
+ if (!maxSlippage) return reject(new Error("Max slippage is not defined."));
+ if (!inAmountInput.amount) return reject(new Error("Input amount is not specified."));
+ if (!account) return reject(new Error("Account information is missing."));
+ if (!swapAssets.fromAsset) return reject(new Error("From asset is not specified."));
+ if (!swapAssets.toAsset) return reject(new Error("To asset is not specified."));

294-296: Ensure proper error handling by providing more context in the error message.

- return reject(new Error(`Transaction preparation failed: ${error.message}`));
+ return reject(new Error(`Transaction preparation failed: ${error.message}`));

Likely invalid or redundant comment.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Outside diff range and nitpick comments (4)
packages/web/components/swap-tool/index.tsx (3)

Line range hint 362-385: Add keyboard accessibility for clickable elements.

- <button onClick={sendSwapTx}>
+ <button onClick={sendSwapTx} onKeyUp={sendSwapTx} onKeyDown={sendSwapTx}>

Ensure that actions triggered by mouse clicks are also accessible via keyboard events to improve accessibility.

Also applies to: 390-409


Line range hint 976-977: Remove redundant Boolean conversion.

- Boolean(swapState.error)
+ swapState.error

Directly use the expression without the Boolean call for cleaner and more efficient code.


Line range hint 386-386: Specify button type to prevent form submission.

- <button onClick={...}>
+ <button type="button" onClick={...}>

Explicitly set the button type to "button" to avoid unintended form submissions when the button is within a form element.

Also applies to: 595-616, 758-770

packages/web/hooks/use-swap.tsx (1)

Line range hint 513-513: Remove redundant Boolean conversion.

The Boolean conversion here is redundant as the expression itself is sufficient for a truthy/falsy evaluation.

- Boolean(quote?.amount.toDec().isPositive()) &&
+ quote?.amount.toDec().isPositive() &&

Comment on lines 395 to 400
({ code }) => {
if (code)
reject(
"Failed to send split route swap exact amount in message"
);
else resolve("multiroute");
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for clarity and maintainability.

Similar to the previous comment, the error handling here can be improved by providing more information about the failure.

- reject("Failed to send split route swap exact amount in message");
+ reject(`Failed to send split route swap exact amount in message with error code: ${code}`);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
({ code }) => {
if (code)
reject(
"Failed to send split route swap exact amount in message"
);
else resolve("multiroute");
({ code }) => {
if (code)
reject(`Failed to send split route swap exact amount in message with error code: ${code}`);
else resolve("multiroute");

packages/web/hooks/use-swap.tsx Show resolved Hide resolved
Comment on lines 376 to 379
({ code }) => {
if (code)
reject("Failed to send swap exact amount in message");
else resolve(pools.length === 1 ? "exact-in" : "multihop");
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for clarity and maintainability.

The error handling in the sendTradeTokenInTx function could be made clearer by using a more descriptive error message. Consider including details about the error code when the transaction fails.

- reject("Failed to send swap exact amount in message");
+ reject(`Failed to send swap exact amount in message with error code: ${code}`);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
({ code }) => {
if (code)
reject("Failed to send swap exact amount in message");
else resolve(pools.length === 1 ? "exact-in" : "multihop");
({ code }) => {
if (code)
reject(`Failed to send swap exact amount in message with error code: ${code}`);
else resolve(pools.length === 1 ? "exact-in" : "multihop");

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Outside diff range and nitpick comments (2)
packages/web/hooks/use-swap.tsx (2)

Line range hint 200-200: Remove redundant Boolean call for cleaner code.

- Boolean(swapAssets.fromAsset) && Boolean(swapAssets.toAsset);
+ swapAssets.fromAsset && swapAssets.toAsset;

Line range hint 510-510: Remove redundant Boolean call for cleaner code.

- Boolean(quoteForCurrentBalance) && !account?.txTypeInProgress;
+ quoteForCurrentBalance && !account?.txTypeInProgress;

packages/web/hooks/use-swap.tsx Outdated Show resolved Hide resolved
packages/web/hooks/use-swap.tsx Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Outside diff range and nitpick comments (3)
packages/web/components/swap-tool/index.tsx (3)

Line range hint 363-386: Add keyboard event handlers for accessibility.

Actions triggered by mouse events should also be accessible via keyboard events to support keyboard-only users. Consider adding onKeyUp, onKeyDown, or onKeyPress handlers to these elements.

Also applies to: 391-410


Line range hint 977-978: Remove redundant Boolean conversion.

The Boolean call here is unnecessary as the value is already coerced into a boolean by the conditional context. Simplify the code by removing the Boolean call.

- Boolean(swapState.error)
+ swapState.error

Line range hint 387-387: Specify button types explicitly.

The default type of a button in HTML is 'submit', which can cause unintended form submissions if placed inside a form. Specify the type explicitly to 'button' to prevent this behavior.

+ type="button"

Add this property to all buttons that do not intend to submit a form.

Also applies to: 596-617, 759-771

packages/web/hooks/use-swap.tsx Show resolved Hide resolved
Comment on lines +382 to +387
({ code }) => {
if (code)
reject(
new Error("Failed to send swap exact amount in message")
);
else resolve(pools.length === 1 ? "exact-in" : "multihop");
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor error handling to include error codes for better diagnostics.

- reject(new Error("Failed to send swap exact amount in message"));
+ reject(new Error(`Failed to send swap exact amount in message with error code: ${code}`));
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
({ code }) => {
if (code)
reject(
new Error("Failed to send swap exact amount in message")
);
else resolve(pools.length === 1 ? "exact-in" : "multihop");
({ code }) => {
if (code)
reject(
new Error(`Failed to send swap exact amount in message with error code: ${code}`)
);
else resolve(pools.length === 1 ? "exact-in" : "multihop");

packages/web/hooks/use-swap.tsx Show resolved Hide resolved
packages/web/hooks/use-swap.tsx Show resolved Hide resolved
packages/web/components/swap-tool/index.tsx Show resolved Hide resolved
@vercel vercel bot temporarily deployed to prod_swap_test June 6, 2024 14:03 Inactive
@jonator jonator merged commit 994085f into stage Jun 6, 2024
28 checks passed
@jonator jonator deleted the jonator/swap-failed branch June 6, 2024 14:05
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