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-84] Move SA module address transfers to use perpetual based SA accounts #1146

Merged
merged 6 commits into from
Mar 7, 2024

Conversation

shrenujb
Copy link
Contributor

@shrenujb shrenujb commented Mar 6, 2024

Changelist

  • Changes to x/clob to use GetCollateralPoolForSubaccount over using the default subaccounts module account
  • Lots of tests changes

Test Plan

Added additional tests as part of this PR
Existing tests should also exercise a lot of this code

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.

Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
Copy link

linear bot commented Mar 6, 2024

Copy link

coderabbitai bot commented Mar 6, 2024

Walkthrough

The modifications primarily involve standardizing the coin transfer methods across various test and keeper files in a blockchain protocol codebase, transitioning from SendCoinsFromModuleToModule to a more generic SendCoins function. Additionally, new functionality related to handling positions in isolated markets, updating fee transfer mechanisms for a new perpetualId parameter, and enhancing subaccount management capabilities with new functions for collateral and insurance fund handling are introduced.

Changes

File Change Summary
protocol/testutil/constants/positions.go Added a new long position for an isolated market with specific parameters.
protocol/x/clob/abci_test.go,
protocol/x/clob/keeper/..._test.go (various),
protocol/x/clob/keeper/process_operations_..._test.go (various),
protocol/x/subaccounts/keeper/transfer.go,
protocol/x/subaccounts/keeper/transfer_test.go
Changed SendCoinsFromModuleToModule to SendCoins. Updated method calls, arguments, handling of addresses and amounts for coin transfers. Added handling for perpetualPositions alongside assetPositions.
protocol/x/clob/keeper/process_single_match.go Added a new argument in persistMatchedOrders affecting fee calculation.
protocol/x/clob/types/expected_keepers.go,
protocol/x/subaccounts/keeper/subaccount.go,
protocol/x/subaccounts/types/expected_keepers.go
Introduced new functions and parameters related to fee transfers and market management (TransferFeesToFeeCollectorModule, GetCollateralPoolFromPerpetualId, GetInsuranceFundModuleAddress).

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

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a5118b7 and 8f36ca0.
Files selected for processing (14)
  • protocol/testutil/constants/positions.go (1 hunks)
  • protocol/x/clob/abci_test.go (1 hunks)
  • protocol/x/clob/keeper/liquidations_test.go (3 hunks)
  • protocol/x/clob/keeper/mev_test.go (1 hunks)
  • protocol/x/clob/keeper/orders_test.go (2 hunks)
  • protocol/x/clob/keeper/process_operations_liquidations_test.go (13 hunks)
  • protocol/x/clob/keeper/process_operations_long_term_test.go (11 hunks)
  • protocol/x/clob/keeper/process_operations_stateful_validation_test.go (2 hunks)
  • protocol/x/clob/keeper/process_single_match.go (1 hunks)
  • protocol/x/clob/types/expected_keepers.go (1 hunks)
  • protocol/x/subaccounts/keeper/subaccount.go (1 hunks)
  • protocol/x/subaccounts/keeper/transfer.go (8 hunks)
  • protocol/x/subaccounts/keeper/transfer_test.go (26 hunks)
  • protocol/x/subaccounts/types/expected_keepers.go (1 hunks)
Files not reviewed due to errors (1)
  • protocol/x/clob/keeper/liquidations_test.go (Error: unable to parse review)
Additional comments: 28
protocol/x/subaccounts/types/expected_keepers.go (1)
  • 76-76: The addition of GetInsuranceFundModuleAddress to the PerpetualsKeeper interface is a logical extension to support the handling of perpetual-based SA accounts. It aligns well with the PR's objectives and follows the established pattern in the interface.
protocol/x/clob/types/expected_keepers.go (1)
  • 66-66: The addition of the perpetualId parameter to the TransferFeesToFeeCollectorModule function is a necessary change to support the handling of fee transfers in the context of perpetual-based SA accounts. It aligns well with the PR's objectives and enhances the protocol's capabilities in managing collateral pools and insurance funds associated with perpetual IDs.
protocol/testutil/constants/positions.go (1)
  • 76-81: The addition of PerpetualPosition_OneISOLong as a test constant for an isolated market position is appropriate and supports the testing of the new functionality introduced by the PR. It aligns with the established pattern for defining test positions and is consistent with the transition to perpetual-based SA accounts.
protocol/x/subaccounts/keeper/transfer.go (4)
  • 126-135: The changes to DepositFundsFromAccountToSubaccount to use SendCoins and introduce collateralPoolAddr for determining the destination address for fund transfers align well with the PR's objectives. These changes enhance the flexibility and capability of the system in handling fund transfers in the context of perpetual-based SA accounts.
  • 189-197: The changes to WithdrawFundsFromSubaccountToAccount to use SendCoins and introduce collateralPoolAddr for determining the source address for fund transfers are consistent with the PR's objectives. These changes improve the system's ability to handle fund transfers flexibly and efficiently.
  • 236-256: > 📝 NOTE

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

The changes to TransferFeesToFeeCollectorModule, including the use of SendCoins, the addition of the perpetualId parameter, and the introduction of collateralPoolAddr, enhance the protocol's capabilities in handling fee transfers in the context of perpetual-based SA accounts. These changes are consistent with the PR's objectives and improve the system's flexibility and specificity in managing fund transfers.

  • 292-302: > 📝 NOTE

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

The updates to TransferInsuranceFundPayments, including the use of SendCoins and the inclusion of the perpetualId parameter, enhance the system's ability to handle insurance fund payments in the context of perpetual-based SA accounts. These changes align with the PR's objectives and improve the protocol's flexibility and specificity in managing fund transfers.

protocol/x/clob/keeper/process_single_match.go (1)
  • 445-445: The addition of a new argument with a default value of 0 in the TransferFeesToFeeCollectorModule function call at line 445 is noted. This change is likely related to the PR's objective of refactoring the coin transfer logic and introducing new parameters for perpetual IDs. However, it's crucial to ensure that this default value aligns with the intended logic for fee transfers within the protocol, especially in the context of perpetual-based SA accounts.
  • Ensure that the default value of 0 correctly represents the intended behavior for fee transfers in this context. If this value is meant to represent a new parameter or a placeholder for future expansion, consider adding a comment to clarify its purpose and ensure it does not inadvertently affect the fee transfer logic.
  • Verify that all calls to TransferFeesToFeeCollectorModule throughout the codebase have been appropriately updated to include this new argument, maintaining consistency and preventing potential errors.
Verification successful

The provided script output contains the contents of transfer.go, expected_keepers.go, and transfer_test.go, which are relevant to the TransferFeesToFeeCollectorModule function and its usage across the codebase. Here's a summary of the findings:

  1. Function Signature Consistency: The function TransferFeesToFeeCollectorModule in transfer.go has been updated to include a new argument perpetualId uint32. This change is reflected in the interface definition in expected_keepers.go, ensuring consistency in the function's signature across its declaration and usage in the interface.

  2. Test Coverage: The transfer_test.go file includes a test case specifically for the TransferFeesToFeeCollectorModule function, indicating that the new argument addition has been considered in the test suite. This test case, TestTransferFeesToFeeCollectorModule, checks various scenarios, including successful transfers to the fee collector module account and handling of edge cases like zero or negative quantums.

  3. Usage in Codebase: The search results from the initial script show that the function is used in process_single_match.go and tested in transfer_test.go. The test file's inclusion of the new argument in its function calls confirms that the change has been integrated into the codebase correctly.

Given these observations, it can be concluded that the addition of the new argument perpetualId uint32 to the TransferFeesToFeeCollectorModule function has been correctly implemented and integrated into the codebase, with consistent function signatures and updated test coverage to reflect the change.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for other occurrences of `TransferFeesToFeeCollectorModule` to ensure consistency.
rg "TransferFeesToFeeCollectorModule" --type go

Length of output: 782



Script:

#!/bin/bash
# Inspect the function signature in transfer.go and expected_keepers.go, and how it's called in transfer_test.go
cat protocol/x/subaccounts/keeper/transfer.go
cat protocol/x/clob/types/expected_keepers.go
cat protocol/x/subaccounts/keeper/transfer_test.go

Length of output: 49370

protocol/x/clob/keeper/process_operations_stateful_validation_test.go (2)
  • 342-345: The change from SendCoinsFromModuleToModule to SendCoins reflects the PR's objective to generalize coin transfer methods. However, ensure that the arguments passed to SendCoins correctly represent the intended source and destination modules. The use of satypes.ModuleAddress and authtypes.NewModuleAddress(authtypes.FeeCollectorName) as arguments seems appropriate, assuming these addresses correctly represent the source and destination of the coin transfer in the context of the test case. It's crucial to verify that these module addresses align with the new perpetual-based SA accounts system and that the amount being transferred (12_500_000+5_000_000) matches the expected values for the test scenario.

Ensure the module addresses and transfer amounts are correctly aligned with the intended test scenarios and the new system's requirements.

  • 638-641: Similar to the previous comment, the change to SendCoins in this test case also requires verification of the correctness of the module addresses and the transfer amount. Given the context of a "Fails with conditional order when considering state fill amount" test case, it's essential to ensure that the satypes.ModuleAddress and authtypes.NewModuleAddress(authtypes.FeeCollectorName) accurately represent the transaction's source and destination. Additionally, the amount being transferred (12_500_000+5_000_000) must be scrutinized to confirm it aligns with the test's expectations and the protocol's logic under the new perpetual-based SA accounts system.

Double-check the module addresses and transfer amounts for accuracy and alignment with the protocol's updated logic and test case requirements.

protocol/x/clob/keeper/process_operations_long_term_test.go (4)
  • 40-43: The update from SendCoinsFromModuleToModule to SendCoins, and the change in target addresses to ModuleAddress and NewModuleAddress(FeeCollectorName) align with the PR's objective to generalize coin transfers. Ensure that ModuleAddress and NewModuleAddress(FeeCollectorName) are correctly defined and accessible in this context.
  • 40-43: The adjustment in the amount calculation within the SendCoins call, specifically the addition of amounts (e.g., 25_000_000+10_000_000), appears to be correctly implemented. However, ensure that these amounts accurately reflect the intended logic for coin transfers in each test case scenario.
  • 109-112: The changes to the SendCoins method call, including the update in target addresses and the adjustments in amount calculations, are consistently applied across all test cases. This consistency is crucial for maintaining the integrity of the test suite and ensuring that all scenarios are covered under the new logic for coin transfers.

Also applies to: 189-192, 262-265, 336-339, 409-412, 487-490, 574-577, 662-665, 750-753, 841-844

  • 571-587: > 📝 NOTE

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

The modifications to existing test cases and the introduction of new test scenarios appear to be thorough and well-aligned with the PR objectives. These changes are essential for validating the updated logic for handling coin transfers and ensuring that the system behaves as expected under various scenarios.

protocol/x/subaccounts/keeper/transfer_test.go (4)
  • 33-34: The addition of perpetualPositions to the test setup is consistent with the PR's objective to transition to perpetual-based SA accounts. This change allows the tests to cover scenarios involving perpetual positions, which is crucial for ensuring the system's behavior aligns with the new logic.
  • 225-249: > 📝 NOTE

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

The setup for the test cases, including the creation of test markets, liquidity tiers, perpetuals, and funding of accounts, is thorough and correctly implemented. This setup is crucial for accurately simulating the protocol's environment and ensuring the tests are meaningful and cover a wide range of scenarios.

  • 578-592: > 📝 NOTE

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

The tests for TransferFeesToFeeCollectorModule effectively cover a range of scenarios, including successful transfers, insufficient funds, and the handling of isolated market accounts. The use of perpetualId in these tests is a good practice, ensuring that the functionality works correctly across different perpetual markets.

  • 885-901: > 📝 NOTE

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

The tests for TransferInsuranceFundPayments are comprehensive, covering various scenarios including successful transfers, insufficient funds, and the handling of isolated markets. The setup and assertions in these tests are correctly implemented, ensuring that the insurance fund payment functionality behaves as expected under different conditions.

protocol/x/clob/abci_test.go (5)
  • 1338-1344: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [113-180]

The test TestEndBlocker_Failure correctly checks for a panic condition when cancelled and expired order IDs overlap. However, ensure that using panic is a deliberate choice in production code, as it can lead to abrupt termination of the application. Consider handling errors gracefully where possible.

  • 1338-1344: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [182-634]

The test TestEndBlocker_Success is comprehensive and effectively uses mock objects to simulate various scenarios. It demonstrates good practice in unit testing by covering multiple cases and asserting expected outcomes clearly.

  • 1338-1344: > 📝 NOTE

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

The test TestPrepareCheckState_WithProcessProposerMatchesEventsWithBadBlockHeight effectively checks for a panic when the block height of ProcessProposerMatchesEvents does not match the current block height, ensuring the system's robustness against inconsistent state information.

  • 1338-1344: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1379-1415]

The test TestCommitBlocker_WithProcessProposerMatchesEventsWithBadBlockHeight correctly checks for a panic when the block height of ProcessProposerMatchesEvents does not match the current block height during CommitBlocker, complementing the coverage provided by the previous test on PrepareCheckState.

  • 1338-1344: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1417-1508]

The test TestBeginBlocker_Success effectively covers scenarios with varying initial states for the BeginBlocker function, ensuring that ProcessProposerMatchesEvents is correctly initialized. It demonstrates thorough testing practices by simulating different starting conditions and asserting expected outcomes clearly.

protocol/x/clob/keeper/process_operations_liquidations_test.go (5)
  • 196-206: The changes from lines 196 to 206 update the method call from a specific module-to-module transfer to a more generalized SendCoins call. This aligns with the PR's objective to refactor coin transfer logic for more flexibility. However, ensure that the SendCoins method is properly implemented to handle the nuances of module-to-module transfers, including authorization and accounting checks.
  • 271-280: > 📝 NOTE

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

The changes from lines 274 to 290 demonstrate an update in the method call to SendCoins and the handling of insurance fund transfers. It's crucial to verify that the insurance fund's module address is correctly configured and that the SendCoins method accurately reflects the intended logic for transferring funds to the insurance fund. Additionally, ensure that the amount being transferred (250_000_000 and 1_000_000) is calculated based on the correct business logic and that edge cases are handled appropriately.

  • 360-370: The changes from lines 360 to 370 adjust the SendCoins calls to handle multiple partial fills in a liquidation scenario. It's important to ensure that the logic for calculating the amount to be transferred (62_500_000) accurately reflects the partial liquidation amounts and that the method correctly handles multiple transfers in a single transaction. Consider adding tests to verify the behavior in scenarios with multiple partial fills.
  • 455-464: > 📝 NOTE

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

The changes from lines 458 to 474 introduce logic for handling negative insurance fund deltas in liquidation scenarios. This is a critical area that requires careful validation to ensure that the insurance fund's balance is correctly updated and that negative deltas are handled in a way that maintains the integrity of the fund. It's advisable to thoroughly test this logic under various scenarios to ensure robustness.

  • 575-590: > 📝 NOTE

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

The changes from lines 563 to 587 deal with both positive and negative insurance fund deltas within the same liquidation process. This scenario requires precise logic to ensure that the insurance fund is correctly updated for each fill. Special attention should be paid to the order in which fills are processed and how their cumulative effect on the insurance fund is calculated. Extensive testing is recommended to validate the correctness of this implementation.

Verification successful

Given the lack of results from the initial search commands and the potential inaccuracies in the search terms or directory paths used, a broader search approach is warranted to verify the implementation and testing of logic dealing with both positive and negative insurance fund deltas within liquidations. Let's expand the search to include keywords related to the functionality and verify the structure of the test directories in the repository.


The executed scripts provided a wealth of information regarding the handling of insurance fund deltas in liquidations and the structure of test directories in the repository. The search for "insurance fund" and "liquidation" keywords across the protocol/ directory yielded numerous matches, indicating extensive coverage of these topics within the codebase. This suggests that the logic for processing fills with both positive and negative insurance fund deltas is indeed implemented and tested, as indicated by references to insurance fund deltas in various test files, such as protocol/x/clob/keeper/liquidations_test.go and protocol/x/clob/keeper/process_operations_liquidations_test.go.

Furthermore, the discovery of test directories, such as ./protocol/x/clob/keeper/, confirms the existence of tests related to liquidations and insurance fund deltas. This aligns with the original review comment's emphasis on the importance of testing the logic for handling mixed insurance fund deltas within liquidations.

Given the evidence from the script outputs, it is reasonable to conclude that the codebase contains both the implementation and testing of logic for processing fills with mixed insurance fund deltas in liquidations. The presence of specific test files and references within the code to insurance fund deltas supports the claim that this functionality is thoroughly considered and validated.

Therefore, based on the information gathered, the review comment regarding the need for precise logic and extensive testing for handling both positive and negative insurance fund deltas within the same liquidation process is verified as accurate and consistent with the codebase's current state.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the logic for processing fills with both positive and negative deltas
grep -r "processFillsWithMixedDeltas" protocol/
# Verify tests for fills with mixed insurance fund deltas
grep -r "TestFillsWithMixedDeltas" protocol/tests/

Length of output: 147



Script:

#!/bin/bash
# Broaden the search for functionality related to insurance fund deltas in liquidations
grep -r "insurance fund" protocol/
grep -r "liquidation" protocol/
# Verify the structure of the test directories in the repository
find . -type d -name "*test*"

Length of output: 118482

Comment on lines 72 to 83
func (k Keeper) GetCollateralPoolFromPerpetualId(ctx sdk.Context, perpetualId uint32) (sdk.AccAddress, error) {
perpetual, err := k.perpetualsKeeper.GetPerpetual(ctx, perpetualId)
if err != nil {
return nil, err
}

if perpetual.Params.MarketType == perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_ISOLATED {
return authtypes.NewModuleAddress(types.ModuleName + ":" + lib.UintToString(perpetual.GetId())), nil
}

return authtypes.NewModuleAddress(types.ModuleName), nil
}
Copy link

Choose a reason for hiding this comment

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

The function GetCollateralPoolFromPerpetualId correctly implements the logic to retrieve the collateral pool address based on the perpetual ID. It differentiates between isolated and non-isolated market types, returning the appropriate module address. This aligns well with the PR's objectives to support perpetual-based SA accounts. However, consider adding error handling for the case where the perpetualId does not exist in the system, as the current implementation assumes the perpetualsKeeper.GetPerpetual call will always succeed.

@@ -647,7 +647,7 @@ func TestPlaceShortTermOrder(t *testing.T) {
memClob := memclob.NewMemClobPriceTimePriority(false)
mockBankKeeper := &mocks.BankKeeper{}
mockBankKeeper.On(
"SendCoinsFromModuleToModule",
"SendCoins",
Copy link

Choose a reason for hiding this comment

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

The mock setup for "SendCoins" in the TestPlaceShortTermOrder test function is overly broad and does not specify the expected arguments. This could lead to false positives in the test if the SendCoins function is called with unexpected arguments.

Consider specifying the expected arguments to ensure the test accurately reflects the intended behavior. For example:

mockBankKeeper.On(
  "SendCoins",
  mock.AnythingOfType("*context.Context"),
  expectedFromAddress,
  expectedToAddress,
  expectedAmount,
).Return(nil)

@@ -875,7 +875,7 @@
memClob := memclob.NewMemClobPriceTimePriority(false)
mockBankKeeper := &mocks.BankKeeper{}
mockBankKeeper.On(
"SendCoinsFromModuleToModule",
"SendCoins",
Copy link

Choose a reason for hiding this comment

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

Similar to the previous comment, the mock setup for "SendCoins" in the TestAddPreexistingStatefulOrder test function is overly broad. Specifying expected arguments would improve the test's accuracy and reliability.

Adjust the mock setup to include expected arguments for better test accuracy.

Comment on lines 647 to 653
memClob := memclob.NewMemClobPriceTimePriority(false)
mockBankKeeper := &mocks.BankKeeper{}
mockBankKeeper.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
mock.Anything,
mock.Anything,
Copy link

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [650-878]

In both TestPlaceShortTermOrder and TestAddPreexistingStatefulOrder, the mock.Anything matcher is used for all arguments in the SendCoins mock setup. This approach lacks specificity and could potentially mask issues if the SendCoins method is called with incorrect arguments during the test execution.

It's recommended to replace mock.Anything with more specific matchers or actual expected values to ensure the mock setup accurately reflects the expected behavior and arguments of the SendCoins method call.

Comment on lines +1247 to +1253
mockBankKeeper.On(
"SendCoins",
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Return(nil)
Copy link

Choose a reason for hiding this comment

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

The SendCoins mock setup is present but not utilized within the TestGetMidPrices function. This could be a leftover from copy-pasting the setup code from another test function or an indication of an incomplete test scenario where SendCoins interaction was expected but not implemented. It's important to ensure that mock setups are relevant to the test cases being executed to avoid confusion and maintain clarity in test intentions.

Comment on lines 1338 to 1344
constants.Usdc.Denom,
).Return(sdk.NewCoin(constants.Usdc.Denom, sdkmath.NewIntFromBigInt(new(big.Int))))
mockBankKeeper.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
mock.Anything,
mock.Anything,
Copy link

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [636-1339]

The test TestLiquidateSubaccounts thoroughly simulates the liquidation process with a detailed setup. However, it contains commented-out assertions at the end, suggesting that some verifications might be incomplete. Ensure that the test fully verifies the intended behavior by completing or updating these assertions.

Consider completing or updating the commented-out assertions at the end of the test to ensure comprehensive verification of the liquidation process.


📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1510-1773]

The test TestPrepareCheckState is comprehensive, simulating various states and checking for expected operations in the memclob. However, it contains commented-out assertions, suggesting that some verifications might be incomplete. Ensure that the test fully verifies the intended behavior by completing or updating these assertions.

Consider completing or updating the commented-out assertions to ensure comprehensive verification of the PrepareCheckState function's behavior.

Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
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: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8f36ca0 and 7dfad36.
Files selected for processing (1)
  • protocol/x/subaccounts/keeper/subaccount.go (1 hunks)

Comment on lines 62 to 89
sdk.AccAddress,
error,
) {
poolName, err := k.GetCollateralPoolNameForSubaccount(ctx, subaccountId)
if err != nil {
return nil, err
}
return authtypes.NewModuleAddress(poolName), nil
}

func (k Keeper) GetCollateralPoolNameForSubaccount(ctx sdk.Context, subaccountId types.SubaccountId) (string, error) {
// Use the default collateral pool if the subaccount has no perpetual positions.
subaccount := k.GetSubaccount(ctx, subaccountId)
if len(subaccount.PerpetualPositions) == 0 {
return types.ModuleName, nil
return types.ModuleAddress, nil
}

// Get the first perpetual position and return the collateral pool name.
perpetual, err := k.perpetualsKeeper.GetPerpetual(ctx, subaccount.PerpetualPositions[0].PerpetualId)
return k.GetCollateralPoolFromPerpetualId(ctx, subaccount.PerpetualPositions[0].PerpetualId)
}

// GetCollateralPoolForSubaccountWithPerpetuals returns the collateral pool address based on the
// perpetual passes in as an argument.
func (k Keeper) GetCollateralPoolFromPerpetualId(ctx sdk.Context, perpetualId uint32) (sdk.AccAddress, error) {
perpetual, err := k.perpetualsKeeper.GetPerpetual(ctx, perpetualId)
if err != nil {
panic(fmt.Sprintf("GetCollateralPoolNameForSubaccount: %v", err))
return nil, err
}

if perpetual.Params.MarketType == perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_ISOLATED {
return types.ModuleName + ":" + lib.UintToString(perpetual.GetId()), nil
return authtypes.NewModuleAddress(types.ModuleName + ":" + lib.UintToString(perpetual.GetId())), nil
}

return types.ModuleName, nil
}

// IsIsolatedMarketSubaccount returns whether a subaccount is isolated to a specific market.
func (k Keeper) IsIsolatedMarketSubaccount(ctx sdk.Context, subaccountId types.SubaccountId) (bool, error) {
poolName, err := k.GetCollateralPoolNameForSubaccount(ctx, subaccountId)
if err != nil {
panic(fmt.Sprintf("IsIsolatedMarketSubaccount: %v", err))
}
return poolName != types.ModuleName, nil
return authtypes.NewModuleAddress(types.ModuleName), nil
}

// GetSubaccount returns a subaccount from its index.
Copy link

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [7-7]

Using math/rand for generating random values in a blockchain context might not be secure enough, especially for operations that require a high degree of unpredictability. Consider using crypto/rand instead to ensure that the randomness meets the security requirements of the protocol.

- import "math/rand"
+ import "crypto/rand"

Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
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 7dfad36 and 4a3d396.
Files selected for processing (1)
  • protocol/x/clob/keeper/process_single_match.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/x/clob/keeper/process_single_match.go

…ented

Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
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: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4a3d396 and 4a7d3a9.
Files selected for processing (1)
  • protocol/x/perpetuals/simulation/genesis.go (1 hunks)

if i%2 == 0 {
marketType = types.PerpetualMarketType_PERPETUAL_MARKET_TYPE_ISOLATED
}
// TODO: add isolated markets when order placements for isolated markets are supported
Copy link

Choose a reason for hiding this comment

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

The placeholder comment at line 193 indicates future support for isolated markets. While this is a good way to mark areas for future development, it's important to ensure that any associated tasks or features are tracked in your project management tool. This ensures that the intention to support isolated markets doesn't get lost or forgotten.

Would you like me to help create a tracking issue for the implementation of isolated markets in your project management tool?

Additionally, consider adding more context to the comment about what needs to be done or any prerequisites that must be met before isolated markets can be supported. This can help future developers understand the scope of the work and any dependencies.

protocol/x/subaccounts/keeper/subaccount.go Outdated Show resolved Hide resolved
protocol/x/subaccounts/keeper/transfer.go Outdated Show resolved Hide resolved
protocol/x/subaccounts/keeper/transfer_test.go Outdated Show resolved Hide resolved
Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
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: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4a7d3a9 and 78ebcda.
Files selected for processing (3)
  • protocol/x/subaccounts/keeper/subaccount.go (1 hunks)
  • protocol/x/subaccounts/keeper/transfer.go (8 hunks)
  • protocol/x/subaccounts/keeper/transfer_test.go (45 hunks)
Files skipped from review as they are similar to previous changes (2)
  • protocol/x/subaccounts/keeper/transfer.go
  • protocol/x/subaccounts/keeper/transfer_test.go
Additional comments: 1
protocol/x/subaccounts/keeper/subaccount.go (1)
  • 65-72: The logic in GetCollateralPoolForSubaccount correctly handles the case where a subaccount has no perpetual positions by returning the default collateral pool. This approach aligns with the PR's objectives and ensures that the function behaves as expected in different scenarios. Good job on implementing this logic clearly and concisely.

Comment on lines +76 to +86
func (k Keeper) GetCollateralPoolFromPerpetualId(ctx sdk.Context, perpetualId uint32) (sdk.AccAddress, error) {
perpetual, err := k.perpetualsKeeper.GetPerpetual(ctx, perpetualId)
if err != nil {
panic(fmt.Sprintf("GetCollateralPoolNameForSubaccount: %v", err))
return nil, err
}

if perpetual.Params.MarketType == perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_ISOLATED {
return types.ModuleName + ":" + lib.UintToString(perpetual.GetId()), nil
return authtypes.NewModuleAddress(types.ModuleName + ":" + lib.UintToString(perpetual.GetId())), nil
}

return types.ModuleName, nil
}

// IsIsolatedMarketSubaccount returns whether a subaccount is isolated to a specific market.
func (k Keeper) IsIsolatedMarketSubaccount(ctx sdk.Context, subaccountId types.SubaccountId) (bool, error) {
poolName, err := k.GetCollateralPoolNameForSubaccount(ctx, subaccountId)
if err != nil {
panic(fmt.Sprintf("IsIsolatedMarketSubaccount: %v", err))
}
return poolName != types.ModuleName, nil
return authtypes.NewModuleAddress(types.ModuleName), nil
Copy link

Choose a reason for hiding this comment

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

The implementation of GetCollateralPoolFromPerpetualId seems to align with the PR's objectives of handling perpetual-based SA accounts. However, based on previous feedback, consider the following improvements:

  • Error Handling: Enhance error handling by wrapping the error returned from k.perpetualsKeeper.GetPerpetual with additional context. This will aid in debugging by providing more information about the error's origin.
  • Address Generation: The use of string concatenation for generating module addresses (types.ModuleName + ":" + lib.UintToString(perpetual.GetId())) could be improved. Define a utility function that ensures consistent and safe address generation across the application. This approach will help maintain consistency and potentially prevent errors related to address formatting.
  • Performance Consideration: Evaluate the performance impact of generating new module addresses on-the-fly, especially if this function is called frequently. Consider caching or pre-generating addresses for known perpetual IDs to improve performance in high-throughput scenarios.

Comment on lines 62 to 89
sdk.AccAddress,
error,
) {
poolName, err := k.GetCollateralPoolNameForSubaccount(ctx, subaccountId)
if err != nil {
return nil, err
}
return authtypes.NewModuleAddress(poolName), nil
}

func (k Keeper) GetCollateralPoolNameForSubaccount(ctx sdk.Context, subaccountId types.SubaccountId) (string, error) {
// Use the default collateral pool if the subaccount has no perpetual positions.
subaccount := k.GetSubaccount(ctx, subaccountId)
if len(subaccount.PerpetualPositions) == 0 {
return types.ModuleName, nil
return types.ModuleAddress, nil
}

// Get the first perpetual position and return the collateral pool name.
perpetual, err := k.perpetualsKeeper.GetPerpetual(ctx, subaccount.PerpetualPositions[0].PerpetualId)
return k.GetCollateralPoolFromPerpetualId(ctx, subaccount.PerpetualPositions[0].PerpetualId)
}

// GetCollateralPoolForSubaccountWithPerpetuals returns the collateral pool address based on the
// perpetual passed in as an argument.
func (k Keeper) GetCollateralPoolFromPerpetualId(ctx sdk.Context, perpetualId uint32) (sdk.AccAddress, error) {
perpetual, err := k.perpetualsKeeper.GetPerpetual(ctx, perpetualId)
if err != nil {
panic(fmt.Sprintf("GetCollateralPoolNameForSubaccount: %v", err))
return nil, err
}

if perpetual.Params.MarketType == perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_ISOLATED {
return types.ModuleName + ":" + lib.UintToString(perpetual.GetId()), nil
return authtypes.NewModuleAddress(types.ModuleName + ":" + lib.UintToString(perpetual.GetId())), nil
}

return types.ModuleName, nil
}

// IsIsolatedMarketSubaccount returns whether a subaccount is isolated to a specific market.
func (k Keeper) IsIsolatedMarketSubaccount(ctx sdk.Context, subaccountId types.SubaccountId) (bool, error) {
poolName, err := k.GetCollateralPoolNameForSubaccount(ctx, subaccountId)
if err != nil {
panic(fmt.Sprintf("IsIsolatedMarketSubaccount: %v", err))
}
return poolName != types.ModuleName, nil
return authtypes.NewModuleAddress(types.ModuleName), nil
}

// GetSubaccount returns a subaccount from its index.
Copy link

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [7-7]

Consider replacing math/rand with crypto/rand for generating random values, especially in contexts where security is a concern. While the direct use of math/rand is not observed in the provided code snippet, it's crucial to ensure that any random number generation, particularly in blockchain or financial applications, utilizes a cryptographically secure random number generator. This change enhances the security of the application by making it more resistant to predictability and attacks.

@shrenujb shrenujb merged commit a6429da into main Mar 7, 2024
16 of 17 checks passed
@shrenujb shrenujb deleted the tra84 branch March 7, 2024 17:40
Eric-Warehime pushed a commit to skip-mev/v4-chain that referenced this pull request Mar 12, 2024
…ccounts (dydxprotocol#1146)

Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange
Signed-off-by: Eric <eric.warehime@gmail.com>
Eric-Warehime added a commit to skip-mev/v4-chain that referenced this pull request Mar 12, 2024
commit d98f859
Author: Eric <eric.warehime@gmail.com>
Date:   Mon Mar 11 12:46:53 2024 -0700

    Update sample pregenesis

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 7f178fe
Author: Mohammed Affan <affanmd@nyu.edu>
Date:   Mon Mar 11 13:46:08 2024 -0400

    [OTE-209] Emit metrics gated through execution mode (dydxprotocol#1157)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 47e365d
Author: dydxwill <119354122+dydxwill@users.noreply.github.com>
Date:   Mon Mar 11 13:43:16 2024 -0400

    add aggregate comlink response code stats (dydxprotocol#1162)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 7774ad9
Author: shrenujb <98204323+shrenujb@users.noreply.github.com>
Date:   Fri Mar 8 17:30:49 2024 -0500

    [TRA-70] Add state migrations for isolated markets (dydxprotocol#1155)

    Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 89c7405
Author: Jonathan Fung <121899091+jonfung-dydx@users.noreply.github.com>
Date:   Thu Mar 7 17:28:06 2024 -0500

    [CT-517] E2E tests batch cancel (dydxprotocol#1149)

    * more end to end test

    * extraprint

    * more e2e test

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 41a3a41
Author: Teddy Ding <teddy@dydx.exchange>
Date:   Thu Mar 7 15:42:30 2024 -0500

    [OTE-200] OIMF protos (dydxprotocol#1125)

    * OIMF protos

    * add default genesis value, modify methods interface

    * fix genesis file

    * fix integration test

    * lint

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 2a062b1
Author: Teddy Ding <teddy@dydx.exchange>
Date:   Thu Mar 7 15:24:15 2024 -0500

    Add `v5` upgrade handler and set up container upgrade test (dydxprotocol#1153)

    * wip

    * update preupgrade_genesis

    * fix preupgrade_genesis.json

    * nit

    * setupUpgradeStoreLoaders for v5.0.0

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit b7942b3
Author: jayy04 <103467857+jayy04@users.noreply.github.com>
Date:   Thu Mar 7 13:43:48 2024 -0500

    [CT-647] construct the initial orderbook snapshot (dydxprotocol#1147)

    * [CT-647] construct the initial orderbook snapshot

    * [CT-647] initialize new streams and send orderbook snapshot (dydxprotocol#1152)

    * [CT-647] initialize new streams and send orderbook snapshot

    * use sync once

    * comments

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit c67a3c6
Author: shrenujb <98204323+shrenujb@users.noreply.github.com>
Date:   Thu Mar 7 12:40:37 2024 -0500

    [TRA-84] Move SA module address transfers to use perpetual based SA accounts (dydxprotocol#1146)

    Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange
    Signed-off-by: Eric <eric.warehime@gmail.com>

commit dba23e0
Author: Mohammed Affan <affanmd@nyu.edu>
Date:   Thu Mar 7 10:34:11 2024 -0500

    update readme link to point to the right page (dydxprotocol#1151)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit b5870d5
Author: Tian <tian@dydx.exchange>
Date:   Wed Mar 6 16:43:04 2024 -0500

    [TRA-86] scaffold x/vault (dydxprotocol#1148)

    * scaffold x/vault

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 0eca041
Author: jayy04 <103467857+jayy04@users.noreply.github.com>
Date:   Wed Mar 6 10:48:42 2024 -0500

    [CT-652] add command line flag for full node streaming (dydxprotocol#1145)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit b319cb8
Author: jayy04 <103467857+jayy04@users.noreply.github.com>
Date:   Tue Mar 5 21:58:35 2024 -0500

    [CT-646] stream offchain updates through stream manager (dydxprotocol#1138)

    * [CT-646] stream offchain updates through stream manager

    * comments

    * fix lint

    * get rid of finished

    * comments

    * comments

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 1c54620
Author: shrenujb <98204323+shrenujb@users.noreply.github.com>
Date:   Tue Mar 5 16:34:19 2024 -0500

    [TRA-78] Add function to retrieve collateral pool addr for a subaccount (dydxprotocol#1142)

    Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
    Signed-off-by: Eric <eric.warehime@gmail.com>

commit b8c1d62
Author: dydxwill <119354122+dydxwill@users.noreply.github.com>
Date:   Tue Mar 5 15:03:28 2024 -0500

    [OTE-141] implement post /compliance/geoblock (dydxprotocol#1129)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit ab8c570
Author: Jonathan Fung <121899091+jonfung-dydx@users.noreply.github.com>
Date:   Tue Mar 5 11:19:53 2024 -0500

    Fix mock-gen dydxprotocol#1140

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 12506a1
Author: shrenujb <98204323+shrenujb@users.noreply.github.com>
Date:   Mon Mar 4 21:33:28 2024 -0500

    [TRA-64] Use market specific insurance fund for cross or isolated markets (dydxprotocol#1132)

    Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 929f09e
Author: Jonathan Fung <121899091+jonfung-dydx@users.noreply.github.com>
Date:   Mon Mar 4 13:48:04 2024 -0800

    [CT-514] Clob `MsgBatchCancel` functionality (dydxprotocol#1110)

    * wip implementation

    * use new cometbft

    * Revert "use new cometbft"

    This reverts commit e5b8a03.

    * go mod tidy

    * basic e2e test

    * more msgBatchCancels in code

    * repeated fixed32 -> uint32

    * remove debug prints

    * update cometbft replace go.mod sha

    * one more debug print

    * typo

    * regen indexer protos

    * update comment on proto

    * proto comment changes

    * extract stateful validation into own fn

    * pr format comments

    * clean up test file

    * new return type with success and failure

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 41de83e
Author: dydxwill <119354122+dydxwill@users.noreply.github.com>
Date:   Mon Mar 4 12:22:16 2024 -0500

    add index to address read replica lag (dydxprotocol#1137)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 735d9a8
Author: dydxwill <119354122+dydxwill@users.noreply.github.com>
Date:   Mon Mar 4 11:56:59 2024 -0500

    rename (dydxprotocol#1136)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 86617dd
Author: jayy04 <103467857+jayy04@users.noreply.github.com>
Date:   Mon Mar 4 10:43:31 2024 -0500

    [CT-644] instantiate grpc stream manager (dydxprotocol#1134)

    * [CT-644] instantiate grpc stream manager

    * update type

    * update channel type

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 32afd64
Author: Eric <eric.warehime@gmail.com>
Date:   Mon Mar 11 12:41:06 2024 -0700

    Update go version in Dockerfile

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit ba27204
Author: Eric <eric.warehime@gmail.com>
Date:   Fri Mar 8 09:44:04 2024 -0800

    Add slinky utils, use that to convert between market and currency pair

commit 667a804
Author: Eric <eric.warehime@gmail.com>
Date:   Wed Mar 6 20:43:40 2024 -0800

    Update error messages

commit d53292c
Author: Eric <eric.warehime@gmail.com>
Date:   Wed Mar 6 20:16:01 2024 -0800

    Update docstrings, rename OracleClient

commit daad125
Author: Eric <eric.warehime@gmail.com>
Date:   Mon Mar 4 10:51:23 2024 -0800

    VoteExtension slinky logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants