Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

refactor: use toError and NEP-141 storage utils from internal-utils#548

Merged
cawabunga-bytes merged 2 commits into
betafrom
refactor/migrate-to-internal-utils
Jul 23, 2025
Merged

refactor: use toError and NEP-141 storage utils from internal-utils#548
cawabunga-bytes merged 2 commits into
betafrom
refactor/migrate-to-internal-utils

Conversation

@jobotics

@jobotics jobotics commented Jul 17, 2025

Copy link
Copy Markdown
Collaborator

Depends on defuse-protocol/sdk-monorepo#41

Summary by CodeRabbit

  • Refactor

    • Unified error handling by replacing local error conversion utilities with a standardized external package across multiple features and services.
    • Updated imports and internal usage to reflect this change for improved consistency.
  • Chores

    • Removed unused code related to NEAR NEP-141 token storage balance queries.
    • Cleaned up and reorganized imports in affected files.

@jobotics
jobotics requested a review from cawabunga-bytes July 17, 2025 22:11
@coderabbitai

coderabbitai Bot commented Jul 17, 2025

Copy link
Copy Markdown

Walkthrough

The changes consolidate error handling by replacing local implementations of the toError utility function with the standardized toError from the external @defuse-protocol/internal-utils package across multiple files. Additionally, NEAR NEP-141 storage balance functions are removed from the blockchain balance service and now sourced from the same external package, with corresponding import and usage updates.

Changes

File(s) Change Summary
src/features/gift/actors/giftMakerRootMachine.ts,
src/features/otcDesk/actors/otcMakerRootMachine.ts
Replaced local toError import with errors.toError from @defuse-protocol/internal-utils in error handling.
src/features/machines/depositMachine.ts,
src/features/machines/poaBridgeInfoActor.ts,
src/features/machines/signIntentMachine.ts,
src/features/machines/swapIntentMachine.ts
Removed local toError function and replaced usages with errors.toError from external package.
src/services/blockchainBalanceService.ts Removed exported NEAR NEP-141 storage balance functions and related imports.
src/services/nep141StorageService.ts Now imports NEAR NEP-141 storage balance functions from external package; adds nearClient as argument.
src/utils/errors.ts Removed local toError implementation and replaced internal calls with errors.toError from external package.

Poem

In burrows deep, a change takes root,
Old errors hop away, new ones in pursuit.
With toError now from one true source,
The code runs steady, on its course.
NEAR tokens leap to pastures new,
This rabbit cheers the streamlined view!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
src/features/machines/swapIntentMachine.ts (1)

424-431: Guard against missing event.error in all onError handlers

XState’s invoked service failures populate the error on event.data (or event.data.error) rather than event.error, so calling errors.toError(event.error) can end up with Error: undefined. Update each occurrence in src/features/machines/swapIntentMachine.ts to fall back to event.data or a literal:

Affected locations:

  • Line 428
  • Line 479
  • Line 540
  • Line 593

Suggested patch for each handler:

- reason: extractWalletErrorCode(event.error, "ERR_USER_DIDNT_SIGN"),
+ reason: extractWalletErrorCode(event.error ?? event.data, "ERR_USER_DIDNT_SIGN"),

- error: errors.toError(event.error),
+ error: errors.toError(event.error ?? event.data ?? "Unknown error"),

This ensures you never pass undefined into your error converter.

♻️ Duplicate comments (6)
src/features/machines/swapIntentMachine.ts (3)

476-482: Duplicate concern – see previous comment

The same null-safety issue applies here.


537-542: Duplicate concern – see previous comment

Same pattern; check the presence of event.error before calling toError.


590-595: Duplicate concern – see previous comment

Same pattern in the broadcasting error path.

src/utils/errors.ts (1)

35-36: Same semantic verification applies

findError relies on the same guarantee as hasMessage.

src/features/machines/signIntentMachine.ts (2)

168-176: Null-safety on event.error (same issue as swapIntentMachine)

Apply the same guard when converting the error payload.


225-229: Null-safety on event.error (same issue as swapIntentMachine)

🧹 Nitpick comments (3)
src/features/machines/swapIntentMachine.ts (1)

2-2: Import only what you need to keep the bundle slim

If the @defuse-protocol/internal-utils package exports toError as a named export, prefer

-import { errors } from "@defuse-protocol/internal-utils"
+import { toError } from "@defuse-protocol/internal-utils"

and call toError(...) directly.
Bringing in the whole errors namespace may pull extra helpers into the bundle that are never referenced elsewhere in this module.

src/utils/errors.ts (1)

1-1: Rename the imported symbol to avoid cognitive collision

Inside utils/errors.ts importing an object literally named errors can be confusing:

import { errors as internalErrors } from "@defuse-protocol/internal-utils"

Using an alias keeps a clear distinction between this file’s helpers and the upstream package.

src/features/machines/signIntentMachine.ts (1)

1-1: Alias import for clarity (same as utils/errors.ts)

import { errors as internalErrors } from "@defuse-protocol/internal-utils"

and update calls accordingly to avoid the double-meaning of “errors” in this domain model.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 41e7079 and 2235226.

📒 Files selected for processing (9)
  • src/features/gift/actors/giftMakerRootMachine.ts (2 hunks)
  • src/features/machines/depositMachine.ts (2 hunks)
  • src/features/machines/poaBridgeInfoActor.ts (2 hunks)
  • src/features/machines/signIntentMachine.ts (3 hunks)
  • src/features/machines/swapIntentMachine.ts (5 hunks)
  • src/features/otcDesk/actors/otcMakerRootMachine.ts (2 hunks)
  • src/services/blockchainBalanceService.ts (0 hunks)
  • src/services/nep141StorageService.ts (2 hunks)
  • src/utils/errors.ts (3 hunks)
💤 Files with no reviewable changes (1)
  • src/services/blockchainBalanceService.ts
🔇 Additional comments (11)
src/services/nep141StorageService.ts (2)

1-5: LGTM! Clean refactoring to external utilities package.

The import changes correctly move NEP-141 storage balance functions from the local blockchain service to the external @defuse-protocol/internal-utils package, and the nearClient import is properly added to provide the required nearProvider parameter.


46-46: Correct usage of nearProvider parameter.

The nearProvider: nearClient parameter is properly passed to both function calls, maintaining the same functionality while using the external utility functions.

Also applies to: 51-51

src/features/gift/actors/giftMakerRootMachine.ts (2)

1-1: LGTM! Consolidating error utilities to external package.

The import correctly changes from local toError utility to the standardized errors module from @defuse-protocol/internal-utils.


221-221: Correct usage of external toError utility.

The function call is properly updated to use errors.toError(event.error) instead of the local toError(event.error), maintaining identical functionality while using the external utility.

src/features/machines/depositMachine.ts (2)

1-1: LGTM! Consolidating error utilities to external package.

The import correctly adds the errors module from @defuse-protocol/internal-utils to replace the local toError utility.


214-214: Correct usage of external toError utility.

The function call is properly updated to use errors.toError(event.error) instead of the local toError(event.error), maintaining identical functionality while using the external utility.

src/features/otcDesk/actors/otcMakerRootMachine.ts (2)

1-1: LGTM! Consolidating error utilities to external package.

The import correctly changes from local toError utility to the standardized errors module from @defuse-protocol/internal-utils.


132-132: Correct usage of external toError utility.

The function call is properly updated to use errors.toError(event.error) instead of the local toError(event.error), maintaining identical functionality while using the external utility.

src/features/machines/poaBridgeInfoActor.ts (2)

1-1: LGTM! Consolidating error utilities to external package.

The import correctly adds the errors module from @defuse-protocol/internal-utils to replace the local toError utility.


94-94: Correct usage of external toError utility.

The function call is properly updated to use errors.toError(event.error) instead of the local toError(event.error), maintaining identical functionality while using the external utility.

src/utils/errors.ts (1)

12-13: Ensure third-party toError preserves original semantics

The previous local toError returned a new Error with the original value stringified.
If internal-utils throws for non-Error inputs, hasMessage will throw instead of returning false.

Verify that:

errors.toError(undefined) // does NOT throw

and behaves like the old helper.

@cawabunga-bytes
cawabunga-bytes merged commit 2691ed3 into beta Jul 23, 2025
2 of 3 checks passed
@cawabunga-bytes
cawabunga-bytes deleted the refactor/migrate-to-internal-utils branch July 23, 2025 12:38
cawabunga-bytes added a commit that referenced this pull request Jul 23, 2025
* beta:
  refactor: migrate failover and request utils (#554)
  refactor: migrate publishIntent(s) function (#552)
  refactor: migrate logger from internal-utils (#551)
  refactor: migrate poaBridge utils (#550)
  refactor: use toError and NEP-141 storage utils from internal-utils (#548)
cawabunga-bytes added a commit that referenced this pull request Jul 23, 2025
* beta:
  refactor: migrate runtime and waitForIntentSettlement utils (#553)
  refactor: migrate failover and request utils (#554)
  refactor: migrate publishIntent(s) function (#552)
  refactor: migrate logger from internal-utils (#551)
  refactor: migrate poaBridge utils (#550)
  refactor: use toError and NEP-141 storage utils from internal-utils (#548)
@amdefuse

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.0.0-beta.204 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants