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

Fix/minimum receive ibc wasm #344

Merged
merged 2 commits into from
Oct 3, 2024
Merged

Fix/minimum receive ibc wasm #344

merged 2 commits into from
Oct 3, 2024

Conversation

haunv3
Copy link
Contributor

@haunv3 haunv3 commented Oct 3, 2024

Summary by CodeRabbit

  • New Features

    • Updated the version of the @oraichain/oraidex-universal-swap package to 1.1.12.
    • Enhanced functionality for calculating minimum receive amounts during token swaps.
    • Introduced a new method for processing universal swaps with improved handling of recipient addresses.
  • Bug Fixes

    • Improved error handling for invalid swap routes and conditions.
  • Tests

    • Added new test cases and refined existing tests for better clarity and accuracy in testing token swaps.

Copy link

coderabbitai bot commented Oct 3, 2024

Walkthrough

The changes in this pull request involve updates to the @oraichain/oraidex-universal-swap package. The package.json file has been modified to increment the version number from 1.1.11 to 1.1.12. Additionally, significant modifications have been made to the UniversalSwapHandler class in handler.ts, including method signature updates, logic adjustments for token swaps, enhanced error handling, and the introduction of a new method. A new TypeScript file for decoding a memo string has also been added, alongside updates to the test suite to improve clarity and structure.

Changes

File Change Summary
packages/universal-swap/package.json Version updated from 1.1.11 to 1.1.12.
packages/universal-swap/src/handler.ts - Updated caculateMinimumReceive method to use simulateAmount.
- Enhanced error handling in generateMsgsSwap.
- Refined message generation logic in generateMsgsSmartRouterSwap and generateMsgsSwap.
- Updated processUniversalSwap method for better recipient handling.
- Refactored code for clarity and maintainability.
packages/universal-swap/src/universal-demos/decode-memo.ts Introduced a new file that decodes a base64-encoded memo string and logs the result.
packages/universal-swap/tests/index.spec.ts - Updated test cases with additional parameters for clarity.
- Mocked handleSimulateSwap function for controlled testing.
- Refined test logic to align with new parameters.

Possibly related PRs

Suggested reviewers

  • trung2891
  • quangdz1704
  • trungbach

🐰 In the garden where swaps take flight,
A version blooms, oh what a sight!
With tokens dancing, logic refined,
Error checks and tests aligned.
Hooray for changes, let’s celebrate,
In the world of swaps, we elevate! 🎉


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.

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

@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

🧹 Outside diff range and nitpick comments (6)
packages/universal-swap/src/universal-demos/decode-memo.ts (2)

3-14: Consider removing the IIFE if using a module system.

The entire code is wrapped in an Immediately Invoked Function Expression (IIFE). While this is a good practice to create a private scope in non-module environments, it might be unnecessary if you're using a module system (which is implied by the import statement at the top).

In a module system, each file already has its own scope, so the IIFE doesn't provide additional encapsulation. Removing it could make the code slightly cleaner and more straightforward.

If you decide to keep the IIFE, consider adding a comment explaining why it's being used in this context.


8-13: Remove or replace console.log in production code.

The use of console.dir() for logging the decoded memo might be intended for debugging purposes. In production code, it's generally recommended to use a proper logging system or remove such statements entirely.

Consider:

  1. Removing the console.log statement if it's not needed in production.
  2. Replacing it with a proper logging system that can be configured based on the environment.
  3. If keeping it for debugging, wrap it in a condition that checks for a debug flag or non-production environment.

Example of conditional logging:

if (process.env.NODE_ENV !== 'production') {
  console.dir({ encodedMemo }, { depth: null });
}
packages/universal-swap/tests/index.spec.ts (4)

Line range hint 1845-1889: Consider adding a test case for error handling in combineSwapMsgOraichain.

The current test cases cover various happy path scenarios for combining swap messages on Oraichain. To improve test coverage, consider adding a test case that checks how the function handles error conditions, such as invalid token addresses or insufficient balances.


Line range hint 1845-1889: Consider refactoring fee-related tests for better readability.

The test cases for checkRelayerFee and checkFeeRelayerNotOrai are well-structured, but their readability could be improved. Consider grouping these related tests into a describe block and using more descriptive test names. For example:

