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

Add MinRebalanceAmount to RFQ relayer #2263

Merged
merged 10 commits into from
Mar 13, 2024
Merged

Conversation

dwasse
Copy link
Collaborator

@dwasse dwasse commented Mar 13, 2024

Description
Adds MinRebalanceAmount which puts a floor on the rebalance size.

Summary by CodeRabbit

  • New Features
    • Enhanced inventory management with a minimum threshold for rebalance amounts.
  • Refactor
    • Updated logic to consider minimum and maximum rebalance amounts.
    • Improved configuration handling for rebalance amounts with new minimum threshold settings.
  • Tests
    • Expanded testing suite to cover new minimum rebalance amount configurations and logic.

@github-actions github-actions bot added go Pull requests that update Go code size/s labels Mar 13, 2024
Copy link
Contributor

coderabbitai bot commented Mar 13, 2024

Walkthrough

The update enhances rebalance management for RFQ services by introducing a minimum rebalance amount. This change ensures rebalance operations are first limited by the minimum amount before being capped by the maximum limit. It provides better control over inventory rebalancing, improving efficiency and effectiveness.

Changes

Files Change Summaries
.../inventory/manager.go Added logic to clip rebalance amount by the minimum value before the maximum limit.
.../relayer/relconfig/config.go Introduced MinRebalanceAmount field of type string in the TokenConfig struct.
.../relayer/relconfig/getters.go Implemented GetMinRebalanceAmount function to retrieve the minimum rebalance amount considering token configurations and scaling.

A whimsical ode by a coding hare,
To changes made with skill and care.
🌟 In the realm of code, where logic plays,
A new min bound sets the stage.
🚀 From minimum to max, we finely tune,
Ensuring our rebalance sings a harmonious tune.
🐇 With each hop forward, improvements we share,
Crafting a future, exceptionally rare.

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-tests 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 tests 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 tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

cloudflare-workers-and-pages bot commented Mar 13, 2024

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 43436e3
Status: ✅  Deploy successful!
Preview URL: https://2426b6a1.sanguine-fe.pages.dev
Branch Preview URL: https://feat-min-rebalance-amount.sanguine-fe.pages.dev

View logs

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.

Review Status

Actionable comments generated: 2

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between f6e4834 and 7347ae6.
Files selected for processing (4)
  • services/rfq/relayer/inventory/manager.go (1 hunks)
  • services/rfq/relayer/inventory/manager_test.go (6 hunks)
  • services/rfq/relayer/relconfig/config.go (1 hunks)
  • services/rfq/relayer/relconfig/getters.go (1 hunks)
Check Runs (15)
Go Generate (Module Changes) (services/stiprelayer) completed (3)
Go Generate (Module Changes) (services/rfq) completed (1)
Lint (services/rfq) completed (5)
  • services/rfq/relayer/relconfig/getters.go: 554-554: 554-580 lines are duplicate of relayer/relconfig/getters.go:586-612 (dupl)
  • services/rfq/relayer/relconfig/getters.go: 586-586: 586-612 lines are duplicate of relayer/relconfig/getters.go:554-580 (dupl)
Go Coverage (1.21.x, ubuntu-latest, services/stiprelayer) completed (7)
Go Coverage (1.21.x, ubuntu-latest, services/rfq) completed (5)
Get PR Metadata completed (2)
SonarCloud completed (2)
general-linters completed (1)
Change Detection completed (13)
label-statuses completed (3)
pr-labeler completed (2)
Build (1.21.x, ubuntu-latest) completed (3)
run-goreleaser (services/stiprelayer) completed (7)
run-goreleaser (services/rfq) completed (7)
build-goreleaser completed (2)
Additional comments: 3
services/rfq/relayer/inventory/manager_test.go (1)
  • 98-104: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [91-125]

The addition of minRebalanceAmount to the getConfig function and its subsequent usage in configuring MinRebalanceAmount for token configurations is correctly implemented. This change aligns with the PR's objective to introduce a minimum threshold for rebalance transactions. However, ensure that the minRebalanceAmount and maxRebalanceAmount parameters are validated for proper formatting and logical values (e.g., minRebalanceAmount should not exceed maxRebalanceAmount) before being used in configurations.

services/rfq/relayer/relconfig/config.go (1)
  • 105-106: Adding the MinRebalanceAmount field to the TokenConfig struct is correctly implemented and aligns with the PR's objective. However, consider the implications of using a string type for monetary values. While this allows for flexibility in specifying amounts, it also introduces the need for parsing and validation. Ensure that there are appropriate validations in place to parse these string values into numerical types safely and accurately, especially considering the potential for decimal values and the need to handle different token decimals.
services/rfq/relayer/relconfig/getters.go (1)
  • 550-580: The implementation of GetMinRebalanceAmount correctly retrieves and scales the minimum rebalance amount based on token decimals, which is crucial for accurate calculations. However, the static analysis tool flagged a potential duplication issue with lines 554-580 being similar to lines 586-612. Upon inspection, it seems there might have been a misunderstanding or a copy-paste error, as the latter lines do not exist in the provided code snippet. This might be a false positive from the tool. Nonetheless, ensure that there is no unintended code duplication within the project that could lead to maintenance issues.

Comment on lines 475 to 479
// clip the rebalance amount by the configured min
minAmount := cfg.GetMinRebalanceAmount(maxTokenData.ChainID, maxTokenData.Addr)
if amount.Cmp(minAmount) < 0 {
amount = minAmount
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation correctly applies the MinRebalanceAmount before considering the maximum rebalance amount. This ensures that rebalances are not executed for amounts below the specified minimum threshold, aligning with the PR's objectives. However, it's important to ensure that cfg.GetMinRebalanceAmount and cfg.GetMaxRebalanceAmount handle potential errors gracefully and return sensible defaults in case of misconfigurations or missing values. Additionally, consider adding logging or metrics around rebalance amounts to facilitate monitoring and debugging of the rebalancing process.

Comment on lines 155 to 163
cfgWithMax := getConfig("10", "1000000000")
rebalance, err = inventory.GetRebalance(cfgWithMax, tokens, origin, usdcDataOrigin.Addr)
i.NoError(err)
expected = &inventory.RebalanceData{
OriginMetadata: &usdcDataOrigin,
DestMetadata: &usdcDataDest,
Amount: big.NewInt(1e7),
}
i.Equal(expected, rebalance)
Copy link
Contributor

Choose a reason for hiding this comment

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

The test case setup using getConfig("10", "1000000000") to set a minimum rebalance amount demonstrates the intended functionality. However, it's important to include additional test cases that cover edge cases, such as when the minRebalanceAmount is higher than the available balance, to ensure robustness and handle potential errors gracefully.

Would you like me to help generate these additional test cases?

Copy link

codecov bot commented Mar 13, 2024

Codecov Report

Attention: Patch coverage is 52.38095% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 47.87293%. Comparing base (e513a28) to head (43436e3).
Report is 13 commits behind head on master.

Files Patch % Lines
services/rfq/relayer/relconfig/getters.go 25.00000% 9 Missing ⚠️
services/rfq/relayer/inventory/manager.go 88.88889% 1 Missing ⚠️
Additional details and impacted files
@@                 Coverage Diff                 @@
##              master       #2263         +/-   ##
===================================================
+ Coverage   47.84597%   47.87293%   +0.02695%     
===================================================
  Files            360         360                 
  Lines          26903       26915         +12     
  Branches          83          83                 
===================================================
+ Hits           12872       12885         +13     
- Misses         12691       12692          +1     
+ Partials        1340        1338          -2     
Flag Coverage Δ
rfq 32.45013% <52.38095%> (+0.27206%) ⬆️
stiprelayer 3.33333% <ø> (ø)

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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between 7347ae6 and e74b193.
Files selected for processing (2)
  • services/rfq/relayer/inventory/manager.go (1 hunks)
  • services/rfq/relayer/inventory/manager_test.go (6 hunks)
Check Runs (13)
codecov/patch completed (4)
Go Generate (Module Changes) (services/stiprelayer) completed (6)
Go Generate (Module Changes) (services/rfq) completed (3)
Go Coverage (1.21.x, ubuntu-latest, services/stiprelayer) completed (4)
Go Coverage (1.21.x, ubuntu-latest, services/rfq) completed (4)
Lint (services/rfq) completed (5)
Build (1.21.x, ubuntu-latest) completed (2)
Get PR Metadata completed (2)
SonarCloud completed (2)
Change Detection completed (13)
general-linters completed (1)
label-statuses completed (3)
pr-labeler completed (2)
Files skipped from review as they are similar to previous changes (2)
  • services/rfq/relayer/inventory/manager.go
  • services/rfq/relayer/inventory/manager_test.go

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.

Review Status

Actionable comments generated: 3

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between e74b193 and a2423d9.
Files selected for processing (3)
  • services/rfq/relayer/inventory/manager.go (2 hunks)
  • services/rfq/relayer/relconfig/config.go (1 hunks)
  • services/rfq/relayer/relconfig/getters.go (1 hunks)
Check Runs (14)
codecov/patch completed (3)
  • services/rfq/relayer/relconfig/getters.go: 556-556: Added line #L556 was not covered by tests
  • services/rfq/relayer/relconfig/getters.go: 561-563: Added lines #L561 - L563 were not covered by tests
  • services/rfq/relayer/relconfig/getters.go: 567-569: Added lines #L567 - L569 were not covered by tests
Go Generate (Module Changes) (services/stiprelayer) completed (6)
Lint (services/rfq) completed (4)
  • services/rfq/relayer/relconfig/config.go: 98-98: defualts is a misspelling of defaults (misspell)
Go Coverage (1.21.x, ubuntu-latest, services/stiprelayer) completed (4)
Go Coverage (1.21.x, ubuntu-latest, services/rfq) completed (4)
run-goreleaser (services/stiprelayer) completed (7)
run-goreleaser (services/rfq) completed (7)
Build (1.21.x, ubuntu-latest) completed (2)
Get PR Metadata completed (2)
Change Detection completed (13)
general-linters completed (1)
label-statuses completed (3)
pr-labeler completed (2)
build-goreleaser completed (2)
Additional comments: 2
services/rfq/relayer/relconfig/config.go (1)
  • 106-107: The addition of MinRebalanceAmount to the TokenConfig struct aligns with the PR objectives to introduce a minimum threshold for rebalance transactions. However, it's important to ensure that the type string is the most appropriate choice for representing amounts, considering the operations that will be performed on these values.

Consider verifying if a numeric type would be more suitable for representing MinRebalanceAmount, especially if arithmetic operations or comparisons are involved. Using a string requires parsing to a numeric type before any such operations, which could introduce overhead and potential for errors.

services/rfq/relayer/relconfig/getters.go (1)
  • 556-570: The function GetMinRebalanceAmount correctly implements the logic to retrieve and scale the minimum rebalance amount by token decimals. However, there are a few points to consider:
  • Ensure that the function is covered by unit tests to validate its behavior under various scenarios, including error handling and edge cases.
  • Verify the handling of MinRebalanceAmount as a string, especially the implications of parsing and scaling this value. Consider the potential for parsing errors and the impact on performance.

Comment on lines 469 to 479
// no need to rebalance since amount would not be positive
if amount.Cmp(big.NewInt(0)) <= 0 {
//nolint:nilnil
return nil, nil
}

// clip the rebalance amount by the configured min
minAmount := cfg.GetMinRebalanceAmount(maxTokenData.ChainID, maxTokenData.Addr)
if amount.Cmp(minAmount) < 0 {
amount = minAmount
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation of clipping the rebalance amount by the configured minimum before the maximum is correctly aligned with the PR objectives. However, it's crucial to ensure that the cfg.GetMinRebalanceAmount and cfg.GetMaxRebalanceAmount methods handle potential errors gracefully. If these methods can return errors, the current implementation might panic or behave unexpectedly when encountering misconfigurations or missing values. Consider adding error handling for these method calls to improve robustness.

Additionally, incorporating logging or metrics around rebalance amounts could facilitate monitoring and debugging of the rebalancing process. This would provide visibility into the operation of the rebalance logic, especially in production environments.

Would you like assistance in adding error handling and logging around these configurations?

@@ -95,13 +95,16 @@
// For now, specify the USD price of the token in the config.
PriceUSD float64 `yaml:"price_usd"`
// MinQuoteAmount is the minimum amount to quote for this token in human-readable units.
// for USDC-through-cctp pairs this defualts to $1,000.
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a typographical error in the comment above MinQuoteAmount. It should be "defaults" instead of "defualts".

- // for USDC-through-cctp pairs this defualts to $1,000.
+ // for USDC-through-cctp pairs this defaults to $1,000.

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.

Suggested change
// for USDC-through-cctp pairs this defualts to $1,000.
// for USDC-through-cctp pairs this defaults to $1,000.

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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between a2423d9 and 112197b.
Files selected for processing (1)
  • services/rfq/relayer/relconfig/config.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • services/rfq/relayer/relconfig/config.go

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.

Review Status

Actionable comments generated: 1

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between 112197b and 43436e3.
Files selected for processing (2)
  • services/rfq/relayer/inventory/manager.go (2 hunks)
  • services/rfq/relayer/inventory/manager_test.go (6 hunks)
Check Runs (1)
codecov/patch completed (4)
  • services/rfq/relayer/inventory/manager.go: 373-373: Added line #L373 was not covered by tests
Files skipped from review as they are similar to previous changes (1)
  • services/rfq/relayer/inventory/manager_test.go
Additional comments: 1
services/rfq/relayer/inventory/manager.go (1)
  • 469-478: The implementation of clipping the rebalance amount by the configured minimum amount (lines #L476-L478) before clipping it by the maximum amount is a critical addition to the rebalancing logic. This change aligns with the PR's objective to introduce MinRebalanceAmount for improving the efficiency of rebalancing operations. The logic correctly checks if the calculated rebalance amount is less than the minimum required amount and, if so, decides against proceeding with the rebalance.

This change effectively ensures that rebalancing operations are economically viable by preventing transactions below the specified minimum rebalance amount. It's a well-integrated addition that enhances the rebalancing strategy's effectiveness.

@@ -370,7 +370,7 @@
if err != nil {
return fmt.Errorf("could not get rebalance: %w", err)
}
if rebalance == nil {
if rebalance == nil || rebalance.Amount.Cmp(big.NewInt(0)) <= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

The logic to check if the rebalance amount is positive and proceed with rebalancing is sound. However, as noted in previous comments and static analysis hints, this new logic introduced at line #L373 is not covered by unit tests. It's crucial to ensure that this new behavior, especially the handling of MinRebalanceAmount, is thoroughly tested to prevent potential issues in rebalancing operations.

Consider adding unit tests that specifically cover scenarios where the rebalance amount is clipped by the minimum amount before the maximum. This will help validate the correctness of the new logic and ensure the feature works as intended across various scenarios.

@dwasse dwasse merged commit 60ca5cd into master Mar 13, 2024
46 checks passed
@dwasse dwasse deleted the feat/min-rebalance-amount branch March 13, 2024 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go Pull requests that update Go code needs-go-generate-services/rfq size/s
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants