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-134] construct simple vault orders #1206

Merged
merged 2 commits into from
Mar 20, 2024
Merged

[TRA-134] construct simple vault orders #1206

merged 2 commits into from
Mar 20, 2024

Conversation

tqin7
Copy link
Contributor

@tqin7 tqin7 commented Mar 19, 2024

Changelist

construct simple vault orders

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 19, 2024

Copy link
Contributor

coderabbitai bot commented Mar 19, 2024

Walkthrough

The recent updates introduce mathematical rounding capabilities for big.Rat values, enhance vault order management in the protocol, and refine keeper interfaces for better data retrieval and management. Specifically, a new function for rounding big.Rat values has been added, alongside tests for its verification. Vault order handling sees the introduction of new functions and constants for improved strategy parameter management and order retrieval. Keeper interfaces have been streamlined to focus on essential data retrieval methods, improving interaction with CLOB pairs, perpetuals, and market prices.

Changes

File Path Change Summary
protocol/lib/big_math.go, protocol/lib/big_math_test.go Introduced BigRatRoundToNearestMultiple function and its tests for rounding big.Rat values.
protocol/x/vault/keeper/orders.go, protocol/x/vault/keeper/orders_test.go Added constants, implemented order management functions, and added tests for vault strategy parameters and CLOB order retrieval.
protocol/x/vault/types/expected_keepers.go Modified ClobKeeper, PerpetualsKeeper, and PricesKeeper interfaces to streamline data retrieval methods.

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.

)

// TODO (TRA-118): store vault strategy constants in x/vault state.
const (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

some of the constants here will be used when strategy is more fully implemented

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 9888453 and 01bfe33.
Files selected for processing (5)
  • protocol/lib/big_math.go (1 hunks)
  • protocol/lib/big_math_test.go (1 hunks)
  • protocol/x/vault/keeper/orders.go (2 hunks)
  • protocol/x/vault/keeper/orders_test.go (1 hunks)
  • protocol/x/vault/types/expected_keepers.go (2 hunks)
Files skipped from review as they are similar to previous changes (5)
  • protocol/lib/big_math.go
  • protocol/lib/big_math_test.go
  • protocol/x/vault/keeper/orders.go
  • protocol/x/vault/keeper/orders_test.go
  • protocol/x/vault/types/expected_keepers.go

Pair: constants.TestMarketParams[1].Pair,
Exponent: constants.TestMarketParams[1].Exponent,
MinExchanges: constants.TestMarketParams[1].MinExchanges,
MinPriceChangePpm: 4_200, // Set a high min price change to test spread calculation.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Add this to the test cases instead if it's a variable to test against.

// Expected orders.
expectedOrders []clobtypes.Order
}{
"Get orders from Vault for Clob Pair 0": {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Non-blocking, the test cases are quite verbose and probably can be simplified to hold several parameters about each order rather than all the entire orders / constants could be declared for the orders.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah agreed. let me simplify these tests

GoodTilOneof: goodTilBlockTime,
}

orders = append(orders, ask, bid)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Non-blocking, given you already know the number of layers, the slice could have been allocated with length 2*layers and this could be:

orders[2*i] = ask
orders[2*i+1] = bid

func (k Keeper) GetVaultClobOrderClientId(
ctx sdk.Context,
side clobtypes.Order_Side,
layer uint8,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: layer = 0 is invalid, given layer always starts at 1?

Copy link
Contributor Author

@tqin7 tqin7 Mar 19, 2024

Choose a reason for hiding this comment

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

technically layer = 0 is okay for this function. it's up to other functions on what value of layer to pass in. adding a check on layer != 0 is ok but might make using this function a bit too complicated as parent func has to check whether there's an error

},
"Sell, Block Height Odd (negative), Layer 202": {
side: clobtypes.Order_SIDE_SELL, // 1<<31
blockHeight: -678987, // 1<<30
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Weird test case given this is definitely an invalid state for the ctx.BlockHeight to be negative. Non-blocking though, maybe note that somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah just added this test case as blockHeight is an int64. wanted to make sure that a negative value doesn't break this function. will add a note on this

func (k Keeper) GetVaultClobOrders(
ctx sdk.Context,
vaultId types.VaultId,
) (orders []clobtypes.Order) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Why not return error as well? Not obvious an error occurred if an empty slice of orders is returned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call! Will add error in return

func (k Keeper) GetVaultClobOrders(
ctx sdk.Context,
vaultId types.VaultId,
) (orders []clobtypes.Order) {
Copy link
Contributor

Choose a reason for hiding this comment

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

minor nit: non-blocking, do you expect the orders returned to be passed around into other functions / helpers alot? Could make sense to return a slice of pointers to order structs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! A slice of pointers will be more efficient

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 01bfe33 and 31abd94.
Files selected for processing (3)
  • protocol/x/vault/keeper/orders.go (2 hunks)
  • protocol/x/vault/keeper/orders_test.go (1 hunks)
  • protocol/x/vault/types/errors.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • protocol/x/vault/keeper/orders.go
  • protocol/x/vault/keeper/orders_test.go
Additional comments: 2
protocol/x/vault/types/errors.go (2)
  • 13-17: The addition of ErrClobPairNotFound is clear and follows the established pattern for error registration. The error code is unique and the message is descriptive.
  • 18-22: The addition of ErrMarketParamNotFound is clear and follows the established pattern for error registration. The error code is unique and the message is descriptive.

func BigRatRoundToNearestMultiple(
value *big.Rat,
base uint32,
up bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: should we use an enum for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think a boolean should suffice?

)
quotientFloored := new(big.Int).Div(quotient.Num(), quotient.Denom())

if up && quotientFloored.Cmp(quotient.Num()) != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

if quotient.Num() == quotientFloored, doesnt that mean that quotient.Denom() = 1? Why does that mean we want to round up?

Copy link
Contributor

@vincentwschau vincentwschau Mar 20, 2024

Choose a reason for hiding this comment

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

quotientFloored.Cmp(quotient.Num()) != 0 is only true when quotient.Num() != quotientFloored (ref).
So this conditional is saying:

  • if we want to round up (up == true) and if quotient.Num() != quotientFloored then add one to the floored quotient

So if quotient.Denom() == 1, then quotientFloored.Cmp(quotient.Num()) == 0 and this conditional wouldn't round up.

const (
// Determines how many layers of orders a vault places.
// E.g. if num_levels=2, a vault places 2 asks and 2 bids.
NUM_LAYERS = uint8(2)
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we call this layers instead of max_orders_placed_by_vault_per_side. Does layers mean something in trading?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh it's just a made-up term. a vault places orders in terms of layers (layer 1, layer 2, ... where each layer gets further and further away from oracle price)

Comment on lines +88 to +91
spreadPpm := lib.Max(
MIN_BASE_SPREAD_PPM,
BASE_SPREAD_MIN_PRICE_CHANGE_PREMIUM_PPM+marketParam.MinPriceChangePpm,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this the spreadPpm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +112 to +150
// Construct ask at this layer.
ask := clobtypes.Order{
OrderId: clobtypes.OrderId{
SubaccountId: vault,
ClientId: k.GetVaultClobOrderClientId(ctx, clobtypes.Order_SIDE_SELL, uint8(i+1)),
OrderFlags: clobtypes.OrderIdFlags_LongTerm,
ClobPairId: clobPair.Id,
},
Side: clobtypes.Order_SIDE_SELL,
Quantums: clobPair.StepBaseQuantums, // TODO (TRA-144): Implement order size
Subticks: lib.BigRatRoundToNearestMultiple(
askSubticks,
clobPair.SubticksPerTick,
true, // round up for asks
),
GoodTilOneof: goodTilBlockTime,
}

// Construct bid at this layer.
bid := clobtypes.Order{
OrderId: clobtypes.OrderId{
SubaccountId: vault,
ClientId: k.GetVaultClobOrderClientId(ctx, clobtypes.Order_SIDE_BUY, uint8(i+1)),
OrderFlags: clobtypes.OrderIdFlags_LongTerm,
ClobPairId: clobPair.Id,
},
Side: clobtypes.Order_SIDE_BUY,
Quantums: clobPair.StepBaseQuantums, // TODO (TRA-144): Implement order size
Subticks: lib.BigRatRoundToNearestMultiple(
bidSubticks,
clobPair.SubticksPerTick,
false, // round down for bids
),
GoodTilOneof: goodTilBlockTime,
}

orders[2*i] = &ask
orders[2*i+1] = &bid
}
Copy link
Contributor

Choose a reason for hiding this comment

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

a lot of duplicate code here, could we move it into a subfunction and reuse?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't think it's too much duplicate code? I feel having ask and bid explicitly here makes it very clear what each order is like and keeps the function simple

@tqin7 tqin7 merged commit bd041ab into main Mar 20, 2024
17 checks passed
@tqin7 tqin7 deleted the tq/tra-134 branch March 20, 2024 16:20
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