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

BE-675 | InGivenOut APIs for balancer, stableswap, transmuter pools #603

Merged
merged 12 commits into from
Jan 21, 2025

Conversation

deividaspetraitis
Copy link
Collaborator

@deividaspetraitis deividaspetraitis commented Jan 17, 2025

This pull request implements InGivenOut APIs for balancer, stableswap, transmuter pools, more specifically:

  • CalculateTokenInByTokenOut
  • ChargeTakerFeeExactOut

New APIs ( and some existing ) were covered with unit test while all other changes are a ripple effect caused by introducing these new APIs.

Summary by CodeRabbit

  • New Features

    • Added support for calculating token input amounts based on token output.
    • Introduced methods for charging taker fees on exact output amounts.
    • Enhanced pool routing with more detailed token denomination handling.
  • Improvements

    • Updated struct field tags for improved JSON serialization.
    • Expanded routable pool interfaces with new calculation methods.
  • Technical Updates

    • Modified function signatures across multiple pool implementation types.
    • Added token input denomination tracking in candidate pool structures.

@deividaspetraitis deividaspetraitis self-assigned this Jan 17, 2025
@@ -4,6 +4,7 @@ package types
// candidate pool to be used for routing.
type CandidatePool struct {
ID uint64
TokenInDenom string
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note: Adding new TokenInDenom field at this point led to more elegant and simpler to understand implementation than alternative appoach by simply renaming existing field to be used for either ExactIn or ExactOut.

Copy link
Member

Choose a reason for hiding this comment

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

Please add a note that only one must be set at a time

@deividaspetraitis deividaspetraitis marked this pull request as ready for review January 20, 2025 13:00
Copy link
Contributor

coderabbitai bot commented Jan 20, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

This pull request introduces comprehensive changes across multiple files in the router and domain layers, focusing on enhancing token routing and pool calculation capabilities. The modifications primarily involve adding new methods to routable pool implementations, updating method signatures to include token input denominations, and standardizing JSON struct tags. These changes aim to provide more flexible and explicit handling of token calculations, fees, and pool routing logic.

Changes

File Change Summary
domain/mocks/pool_mock.go Added methods: CalculateTokenInByTokenOut and ChargeTakerFeeExactOut, along with a function pointer and a mocked token input variable.
domain/routable_pool.go Added two interface methods: CalculateTokenInByTokenOut and ChargeTakerFeeExactOut.
ingest/types/candidate_route.go Added TokenInDenom field to CandidatePool struct.
pools/usecase/pools_usecase.go Modified NewRoutablePool function calls to include token input denominations.
router/usecase/pools/pool_factory.go Updated NewRoutablePool function signature to accept token input denomination.
router/usecase/pools/* Updated multiple pool implementations with:
- New methods for token input/output calculations
- Standardized JSON struct tags.
router/usecase/routertesting/quote.go Updated newRoutablePool method to include both tokenInDenom and tokenOutDenom parameters.

Possibly related PRs

Suggested reviewers

  • p0mvn

Poem

🐰 Routing tokens with grace and might,
Through pools that shine so crystal bright,
Input, output, fees galore,
Our code now opens a new door!
Hop along the blockchain's delight! 🌈


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a915a28 and 6ae40cb.

⛔ Files ignored due to path filters (2)
  • router/usecase/quote_test.go is excluded by !**/*_test.go
  • router/usecase/route/route_test.go is excluded by !**/*_test.go
📒 Files selected for processing (5)
  • domain/mocks/pool_mock.go (6 hunks)
  • router/usecase/pools/routable_balancer_pool.go (3 hunks)
  • router/usecase/pools/routable_concentrated_pool.go (5 hunks)
  • router/usecase/pools/routable_cw_transmuter_pool.go (4 hunks)
  • router/usecase/pools/routable_stableswap_pool.go (2 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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: 13

🧹 Nitpick comments (2)
router/usecase/pools/routable_balancer_pool.go (1)

38-46: Enhance error handling in CalculateTokenInByTokenOut.

Consider wrapping the error with additional context to aid in debugging.

 func (r *routableBalancerPoolImpl) CalculateTokenInByTokenOut(ctx context.Context, tokenOut sdk.Coin) (sdk.Coin, error) {
 	tokenIn, err := r.ChainPool.CalcInAmtGivenOut(sdk.Context{}, sdk.Coins{tokenOut}, r.TokenInDenom, r.GetSpreadFactor())
 	if err != nil {
-		return sdk.Coin{}, err
+		return sdk.Coin{}, fmt.Errorf("failed to calculate token in amount: %w", err)
 	}

 	return tokenIn, nil
 }
router/usecase/pools/routable_cw_transmuter_pool.go (1)

76-95: LGTM! Implementation correctly follows transmuter pool logic.

The implementation properly validates pool type and balance before performing a no-slippage swap.

Fix typo in comment.

There's a typo in the comment: "Esnure" should be "Ensure".

-	// Esnure that the pool is concentrated
+	// Ensure that the pool is concentrated
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e083e06 and ceee930.

⛔ Files ignored due to path filters (7)
  • pools/usecase/pools_usecase_test.go is excluded by !**/*_test.go
  • router/usecase/pools/routable_concentrated_pool_test.go is excluded by !**/*_test.go
  • router/usecase/pools/routable_cw_alloy_transmuter_pool_test.go is excluded by !**/*_test.go
  • router/usecase/pools/routable_cw_orderbook_pool_test.go is excluded by !**/*_test.go
  • router/usecase/pools/routable_cw_transmuter_pool_test.go is excluded by !**/*_test.go
  • router/usecase/pools/routable_pool_test.go is excluded by !**/*_test.go
  • router/usecase/quote_test.go is excluded by !**/*_test.go
📒 Files selected for processing (15)
  • domain/mocks/pool_mock.go (4 hunks)
  • domain/mocks/pools_usecase_mock.go (1 hunks)
  • domain/routable_pool.go (1 hunks)
  • ingest/types/candidate_route.go (1 hunks)
  • pools/usecase/pools_usecase.go (2 hunks)
  • router/usecase/pools/pool_factory.go (5 hunks)
  • router/usecase/pools/routable_balancer_pool.go (3 hunks)
  • router/usecase/pools/routable_concentrated_pool.go (4 hunks)
  • router/usecase/pools/routable_cw_alloy_transmuter_pool.go (4 hunks)
  • router/usecase/pools/routable_cw_orderbook_pool.go (4 hunks)
  • router/usecase/pools/routable_cw_pool.go (4 hunks)
  • router/usecase/pools/routable_cw_transmuter_pool.go (3 hunks)
  • router/usecase/pools/routable_result_pool.go (3 hunks)
  • router/usecase/pools/routable_stableswap_pool.go (2 hunks)
  • router/usecase/routertesting/quote.go (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (21)
ingest/types/candidate_route.go (1)

7-7: Well-structured addition of TokenInDenom field!

The new field enhances the clarity of token flow direction and supports both ExactIn and ExactOut scenarios elegantly.

domain/routable_pool.go (1)

62-65: Clean interface extension for ExactOut operations!

The new methods CalculateTokenInByTokenOut and ChargeTakerFeeExactOut complete the interface's support for both ExactIn and ExactOut operations, maintaining consistency with existing patterns.

router/usecase/routertesting/quote.go (1)

44-48: Comprehensive test helper adaptation!

The newRoutablePool method has been correctly updated to support both ExactIn and ExactOut scenarios, with proper parameter ordering.

router/usecase/pools/routable_balancer_pool.go (1)

22-25: LGTM: Consistent struct tag formatting!

The JSON struct tags now use backticks consistently, following Go best practices.

router/usecase/pools/pool_factory.go (2)

19-19: LGTM! The changes to support InGivenOut APIs are consistent.

The addition of tokenInDenom parameter and its propagation to all pool types is implemented correctly.

Also applies to: 40-40, 60-60, 85-85, 91-91


96-96: LGTM! The changes to support InGivenOut APIs in CosmWasm pools are consistent.

The addition of tokenInDenom parameter and its propagation to CosmWasm pool types is implemented correctly.

Also applies to: 119-119

router/usecase/pools/routable_cw_pool.go (1)

33-40: LGTM! The struct field tag changes improve readability.

The change from double quotes to backticks for JSON tags is a good practice.

router/usecase/pools/routable_concentrated_pool.go (1)

28-32: LGTM! The struct field tag changes improve readability.

The change from double quotes to backticks for JSON tags is a good practice.

router/usecase/pools/routable_cw_orderbook_pool.go (1)

26-32: LGTM! The struct field tag changes improve readability.

The change from double quotes to backticks for JSON tags is a good practice.

domain/mocks/pool_mock.go (2)

22-22: LGTM! Function pointer for mocking token calculation.

The function pointer allows flexible mocking of token in calculation behavior during tests.


37-37: LGTM! Mock variable for token input.

The mockedTokenIn variable complements the existing mockedTokenOut for comprehensive mocking capabilities.

router/usecase/pools/routable_cw_alloy_transmuter_pool.go (1)

23-29: LGTM! Improved JSON struct tags.

Changed from double quotes to backticks for better readability and to allow special characters in tags.

pools/usecase/pools_usecase.go (2)

183-183: LGTM! Added token in denomination parameter.

The change correctly propagates the token in denomination to the routable pool creation.


266-268: LGTM! Clear documentation for empty parameters.

The comment clearly explains why empty strings are used for token denominations in spot price calculation.

router/usecase/pools/routable_stableswap_pool.go (3)

22-25: LGTM!

The JSON struct tag formatting changes improve code readability.


38-46: LGTM! Implementation is correct and consistent.

The implementation correctly uses CalcInAmtGivenOut with proper error handling and spread factor consideration.


63-68: LGTM! Fee calculation is correctly implemented.

The implementation properly utilizes poolmanager.CalcTakerFeeExactOut for fee calculation.

router/usecase/pools/routable_cw_transmuter_pool.go (2)

21-26: LGTM!

The JSON struct tag formatting changes improve code readability.


118-122: LGTM! Fee calculation is correctly implemented.

The implementation properly utilizes poolmanager.CalcTakerFeeExactOut for fee calculation.

router/usecase/pools/routable_result_pool.go (1)

28-35: LGTM!

The JSON struct tag formatting changes improve code readability.

domain/mocks/pools_usecase_mock.go (1)

114-114: LGTM! Parameter update is correct.

The NewRoutablePool call has been properly updated to include the required tokenInDenom parameter.

router/usecase/pools/routable_cw_pool.go Show resolved Hide resolved
router/usecase/pools/routable_cw_pool.go Show resolved Hide resolved
domain/mocks/pool_mock.go Outdated Show resolved Hide resolved
router/usecase/pools/routable_result_pool.go Show resolved Hide resolved
router/usecase/pools/routable_result_pool.go Show resolved Hide resolved
@@ -4,6 +4,7 @@ package types
// candidate pool to be used for routing.
type CandidatePool struct {
ID uint64
TokenInDenom string
Copy link
Member

Choose a reason for hiding this comment

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

Please add a note that only one must be set at a time

Comment on lines +40 to 41
TokenInDenom: tokenInDenom,
TokenOutDenom: tokenOutDenom,
Copy link
Member

Choose a reason for hiding this comment

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

This TokenInDenom and TokenOutDenom makes sense in the context of this PR but I am worried that if we were to look at it from a higher level, it would be confusing and unclear that only one of can be set at a time. Is there a way we could enhance the abstraction to reflect the restriction?

Copy link
Member

Choose a reason for hiding this comment

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

(Since I don't have a specific recommendation and, if you've already spent meaningful time on thinking through this, please feel free not to block on this comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually both will be set since when we are requesting a quote we specify denoms for both tokenIn and tokenOut, however depending on swap method we specify amount only for one of those.

router/usecase/pools/routable_balancer_pool.go Outdated Show resolved Hide resolved
router/usecase/pools/routable_cw_transmuter_pool.go Outdated Show resolved Hide resolved
@p0mvn
Copy link
Member

p0mvn commented Jan 21, 2025

Nice work!

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.

2 participants