Skip to content

Commit

Permalink
added testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sr33j committed Sep 4, 2024
1 parent 406aafb commit 0f7a200
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
16 changes: 7 additions & 9 deletions protocol/x/vault/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,13 @@ func (k Keeper) GetVaultClobOrders(
leveragePpmI.Add(leveragePpmI, leveragePpm)

// Calculate skew.
// Also, if the side would increase the vault's inventory, make the order post-only.
skewPpmI := lib.BigMulPpm(leveragePpmI, skewFactorPpm, true)
timeInForceType := clobtypes.Order_TIME_IN_FORCE_UNSPECIFIED
if leveragePpm.Sign() < 0 {
if side == clobtypes.Order_SIDE_SELL {
// ask when short: skew_i = (skew_factor * leverage_i - 1)^2 - 1
skewPpmI.Sub(skewPpmI, lib.BigIntOneMillion())
skewPpmI = lib.BigMulPpm(skewPpmI, skewPpmI, true)
skewPpmI.Sub(skewPpmI, lib.BigIntOneMillion())

// post-only order for sell orders when short
timeInForceType = clobtypes.Order_TIME_IN_FORCE_POST_ONLY

} else {
// bid when short: skew_i = -skew_factor * leverage_i
skewPpmI.Neg(skewPpmI)
Expand All @@ -307,9 +301,6 @@ func (k Keeper) GetVaultClobOrders(
skewPpmI = lib.BigMulPpm(skewPpmI, skewPpmI, true)
skewPpmI.Sub(skewPpmI, lib.BigIntOneMillion())
skewPpmI.Neg(skewPpmI)

// post-only order for buy orders when long
timeInForceType = clobtypes.Order_TIME_IN_FORCE_POST_ONLY
}
}

Expand Down Expand Up @@ -356,6 +347,13 @@ 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,
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 @@ -459,6 +459,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 @@ -534,6 +535,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 @@ -614,6 +623,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 @@ -694,6 +713,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 @@ -762,6 +791,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 @@ -786,6 +823,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 @@ -796,6 +834,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 @@ -817,6 +856,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 @@ -945,6 +985,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 @@ -959,6 +1000,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 @@ -971,13 +1013,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 0f7a200

Please sign in to comment.