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

adding originUserAddress for refunds [SLT-281] #3206

Merged
merged 3 commits into from
Sep 29, 2024

Conversation

Defi-Moses
Copy link
Collaborator

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

For aggregator integrations using the UI, the aggregator should supply the origin user address so that SynapseRFQ can refund in the case of a refund.

Simply adds the logic from the SDK

Summary by CodeRabbit

  • New Features

    • Introduced an optional originUserAddress parameter to the bridging API routes, enhancing request capabilities.
    • Updated controllers to process the new originUserAddress parameter for improved transaction handling.
    • Added validation for originUserAddress to ensure it is a valid address format.
  • Tests

    • Added test cases to validate responses for both valid and invalid originUserAddress inputs in the bridge routes.

Copy link
Contributor

coderabbitai bot commented Sep 28, 2024

Walkthrough

The pull request introduces an optional query parameter, originUserAddress, to multiple controller functions and API routes within the REST API. This parameter is designed to enhance the functionality of bridging operations by allowing the inclusion of the user's address from the origin chain. The changes involve modifying the function signatures and the way parameters are handled in the relevant files, ensuring that existing functionality remains intact while accommodating the new input.

Changes

File Change Summary
packages/rest-api/src/controllers/bridgeController.ts, packages/rest-api/src/controllers/bridgeTxInfoController.ts Added originUserAddress parameter, destructured from req.query, to both controller functions.
packages/rest-api/src/routes/bridgeRoute.ts, packages/rest-api/src/routes/bridgeTxInfoRoute.ts Introduced originUserAddress as an optional string query parameter in the respective API routes.
packages/rest-api/src/tests/bridgeRoute.test.ts, packages/rest-api/src/tests/bridgeTxInfoRoute.test.ts Added test cases to validate the behavior of the API with the new originUserAddress parameter.

Possibly related PRs

Suggested labels

size/m, M-deps

Suggested reviewers

  • Defi-Moses
  • trajan0x
  • bigboydiamonds

Poem

In the code where bunnies hop,
A new address we now drop.
With originUserAddress in tow,
Our bridges will surely grow!
Hopping through the lines so bright,
Making transactions feel just right! 🐇✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2c4a203 and 877b09b.

📒 Files selected for processing (1)
  • packages/rest-api/src/routes/bridgeTxInfoRoute.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/rest-api/src/routes/bridgeTxInfoRoute.ts

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>, 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 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
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 (6)
packages/rest-api/src/controllers/bridgeTxInfoController.ts (3)

14-22: LGTM! Consider adding type annotations for improved type safety.

The addition of originUserAddress to the destructured query parameters aligns well with the PR objective of incorporating the origin user address for refunds. This change enhances the functionality for processing refunds when necessary.

To improve type safety and code readability, consider adding type annotations to the destructured parameters. For example:

const {
  fromChain,
  toChain,
  amount,
  destAddress,
  fromToken,
  toToken,
  originUserAddress,
}: {
  fromChain: string,
  toChain: string,
  amount: string,
  destAddress: string,
  fromToken: string,
  toToken: string,
  originUserAddress?: string
} = req.query;

This will help catch potential type-related issues early and improve code maintainability.


33-36: LGTM! Consider adding input validation for originUserAddress.

The implementation of originUserAddress in the Synapse.allBridgeQuotes call is correct and handles both the presence and absence of the parameter well. The use of toString() ensures type consistency.

To enhance security and data integrity, consider adding input validation for originUserAddress. Here's a suggested implementation:

import { isAddress } from '@ethersproject/address';

// ... (earlier in the function)

const originUserAddressObj = originUserAddress && isAddress(originUserAddress.toString())
  ? { originUserAddress: originUserAddress.toString() }
  : {};

const quotes = await Synapse.allBridgeQuotes(
  Number(fromChain),
  Number(toChain),
  fromToken,
  toToken,
  amountInWei,
  originUserAddressObj
);

This ensures that originUserAddress is only included if it's a valid Ethereum address, preventing potential issues with invalid input.


Line range hint 1-62: Overall, the changes successfully implement the PR objective.

The addition of originUserAddress to the bridgeTxInfoController function effectively implements the PR objective of incorporating the origin user address for refunds. The changes are well-integrated into the existing code structure and handle both the presence and absence of the new parameter appropriately.

To further improve the implementation, consider the following architectural advice:

  1. Implement proper error handling for cases where originUserAddress is provided but invalid. This could involve adding a specific error message in the catch block or creating a custom error type.

  2. If originUserAddress becomes a common parameter across multiple endpoints, consider creating a middleware function to validate and process it before it reaches the controller. This would promote code reuse and maintain a consistent approach to handling this parameter across the application.

  3. Document the new originUserAddress parameter in the API documentation or OpenAPI/Swagger specifications if they exist. This will help maintain up-to-date documentation for API consumers.