describe('Relayer Fee Checks', () => {
  it.each`
    fromDenom           | fromChainId  | relayerFeeAmount | expectedSufficiency
    ${'oraichain-token'}| ${'Oraichain'}| ${'0'}           | ${true}
    ${'oraichain-token'}| ${'Oraichain'}| ${'1000000'}     | ${false}
    // ... other test cases ...
  `('should correctly determine fee sufficiency for $fromDenom on $fromChainId', 
    async ({fromDenom, fromChainId, relayerFeeAmount, expectedSufficiency}) => {
      // Test implementation
    }
  );
});

This structure would make the tests more readable and easier to maintain.


Line range hint 1845-1889: Enhance documentation for complex smart routing test cases.

The test cases for smart routing and IBC transfers are comprehensive and cover various scenarios. However, due to their complexity, it would be beneficial to add more detailed comments explaining the purpose and expected outcome of each test case. This would make it easier for other developers to understand and maintain these tests in the future.

For example, before each test case, you could add a comment like:

// This test case verifies that the smart routing algorithm correctly handles
// a scenario where tokens are swapped from Oraichain to Osmosis through an
// intermediary chain, with multiple swap operations on each chain.

Adding such documentation would significantly improve the maintainability of these complex test cases.


Line range hint 1845-1889: Fix typo in function name and consider adding more test cases.

There's a typo in the function name caculateMinimumReceive. It should be calculateMinimumReceive.

-const minimumReceive = await universalSwap.caculateMinimumReceive();
+const minimumReceive = await universalSwap.calculateMinimumReceive();

Additionally, consider adding more test cases to cover edge scenarios, such as:

  1. When the relayer fee is equal to the swap amount
  2. When the bridge fee is 100%
  3. When dealing with tokens that have different decimal places

These additional test cases would help ensure the robustness of the calculateMinimumReceive function across various scenarios.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c8e6eb5 and 287e1c5.

📒 Files selected for processing (4)
  • packages/universal-swap/package.json (1 hunks)
  • packages/universal-swap/src/handler.ts (1 hunks)
  • packages/universal-swap/src/universal-demos/decode-memo.ts (1 hunks)
  • packages/universal-swap/tests/index.spec.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/universal-swap/package.json
🔇 Additional comments (4)
packages/universal-swap/src/universal-demos/decode-memo.ts (2)

1-1: LGTM: Import statement is correct.

The import of Memo from the proto file is appropriate for the functionality of this module.


4-7: ⚠️ Potential issue

Ensure compatibility with target environment.

The code uses Buffer.from() to decode the base64 string, which is specific to Node.js environments. If this code is intended to run in a browser or other non-Node.js environments, consider using a more universal method like atob() combined with Uint8Array.from().

Example:

const uint8Array = Uint8Array.from(atob(memo), c => c.charCodeAt(0));

To verify the intended runtime environment, please run:

packages/universal-swap/tests/index.spec.ts (1)

Line range hint 1-1889: Overall, the test suite is comprehensive and well-structured.

The test cases in this file cover a wide range of scenarios for the UniversalSwapHandler class and related functions, including complex cases involving smart routing and IBC transfers. The use of it.each for parameterized tests is a good practice that allows for concise testing of multiple scenarios.

Some areas for improvement have been identified:

  1. Adding error handling test cases for certain functions.
  2. Refactoring some test structures for better readability.
  3. Enhancing documentation for complex test cases.
  4. Fixing a typo in a function name.
  5. Adding more edge cases for the calculateMinimumReceive function.

Addressing these points will further improve the quality and maintainability of this test suite. Great job on creating such a comprehensive set of tests!

packages/universal-swap/src/handler.ts (1)

1210-1210: ⚠️ Potential issue

Fix typo in method name caculateMinimumReceive

There's a typo in the method name caculateMinimumReceive; it should be calculateMinimumReceive.

Apply this diff to correct the typo:

-async caculateMinimumReceive() {
+async calculateMinimumReceive() {

Likely invalid or redundant comment.

@haunv3 haunv3 merged commit fd9dc2b into main Oct 3, 2024
3 of 4 checks passed
@haunv3 haunv3 deleted the fix/minimum-receive-ibc-wasm branch October 3, 2024 10:40
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.

3 participants