Skip to content

Commit

Permalink
Fix tests and add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
lumos42 committed Sep 15, 2023
1 parent c353ed6 commit 3fa6abd
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 40 deletions.
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 @@ -65,10 +65,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 @@ -163,7 +163,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 @@ -207,7 +207,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 @@ -218,7 +218,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 @@ -259,15 +259,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 @@ -642,20 +642,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 @@ -664,12 +664,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 @@ -678,8 +678,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 @@ -688,12 +688,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 @@ -706,8 +706,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 @@ -716,12 +716,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
132 changes: 118 additions & 14 deletions plugin/evm/orderbook/memory_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,117 @@ func TestgetDatabase(t *testing.T) {
assert.NotNil(t, inMemoryDatabase)
}

func TestAddSequence(t *testing.T) {
baseAssetQuantity := big.NewInt(10)
db := getDatabase()

t.Run("Long orders", func(t *testing.T) {
order1 := createLimitOrder(LONG, userAddress, baseAssetQuantity, big.NewInt(20), status, big.NewInt(2), big.NewInt(1))
db.Add(&order1)

assert.Equal(t, 1, len(db.Orders))
assert.Equal(t, 1, len(db.LongOrders[market]))
assert.Equal(t, db.LongOrders[market][0].Id, order1.Id)

order2 := createLimitOrder(LONG, userAddress, baseAssetQuantity, big.NewInt(21), status, big.NewInt(2), big.NewInt(2))
db.Add(&order2)

assert.Equal(t, 2, len(db.Orders))
assert.Equal(t, 2, len(db.LongOrders[market]))
assert.Equal(t, db.LongOrders[market][0].Id, order2.Id)
assert.Equal(t, db.LongOrders[market][1].Id, order1.Id)

order3 := createLimitOrder(LONG, userAddress, baseAssetQuantity, big.NewInt(19), status, big.NewInt(2), big.NewInt(3))
db.Add(&order3)

assert.Equal(t, 3, len(db.Orders))
assert.Equal(t, 3, len(db.LongOrders[market]))
assert.Equal(t, db.LongOrders[market][0].Id, order2.Id)
assert.Equal(t, db.LongOrders[market][1].Id, order1.Id)
assert.Equal(t, db.LongOrders[market][2].Id, order3.Id)

// block number
order4 := createLimitOrder(LONG, userAddress, baseAssetQuantity, big.NewInt(20), status, big.NewInt(3), big.NewInt(4))
db.Add(&order4)

assert.Equal(t, 4, len(db.Orders))
assert.Equal(t, 4, len(db.LongOrders[market]))
assert.Equal(t, db.LongOrders[market][0].Id, order2.Id)
assert.Equal(t, db.LongOrders[market][1].Id, order1.Id)
assert.Equal(t, db.LongOrders[market][2].Id, order4.Id)
assert.Equal(t, db.LongOrders[market][3].Id, order3.Id)

// ioc order
order5 := createIOCOrder(LONG, userAddress, baseAssetQuantity, big.NewInt(20), status, big.NewInt(2), big.NewInt(5), big.NewInt(2))
db.Add(&order5)

assert.Equal(t, 5, len(db.Orders))
assert.Equal(t, 5, len(db.LongOrders[market]))
assert.Equal(t, db.LongOrders[market][0].Id, order2.Id)
assert.Equal(t, db.LongOrders[market][1].Id, order5.Id)
assert.Equal(t, db.LongOrders[market][2].Id, order1.Id)
assert.Equal(t, db.LongOrders[market][3].Id, order4.Id)
assert.Equal(t, db.LongOrders[market][4].Id, order3.Id)
})

t.Run("Short orders", func(t *testing.T) {
baseAssetQuantity = big.NewInt(-10)
order1 := createLimitOrder(SHORT, userAddress, baseAssetQuantity, big.NewInt(20), status, big.NewInt(2), big.NewInt(6))
db.Add(&order1)

assert.Equal(t, 6, len(db.Orders))
assert.Equal(t, 1, len(db.ShortOrders[market]))
assert.Equal(t, db.ShortOrders[market][0].Id, order1.Id)

order2 := createLimitOrder(SHORT, userAddress, baseAssetQuantity, big.NewInt(19), status, big.NewInt(2), big.NewInt(7))
db.Add(&order2)

assert.Equal(t, 7, len(db.Orders))
assert.Equal(t, 2, len(db.ShortOrders[market]))
assert.Equal(t, db.ShortOrders[market][0].Id, order2.Id)
assert.Equal(t, db.ShortOrders[market][1].Id, order1.Id)

order3 := createLimitOrder(SHORT, userAddress, baseAssetQuantity, big.NewInt(21), status, big.NewInt(2), big.NewInt(8))
db.Add(&order3)

assert.Equal(t, 8, len(db.Orders))
assert.Equal(t, 3, len(db.ShortOrders[market]))
assert.Equal(t, db.ShortOrders[market][0].Id, order2.Id)
assert.Equal(t, db.ShortOrders[market][1].Id, order1.Id)
assert.Equal(t, db.ShortOrders[market][2].Id, order3.Id)

// block number
order4 := createLimitOrder(SHORT, userAddress, baseAssetQuantity, big.NewInt(20), status, big.NewInt(3), big.NewInt(9))
db.Add(&order4)

assert.Equal(t, 9, len(db.Orders))
assert.Equal(t, 4, len(db.ShortOrders[market]))
assert.Equal(t, db.ShortOrders[market][0].Id, order2.Id)
assert.Equal(t, db.ShortOrders[market][1].Id, order1.Id)
assert.Equal(t, db.ShortOrders[market][2].Id, order4.Id)
assert.Equal(t, db.ShortOrders[market][3].Id, order3.Id)

// ioc order
order5 := createIOCOrder(SHORT, userAddress, baseAssetQuantity, big.NewInt(20), status, big.NewInt(2), big.NewInt(10), big.NewInt(2))
db.Add(&order5)

assert.Equal(t, 10, len(db.Orders))
assert.Equal(t, 5, len(db.ShortOrders[market]))
assert.Equal(t, db.ShortOrders[market][0].Id, order2.Id)
assert.Equal(t, db.ShortOrders[market][1].Id, order5.Id)
assert.Equal(t, db.ShortOrders[market][2].Id, order1.Id)
assert.Equal(t, db.ShortOrders[market][3].Id, order4.Id)
assert.Equal(t, db.ShortOrders[market][4].Id, order3.Id)
})
}

func TestAdd(t *testing.T) {
baseAssetQuantity := big.NewInt(-10)
inMemoryDatabase := getDatabase()
salt := big.NewInt(time.Now().Unix())
limitOrder := createLimitOrder(positionType, userAddress, baseAssetQuantity, price, status, blockNumber, salt)
inMemoryDatabase.Add(&limitOrder)
returnedOrder := inMemoryDatabase.OrderMap[limitOrder.Id]
returnedOrder := inMemoryDatabase.Orders[limitOrder.Id]
assert.Equal(t, limitOrder.PositionType, returnedOrder.PositionType)
assert.Equal(t, limitOrder.Trader, returnedOrder.Trader)
assert.Equal(t, limitOrder.BaseAssetQuantity, returnedOrder.BaseAssetQuantity)
Expand Down Expand Up @@ -305,7 +409,7 @@ func TestUpdateFulfilledBaseAssetQuantityLimitOrder(t *testing.T) {
filledQuantity := big.NewInt(2)

inMemoryDatabase.UpdateFilledBaseAssetQuantity(filledQuantity, limitOrder.Id, 69)
updatedLimitOrder := inMemoryDatabase.OrderMap[limitOrder.Id]
updatedLimitOrder := inMemoryDatabase.Orders[limitOrder.Id]

assert.Equal(t, updatedLimitOrder.FilledBaseAssetQuantity, big.NewInt(0).Neg(filledQuantity))
assert.Equal(t, updatedLimitOrder.FilledBaseAssetQuantity, filledQuantity.Mul(filledQuantity, big.NewInt(-1)))
Expand All @@ -320,7 +424,7 @@ func TestUpdateFulfilledBaseAssetQuantityLimitOrder(t *testing.T) {

filledQuantity := big.NewInt(2)
inMemoryDatabase.UpdateFilledBaseAssetQuantity(filledQuantity, limitOrder.Id, 69)
updatedLimitOrder := inMemoryDatabase.OrderMap[limitOrder.Id]
updatedLimitOrder := inMemoryDatabase.Orders[limitOrder.Id]

assert.Equal(t, updatedLimitOrder.FilledBaseAssetQuantity, filledQuantity)
})
Expand Down Expand Up @@ -437,15 +541,15 @@ func TestAccept(t *testing.T) {

err := inMemoryDatabase.SetOrderStatus(orderId1, FulFilled, "", 51)
assert.Nil(t, err)
assert.Equal(t, inMemoryDatabase.OrderMap[orderId1].getOrderStatus().Status, FulFilled)
assert.Equal(t, inMemoryDatabase.Orders[orderId1].getOrderStatus().Status, FulFilled)

inMemoryDatabase.Accept(51, 51)

// fulfilled order is deleted
_, ok := inMemoryDatabase.OrderMap[orderId1]
_, ok := inMemoryDatabase.Orders[orderId1]
assert.False(t, ok)
// unfulfilled order still exists
_, ok = inMemoryDatabase.OrderMap[orderId2]
_, ok = inMemoryDatabase.Orders[orderId2]
assert.True(t, ok)
})

Expand All @@ -454,11 +558,11 @@ func TestAccept(t *testing.T) {
orderId := addLimitOrder(inMemoryDatabase)
err := inMemoryDatabase.SetOrderStatus(orderId, FulFilled, "", 51)
assert.Nil(t, err)
assert.Equal(t, inMemoryDatabase.OrderMap[orderId].getOrderStatus().Status, FulFilled)
assert.Equal(t, inMemoryDatabase.Orders[orderId].getOrderStatus().Status, FulFilled)

inMemoryDatabase.Accept(52, 52)

_, ok := inMemoryDatabase.OrderMap[orderId]
_, ok := inMemoryDatabase.Orders[orderId]
assert.False(t, ok)
})

Expand All @@ -467,11 +571,11 @@ func TestAccept(t *testing.T) {
orderId := addLimitOrder(inMemoryDatabase)
err := inMemoryDatabase.SetOrderStatus(orderId, FulFilled, "", 51)
assert.Nil(t, err)
assert.Equal(t, inMemoryDatabase.OrderMap[orderId].getOrderStatus().Status, FulFilled)
assert.Equal(t, inMemoryDatabase.Orders[orderId].getOrderStatus().Status, FulFilled)

inMemoryDatabase.Accept(50, 50)

_, ok := inMemoryDatabase.OrderMap[orderId]
_, ok := inMemoryDatabase.Orders[orderId]
assert.True(t, ok)
})

Expand All @@ -480,7 +584,7 @@ func TestAccept(t *testing.T) {
orderId := addLimitOrder(inMemoryDatabase)
inMemoryDatabase.Accept(50, 50)

_, ok := inMemoryDatabase.OrderMap[orderId]
_, ok := inMemoryDatabase.Orders[orderId]
assert.True(t, ok)
})
}
Expand All @@ -501,7 +605,7 @@ func TestRevertLastStatus(t *testing.T) {
err := inMemoryDatabase.RevertLastStatus(orderId)
assert.Nil(t, err)

assert.Equal(t, len(inMemoryDatabase.OrderMap[orderId].LifecycleList), 0)
assert.Equal(t, len(inMemoryDatabase.Orders[orderId].LifecycleList), 0)
})

t.Run("revert status for fulfilled order", func(t *testing.T) {
Expand All @@ -513,8 +617,8 @@ func TestRevertLastStatus(t *testing.T) {
err = inMemoryDatabase.RevertLastStatus(orderId)
assert.Nil(t, err)

assert.Equal(t, len(inMemoryDatabase.OrderMap[orderId].LifecycleList), 1)
assert.Equal(t, inMemoryDatabase.OrderMap[orderId].LifecycleList[0].BlockNumber, uint64(2))
assert.Equal(t, len(inMemoryDatabase.Orders[orderId].LifecycleList), 1)
assert.Equal(t, inMemoryDatabase.Orders[orderId].LifecycleList[0].BlockNumber, uint64(2))
})

t.Run("revert status for accepted + fulfilled order - expect error", func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions plugin/evm/orderbook/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func (db *MockLimitOrderDatabase) GetAllOrders() []Order {
return args.Get(0).([]Order)
}

func (db *MockLimitOrderDatabase) GetMarketOrders(market Market) []Order {
args := db.Called()
return args.Get(0).([]Order)
}

func (db *MockLimitOrderDatabase) Add(order *Order) {
}

Expand Down
Loading

0 comments on commit 3fa6abd

Please sign in to comment.