Skip to content

Commit

Permalink
Sp/vault post only orders (#2198)
Browse files Browse the repository at this point in the history
  • Loading branch information
sr33j authored Sep 19, 2024
1 parent aaa0860 commit 25dbec5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions protocol/x/vault/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,20 @@ func (k Keeper) GetVaultClobOrders(
maxSubticks,
)

// If the side would increase the vault's inventory, make the order post-only.
timeInForceType := clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED
if (side == clobtypes.Order_SIDE_SELL && inventory.Sign() <= 0) ||
(side == clobtypes.Order_SIDE_BUY && inventory.Sign() >= 0) {
timeInForceType = clobtypes.Order_TIME_IN_FORCE_POST_ONLY
}

return &clobtypes.Order{
OrderId: *orderId,
Side: side,
Quantums: orderSize.Uint64(), // Validated to be a uint64 above.
Subticks: subticksRounded,
GoodTilOneof: goodTilBlockTime,
TimeInForce: timeInForceType,
}
}

Expand Down
44 changes: 44 additions & 0 deletions protocol/x/vault/keeper/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ func TestGetVaultClobOrders(t *testing.T) {
/* --- Expectations --- */
expectedOrderSubticks []uint64
expectedOrderQuantums []uint64
expectedTimeInForce []clobtypes.Order_TimeInForce
expectedErr error
}{
"Success - Vault Clob 0, 2 layers, leverage 0, doesn't cross oracle price": {
Expand Down Expand Up @@ -568,6 +569,14 @@ func TestGetVaultClobOrders(t *testing.T) {
20_000_000_000,
20_000_000_000,
},
// post-only if increases inventory
// vault is flat, all orders should be post-only
expectedTimeInForce: []clobtypes.Order_TimeInForce{
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
},
},
"Success - Vault Clob 1, 3 layers, leverage -0.6, doesn't cross oracle price": {
vaultQuotingParams: vaulttypes.QuotingParams{
Expand Down Expand Up @@ -648,6 +657,16 @@ func TestGetVaultClobOrders(t *testing.T) {
41_666_000,
41_666_000,
},
// post-only if increases inventory
// vault is short, sell orders should be post-only
expectedTimeInForce: []clobtypes.Order_TimeInForce{
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED,
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED,
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED,
},
},
"Success - Vault Clob 1, 3 layers, leverage -3, crosses oracle price": {
vaultQuotingParams: vaulttypes.QuotingParams{
Expand Down Expand Up @@ -728,6 +747,16 @@ func TestGetVaultClobOrders(t *testing.T) {
33_333_000,
33_333_000,
},
// post-only if increases inventory
// vault is short, sell orders should be post-only
expectedTimeInForce: []clobtypes.Order_TimeInForce{
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED,
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED,
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED,
},
},
"Success - Vault Clob 1, 2 layers, leverage 3, crosses oracle price": {
vaultQuotingParams: vaulttypes.QuotingParams{
Expand Down Expand Up @@ -796,6 +825,14 @@ func TestGetVaultClobOrders(t *testing.T) {
333_333_000,
333_333_000,
},
// post-only if increases inventory
// vault is long, buy orders should be post-only
expectedTimeInForce: []clobtypes.Order_TimeInForce{
clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED,
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED,
clobtypes.Order_TIME_IN_FORCE_POST_ONLY,
},
},
"Success - Get orders from Vault for Clob Pair 1, No Orders due to Zero Order Size": {
vaultQuotingParams: vaulttypes.QuotingParams{
Expand All @@ -820,6 +857,7 @@ func TestGetVaultClobOrders(t *testing.T) {
// round down to nearest multiple of step_base_quantums=1_000.
// order size is 0.
expectedOrderQuantums: []uint64{},
expectedTimeInForce: []clobtypes.Order_TimeInForce{},
},
"Success - Clob Pair doesn't exist, Empty orders": {
vaultQuotingParams: vaulttypes.DefaultQuotingParams(),
Expand All @@ -830,6 +868,7 @@ func TestGetVaultClobOrders(t *testing.T) {
perpetual: constants.EthUsd_NoMarginRequirement,
expectedOrderSubticks: []uint64{},
expectedOrderQuantums: []uint64{},
expectedTimeInForce: []clobtypes.Order_TimeInForce{},
},
"Success - Clob Pair in status final settlement, Empty orders": {
vaultQuotingParams: vaulttypes.DefaultQuotingParams(),
Expand All @@ -851,6 +890,7 @@ func TestGetVaultClobOrders(t *testing.T) {
perpetual: constants.EthUsd_NoMarginRequirement,
expectedOrderSubticks: []uint64{},
expectedOrderQuantums: []uint64{},
expectedTimeInForce: []clobtypes.Order_TimeInForce{},
},
"Error - Vault equity is zero": {
vaultQuotingParams: vaulttypes.DefaultQuotingParams(),
Expand Down Expand Up @@ -979,6 +1019,7 @@ func TestGetVaultClobOrders(t *testing.T) {
side clobtypes.Order_Side,
quantums uint64,
subticks uint64,
timeInForce clobtypes.Order_TimeInForce,
) *clobtypes.Order {
return &clobtypes.Order{
OrderId: clobtypes.OrderId{
Expand All @@ -993,6 +1034,7 @@ func TestGetVaultClobOrders(t *testing.T) {
GoodTilOneof: &clobtypes.Order_GoodTilBlockTime{
GoodTilBlockTime: uint32(ctx.BlockTime().Unix()) + tc.vaultQuotingParams.OrderExpirationSeconds,
},
TimeInForce: timeInForce,
}
}
expectedOrders := make([]*clobtypes.Order, 0)
Expand All @@ -1005,13 +1047,15 @@ func TestGetVaultClobOrders(t *testing.T) {
clobtypes.Order_SIDE_SELL,
tc.expectedOrderQuantums[i],
tc.expectedOrderSubticks[i],
tc.expectedTimeInForce[i],
),
// bid.
buildVaultClobOrder(
uint8(i/2),
clobtypes.Order_SIDE_BUY,
tc.expectedOrderQuantums[i+1],
tc.expectedOrderSubticks[i+1],
tc.expectedTimeInForce[i+1],
),
)
}
Expand Down

0 comments on commit 25dbec5

Please sign in to comment.