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

Remove Party from OrderAmendment #3443

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions execution/2500_GTT_amended_to_GTC_expires_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ func TestGTTAmendToGTCAmendInPlace_OrderGetExpired(t *testing.T) {
// now we edit the order t make it GTC so it should not expire
amendment := &commandspb.OrderAmendment{
OrderId: o1.Id,
PartyId: "aaa",
TimeInForce: types.Order_TIME_IN_FORCE_GTC,
}

amendConf, err := tm.market.AmendOrder(ctx, amendment)
amendConf, err := tm.market.AmendOrder(ctx, amendment, "aaa")
require.NotNil(t, amendConf)
require.NoError(t, err)
assert.Equal(t, types.Order_STATUS_ACTIVE, amendConf.Order.Status)
Expand Down
9 changes: 3 additions & 6 deletions execution/amends_test_flip_gtt_gtc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ func TestOrderBookAmends_FlipToGTT(t *testing.T) {
// now we edit the order t make it GTC so it should not expire
amendment := &commandspb.OrderAmendment{
OrderId: o1.Id,
PartyId: "aaa",
TimeInForce: types.Order_TIME_IN_FORCE_GTT,
ExpiresAt: &types.Timestamp{
Value: now.Add(10 * time.Second).UnixNano(),
},
}

amendConf, err := tm.market.AmendOrder(ctx, amendment)
amendConf, err := tm.market.AmendOrder(ctx, amendment, "aaa")
require.NotNil(t, amendConf)
require.NoError(t, err)
assert.Equal(t, types.Order_STATUS_ACTIVE, amendConf.Order.Status)
Expand All @@ -48,14 +47,13 @@ func TestOrderBookAmends_FlipToGTT(t *testing.T) {
// now we edit the order t make it GTC so it should not expire
amendment2 := &commandspb.OrderAmendment{
OrderId: o1.Id,
PartyId: "aaa",
TimeInForce: types.Order_TIME_IN_FORCE_GTT,
ExpiresAt: &types.Timestamp{
Value: now.Add(20 * time.Second).UnixNano(),
},
}

amendConf2, err := tm.market.AmendOrder(ctx, amendment2)
amendConf2, err := tm.market.AmendOrder(ctx, amendment2, "aaa")
require.NotNil(t, amendConf2)
require.NoError(t, err)
assert.Equal(t, types.Order_STATUS_ACTIVE, amendConf2.Order.Status)
Expand All @@ -64,11 +62,10 @@ func TestOrderBookAmends_FlipToGTT(t *testing.T) {
// now we edit the order t make it GTC so it should not expire
amendment3 := &commandspb.OrderAmendment{
OrderId: o1.Id,
PartyId: "aaa",
TimeInForce: types.Order_TIME_IN_FORCE_GTC,
}

amendConf3, err := tm.market.AmendOrder(ctx, amendment3)
amendConf3, err := tm.market.AmendOrder(ctx, amendment3, "aaa")
require.NotNil(t, amendConf3)
require.NoError(t, err)
assert.Equal(t, types.Order_STATUS_ACTIVE, amendConf3.Order.Status)
Expand Down
6 changes: 3 additions & 3 deletions execution/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,9 @@ func (e *Engine) SubmitOrder(ctx context.Context, orderSubmission *commandspb.Or

// AmendOrder takes order amendment details and attempts to amend the order
// if it exists and is in a editable state.
func (e *Engine) AmendOrder(ctx context.Context, orderAmendment *commandspb.OrderAmendment) (confirmation *types.OrderConfirmation, returnedErr error) {
func (e *Engine) AmendOrder(ctx context.Context, orderAmendment *commandspb.OrderAmendment, party string) (confirmation *types.OrderConfirmation, returnedErr error) {
defer func() {
e.notifyFailureOnError(ctx, returnedErr, orderAmendment, orderAmendment.PartyId)
e.notifyFailureOnError(ctx, returnedErr, orderAmendment, party)
}()

if e.log.IsDebug() {
Expand All @@ -476,7 +476,7 @@ func (e *Engine) AmendOrder(ctx context.Context, orderAmendment *commandspb.Orde

// we're passing a pointer here, so we need the wasActive var to be certain we're checking the original
// order status. It's possible order.Status will reflect the new status value if we don't
conf, err := mkt.AmendOrder(ctx, orderAmendment)
conf, err := mkt.AmendOrder(ctx, orderAmendment, party)
if err != nil {
return nil, err
}
Expand Down
207 changes: 12 additions & 195 deletions execution/event_generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,34 +537,33 @@ func TestEvents_Amending(t *testing.T) {

amendment := &commandspb.OrderAmendment{
OrderId: o1.Id,
PartyId: o1.PartyId,
MarketId: o1.MarketId,
Price: &types.Price{Value: 11},
}

amendConf, err := tm.market.AmendOrder(ctx, amendment)
amendConf, err := tm.market.AmendOrder(ctx, amendment, o1.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

amendment.Price = &types.Price{Value: 9}
amendConf, err = tm.market.AmendOrder(ctx, amendment)
amendConf, err = tm.market.AmendOrder(ctx, amendment, o1.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

amendment.Price = nil
amendment.SizeDelta = 3
amendConf, err = tm.market.AmendOrder(ctx, amendment)
amendConf, err = tm.market.AmendOrder(ctx, amendment, o1.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

amendment.SizeDelta = -2
amendConf, err = tm.market.AmendOrder(ctx, amendment)
amendConf, err = tm.market.AmendOrder(ctx, amendment, o1.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

amendment.SizeDelta = 1
amendment.Price = &types.Price{Value: 10}
amendConf, err = tm.market.AmendOrder(ctx, amendment)
amendConf, err = tm.market.AmendOrder(ctx, amendment, o1.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

Expand Down Expand Up @@ -617,22 +616,21 @@ func TestEvents_MovingPegsAround(t *testing.T) {

amendment := &commandspb.OrderAmendment{
OrderId: o2.Id,
PartyId: o2.PartyId,
MarketId: o2.MarketId,
Price: &types.Price{Value: 8},
}

amendConf, err := tm.market.AmendOrder(ctx, amendment)
amendConf, err := tm.market.AmendOrder(ctx, amendment, o2.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

amendment.Price = &types.Price{Value: 18}
amendConf, err = tm.market.AmendOrder(ctx, amendment)
amendConf, err = tm.market.AmendOrder(ctx, amendment, o2.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

amendment.Price = &types.Price{Value: 22}
amendConf, err = tm.market.AmendOrder(ctx, amendment)
amendConf, err = tm.market.AmendOrder(ctx, amendment, o2.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

Expand Down Expand Up @@ -685,12 +683,11 @@ func TestEvents_MovingPegsAround2(t *testing.T) {

amendment := &commandspb.OrderAmendment{
OrderId: o1.Id,
PartyId: o1.PartyId,
MarketId: o1.MarketId,
Price: &types.Price{Value: 9},
}

amendConf, err := tm.market.AmendOrder(ctx, amendment)
amendConf, err := tm.market.AmendOrder(ctx, amendment, o1.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

Expand Down Expand Up @@ -742,12 +739,11 @@ func TestEvents_AmendOrderToSelfTrade(t *testing.T) {

amendment := &commandspb.OrderAmendment{
OrderId: o3.Id,
PartyId: o3.PartyId,
MarketId: o3.MarketId,
Price: &types.Price{Value: 10},
}

amendConf, err := tm.market.AmendOrder(ctx, amendment)
amendConf, err := tm.market.AmendOrder(ctx, amendment, o3.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

Expand Down Expand Up @@ -799,13 +795,12 @@ func TestEvents_AmendOrderToIncreaseSizeAndPartiallyFill(t *testing.T) {

amendment := &commandspb.OrderAmendment{
OrderId: o3.Id,
PartyId: o3.PartyId,
MarketId: o3.MarketId,
Price: &types.Price{Value: 11},
SizeDelta: 5,
}

amendConf, err := tm.market.AmendOrder(ctx, amendment)
amendConf, err := tm.market.AmendOrder(ctx, amendment, o3.PartyId)
assert.NotNil(t, amendConf)
assert.NoError(t, err)

Expand Down Expand Up @@ -889,96 +884,7 @@ func TestEvents_CloseOutTraderWithNotEnoughLiquidity(t *testing.T) {
assert.Equal(t, 1, tm.market.GetParkedOrderCount())
}

// func TestEvents_CloseOutTraderWithLPOrder(t *testing.T) {
// now := time.Unix(10, 0)
// ctx := context.Background()
// mdb := subscribers.NewMarketDepthBuilder(ctx, nil, true)
// tm := startMarketInAuction(t, ctx, &now)
// addAccountWithAmount(tm, "trader-Z", 4000)

// // place some orders on the book for when we leave auction
// addAccountWithAmount(tm, "trader-X", 100000000)
// addAccountWithAmount(tm, "trader-Y", 100000000)

// orders := []*types.Order{
// getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GFA, "auctionOrder1", types.Side_SIDE_SELL, "trader-X", 5, 1),
// getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GFA, "auctionOrder2", types.Side_SIDE_BUY, "trader-Y", 5, 1),
// getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GTC, "auctionOrder3", types.Side_SIDE_SELL, "trader-X", 10, 3),
// getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GTC, "auctionOrder4", types.Side_SIDE_BUY, "trader-Y", 10, 2),
// }
// for _, o := range orders {
// _, err := tm.market.SubmitOrder(ctx, o)
// assert.NoError(t, err)
// }
// // move time forwards 20 seconds, so the opening auction can end
// now = now.Add(time.Second * 20)
// tm.market.OnChainTimeUpdate(ctx, now)

// o1 := getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GFN, "Order01", types.Side_SIDE_SELL, "trader-Z", 30, 1)
// o1conf, err := tm.market.SubmitOrder(ctx, o1)
// require.NotNil(t, o1conf)
// require.NoError(t, err)

// o4 := getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GTC, "Order04", types.Side_SIDE_BUY, "trader-B", 30, 1)
// o4conf, err := tm.market.SubmitOrder(ctx, o4)
// require.NotNil(t, o4conf)
// require.NoError(t, err)

// o2 := getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GTC, "Order02", types.Side_SIDE_BUY, "trader-B", 1, 99)
// o2conf, err := tm.market.SubmitOrder(ctx, o2)
// require.NotNil(t, o2conf)
// require.NoError(t, err)

// o3 := getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GTC, "Order03", types.Side_SIDE_BUY, "trader-B", 1, 100)
// o3conf, err := tm.market.SubmitOrder(ctx, o3)
// require.NotNil(t, o3conf)
// require.NoError(t, err)

// o5 := getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GTC, "Order05", types.Side_SIDE_SELL, "trader-B", 1, 110)
// o5conf, err := tm.market.SubmitOrder(ctx, o5)
// require.NotNil(t, o5conf)
// require.NoError(t, err)

// buys := []*types.LiquidityOrder{
// {Reference: types.PeggedReference_PEGGED_REFERENCE_BEST_BID, Offset: -1, Proportion: 50},
// }
// sells := []*types.LiquidityOrder{
// {Reference: types.PeggedReference_PEGGED_REFERENCE_BEST_ASK, Offset: 1, Proportion: 50},
// }

// lps := &types.LiquidityProvisionSubmission{
// Fee: "0.05",
// MarketId: tm.market.GetID(),
// CommitmentAmount: 1000,
// Buys: buys,
// Sells: sells}

// err = tm.market.SubmitLiquidityProvision(ctx, lps, "trader-Z", "LPOrder01")
// require.NoError(t, err)
// require.Equal(t, 1, tm.market.GetLPSCount())

// // Change mark price to force Trader-Z to be closed out
// o6 := getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GTC, "Order06", types.Side_SIDE_SELL, "trader-C", 10, 100)
// o6conf, err := tm.market.SubmitOrder(ctx, o6)
// require.NotNil(t, o6conf)
// require.NoError(t, err)
// require.Equal(t, 0, tm.market.GetLPSCount())

// // Check we have the right amount of events
// //assert.Equal(t, uint64(12), tm.orderEventCount)
// //assert.Equal(t, int64(3), tm.market.GetOrdersOnBookCount())
// assert.Equal(t, uint64(15), tm.orderEventCount)
// assert.Equal(t, int64(6), tm.market.GetOrdersOnBookCount())

// processEvents(t, tm, mdb)
// assert.Equal(t, int64(6), mdb.GetOrderCount(tm.market.GetID()))
// assert.Equal(t, uint64(1), mdb.GetOrderCountAtPrice(tm.market.GetID(), types.Side_SIDE_SELL, 100))
// assert.Equal(t, uint64(10), mdb.GetVolumeAtPrice(tm.market.GetID(), types.Side_SIDE_SELL, 100))
// assert.Equal(t, 0, tm.market.GetPeggedOrderCount())
// assert.Equal(t, 0, tm.market.GetParkedOrderCount())
// }

func TestEvents_LPOrderRecalcDueToFill(t *testing.T) {
func TestEvents_LPOrderRecalculationDueToFill(t *testing.T) {
now := time.Unix(10, 0)
ctx := context.Background()
mdb := subscribers.NewMarketDepthBuilder(ctx, nil, true)
Expand Down Expand Up @@ -1106,92 +1012,3 @@ func TestEvents_PeggedOrders(t *testing.T) {
assert.Equal(t, 2, tm.market.GetPeggedOrderCount())
assert.Equal(t, 0, tm.market.GetParkedOrderCount()) //??
}

func TestEvents_Fuzzing(t *testing.T) {
/* now := time.Unix(10, 0)
ctx := context.Background()
mdb := subscribers.NewMarketDepthBuilder(ctx, nil, true)
tm := startMarketInAuction(t, ctx, &now)
leaveAuction(tm, ctx, &now)

seed := int64(1) //time.Now().UnixNano()
r := rand.New(rand.NewSource(seed))
fmt.Println("Seed", seed)

var (
traderCount int = 10
side types.Side
price uint64
size uint64
)

// Create the traders
for i := 0; i < traderCount; i++ {
traderName := fmt.Sprintf("Trader%02d", i)
addAccountWithAmount(tm, traderName, 1000)
}

for i := 1; i < 10000000; i++ {
if i%10000 == 0 {
fmt.Println("Processing ", i)
}

if i == 75 {
fmt.Println("Something fishy....")
}

randomTrader := r.Intn(traderCount)
traderName := fmt.Sprintf("Trader%02d", randomTrader)
orderID := fmt.Sprintf("Order%8d", i)
action := r.Intn(1000)

if r.Intn(2) == 0 {
side = types.Side_SIDE_BUY
} else {
side = types.Side_SIDE_SELL
}

size = uint64(r.Int63n(100) + 1)

if action <= 200 {
// Pegged order
po := getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GTC, orderID, side, traderName, size, 0)
ref := r.Intn(2)
if side == types.Side_SIDE_BUY {
if ref == 0 {
po.PeggedOrder = &types.PeggedOrder{Reference: types.PeggedReference_PEGGED_REFERENCE_MID, Offset: -1 - r.Int63n(10)}
} else {
po.PeggedOrder = &types.PeggedOrder{Reference: types.PeggedReference_PEGGED_REFERENCE_BEST_BID, Offset: -1 - r.Int63n(10)}
}
} else {
if ref == 0 {
po.PeggedOrder = &types.PeggedOrder{Reference: types.PeggedReference_PEGGED_REFERENCE_MID, Offset: r.Int63n(10) + 1}
} else {
po.PeggedOrder = &types.PeggedOrder{Reference: types.PeggedReference_PEGGED_REFERENCE_BEST_ASK, Offset: r.Int63n(10) + 1}
}
}
_, _ = tm.market.SubmitOrder(ctx, po)
processEventsWithCounter(t, tm, mdb, i)
clearEvents(tm)
} else if action < 999 {
// Normal order
if side == types.Side_SIDE_BUY {
price = uint64(50 + r.Intn(60))
} else {
price = uint64(90 + r.Intn(60))
}
o := getMarketOrder(tm, now, types.Order_TYPE_LIMIT, types.Order_TIME_IN_FORCE_GTC, orderID, side, traderName, size, price)
_, _ = tm.market.SubmitOrder(ctx, o)
processEventsWithCounter(t, tm, mdb, i)
clearEvents(tm)
} else {
// Cancel all the order for a trader
tm.market.CancelAllOrders(ctx, traderName)
processEventsWithCounter(t, tm, mdb, i)
clearEvents(tm)
}
// Move time forward
now = now.Add(1 * time.Second)
tm.market.OnChainTimeUpdate(ctx, now)
}*/
}
3 changes: 1 addition & 2 deletions execution/liquidity_provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,9 @@ func TestLiquidity_MustNotBeAbleToCancelOrAmendLPOrder(t *testing.T) {

// Attempt to amend one of the pegged orders
amend := &commandspb.OrderAmendment{OrderId: orders[0].Id,
PartyId: orders[0].PartyId,
MarketId: orders[0].MarketId,
SizeDelta: +5}
amendConf, err := tm.market.AmendOrder(ctx, amend)
amendConf, err := tm.market.AmendOrder(ctx, amend, orders[0].PartyId)
require.Error(t, err)
require.Nil(t, amendConf)
assert.Equal(t, types.OrderError_ORDER_ERROR_EDIT_NOT_ALLOWED, err)
Expand Down