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

refactor(coinswap): returns the result value of swap #379

Closed
wants to merge 3 commits into from

Conversation

dreamer-zq
Copy link
Contributor

@dreamer-zq dreamer-zq commented Sep 28, 2023

returns the result value of swap

Summary by CodeRabbit

  • New Features
    • Introduced a new field to display the swapped amount in the coin swap function.
    • Added two new trade types: BuyDouble and SellDouble.
  • Bug Fixes
    • Updated the Swap function to handle errors more effectively.
  • Refactor
    • Modified several function signatures to return a Coin object instead of an Int object.
  • Tests
    • Adjusted the test suite for the Swap function to capture and handle errors.

@dreamer-zq dreamer-zq self-assigned this Sep 28, 2023
@dreamer-zq dreamer-zq added enhancement New feature or request modules/coinswap Changes, features and bugs related to the coinswap module. labels Sep 28, 2023
Copy link

coderabbitai bot commented Nov 6, 2023

Walkthrough

The changes revolve around enhancing the coinswap module in a blockchain application. The modifications include the introduction of a new tradeFuncs field in the Keeper struct, changes in function signatures to return sdk.Coin instead of sdkmath.Int, and updates to the MsgSwapCoinResponse message in the protocol buffer definition to include an amount field.

Changes

File Change Summary
modules/coinswap/keeper/.../keeper.go Introduced a new tradeFuncs field in the Keeper struct and updated the Swap function to use this field. The return type of Swap is also changed to (sdk.Coin, error).
modules/coinswap/keeper/.../msg_server.go Updated the SwapCoin function to return an Amount field in the MsgSwapCoinResponse response type.
modules/coinswap/keeper/.../swap.go Changed the return type of several functions from sdkmath.Int to sdk.Coin.
modules/coinswap/keeper/.../swap_test.go Updated the test suite for the Swap function to handle the error returned by the function.
modules/coinswap/types/.../utils.go Introduced a new TradeType constant and a GetTradeType function. Added two new trade types: BuyDouble and SellDouble.
proto/irismod/coinswap/.../tx.proto Modified the MsgSwapCoinResponse message to include a new amount field.

Poem

🐇 Hopping through the code, making changes bright,

In the season of fall, under the moonlight. 🌙

Swapping coins, trading types, adding fields with might,

Celebrating this day, when in 1860, Lincoln was in sight. 🎩

So here's to the code, ever so dynamic and tight,

May it run error-free, from morning till night. 🌞🌛


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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 help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

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

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 015d2c0 and 3bad579.
Files ignored due to filter (1)
  • modules/coinswap/types/tx.pb.go
Files selected for processing (7)
  • api/irismod/coinswap/tx.pulsar.go (15 hunks)
  • modules/coinswap/keeper/keeper.go (5 hunks)
  • modules/coinswap/keeper/msg_server.go (1 hunks)
  • modules/coinswap/keeper/swap.go (2 hunks)
  • modules/coinswap/keeper/swap_test.go (10 hunks)
  • modules/coinswap/types/utils.go (1 hunks)
  • proto/irismod/coinswap/tx.proto (1 hunks)
Files not summarized due to errors (1)
  • api/irismod/coinswap/tx.pulsar.go: Error: Message exceeds token limit
Files skipped from review due to trivial changes (1)
  • modules/coinswap/keeper/swap_test.go
Additional comments: 25
modules/coinswap/keeper/msg_server.go (1)
  • 129-138: The changes in this hunk are well implemented. The SwapCoin function now returns an Amount field in the MsgSwapCoinResponse response type. The Swap method in the m.k object is now called with the msg argument, and the returned amount and any error are handled accordingly. This change is consistent with the pull request summary and seems to be a good improvement to the function's design.
proto/irismod/coinswap/tx.proto (1)
  • 140-145: The new amount field in MsgSwapCoinResponse is non-nullable, which means it must always contain a valid cosmos.base.v1beta1.Coin value. Ensure that all functions that return a MsgSwapCoinResponse are updated to include this field and that they never return a null value for amount.
message MsgSwapCoinResponse {
+  cosmos.base.v1beta1.Coin amount = 1 [ (gogoproto.nullable) = false ];
}
modules/coinswap/keeper/swap.go (4)
  • 69-97: The function TradeExactInputForOutput has been updated to return sdk.Coin instead of sdkmath.Int. This change is consistent and makes sense as the function is dealing with tokens (coins) and not just integers. The error handling and the logic of the function seem to be correct.

  • 108-144: The function doubleTradeExactInputForOutput has also been updated to return sdk.Coin instead of sdkmath.Int. The logic and error handling in this function also seem to be correct.

  • 192-217: The function TradeInputForExactOutput has been updated to return sdk.Coin instead of sdkmath.Int. The logic and error handling in this function seem to be correct.

  • 228-264: The function doubleTradeInputForExactOutput has been updated to return sdk.Coin instead of sdkmath.Int. The logic and error handling in this function seem to be correct.

modules/coinswap/types/utils.go (4)
  • 13-18: The new trade types BuyDouble and SellDouble have been added. Ensure that these are handled correctly in all parts of the codebase where trade types are used.

  • 20-21: The new types TradeType and TradeFunc have been introduced. Ensure that these are used correctly throughout the codebase.

  • 33-45: The new function GetTradeType has been added. It seems to correctly determine the trade type based on the given parameters. However, it would be good to add unit tests to verify its correctness.

  • 47-49: No changes have been made to the GetReservePoolAddr function.

modules/coinswap/keeper/keeper.go (3)
  • 25-29: The tradeFuncs map is a good addition to the Keeper struct. It allows for easy extension of trade types and their corresponding functions in the future.

  • 54-66: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [48-62]

The NewKeeper function is correctly updated to initialize the tradeFuncs map with the appropriate trade functions.

  • 69-83: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [71-97]

The Swap function has been correctly updated to use the tradeFuncs map to determine the trade function based on the trade type. The return type of the Swap function has been changed to (sdk.Coin, error) to return the swapped amount. However, it's important to ensure that all calls to this function throughout the codebase have been updated to handle the new return type.

api/irismod/coinswap/tx.pulsar.go (12)
  • 5194-5201: The initialization of the MsgSwapCoinResponse message descriptor and the amount field descriptor looks correct. Ensure that the File_irismod_coinswap_tx_proto.Messages().ByName("MsgSwapCoinResponse") and md_MsgSwapCoinResponse.Fields().ByName("amount") calls return the expected descriptors.

  • 5269-5274: The Range function correctly checks if the Amount field is not nil before calling the provided function f. This prevents a potential nil pointer dereference.

  • 5290-5291: The Has function correctly checks if the Amount field is not nil before returning true. This ensures that the function correctly reports whether the Amount field is populated.

  • 5308-5309: The Clear function correctly sets the Amount field to nil. This ensures that the function correctly clears the Amount field.

  • 5326-5328: The Get function correctly returns the Amount field. This ensures that the function correctly retrieves the Amount field.

  • 5349-5350: The Set function correctly sets the Amount field to the provided value. This ensures that the function correctly sets the Amount field.

  • 5371-5375: The Mutable function correctly checks if the Amount field is nil before creating a new v1beta1.Coin and assigning it to the Amount field. This ensures that the function correctly returns a mutable reference to the Amount field.

  • 5389-5391: The NewField function correctly creates a new v1beta1.Coin and returns it. This ensures that the function correctly creates a new Amount field.

  • 5461-5464: The Size function correctly checks if the Amount field is not nil before calculating its size. This ensures that the function correctly calculates the size of the MsgSwapCoinResponse message.

  • 5494-5507: The Marshal function correctly checks if the Amount field is not nil before marshaling it. This ensures that the function correctly marshals the MsgSwapCoinResponse message.

  • 5557-5592: The Unmarshal function correctly checks the wire type and field number before unmarshaling the Amount field. This ensures that the function correctly unmarshals the MsgSwapCoinResponse message.

6981:
The MsgSwapCoinResponse message correctly includes an Amount field of type *v1beta1.Coin. This ensures that the message correctly represents the response of the SwapCoin function.

  • 7004-7008: The GetAmount function correctly checks if the MsgSwapCoinResponse message is not nil before returning the Amount field. This ensures that the function correctly retrieves the Amount field.

@dreamer-zq dreamer-zq closed this Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request modules/coinswap Changes, features and bugs related to the coinswap module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant