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

API method updates #3122

Merged
merged 3 commits into from
Sep 12, 2024
Merged

API method updates #3122

merged 3 commits into from
Sep 12, 2024

Conversation

Defi-Moses
Copy link
Collaborator

@Defi-Moses Defi-Moses commented Sep 11, 2024

This adds the sdk functions getBridgeTxStatus and getSynapseTxId to the rest API, also adds a sub-method for finding the destination transaction information if a transaction is completed.

docs are already updated. Examples could be significantly improved.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Introduced three new API endpoints:
      • /getSynapseTxId for retrieving a Synapse transaction ID.
      • /getBridgeTxStatus for checking the status of a bridge transaction.
      • /getDestinationTx for retrieving the destination transaction hash.
  • Error Handling
    • Enhanced error handling and validation for all new endpoints to ensure robust responses.

Copy link
Contributor

coderabbitai bot commented Sep 11, 2024

Walkthrough

The changes introduce three new API endpoints to the Express application. The /getSynapseTxId endpoint retrieves a Synapse transaction ID based on query parameters and validates their presence. The /getBridgeTxStatus endpoint fetches the status of a bridge transaction using specified parameters, also including validation. Additionally, the /getDestinationTx endpoint retrieves the destination transaction hash based on other parameters, with similar validation and error handling. All endpoints ensure appropriate responses are sent to clients in case of missing parameters or processing errors.

Changes

File Change Summary
packages/rest-api/src/app.ts Added three new API endpoints: /getSynapseTxId for retrieving Synapse transaction IDs, /getBridgeTxStatus for fetching bridge transaction statuses, and /getDestinationTx for retrieving destination transaction hashes, all with validation and error handling.

Poem

In the code where rabbits hop,
New paths are laid, we’ll never stop!
Synapse IDs and statuses bright,
With every query, we take flight!
Hopping through errors, swift and spry,
Our API grows, oh me, oh my! 🐇✨


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

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

cloudflare-workers-and-pages bot commented Sep 11, 2024

Deploying sanguine-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: eb72eef
Status: ✅  Deploy successful!
Preview URL: https://bedcc419.sanguine-fe.pages.dev
Branch Preview URL: https://api-method-updates.sanguine-fe.pages.dev

View logs

Copy link

codecov bot commented Sep 11, 2024

Bundle Report

Changes will increase total bundle size by 409.81kB (1.14%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
sdk-router-@synapsecns/sdk-router-cjs* 527.05kB 409.81kB ⬆️
synapse-interface-server-cjs 1.47MB 1 bytes ⬇️

ℹ️ *Bundle size includes cached data from a previous commit

Copy link

codecov bot commented Sep 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 37.95310%. Comparing base (2e517b3) to head (eb72eef).
Report is 1 commits behind head on master.

Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #3122         +/-   ##
===================================================
+ Coverage   36.37896%   37.95310%   +1.57413%     
===================================================
  Files            438         418         -20     
  Lines          25512       24222       -1290     
  Branches          82          82                 
===================================================
- Hits            9281        9193         -88     
+ Misses         15486       14287       -1199     
+ Partials         745         742          -3     
Flag Coverage Δ
packages 90.96267% <ø> (ø)
promexporter ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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: 1

Outside diff range and nitpick comments (1)
packages/rest-api/src/app.ts (1)

475-507: Refactor error handling and improve parameter validation.

The /getSynapseTxId endpoint implementation looks good overall. It properly validates the input parameters, handles errors, and returns the expected response. However, consider the following improvements:

  1. Refactor the duplicated error handling code to a separate function for better maintainability.
  2. Make the error messages more descriptive to help users debug issues. For example, include the specific parameter that is missing or invalid.
  3. For consistency, validate and convert all parameters to the expected types. The originChainId parameter is converted to a number, but the bridgeModule and txHash parameters are kept as strings.

Apply this diff to implement the suggested improvements:

+const sendErrorResponse = (res, message, err) => {
+  res.status(400).send({
+    message,
+    error: err.message,
+  });
+};
+
 app.get('/getSynapseTxId', (req, res) => {
   try {
     const query = req.query;
-    const originChainId = Number(query.originChainId);
-    const bridgeModule = String(query.bridgeModule);
-    const txHash = String(query.txHash);
+    const { originChainId, bridgeModule, txHash } = query;
+
+    if (!originChainId || isNaN(Number(originChainId))) {
+      sendErrorResponse(res, 'Invalid originChainId parameter. Expected a number.', new Error('Invalid originChainId'));
+      return;
+    }
+
+    if (!bridgeModule) {
+      sendErrorResponse(res, 'Missing bridgeModule parameter.', new Error('Missing bridgeModule'));
+      return;
+    }
+
+    if (!txHash) {
+      sendErrorResponse(res, 'Missing txHash parameter.', new Error('Missing txHash'));
+      return;
+    }

-    if (!originChainId || !bridgeModule || !txHash) {
-      res.status(400).send({
-        message: 'Invalid request: Missing required parameters',
-      });
-      return;
-    }
     
-    Synapse.getSynapseTxId(originChainId, bridgeModule, txHash)
+    Synapse.getSynapseTxId(Number(originChainId), bridgeModule, txHash)
       .then((synapseTxId) => {
         res.json({ synapseTxId });
       })
       .catch((err) => {
-        res.status(400).send({
-          message:
-            'Ensure that your request matches the following format: /getSynapseTxId?originChainId=8453&bridgeModule=SynapseRFQ&txHash=0x4acd82091b54cf584d50adcad9f57c61055beaca130016ecc3798d3d61f5487a',
-          error: err.message,
-        });
+        sendErrorResponse(
+          res,
+          'Failed to retrieve Synapse transaction ID. Ensure that your request matches the following format: /getSynapseTxId?originChainId=8453&bridgeModule=SynapseRFQ&txHash=0x4acd82091b54cf584d50adcad9f57c61055beaca130016ecc3798d3d61f5487a',
+          err
+        );
       });
   } catch (err) {
-    res.status(400).send({
-      message:
-        'Ensure that your request matches the following format: /getSynapseTxId?originChainId=8453&bridgeModule=SynapseRFQ&txHash=0x4acd82091b54cf584d50adcad9f57c61055beaca130016ecc3798d3d61f5487a',
-      error: err.message,
-    });
+    sendErrorResponse(
+      res,
+      'Invalid request. Ensure that your request matches the following format: /getSynapseTxId?originChainId=8453&bridgeModule=SynapseRFQ&txHash=0x4acd82091b54cf584d50adcad9f57c61055beaca130016ecc3798d3d61f5487a',
+      err
+    );
   }
 });
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2e517b3 and 5d772f0.

Files selected for processing (1)
  • packages/rest-api/src/app.ts (1 hunks)

})
return
}

Copy link
Collaborator

@bigboydiamonds bigboydiamonds Sep 12, 2024

Choose a reason for hiding this comment

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

If we wanted to prevent unnecessary SDK calls in the scenario where Client passes in an unsupported destChainId, we can do a check if a valid chainId is outputted from, and return an error when destination ID isn't supported

Could change line 513 to

const destChainId = CHAINS_BY_ID[Number(query.destChainId)]
(using https://github.com/synapsecns/sanguine/blob/api/method-updates/packages/rest-api/src/constants/chains.ts/#L495)

but if we wanted to provide a more semantic error, we could do something like

  try {
    const query = req.query
    const destChainId = Number(query.destChainId)
    const bridgeModule = String(query.bridgeModule)
    const synapseTxId = String(query.synapseTxId)

    if (!destChainId || !bridgeModule || !synapseTxId) {
      res.status(400).send({
        message: 'Invalid request: Missing required parameters',
      })
      return
    }

   const supportedDestChainId = CHAINS_BY_ID[Number(query.destChainId)]

    if (!supportedDestChainId) {
      res.status(400).send({
        message: 'Invalid destChainId: Unsupported chainId',
      })
      return
    }

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: 2

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5d772f0 and eb72eef.

Files selected for processing (1)
  • packages/rest-api/src/app.ts (1 hunks)
Additional comments not posted (1)
packages/rest-api/src/app.ts (1)

475-507: LGTM!

The /getSynapseTxId endpoint is implemented correctly. It validates the required parameters, invokes the appropriate SDK method, and returns the transaction ID or a detailed error message based on the result.

packages/rest-api/src/app.ts Show resolved Hide resolved
packages/rest-api/src/app.ts Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants