-
Notifications
You must be signed in to change notification settings - Fork 0
/
offchain_updates.go
70 lines (63 loc) · 2.12 KB
/
offchain_updates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package memclob
import (
"testing"
"github.com/jinxprotocol/v4-chain/protocol/indexer/msgsender"
"github.com/jinxprotocol/v4-chain/protocol/indexer/off_chain_updates"
indexershared "github.com/jinxprotocol/v4-chain/protocol/indexer/shared"
"github.com/jinxprotocol/v4-chain/protocol/x/clob/types"
"github.com/stretchr/testify/require"
"gopkg.in/typ.v4/slices"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func RequireCancelOrderOffchainUpdate(
t *testing.T,
ctx sdk.Context,
offchainUpdate *types.OffchainUpdates,
orderId types.OrderId,
) {
messages := offchainUpdate.GetMessages()
require.Len(t, messages, 1)
RequireCancelOrderMessage(t, ctx, &messages[0], orderId)
}
func RequireCancelOrderMessage(t *testing.T, ctx sdk.Context, message *msgsender.Message, orderId types.OrderId) {
expectedMessage, _ := off_chain_updates.CreateOrderRemoveMessageWithReason(
ctx.Logger(),
orderId,
indexershared.OrderRemovalReason_ORDER_REMOVAL_REASON_USER_CANCELED,
off_chain_updates.OrderRemoveV1_ORDER_REMOVAL_STATUS_BEST_EFFORT_CANCELED,
)
require.Equal(t, *message, expectedMessage)
}
// HasMessage checks if OffchainUpdates contains an message for the given OrderId & OffchainUpdateMessageType.
// Useful for when we want to verify an update message was added for a specific scenario.
func HasMessage(
offchainUpdates *types.OffchainUpdates,
orderId types.OrderId,
messageType types.OffchainUpdateMessageType,
) bool {
return slices.ContainsFunc(
offchainUpdates.Messages,
types.OffchainUpdateMessage{
Message: msgsender.Message{},
Type: messageType,
OrderId: orderId,
},
func(a, b types.OffchainUpdateMessage) bool {
return a.OrderId == b.OrderId && a.Type == b.Type
},
)
}
// MessageCountOfType counts the number of messages in OffchainUpdates of a given type (Place/Remove/Update).
// Useful for when we want to verify we've added the correct number of messages.
func MessageCountOfType(
offchainUpdates *types.OffchainUpdates,
messageType types.OffchainUpdateMessageType,
) int {
var count int = 0
for _, msg := range offchainUpdates.Messages {
if msg.Type == messageType {
count += 1
}
}
return count
}