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

Refactor order structures #111

Merged
merged 9 commits into from
Sep 18, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin/evm/limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func (lop *limitOrderProcesser) UpdateLastPremiumFractionFromStorage() {
}
}

orderMap := lop.memoryDb.GetOrderBookData().OrderMap
orderMap := lop.memoryDb.GetOrderBookData().Orders
for orderHash, order := range orderMap {
if order.FilledBaseAssetQuantity.CmpAbs(order.BaseAssetQuantity) > 0 {
log.Info("Order map cleanup - deleting order", "hash", orderHash.String(), "baseAssetQuantity", order.BaseAssetQuantity, "filledBaseAssetQuantity", order.FilledBaseAssetQuantity)
Expand Down
24 changes: 12 additions & 12 deletions plugin/evm/orderbook/contract_events_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,24 +263,24 @@ func (cep *ContractEventsProcessor) handleIOCOrderBookEvent(event *types.Log) {
}
orderId := event.Topics[2]
if !removed {
order := IOCOrder{}
order.DecodeFromRawOrder(args["order"])
limitOrder := Order{
iocOrder := IOCOrder{}
iocOrder.DecodeFromRawOrder(args["order"])
order := Order{
Id: orderId,
Market: Market(order.AmmIndex.Int64()),
PositionType: getPositionTypeBasedOnBaseAssetQuantity(order.BaseAssetQuantity),
Market: Market(iocOrder.AmmIndex.Int64()),
PositionType: getPositionTypeBasedOnBaseAssetQuantity(iocOrder.BaseAssetQuantity),
Trader: getAddressFromTopicHash(event.Topics[1]),
BaseAssetQuantity: order.BaseAssetQuantity,
BaseAssetQuantity: iocOrder.BaseAssetQuantity,
FilledBaseAssetQuantity: big.NewInt(0),
Price: order.Price,
RawOrder: &order,
Salt: order.Salt,
ReduceOnly: order.ReduceOnly,
Price: iocOrder.Price,
RawOrder: &iocOrder,
Salt: iocOrder.Salt,
ReduceOnly: iocOrder.ReduceOnly,
BlockNumber: big.NewInt(int64(event.BlockNumber)),
OrderType: IOC,
}
log.Info("IOCOrder/OrderPlaced", "order", limitOrder, "number", event.BlockNumber)
cep.database.Add(&limitOrder)
log.Info("IOCOrder/OrderPlaced", "order", order, "number", event.BlockNumber)
cep.database.Add(&order)
atvanguard marked this conversation as resolved.
Show resolved Hide resolved
} else {
log.Info("IOCOrder/OrderPlaced removed", "orderId", orderId.String(), "block", event.BlockHash.String(), "number", event.BlockNumber)
cep.database.Delete(orderId)
Expand Down
40 changes: 20 additions & 20 deletions plugin/evm/orderbook/contract_events_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func TestProcessEvents(t *testing.T) {
orderMatchedEventLog1 := getEventLog(OrderBookContractAddress, orderMatchedEventTopics1, orderMatchedEventData1, orderMatchedBlockNumber)
cep.ProcessEvents([]*types.Log{longOrderAcceptedEventLog, shortOrderAcceptedEventLog, orderMatchedEventLog0, orderMatchedEventLog1})

actualLongOrder := db.OrderMap[getIdFromLimitOrder(longOrder)]
actualLongOrder := db.Orders[getIdFromLimitOrder(longOrder)]
assert.Equal(t, fillAmount, actualLongOrder.FilledBaseAssetQuantity)

actualShortOrder := db.OrderMap[getIdFromLimitOrder(shortOrder)]
actualShortOrder := db.Orders[getIdFromLimitOrder(shortOrder)]
assert.Equal(t, big.NewInt(0).Neg(fillAmount), actualShortOrder.FilledBaseAssetQuantity)
})

Expand Down Expand Up @@ -164,7 +164,7 @@ func TestOrderBookMarginAccountClearingHouseEventInLog(t *testing.T) {
cep.ProcessAcceptedEvents([]*types.Log{marginAccountLog, clearingHouseLog}, true)

//OrderBook log - OrderAccepted
actualLimitOrder := *db.GetOrderBookData().OrderMap[getIdFromLimitOrder(order)]
actualLimitOrder := *db.GetOrderBookData().Orders[getIdFromLimitOrder(order)]
args := map[string]interface{}{}
orderBookABI.UnpackIntoMap(args, "OrderAccepted", orderAcceptedEventData)
assert.Equal(t, Market(ammIndex.Int64()), actualLimitOrder.Market)
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestHandleOrderBookEvent(t *testing.T) {
orderAcceptedEventData := []byte{}
log := getEventLog(OrderBookContractAddress, topics, orderAcceptedEventData, blockNumber)
cep.ProcessEvents([]*types.Log{log})
actualLimitOrder := db.GetOrderBookData().OrderMap[orderId]
actualLimitOrder := db.GetOrderBookData().Orders[orderId]
assert.Nil(t, actualLimitOrder)
})
t.Run("When data in log unpack succeeds", func(t *testing.T) {
Expand All @@ -219,7 +219,7 @@ func TestHandleOrderBookEvent(t *testing.T) {
log := getEventLog(OrderBookContractAddress, topics, orderAcceptedEventData, blockNumber)
cep.ProcessEvents([]*types.Log{log})

actualLimitOrder := db.GetOrderBookData().OrderMap[orderId]
actualLimitOrder := db.GetOrderBookData().Orders[orderId]
args := map[string]interface{}{}
orderBookABI.UnpackIntoMap(args, "OrderAccepted", orderAcceptedEventData)
assert.Equal(t, Market(ammIndex.Int64()), actualLimitOrder.Market)
Expand Down Expand Up @@ -260,15 +260,15 @@ func TestHandleOrderBookEvent(t *testing.T) {
log := getEventLog(OrderBookContractAddress, topics, orderCancelAcceptedEventData, blockNumber)
cep.ProcessEvents([]*types.Log{log})
orderId := getIdFromOrder(*limitOrder)
actualLimitOrder := db.GetOrderBookData().OrderMap[orderId]
actualLimitOrder := db.GetOrderBookData().Orders[orderId]
assert.Equal(t, limitOrder, actualLimitOrder)
})
t.Run("When data in log unpack succeeds", func(t *testing.T) {
orderCancelAcceptedEventData, _ := event.Inputs.NonIndexed().Pack(timestamp)
log := getEventLog(OrderBookContractAddress, topics, orderCancelAcceptedEventData, blockNumber)
orderId := getIdFromOrder(*limitOrder)
cep.ProcessEvents([]*types.Log{log})
actualLimitOrder := db.GetOrderBookData().OrderMap[orderId]
actualLimitOrder := db.GetOrderBookData().Orders[orderId]
assert.Equal(t, Cancelled, actualLimitOrder.getOrderStatus().Status)
})
})
Expand Down Expand Up @@ -643,20 +643,20 @@ func TestRemovedEvents(t *testing.T) {
cep.ProcessEvents([]*types.Log{longOrderAcceptedEventLog})

// order exists in memory now
assert.Equal(t, db.OrderMap[longOrderId].Salt, longOrder.Salt)
assert.Equal(t, db.Orders[longOrderId].Salt, longOrder.Salt)

// order should be deleted if OrderAccepted log is removed
longOrderAcceptedEventLog.Removed = true
cep.ProcessEvents([]*types.Log{longOrderAcceptedEventLog})
assert.Nil(t, db.OrderMap[longOrderId])
assert.Nil(t, db.Orders[longOrderId])
})

t.Run("un-cancel an order when OrderCancelAccepted is removed", func(t *testing.T) {
longOrderAcceptedEventLog := getEventLog(OrderBookContractAddress, longOrderAcceptedEventTopics, longOrderAcceptedEventData, blockNumber.Uint64())
cep.ProcessEvents([]*types.Log{longOrderAcceptedEventLog})

// order exists in memory now
assert.Equal(t, db.OrderMap[longOrderId].Salt, longOrder.Salt)
assert.Equal(t, db.Orders[longOrderId].Salt, longOrder.Salt)

// cancel it
orderCancelAcceptedEvent := getEventFromABI(limitOrderrderBookABI, "OrderCancelAccepted")
Expand All @@ -665,12 +665,12 @@ func TestRemovedEvents(t *testing.T) {
orderCancelAcceptedLog := getEventLog(OrderBookContractAddress, orderCancelAcceptedEventTopics, orderCancelAcceptedEventData, blockNumber.Uint64()+2)
cep.ProcessEvents([]*types.Log{orderCancelAcceptedLog})

assert.Equal(t, db.OrderMap[longOrderId].getOrderStatus().Status, Cancelled)
assert.Equal(t, db.Orders[longOrderId].getOrderStatus().Status, Cancelled)

// now uncancel it
orderCancelAcceptedLog.Removed = true
cep.ProcessEvents([]*types.Log{orderCancelAcceptedLog})
assert.Equal(t, db.OrderMap[longOrderId].getOrderStatus().Status, Placed)
assert.Equal(t, db.Orders[longOrderId].getOrderStatus().Status, Placed)
})

t.Run("un-fulfill an order when OrderMatched is removed", func(t *testing.T) {
Expand All @@ -679,8 +679,8 @@ func TestRemovedEvents(t *testing.T) {
cep.ProcessEvents([]*types.Log{longOrderAcceptedEventLog, shortOrderAcceptedEventLog})

// orders exist in memory now
assert.Equal(t, db.OrderMap[longOrderId].Salt, longOrder.Salt)
assert.Equal(t, db.OrderMap[shortOrderId].Salt, shortOrder.Salt)
assert.Equal(t, db.Orders[longOrderId].Salt, longOrder.Salt)
assert.Equal(t, db.Orders[shortOrderId].Salt, shortOrder.Salt)

// fulfill them
orderMatchedEvent := getEventFromABI(orderBookABI, "OrderMatched")
Expand All @@ -689,12 +689,12 @@ func TestRemovedEvents(t *testing.T) {
orderMatchedLog := getEventLog(OrderBookContractAddress, orderMatchedEventTopics, orderMatchedEventData, blockNumber.Uint64()+2)
cep.ProcessEvents([]*types.Log{orderMatchedLog})

assert.Equal(t, db.OrderMap[longOrderId].getOrderStatus().Status, FulFilled)
assert.Equal(t, db.Orders[longOrderId].getOrderStatus().Status, FulFilled)

// now un-fulfill it
orderMatchedLog.Removed = true
cep.ProcessEvents([]*types.Log{orderMatchedLog})
assert.Equal(t, db.OrderMap[longOrderId].getOrderStatus().Status, Placed)
assert.Equal(t, db.Orders[longOrderId].getOrderStatus().Status, Placed)
})

t.Run("revert state of an order when OrderMatchingError is removed", func(t *testing.T) {
Expand All @@ -707,8 +707,8 @@ func TestRemovedEvents(t *testing.T) {
cep.ProcessEvents([]*types.Log{longOrderAcceptedEventLog})

// orders exist in memory now
assert.Equal(t, db.OrderMap[longOrderId].Salt, longOrder.Salt)
assert.Equal(t, db.OrderMap[longOrderId].getOrderStatus().Status, Placed)
assert.Equal(t, db.Orders[longOrderId].Salt, longOrder.Salt)
assert.Equal(t, db.Orders[longOrderId].getOrderStatus().Status, Placed)

// fail matching
orderMatchingError := getEventFromABI(orderBookABI, "OrderMatchingError")
Expand All @@ -717,12 +717,12 @@ func TestRemovedEvents(t *testing.T) {
orderMatchingErrorLog := getEventLog(OrderBookContractAddress, orderMatchingErrorTopics, orderMatchingErrorData, blockNumber.Uint64()+2)
cep.ProcessEvents([]*types.Log{orderMatchingErrorLog})

assert.Equal(t, db.OrderMap[longOrderId].getOrderStatus().Status, Execution_Failed)
assert.Equal(t, db.Orders[longOrderId].getOrderStatus().Status, Execution_Failed)

// now un-fail it
orderMatchingErrorLog.Removed = true
cep.ProcessEvents([]*types.Log{orderMatchingErrorLog})
assert.Equal(t, db.OrderMap[longOrderId].getOrderStatus().Status, Placed)
assert.Equal(t, db.Orders[longOrderId].getOrderStatus().Status, Placed)
})
}

Expand Down
8 changes: 0 additions & 8 deletions plugin/evm/orderbook/matching_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,3 @@ func removeOrdersWithIds(orders []Order, orderIds map[common.Hash]struct{}) []Or
}
return filteredOrders
}

func formatHashSlice(hashes []common.Hash) []string {
var formattedHashes []string
for _, hash := range hashes {
formattedHashes = append(formattedHashes, hash.String())
}
return formattedHashes
}
Loading