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

[TRA-80] Update subaccount-to-subaccount transfers for collateral pools. #1178

Merged
merged 4 commits into from
Mar 15, 2024

Conversation

vincentwschau
Copy link
Contributor

Changelist

Added a new function to x/subaccounts keeper to transfer funds from subaccount to subaccount and send coins between collateral pools. Updated x/sending to use the new function instead of UpdateSubaccounts.

Test Plan

Added new unit tests.

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Copy link
Contributor

coderabbitai bot commented Mar 14, 2024

Walkthrough

The updates across various files mainly focus on enhancing the subaccount functionalities within a protocol, specifically by introducing and testing a new method for transferring funds between subaccounts. These changes span across updating the code generation tool, adding panic statements for error handling, and refining the logic for fund transfers, including the handling of different collateral pools and asset types. Documentation improvements and the addition of specific market and position constants also form part of these updates.

Changes

Files Change Summary
protocol/mocks/SubaccountsKeeper.go
protocol/x/sending/types/expected_keepers.go
protocol/x/subaccounts/types/types.go
Added TransferFundsFromSubaccountToSubaccount method to the SubaccountsKeeper interface. Updated code generation tool and added panic statements in SubaccountsKeeper.go.
protocol/testutil/constants/perpetuals.go Added Iso2Usd_IsolatedMarket to the TestMarketPerpetuals slice.
protocol/testutil/constants/positions.go Added declarations for specific perpetual positions in isolated markets.
protocol/x/sending/keeper/transfer.go Modified ProcessTransfer to use TransferFundsFromSubaccountToSubaccount for direct fund transfers between subaccounts, removing previous update handling logic.
protocol/x/subaccounts/keeper/transfer.go
protocol/x/subaccounts/keeper/transfer_test.go
Introduced and tested the TransferFundsFromSubaccountToSubaccount function, including logic for asset quantum updates, transfers between collateral pools, and error handling.

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
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 UI

Commits Files that changed from the base of the PR and between dd41d0a and 33ea5f7.
Files selected for processing (8)
  • protocol/mocks/SubaccountsKeeper.go (10 hunks)
  • protocol/testutil/constants/perpetuals.go (1 hunks)
  • protocol/testutil/constants/positions.go (1 hunks)
  • protocol/x/sending/keeper/transfer.go (1 hunks)
  • protocol/x/sending/types/expected_keepers.go (1 hunks)
  • protocol/x/subaccounts/keeper/transfer.go (1 hunks)
  • protocol/x/subaccounts/keeper/transfer_test.go (1 hunks)
  • protocol/x/subaccounts/types/types.go (1 hunks)
Additional comments: 8
protocol/x/subaccounts/types/types.go (1)
  • 54-60: The addition of TransferFundsFromSubaccountToSubaccount to the SubaccountsKeeper interface is clear and aligns with the PR objectives. However, it would be beneficial to add documentation for this method to maintain consistency and clarity within the interface.
protocol/x/sending/types/expected_keepers.go (1)
  • 56-62: The addition of TransferFundsFromSubaccountToSubaccount to the SubaccountsKeeper interface in this file is consistent with the changes across the codebase. To enhance clarity and maintain consistency, adding documentation for this new method would be beneficial.
protocol/testutil/constants/positions.go (1)
  • 82-97: The addition of new constants for perpetual positions in isolated markets is correctly implemented and aligns with the PR's objectives to enhance collateral pool management. The naming convention and struct usage are consistent and clear.
protocol/x/sending/keeper/transfer.go (1)
  • 25-31: The modification to use TransferFundsFromSubaccountToSubaccount in the ProcessTransfer function simplifies and potentially improves the efficiency of transfers between subaccounts. It would be helpful to add a comment explaining the rationale behind this change for future maintainers.
protocol/mocks/SubaccountsKeeper.go (1)
  • 195-211: The update to include TransferFundsFromSubaccountToSubaccount in the mock SubaccountsKeeper and the addition of panic statements for unhandled return values are correctly implemented. These changes support testing of the new functionality and follow common practices for mock testing.
protocol/testutil/constants/perpetuals.go (1)
  • 382-382: The addition of Iso2Usd_IsolatedMarket to the TestMarketPerpetuals slice introduces a new isolated market perpetual for testing. This is a straightforward addition and follows the pattern established by other perpetual market constants in the file. It's important for testing infrastructure to keep pace with the features and configurations supported by the application, and this addition appears to do just that.

Ensure that any new features or configurations introduced in the main application are also reflected in the test constants and utilities to maintain comprehensive test coverage and facilitate accurate testing scenarios.

protocol/x/subaccounts/keeper/transfer_test.go (2)
  • 577-725: The tests for TestTransferFundsFromSubaccountToSubaccount_Success cover various scenarios of transferring funds between subaccounts, including isolated and non-isolated subaccounts. It's good to see comprehensive test coverage for different types of transfers. However, consider adding a test case for transferring a zero amount to verify that the function handles this edge case correctly, if applicable to the business logic.
  • 845-1051: The tests for TestTransferFundsFromSubaccountToSubaccount_Failure effectively cover failure scenarios, including insufficient funds and unsupported assets. It's crucial to ensure that all error paths are tested to maintain robustness. One suggestion is to verify that the state of the system remains unchanged after a failed transfer attempt. This can be done by checking the balances of both the sender and recipient subaccounts before and after the operation to ensure they are not altered in case of failure.

protocol/x/subaccounts/keeper/transfer.go Show resolved Hide resolved
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 UI

Commits Files that changed from the base of the PR and between 33ea5f7 and 92a75b0.
Files selected for processing (1)
  • protocol/testing/e2e/funding/funding_e2e_test.go (1 hunks)
Additional comments: 2
protocol/testing/e2e/funding/funding_e2e_test.go (2)
  • 486-486: The gas value for transaction processing has been increased to 110,000 units. While this change is likely necessary to accommodate more complex transactions or state changes, it's important to ensure that this increase aligns with the expected transaction complexity and doesn't inadvertently mask inefficiencies in the code.
  • 483-489: > 📝 NOTE

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

The TestFunding function is comprehensive and covers a wide range of scenarios. To improve readability and maintainability, consider abstracting some of the setup logic into helper functions or adding more comments to describe the purpose and expected outcomes of each test case. This will make it easier for future contributors to understand and modify the test suite.

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 UI

Commits Files that changed from the base of the PR and between 92a75b0 and b11eba5.
Files selected for processing (1)
  • protocol/testutil/constants/positions.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/testutil/constants/positions.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: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b11eba5 and 21155b9.
Files selected for processing (1)
  • protocol/x/subaccounts/keeper/transfer_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/x/subaccounts/keeper/transfer_test.go

quantums: big.NewInt(500),
expectedErr: types.ErrFailedToUpdateSubaccounts,
},
"Send from non-isolated subaccount to isolated subaccount, collateral pool does not have enough balance": {
Copy link
Contributor

Choose a reason for hiding this comment

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

for my understanding - collateral pools should never not have enough balance, right? should we panic instead of returning an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Collateral pools could end up with not enough balance, in the same way the subaccounts module account could end up with not enough balance if withdrawal gating doesn't catch a negative TNC subaccount in time.
In terms of panic'ing, I think that could be fine, however it would diverge from the withdrawal flow, which returns an error.

Copy link
Contributor

@shrenujb shrenujb left a comment

Choose a reason for hiding this comment

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

Core logic looks sound to me! Could you please add integration tests for the new transfer functionality as well?

@vincentwschau vincentwschau merged commit f0fe4cc into main Mar 15, 2024
17 checks passed
@vincentwschau vincentwschau deleted the vincentc/tra-80-transfer-isolated branch March 15, 2024 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants