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-185] keep track of vault shares as integers #1267

Merged
merged 1 commit into from
Mar 28, 2024
Merged

[TRA-185] keep track of vault shares as integers #1267

merged 1 commit into from
Mar 28, 2024

Conversation

tqin7
Copy link
Contributor

@tqin7 tqin7 commented Mar 28, 2024

Changelist

keep track of vault shares as integers (instead of rational numbers)

Test Plan

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

linear bot commented Mar 28, 2024

Copy link
Contributor

coderabbitai bot commented Mar 28, 2024

Walkthrough

The recent updates aim to streamline vault operations by enhancing the BeginBlocker function for decommissioning vaults at the block start, transitioning from big.Rat to big.Int for share representation in tests and logic, refining share management efficiency, and improving equity calculations within the vault module.

Changes

File(s) Summary of Changes
protocol/x/vault/abci.go Added BeginBlocker function to decommission vaults at block start.
.../keeper/msg_server_deposit_to_vault_test.go, .../keeper/shares_test.go, .../keeper/orders_test.go, .../types/num_shares_test.go Switched from big.Rat to big.Int for share representation in tests.
.../keeper/orders.go, .../keeper/shares.go Simplified logic by directly accessing NumShares and removed BigRat conversions.
protocol/x/vault/keeper/vault.go, .../keeper/vault_test.go Enhanced functionality for vault equity calculation and decommissioning, with added tests for decommissioning vaults.
protocol/x/vault/module.go Implemented appmodule.HasBeginBlocker and added BeginBlock function for ABCI BeginBlock logic.
protocol/x/vault/types/num_shares.go Removed ToBigRat method and added BigIntToNumShares function for conversion.

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: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8f5edc7 and ca477c9.
Files ignored due to path filters (1)
  • protocol/x/vault/types/vault.pb.go is excluded by !**/*.pb.go
Files selected for processing (11)
  • protocol/x/vault/abci.go (1 hunks)
  • protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go (10 hunks)
  • protocol/x/vault/keeper/orders.go (1 hunks)
  • protocol/x/vault/keeper/orders_test.go (2 hunks)
  • protocol/x/vault/keeper/shares.go (6 hunks)
  • protocol/x/vault/keeper/shares_test.go (12 hunks)
  • protocol/x/vault/keeper/vault.go (1 hunks)
  • protocol/x/vault/keeper/vault_test.go (1 hunks)
  • protocol/x/vault/module.go (2 hunks)
  • protocol/x/vault/types/num_shares.go (1 hunks)
  • protocol/x/vault/types/num_shares_test.go (1 hunks)
Path instructions used (11)
protocol/x/vault/types/num_shares.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/abci.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/types/num_shares_test.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/keeper/vault.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/keeper/vault_test.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/keeper/shares.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/module.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/keeper/orders.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/keeper/shares_test.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

protocol/x/vault/keeper/orders_test.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

Additional comments (23)
protocol/x/vault/types/num_shares.go (1)
  • 9-15: The implementation of BigIntToNumShares function is correct and handles the conversion from big.Int to NumShares efficiently, including the nil check for the input parameter.
protocol/x/vault/abci.go (1)
  • 8-13: The BeginBlocker function is correctly implemented to trigger the decommissioning of vaults at the start of a block using the DecommissionVaults method on the keeper.
protocol/x/vault/types/num_shares_test.go (1)
  • 12-31: The test function TestBigIntToNumShares is correctly updated to cover the functionality of the BigIntToNumShares function, including the handling of nil inputs.
protocol/x/vault/keeper/vault.go (3)
  • 14-28: The GetVaultEquity function is correctly implemented to calculate the equity of a vault using the subaccountsKeeper. Error handling and the logic for returning the net collateral are appropriately done.
  • 31-63: The DecommissionVaults function is correctly implemented to decommission vaults with positive shares and non-positive equity. The logic for iterating through vaults, checking conditions, and handling errors is appropriately done.
  • 66-82: The DecommissionVault function is correctly implemented to decommission a specific vault by deleting its total shares and all owner shares. The logic for accessing stores and iterating through owner shares for deletion is appropriately done.
protocol/x/vault/keeper/vault_test.go (2)
  • 17-90: The TestDecomissionVaults function correctly tests the decommissioning logic for vaults with positive equity and those without. The setup, assertions, and verification of outcomes are appropriately implemented.
  • 92-134: The TestDecomissionVault function correctly tests the decommissioning of a single vault, ensuring that its total shares and owner shares are deleted as expected. The setup and verification of outcomes are appropriately done.
protocol/x/vault/keeper/shares.go (3)
  • 42-50: > 📝 NOTE

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

The SetTotalShares function correctly implements the logic for setting total shares for a vault, including the check for negative shares. Note that converting big.Int to float32 for metrics might lead to precision loss for very large numbers, but this is likely acceptable in the context of metrics.

  • 84-84: The SetOwnerShares function is correctly implemented to set owner shares for an owner in a vault, including the check for negative shares.
  • 111-137: > 📝 NOTE

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

The MintShares function correctly implements the logic for minting shares based on quantums to deposit, including checks for positive deposit amounts and non-positive equity. The calculations for shares to mint and updates to total and owner shares are appropriately done.

protocol/x/vault/module.go (1)
  • 151-158: The BeginBlock function is correctly implemented to execute ABCI BeginBlock logic for the vault module, including the unwrapping of the SDK context and the call to BeginBlocker.
protocol/x/vault/keeper/orders.go (1)
  • 50-50: The modification in the RefreshAllVaultOrders function to directly access the NumShares field for checking if TotalShares is non-positive simplifies the logic and is correctly implemented.
protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go (4)
  • 35-35: The transition from big.Rat to big.Int for expectedOwnerShares in DepositInstance aligns with the PR's objective of using integers for vault shares. This change simplifies the representation and operations on shares.
  • 58-58: Switching totalSharesHistory to use big.Int instead of big.Rat is consistent with the PR's goal. This modification ensures that the test cases accurately reflect the new integer-based representation of vault shares.
  • 332-332: The use of vaulttypes.BigIntToNumShares for converting big.Int to the internal shares representation is correct and necessary due to the transition to integer-based shares. This ensures that the test validations are performed against the expected types.
  • 343-343: Correctly using vaulttypes.BigIntToNumShares for converting expectedOwnerShares to the internal representation for comparison in tests. This change is essential for maintaining the integrity of the test cases after the transition to integer-based shares.
protocol/x/vault/keeper/shares_test.go (5)
  • 26-27: Switching to big.Int for representing shares in TestGetSetTotalShares is consistent with the PR's goal of using integers for vault shares. This change simplifies the representation and operations on shares.
  • 67-68: Attempting to set negative shares should result in an error, as shares cannot be negative. This test case correctly validates the error handling for such a scenario, ensuring the robustness of the shares management logic.
  • 94-95: The transition to big.Int for numShares in TestGetSetOwnerShares aligns with the PR's objective. This ensures that the test cases accurately reflect the new integer-based representation of vault shares.
  • 125-126: Validating the error handling for setting negative owner shares is crucial. This test case ensures that the system correctly prevents negative shares, aligning with the logical constraints of share management.
  • 143-155: The update to use big.Int for representing shares in the TestMintShares setup and expectations is consistent with the PR's objectives. This change simplifies the representation and operations on shares, making the test cases more straightforward.
protocol/x/vault/keeper/orders_test.go (1)
  • 33-33: Switching to big.Int for representing totalShares in the test setup for TestRefreshAllVaultOrders aligns with the PR's objective of using integers for vault shares. This ensures that the test cases accurately reflect the new integer-based representation of vault shares.

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 8f5edc7 and a10102f.
Files ignored due to path filters (1)
  • protocol/x/vault/types/vault.pb.go is excluded by !**/*.pb.go
Files selected for processing (13)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/vault.ts (4 hunks)
  • proto/dydxprotocol/vault/vault.proto (1 hunks)
  • protocol/x/vault/abci.go (1 hunks)
  • protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go (10 hunks)
  • protocol/x/vault/keeper/orders.go (1 hunks)
  • protocol/x/vault/keeper/orders_test.go (2 hunks)
  • protocol/x/vault/keeper/shares.go (6 hunks)
  • protocol/x/vault/keeper/shares_test.go (12 hunks)
  • protocol/x/vault/keeper/vault.go (1 hunks)
  • protocol/x/vault/keeper/vault_test.go (1 hunks)
  • protocol/x/vault/module.go (2 hunks)
  • protocol/x/vault/types/num_shares.go (1 hunks)
  • protocol/x/vault/types/num_shares_test.go (1 hunks)
Path instructions used (2)
proto/dydxprotocol/vault/vault.proto (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/vault.ts (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

Files skipped from review as they are similar to previous changes (11)
  • protocol/x/vault/abci.go
  • protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go
  • protocol/x/vault/keeper/orders.go
  • protocol/x/vault/keeper/orders_test.go
  • protocol/x/vault/keeper/shares.go
  • protocol/x/vault/keeper/shares_test.go
  • protocol/x/vault/keeper/vault.go
  • protocol/x/vault/keeper/vault_test.go
  • protocol/x/vault/module.go
  • protocol/x/vault/types/num_shares.go
  • protocol/x/vault/types/num_shares_test.go
Additional comments (3)
proto/dydxprotocol/vault/vault.proto (1)
  • 26-29: The transition to using a single bytes field for representing shares in the NumShares message aligns well with the PR's objectives of moving from rational numbers to integers. It's a good practice to document the expected format and limitations (if any) of the num_shares field, especially since it uses a custom type SerializableInt. This will help developers understand how to interact with this field correctly.
indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/vault.ts (2)
  • 70-80: The simplification of the NumShares and NumSharesSDKType interfaces to use a single numShares field is consistent with the changes in the protobuf definition and the PR's objectives. It would be beneficial to add documentation explaining the format and usage of the numShares field, especially for developers unfamiliar with handling Uint8Array for representing large integers.
  • 137-150: > 📝 NOTE

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

The adjustments made to the encoding and decoding functions for the NumShares interface are appropriate and necessary for handling the numShares field as a Uint8Array. These changes ensure that the binary serialization and deserialization processes align with the updated interface definition and the overall objectives of the PR.

@@ -5,6 +5,13 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/x/vault/keeper"
)

func BeginBlocker(
Copy link
Contributor

Choose a reason for hiding this comment

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

i dont think this belongs

return netCollateral, nil
}

// DecommissionVaults decommissions all vaults with positive shares and non-positive equity.
Copy link
Contributor

Choose a reason for hiding this comment

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

this shouldnt be 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 UI

Commits Files that changed from the base of the PR and between 1eeee45 and 93a557e.
Files ignored due to path filters (1)
  • protocol/x/vault/types/vault.pb.go is excluded by !**/*.pb.go
Files selected for processing (11)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/vault.ts (4 hunks)
  • proto/dydxprotocol/vault/vault.proto (1 hunks)
  • protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go (10 hunks)
  • protocol/x/vault/keeper/orders.go (1 hunks)
  • protocol/x/vault/keeper/orders_test.go (2 hunks)
  • protocol/x/vault/keeper/shares.go (6 hunks)
  • protocol/x/vault/keeper/shares_test.go (12 hunks)
  • protocol/x/vault/keeper/vault.go (1 hunks)
  • protocol/x/vault/keeper/vault_test.go (6 hunks)
  • protocol/x/vault/types/num_shares.go (1 hunks)
  • protocol/x/vault/types/num_shares_test.go (1 hunks)
Path instructions used (1)
protocol/x/vault/keeper/vault_test.go (1)

/
Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

Files skipped from review as they are similar to previous changes (10)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/vault.ts
  • proto/dydxprotocol/vault/vault.proto
  • protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go
  • protocol/x/vault/keeper/orders.go
  • protocol/x/vault/keeper/orders_test.go
  • protocol/x/vault/keeper/shares.go
  • protocol/x/vault/keeper/shares_test.go
  • protocol/x/vault/keeper/vault.go
  • protocol/x/vault/types/num_shares.go
  • protocol/x/vault/types/num_shares_test.go
Additional comments (4)
protocol/x/vault/keeper/vault_test.go (4)
  • 23-23: The change from *big.Rat to *big.Int for totalShares aligns with the PR's objective to represent vault shares as integers. This is a crucial change for consistency across the protocol.
  • 51-59: > 📝 NOTE

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

The initialization of totalShares with *big.Int in the test cases ensures that the tests are now compatible with the updated data type for vault shares. This change is necessary for the tests to reflect the new implementation accurately.

  • 125-132: The usage of vaulttypes.BigIntToNumShares for converting *big.Int to the protocol's share type is correct and ensures that the tests are aligned with the new integer representation of shares. This is an essential part of integrating the changes into the existing test suite.
  • 189-190: The creation of shares using vaulttypes.BigIntToNumShares in TestDecommissionVault is consistent with the changes made in TestDecommissionNonPositiveEquityVaults. This consistency is key to ensuring that all parts of the test suite are updated to work with the new integer representation of shares.

@tqin7 tqin7 merged commit 3476108 into main Mar 28, 2024
31 of 32 checks passed
@tqin7 tqin7 deleted the tq/tra-185 branch March 28, 2024 17:08
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.

2 participants