Skip to content

Add can-transfer check #4078

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

Open
wants to merge 1 commit into
base: stage
Choose a base branch
from

Conversation

artakmik-nitka
Copy link
Contributor

What is the purpose of the change:

Linear Task

Brief Changelog

Testing and Verifying

Copy link

vercel bot commented Apr 29, 2025

@artakmik-nitka is attempting to deploy a commit to the OsmoLabs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Apr 29, 2025

Walkthrough

The changes introduce a new validation step in the bridge quote process to ensure that asset transfers between chains are permitted before proceeding. This is accomplished by implementing a new function that queries transfer availability from an external API, using chain and asset identifiers. The quote method now calls this function and throws a specific error if transfers are not allowed, using the API's response for error messaging. Supporting types and mappings for asset denominations are also added to facilitate this check.

Changes

File(s) Change Summary
packages/bridge/src/int3face/index.ts Updated Int3faceBridgeProvider.getQuote to call checkCanTransfer before proceeding; throws error if denied.
packages/bridge/src/int3face/queries.ts Added CanTransferResponse interface, asset denom mapping, and new checkCanTransfer function.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Int3faceBridgeProvider
    participant checkCanTransfer (API)
    User->>Int3faceBridgeProvider: getQuote(params)
    Int3faceBridgeProvider->>checkCanTransfer (API): checkCanTransfer(fromChain, toChain, assetId, env)
    checkCanTransfer (API)-->>Int3faceBridgeProvider: {can_transfer, reason}
    alt can_transfer is true
        Int3faceBridgeProvider-->>User: Return bridge quote
    else can_transfer is false/undefined
        Int3faceBridgeProvider-->>User: Throw BridgeQuoteError with reason
    end
Loading
✨ 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.
    • 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 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 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 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.

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

🧹 Nitpick comments (1)
packages/bridge/src/int3face/index.ts (1)

54-78: Consider extracting the transfer validation logic

The getQuote method is quite complex and the added validation increases its complexity. Consider extracting this logic into a separate method for better maintainability.

+  private async validateTransfer(fromChain: BridgeChain, toChain: BridgeChain, fromAsset: BridgeAsset): Promise<void> {
+    if (toChain.chainId !== "dogecoin") {
+      throw new BridgeQuoteError({
+        bridgeId: Int3faceProviderId,
+        errorType: "UnsupportedQuoteError",
+        message: "Only Dogecoin is supported as a destination chain.",
+      });
+    }
+
+    // Check if transfer is available on Int3face side
+    const canTransfer = await checkCanTransfer(
+      fromChain.chainId,
+      toChain.chainId,
+      fromAsset.denom,
+      this.ctx.env
+    );
+
+    if (!canTransfer || canTransfer.can_transfer === false) {
+      throw new BridgeQuoteError({
+        bridgeId: Int3faceProviderId,
+        errorType: "UnsupportedQuoteError",
+        message: canTransfer?.reason || "Transfer verification failed or is not available at this time",
+      });
+    }
+  }

 async getQuote(params: GetBridgeQuoteParams): Promise<BridgeQuote> {
   const { fromAddress, fromChain, toChain, toAddress, fromAsset, fromAmount } = params;
 
-  if (toChain.chainId !== "dogecoin") {
-    throw new BridgeQuoteError({
-      bridgeId: Int3faceProviderId,
-      errorType: "UnsupportedQuoteError",
-      message: "Only Dogecoin is supported as a destination chain.",
-    });
-  }
-
-  // Check if transfer is available on Int3face side
-  const canTransfer = await checkCanTransfer(
-      fromChain.chainId,
-      toChain.chainId,
-      fromAsset.denom,
-      this.ctx.env
-  );
-
-  if (!canTransfer?.can_transfer) {
-    throw new BridgeQuoteError({
-      bridgeId: Int3faceProviderId,
-      errorType: "UnsupportedQuoteError",
-      message: canTransfer?.reason || "Transfer is not available at this time",
-    });
-  }
+  await this.validateTransfer(fromChain, toChain, fromAsset);

This refactoring separates concerns, making the code more maintainable and easier to test. The validateTransfer method handles all the validation logic while getQuote focuses on its primary responsibility.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cd3a03c and 22b0309.

📒 Files selected for processing (2)
  • packages/bridge/src/int3face/index.ts (3 hunks)
  • packages/bridge/src/int3face/queries.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/bridge/src/int3face/index.ts (3)
packages/bridge/src/int3face/queries.ts (1)
  • checkCanTransfer (54-75)
packages/bridge/src/errors.ts (1)
  • BridgeQuoteError (2-18)
packages/bridge/src/int3face/utils.ts (1)
  • Int3faceProviderId (1-1)
packages/bridge/src/int3face/queries.ts (1)
packages/utils/src/api-client.ts (1)
  • apiClient (54-148)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (4)
packages/bridge/src/int3face/queries.ts (1)

45-48: Well-structured interface for API response

This interface appropriately represents the response from the can-transfer API with proper typing for both the boolean flag and the reason message.

packages/bridge/src/int3face/index.ts (3)

24-24: Correctly imports the new function

The import statement is properly added for the new checkCanTransfer function.


54-54: Proper parameter destructuring to support new feature

The code now correctly destructures fromChain from the parameters to use in the transfer check.


65-70: Implementation of the can-transfer validation check

The implementation correctly uses the newly added checkCanTransfer function to validate whether the transfer is allowed before proceeding with quote generation.

Comment on lines +54 to +75
export async function checkCanTransfer(
srcChainId: string | number,
destChainId: string,
assetId: string,
env: "testnet" | "mainnet"
): Promise<CanTransferResponse> {
let srcChainIdConverted;

if (typeof srcChainId === "string" && srcChainId.startsWith("osmosis")) {
srcChainIdConverted = "osmosis"
} else {
srcChainIdConverted = srcChainId.toString();
}

const origin = env === "mainnet"
? "https://api.mainnet.int3face.zone/int3face"
: "https://api.testnet.int3face.zone/int3face";

const url = new URL(`/bridge/v1beta1/can-transfer/${srcChainIdConverted}/${destChainId}/${denomOfInt3face[assetId]}`, origin);

return apiClient<CanTransferResponse>(url.toString());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling and use the safer denom lookup method

The function needs to handle possible API errors and provide better context for debugging. Also, it should use a safer method to get the denom to avoid potential undefined values in the URL.

 export async function checkCanTransfer(
     srcChainId: string | number,
     destChainId: string,
     assetId: string,
     env: "testnet" | "mainnet"
 ): Promise<CanTransferResponse> {
   let srcChainIdConverted;

   if (typeof srcChainId === "string" && srcChainId.startsWith("osmosis")) {
     srcChainIdConverted = "osmosis"
   } else {
     srcChainIdConverted = srcChainId.toString();
   }

   const origin = env === "mainnet"
       ? "https://api.mainnet.int3face.zone/int3face"
       : "https://api.testnet.int3face.zone/int3face";

-  const url = new URL(`/bridge/v1beta1/can-transfer/${srcChainIdConverted}/${destChainId}/${denomOfInt3face[assetId]}`, origin);
+  const denom = getInt3faceDenom(assetId);
+  const url = new URL(`/bridge/v1beta1/can-transfer/${srcChainIdConverted}/${destChainId}/${denom}`, origin);

-  return apiClient<CanTransferResponse>(url.toString());
+  try {
+    return await apiClient<CanTransferResponse>(url.toString());
+  } catch (error) {
+    console.error(`Failed to check transfer availability: ${error}`);
+    // Rethrow the error to be handled by the caller
+    throw error;
+  }
 }

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +50 to +52
const denomOfInt3face: Record<string, string> = {
DOGE: 'dogecoin-doge'
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling for unsupported assets

The mapping only contains one entry for DOGE. When this mapping is used in checkCanTransfer, there's no validation to ensure the assetId exists in the mapping, which could lead to undefined values in the API URL.

 const denomOfInt3face: Record<string, string> = {
   DOGE: 'dogecoin-doge'
 }
+
+export function getInt3faceDenom(assetId: string): string {
+  const denom = denomOfInt3face[assetId];
+  if (!denom) {
+    throw new Error(`Unsupported asset ID: ${assetId}`);
+  }
+  return denom;
+}
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const denomOfInt3face: Record<string, string> = {
DOGE: 'dogecoin-doge'
}
const denomOfInt3face: Record<string, string> = {
DOGE: 'dogecoin-doge'
}
export function getInt3faceDenom(assetId: string): string {
const denom = denomOfInt3face[assetId];
if (!denom) {
throw new Error(`Unsupported asset ID: ${assetId}`);
}
return denom;
}

Comment on lines +72 to +78
if (!canTransfer?.can_transfer) {
throw new BridgeQuoteError({
bridgeId: Int3faceProviderId,
errorType: "UnsupportedQuoteError",
message: canTransfer?.reason || "Transfer is not available at this time",
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve null handling in the can-transfer check

The current check may not handle all edge cases properly. It's better to be more explicit about the conditions that should trigger the error.

-    if (!canTransfer?.can_transfer) {
+    if (!canTransfer || canTransfer.can_transfer === false) {
       throw new BridgeQuoteError({
         bridgeId: Int3faceProviderId,
         errorType: "UnsupportedQuoteError",
-        message: canTransfer?.reason || "Transfer is not available at this time",
+        message: canTransfer?.reason || "Transfer verification failed or is not available at this time",
       });
     }

This change makes the check more explicit by verifying both that the response exists and that the can_transfer property is specifically false. It also provides a more descriptive message when the reason is empty or when canTransfer is null.

📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!canTransfer?.can_transfer) {
throw new BridgeQuoteError({
bridgeId: Int3faceProviderId,
errorType: "UnsupportedQuoteError",
message: canTransfer?.reason || "Transfer is not available at this time",
});
}
if (!canTransfer || canTransfer.can_transfer === false) {
throw new BridgeQuoteError({
bridgeId: Int3faceProviderId,
errorType: "UnsupportedQuoteError",
message: canTransfer?.reason || "Transfer verification failed or is not available at this time",
});
}

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.

1 participant