-
Notifications
You must be signed in to change notification settings - Fork 0
/
collateralization.go
178 lines (158 loc) · 5.94 KB
/
collateralization.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package memclob
import (
"fmt"
"testing"
clobtypes "github.com/jinxprotocol/v4-chain/protocol/x/clob/types"
satypes "github.com/jinxprotocol/v4-chain/protocol/x/subaccounts/types"
"github.com/stretchr/testify/require"
)
// CollateralizationCheck is a testing utility struct used to represent a collateralization check.
type CollateralizationCheck struct {
CollatCheck map[satypes.SubaccountId][]clobtypes.PendingOpenOrder
Result map[satypes.SubaccountId]satypes.UpdateResult
}
// CreateCollatCheckFunction creates a collateralization check function that can be used when calling the `PlaceOrder`
// function on the memclob. It asserts that the passed in parameters to the collateralization check function are
// correct, and returns the specified collateralization check results.
// Note that this function takes a pointer parameter representing the number of collateralization checks, and
// increments it after each performed collateralization check such that the caller can assert the expected number of
// collateralization checks are performed.
func CreateCollatCheckFunction(
t *testing.T,
collateralCheckCounter *int,
expectedCollatCheckParams map[int]map[satypes.SubaccountId][]clobtypes.PendingOpenOrder,
collatCheckFailures map[int]map[satypes.SubaccountId]satypes.UpdateResult,
) (
collatCheckFn clobtypes.AddOrderToOrderbookCollateralizationCheckFn,
) {
collatCheckFn = func(
subaccountMatchedOrders map[satypes.SubaccountId][]clobtypes.PendingOpenOrder,
) (
success bool,
successPerUpdate map[satypes.SubaccountId]satypes.UpdateResult,
) {
// Before returning, increment the number of collateralization checks.
defer func() {
*collateralCheckCounter++
}()
collatCheckNum := *collateralCheckCounter
// Verify the parameters are as expected.
expectedSubaccountMatchedOrders := expectedCollatCheckParams[collatCheckNum]
expectedNumSubaccounts := len(expectedSubaccountMatchedOrders)
require.Len(
t,
subaccountMatchedOrders,
expectedNumSubaccounts,
fmt.Sprintf(
"Different number of subaccounts. Collateral check %d",
collatCheckNum,
),
)
for subaccount, pendingMatches := range subaccountMatchedOrders {
require.ElementsMatch(
t,
pendingMatches,
expectedSubaccountMatchedOrders[subaccount],
fmt.Sprintf(
`Elements differ. List A is actual, list B is expected.
Collateral check number: %d, subaccount.owner: %s, subaccount.number: %d`,
collatCheckNum,
subaccount.Owner,
subaccount.Number,
),
)
}
// Return the result of the collateralization check.
subaccountCollatCheckResult := make(map[satypes.SubaccountId]satypes.UpdateResult)
success = true
for subaccountId := range subaccountMatchedOrders {
expectedUpdateResult, exists := collatCheckFailures[collatCheckNum][subaccountId]
// If `collatCheckFailures` contains a successful update result, we should throw an error since
// `collatCheckFailures` should not have entries for successful updates.
// Else if no update result was specified for this subaccount, then we can assume success.
if exists && expectedUpdateResult.IsSuccess() {
require.Fail(
t,
fmt.Sprintf(
"UpdateResult for collateralization check %d, subaccount %s should not be marked as successful.",
collatCheckNum,
subaccountId.String(),
),
)
} else if !exists {
expectedUpdateResult = satypes.Success
}
if !expectedUpdateResult.IsSuccess() {
success = false
}
subaccountCollatCheckResult[subaccountId] = expectedUpdateResult
}
return success, subaccountCollatCheckResult
}
return collatCheckFn
}
// AlwaysSuccessfulCollatCheckFn is a collateralization check function that always returns success.
func AlwaysSuccessfulCollatCheckFn(
subaccountMatchedOrders map[satypes.SubaccountId][]clobtypes.PendingOpenOrder,
) (success bool, successPerUpdate map[satypes.SubaccountId]satypes.UpdateResult) {
return true, nil
}
// CreateSimpleCollatCheckFunction creates a collateralization check function that can be used when
// calling the `PlaceOrder` function on the memclob. It asserts that the passed in parameters to
// the collateralization check function are correct, and returns the specified collateralization check results.
// Note that this function takes a pointer parameter representing the number of collateralization checks, and
// increments it after each performed collateralization check such that the caller can assert the expected number of
// collateralization checks are performed.
func CreateSimpleCollatCheckFunction(
t *testing.T,
collateralCheckCounter *int,
expectedCollatCheck map[int]CollateralizationCheck,
) (
collatCheckFn clobtypes.AddOrderToOrderbookCollateralizationCheckFn,
) {
collatCheckFn = func(
subaccountOpenOrders map[satypes.SubaccountId][]clobtypes.PendingOpenOrder,
) (
success bool,
successPerUpdate map[satypes.SubaccountId]satypes.UpdateResult,
) {
// Before returning, increment the number of collateralization checks.
defer func() {
*collateralCheckCounter++
}()
collatCheckNum := *collateralCheckCounter
// Verify the parameters are as expected.
collatCheck := expectedCollatCheck[collatCheckNum]
expectedNumSubaccounts := len(collatCheck.CollatCheck)
require.Len(
t,
subaccountOpenOrders,
expectedNumSubaccounts,
fmt.Sprintf(
"Different number of subaccounts. Collateral check %d",
collatCheckNum,
),
)
success = true
for subaccount, openOrders := range subaccountOpenOrders {
require.ElementsMatch(
t,
openOrders,
collatCheck.CollatCheck[subaccount],
fmt.Sprintf(
`Elements differ. List A is actual, list B is expected.
Collateral check number: %d, subaccount.owner: %s, subaccount.number: %d`,
collatCheckNum,
subaccount.Owner,
subaccount.Number,
),
)
if !collatCheck.Result[subaccount].IsSuccess() {
success = false
}
}
// Return the result of the collateralization check.
return success, collatCheck.Result
}
return collatCheckFn
}