These suggestions will enhance the robustness, maintainability, and user-friendliness of the implementation.

packages/rest-api/src/controllers/bridgeController.ts (3)

14-21: LGTM! Consider adding type annotations for improved type safety.

The addition of originUserAddress to the destructured parameters aligns well with the PR objective. This change enables the incorporation of the origin user address for refund processing.

Consider adding type annotations to the destructured parameters for improved type safety. For example:

const {
  fromChain,
  toChain,
  amount,
  fromToken,
  toToken,
  originUserAddress,
}: {
  fromChain: string,
  toChain: string,
  amount: string,
  fromToken: string,
  toToken: string,
  originUserAddress?: string
} = req.query;

This will help catch potential type-related issues early in development.


33-36: LGTM! Consider using object spread for improved readability.

The conditional passing of originUserAddress to Synapse.allBridgeQuotes is implemented correctly. It ensures that the origin user address is included only when present, which aligns with the PR objective.

For improved readability, consider using object spread syntax:

Synapse.allBridgeQuotes(
  Number(fromChain),
  Number(toChain),
  fromToken,
  toToken,
  amountInWei,
  originUserAddress ? { originUserAddress: originUserAddress.toString() } : {}
)

This change would make the code more concise while maintaining the same functionality.


Line range hint 14-36: Consider adding validation for originUserAddress.

While the implementation of originUserAddress is correct, it might be beneficial to add some validation to ensure the address is in the expected format before using it.

Consider adding a validation check for originUserAddress before using it:

import { isAddress } from '@ethersproject/address';

// ... existing code ...

if (originUserAddress && !isAddress(originUserAddress)) {
  return res.status(400).json({ error: 'Invalid origin user address' });
}

// ... rest of the function ...

This additional check will help prevent potential issues with invalid addresses being passed to the bridging function.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between eae6830 and c695ad4.

📒 Files selected for processing (4)
  • packages/rest-api/src/controllers/bridgeController.ts (2 hunks)
  • packages/rest-api/src/controllers/bridgeTxInfoController.ts (2 hunks)
  • packages/rest-api/src/routes/bridgeRoute.ts (1 hunks)
  • packages/rest-api/src/routes/bridgeTxInfoRoute.ts (1 hunks)
🔇 Additional comments (2)
packages/rest-api/src/routes/bridgeRoute.ts (2)

Line range hint 1-57: Overall changes look good, but a few points need attention.

The addition of the originUserAddress parameter aligns with the PR objectives of enhancing aggregator integrations for refund processing. However, please address the following points:

  1. Add validation for the new originUserAddress parameter as suggested in the previous comment.
  2. Ensure that the bridgeController is updated to handle this new parameter. If not, update it to process the originUserAddress for refund scenarios.
  3. Verify that the addition of this optional parameter doesn't break existing functionality for requests that don't include it.

To check if the bridgeController has been updated, run:

#!/bin/bash
# Search for recent changes in the bridgeController related to originUserAddress
git log -p -S "originUserAddress" -- src/controllers/bridgeController.ts

This will show us if and how the originUserAddress is being used in the controller.

Consider documenting the purpose and usage of the originUserAddress parameter in the project's API documentation or README file to ensure other developers understand its importance for refund processing.


52-57: LGTM! Consider adding validation for the new parameter.

The addition of the originUserAddress parameter to the OpenAPI documentation is correct and aligns with the PR objectives. However, consider adding validation for this new parameter to ensure it's a valid Ethereum address when provided.

Add the following validation to the existing validation checks:

check('originUserAddress')
  .optional()
  .isEthereumAddress()
  .withMessage('Invalid Ethereum address for originUserAddress'),

To ensure the new parameter is being used in the controller, let's check the bridgeController:

packages/rest-api/src/routes/bridgeTxInfoRoute.ts Outdated Show resolved Hide resolved
Copy link

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

Deploying sanguine-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: 877b09b
Status: ✅  Deploy successful!
Preview URL: https://55f4d9a9.sanguine-fe.pages.dev
Branch Preview URL: https://feat-api-custom-refund-addre.sanguine-fe.pages.dev

View logs

Copy link

codecov bot commented Sep 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.56974%. Comparing base (0240714) to head (877b09b).
Report is 2 commits behind head on master.

Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #3206         +/-   ##
===================================================
- Coverage   90.67524%   90.56974%   -0.10551%     
===================================================
  Files             60          54          -6     
  Lines           1244        1018        -226     
  Branches         148          82         -66     
===================================================
- Hits            1128         922        -206     
+ Misses           112          93         -19     
+ Partials           4           3          -1     
Flag Coverage Δ
packages 90.56974% <ø> (ø)
solidity ?

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.

@Defi-Moses Defi-Moses changed the title adding originUserAddress for refunds adding originUserAddress for refunds [SLT-281] Sep 28, 2024
@github-actions github-actions bot added size/s and removed size/xs labels Sep 28, 2024
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: 0

🧹 Outside diff range and nitpick comments (4)
packages/rest-api/src/tests/bridgeTxInfoRoute.test.ts (2)

32-51: LGTM! Consider adding an assertion for originUserAddress.

The new test case for valid input with a valid originUserAddress is well-structured and consistent with the existing tests. It correctly tests the API response and structure.

Consider adding an assertion to verify that the originUserAddress is correctly handled in the response, if applicable. For example:

expect(response.body[0]).toHaveProperty('originUserAddress', '0x742d35Cc6634C0532925a3b844Bc454e4438f44e')

This would ensure that the originUserAddress is correctly processed and returned by the API.


53-69: LGTM! Consider using a constant for the error message.

The new test case for an invalid originUserAddress is well-structured and correctly tests the error handling of the API.

For consistency and maintainability, consider defining the error message as a constant at the top of the file or in a separate constants file. This approach is often used for error messages that might be reused or changed in the future. For example:

const INVALID_ORIGIN_USER_ADDRESS_ERROR = 'Invalid originUserAddress address'

// In the test case
expect(response.body.error).toHaveProperty('message', INVALID_ORIGIN_USER_ADDRESS_ERROR)

This makes it easier to update the error message across all occurrences if needed.

packages/rest-api/src/tests/bridgeRoute.test.ts (2)

28-43: LGTM! Consider adding an assertion for originUserAddress.

The new test case for a valid originUserAddress is well-structured and consistent with the existing tests. It correctly checks for the expected response status and body structure.

Consider adding an assertion to verify that the originUserAddress is correctly handled in the response. For example:

expect(response.body[0]).toHaveProperty('originUserAddress', '0x742d35Cc6634C0532925a3b844Bc454e4438f44e')

This would ensure that the API is correctly processing and returning the originUserAddress.


76-91: LGTM! Consider testing with different invalid address formats.

The new test case for an invalid originUserAddress is well-structured and correctly checks for the expected error response.

To improve the robustness of the test, consider adding more test cases with different types of invalid addresses. For example:

  1. An address with an invalid checksum
  2. An address that's too short
  3. An address that's too long

This would ensure that the API correctly handles various types of invalid addresses. You could implement this as separate test cases or use a parameterized test if your testing framework supports it.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c695ad4 and 2c4a203.

📒 Files selected for processing (4)
  • packages/rest-api/src/routes/bridgeRoute.ts (3 hunks)
  • packages/rest-api/src/routes/bridgeTxInfoRoute.ts (2 hunks)
  • packages/rest-api/src/tests/bridgeRoute.test.ts (2 hunks)
  • packages/rest-api/src/tests/bridgeTxInfoRoute.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/rest-api/src/routes/bridgeRoute.ts
  • packages/rest-api/src/routes/bridgeTxInfoRoute.ts
🔇 Additional comments (2)
packages/rest-api/src/tests/bridgeTxInfoRoute.test.ts (1)

32-69: Great addition of test cases for originUserAddress!

The two new test cases effectively cover the handling of the originUserAddress parameter in the Bridge TX Info Route. They test both the successful case with a valid address and the error case with an invalid address. These additions improve the overall test coverage and help ensure the correct implementation of the new functionality.

To further enhance these tests, consider:

  1. Adding an assertion for originUserAddress in the successful case, if applicable.
  2. Using a constant for the error message in the invalid case for better maintainability.

These changes will make the tests more robust and easier to maintain in the future.

packages/rest-api/src/tests/bridgeRoute.test.ts (1)

Line range hint 28-91: Overall, the new test cases are well-implemented and valuable additions.

The new test cases for originUserAddress effectively cover both valid and invalid scenarios, enhancing the test coverage for the Bridge Route API. They are consistent with the existing test structure and provide good validation for the new functionality.

To further improve the test suite, consider:

  1. Implementing the suggested assertions and additional test cases for invalid addresses.
  2. Ensuring that these tests are part of your CI/CD pipeline to catch any regressions related to the originUserAddress functionality.
  3. Updating the API documentation to reflect the new originUserAddress parameter, if not already done.

Great job on improving the test coverage!

Copy link

codecov bot commented Sep 28, 2024

Bundle Report

Changes will decrease total bundle size by 1.76kB (-0.0%) ⬇️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
synapse-interface-client-array-push* 7.28MB 147 bytes (0.0%) ⬆️
synapse-interface-server-cjs 1.49MB 127 bytes (0.01%) ⬆️
widget-cjs-esm* 271.27kB 2.03kB (-0.74%) ⬇️

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

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