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

test(perp): tests for denoms #1679

Merged
merged 3 commits into from
Nov 27, 2023
Merged

Conversation

matthiasmatt
Copy link
Contributor

@matthiasmatt matthiasmatt commented Nov 24, 2023

Description

Purpose

Why is this PR important?

Summary by CodeRabbit

  • New Features

    • Introduced new actions for settling trading positions within the system.
  • Tests

    • Added tests for new trading position settlement functionality and collateral denomination changes.
  • Documentation

    • Updated CHANGELOG with details of new tests added for the perpetual module message server.

@matthiasmatt matthiasmatt requested a review from a team as a code owner November 24, 2023 12:03
Copy link
Contributor

coderabbitai bot commented Nov 24, 2023

Walkthrough

The recent updates introduce new testing and functionality for a trading system's position settlement. Two new action functions have been added to handle the settlement of trading positions, including a check for expected failures. Additionally, new test functions ensure the robustness of the ChangeCollateralDenom and SettlePosition message handlers. The changes are encapsulated in a pull request that enhances the perp module's msg server tests without altering any existing interfaces or function signatures.

Changes

File Path Change Summary
x/perp/v2/integration/action/tx.go Added functions for settling positions and checking for expected failures in trading system.
x/perp/v2/keeper/msg_server_test.go Introduced new test functions for ChangeCollateralDenom and SettlePosition message handlers.
CHANGELOG.md Documented the addition of tests for the perp module msg server in pull request #1679.

Poem

In the code's woven warren, a rabbit hops with glee,
For new tests and functions in the perp module's spree. 🎉
With positions to settle, and checks to ensure,
The trading system's robust, its logic now more pure. 📈

No signature altered, no structure defaced,
Just a rabbit's touch, with elegance and grace. 🐰
Pull request merged, the changelog now sings,
Of the rabbit's craft, and the stability it brings. 📜


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

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

@matthiasmatt matthiasmatt changed the title Mat/more tests for denoms test(perp): tests for denoms Nov 24, 2023
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 f1f17bd and e9cbfee.
Files selected for processing (2)
  • x/perp/v2/integration/action/tx.go (2 hunks)
  • x/perp/v2/keeper/msg_server_test.go (2 hunks)
Additional comments: 4
x/perp/v2/integration/action/tx.go (1)
  • 3-8: The import of the "fmt" package is added, but there is no usage of it in the provided code. Verify if it's used elsewhere in the file or consider removing it if it's not needed.
x/perp/v2/keeper/msg_server_test.go (3)
  • 17-20: The import of the sudoerTypes package is correctly reflected in the code. This import is necessary for the new test functions that have been added to the file.

  • 344-375: The tests for ChangeCollateralDenom correctly handle various error scenarios, including insufficient permissions, invalid denom, and invalid sender address. These tests ensure that the message handler behaves as expected when encountering different types of invalid input.

  • 378-410: The new test cases for MsgServerSettlePosition and MsgServerSettlePositionShouldFail are implemented as described in the summary. They test the settlement of positions under different market conditions, ensuring that the functionality works as intended and that the system behaves correctly when attempting to settle positions in an open market.

Comment on lines +189 to +251
type msgServerSettlePosition struct {
pair asset.Pair
traderAddress sdk.AccAddress
Version uint64
}

func (m msgServerSettlePosition) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)

// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.Version,
})

return ctx, err, true
}

func MsgServerSettlePosition(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePosition{
pair: pair,
traderAddress: traderAddress,
Version: version,
}
}

type msgServerSettlePositionShouldFail struct {
pair asset.Pair
traderAddress sdk.AccAddress
Version uint64
}

func (m msgServerSettlePositionShouldFail) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)

// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.Version,
})
if err == nil {
return ctx, fmt.Errorf("should fail but no error returned"), true
}

return ctx, nil, true
}

func MsgServerSettlePositionShouldFail(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePositionShouldFail{
pair: pair,
traderAddress: traderAddress,
Version: version,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The new structs msgServerSettlePosition and msgServerSettlePositionShouldFail have been added, and they implement the action.Action interface. The Do methods for both structs are correctly implemented, and the error handling in msgServerSettlePositionShouldFail is appropriate for a test that expects failure. However, the field Version in both structs should follow Go naming conventions and start with a lowercase letter unless it is exported.

- Version       uint64
+ version       uint64

Commitable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
type msgServerSettlePosition struct {
pair asset.Pair
traderAddress sdk.AccAddress
Version uint64
}
func (m msgServerSettlePosition) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)
// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.Version,
})
return ctx, err, true
}
func MsgServerSettlePosition(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePosition{
pair: pair,
traderAddress: traderAddress,
Version: version,
}
}
type msgServerSettlePositionShouldFail struct {
pair asset.Pair
traderAddress sdk.AccAddress
Version uint64
}
func (m msgServerSettlePositionShouldFail) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)
// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.Version,
})
if err == nil {
return ctx, fmt.Errorf("should fail but no error returned"), true
}
return ctx, nil, true
}
func MsgServerSettlePositionShouldFail(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePositionShouldFail{
pair: pair,
traderAddress: traderAddress,
Version: version,
}
type msgServerSettlePosition struct {
pair asset.Pair
traderAddress sdk.AccAddress
version uint64
}
func (m msgServerSettlePosition) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)
// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.version,
})
return ctx, err, true
}
func MsgServerSettlePosition(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePosition{
pair: pair,
traderAddress: traderAddress,
version: version,
}
}
type msgServerSettlePositionShouldFail struct {
pair asset.Pair
traderAddress sdk.AccAddress
version uint64
}
func (m msgServerSettlePositionShouldFail) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
msgServer := keeper.NewMsgServerImpl(app.PerpKeeperV2)
// don't need to check response because it's already checked in clearing_house tests
_, err := msgServer.SettlePosition(sdk.WrapSDKContext(ctx), &types.MsgSettlePosition{
Pair: m.pair,
Sender: m.traderAddress.String(),
Version: m.version,
})
if err == nil {
return ctx, fmt.Errorf("should fail but no error returned"), true
}
return ctx, nil, true
}
func MsgServerSettlePositionShouldFail(
traderAddress sdk.AccAddress,
pair asset.Pair,
version uint64,
) action.Action {
return msgServerSettlePositionShouldFail{
pair: pair,
traderAddress: traderAddress,
version: version,
}

Comment on lines 340 to 341
})
require.ErrorContains(t, err, "spendable balance is smaller than 1luna")
Copy link
Contributor

Choose a reason for hiding this comment

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

The error message in the test case for DonateToEcosystemFund seems to be missing the actual balance in the error string. It would be more informative if the error message included the available balance.

Copy link

codecov bot commented Nov 24, 2023

Codecov Report

Merging #1679 (84176cd) into master (f1f17bd) will increase coverage by 0.14%.
The diff coverage is n/a.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1679      +/-   ##
==========================================
+ Coverage   73.75%   73.90%   +0.14%     
==========================================
  Files         192      192              
  Lines       15388    15388              
==========================================
+ Hits        11350    11373      +23     
+ Misses       3379     3355      -24     
- Partials      659      660       +1     

see 1 file with indirect coverage changes

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e9cbfee and 84176cd.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)

Comment on lines 71 to +74
* [#1639](https://github.com/NibiruChain/nibiru/pull/1639) - fix(perp): by default, disable new markets until they are toggled on.
* [#1652](https://github.com/NibiruChain/nibiru/pull/1652) - test: refactors cli.network suites with 'Integration' to use common function
* [#1659](https://github.com/NibiruChain/nibiru/pull/1659) - refactor(oracle): curate oracle default whitelist
* [#1679](https://github.com/NibiruChain/nibiru/pull/1679) - test(perp): add more tests for perp module msg server
Copy link
Contributor

Choose a reason for hiding this comment

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

The changelog entry for pull request #1679 is duplicated. It should only appear once to maintain clarity and avoid confusion.

@Unique-Divine Unique-Divine merged commit b102818 into master Nov 27, 2023
17 checks passed
@Unique-Divine Unique-Divine deleted the mat/more-tests-for-denoms branch November 27, 2023 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants