@@ -2,6 +2,7 @@ package orderbookgrpcclientdomain
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
6
7
cosmwasmdomain "github.com/osmosis-labs/sqs/domain/cosmwasm"
7
8
orderbookdomain "github.com/osmosis-labs/sqs/domain/orderbook"
@@ -21,8 +22,22 @@ type OrderBookClient interface {
21
22
// GetTickUnrealizedCancels fetches unrealized cancels by tick from the orderbook contract.
22
23
GetTickUnrealizedCancels (ctx context.Context , contractAddress string , tickIDs []int64 ) ([]UnrealizedTickCancels , error )
23
24
25
+ // FetchTickUnrealizedCancels fetches the unrealized cancels for a given tick ID and contract address.
26
+ // It returns the unrealized cancels and an error if any.
27
+ // Errors if:
28
+ // - failed to fetch unrealized cancels
29
+ // - mismatch in number of unrealized cancels fetched
30
+ FetchTickUnrealizedCancels (ctx context.Context , chunkSize int , contractAddress string , tickIDs []int64 ) ([]UnrealizedTickCancels , error )
31
+
24
32
// QueryTicks fetches ticks by tickIDs from the orderbook contract.
25
33
QueryTicks (ctx context.Context , contractAddress string , ticks []int64 ) ([]orderbookdomain.Tick , error )
34
+
35
+ // FetchTicksForOrderbook fetches the ticks in chunks of maxQueryTicks at the time for a given tick ID and contract address.
36
+ // It returns the ticks and an error if any.
37
+ // Errors if:
38
+ // - failed to fetch ticks
39
+ // - mismatch in number of ticks fetched
40
+ FetchTicks (ctx context.Context , chunkSize int , contractAddress string , tickIDs []int64 ) ([]orderbookdomain.Tick , error )
26
41
}
27
42
28
43
// orderbookClientImpl is an implementation of OrderbookCWAPIClient.
@@ -39,7 +54,7 @@ func New(wasmClient wasmtypes.QueryClient) *orderbookClientImpl {
39
54
}
40
55
}
41
56
42
- // GetOrdersByTick implements OrderbookCWAPIClient .
57
+ // GetOrdersByTick implements OrderBookClient .
43
58
func (o * orderbookClientImpl ) GetOrdersByTick (ctx context.Context , contractAddress string , tick int64 ) ([]orderbookplugindomain.Order , error ) {
44
59
ordersByTick := ordersByTick {Tick : tick }
45
60
@@ -51,7 +66,7 @@ func (o *orderbookClientImpl) GetOrdersByTick(ctx context.Context, contractAddre
51
66
return orders .Orders , nil
52
67
}
53
68
54
- // GetActiveOrders implements OrderbookCWAPIClient .
69
+ // GetActiveOrders implements OrderBookClient .
55
70
func (o * orderbookClientImpl ) GetActiveOrders (ctx context.Context , contractAddress string , ownerAddress string ) (orderbookdomain.Orders , uint64 , error ) {
56
71
var orders activeOrdersResponse
57
72
if err := cosmwasmdomain .QueryCosmwasmContract (ctx , o .wasmClient , contractAddress , activeOrdersRequest {OrdersByOwner : ordersByOwner {Owner : ownerAddress }}, & orders ); err != nil {
@@ -61,7 +76,7 @@ func (o *orderbookClientImpl) GetActiveOrders(ctx context.Context, contractAddre
61
76
return orders .Orders , orders .Count , nil
62
77
}
63
78
64
- // GetTickUnrealizedCancels implements OrderbookCWAPIClient .
79
+ // GetTickUnrealizedCancels implements OrderBookClient .
65
80
func (o * orderbookClientImpl ) GetTickUnrealizedCancels (ctx context.Context , contractAddress string , tickIDs []int64 ) ([]UnrealizedTickCancels , error ) {
66
81
var unrealizedCancels unrealizedCancelsResponse
67
82
if err := cosmwasmdomain .QueryCosmwasmContract (ctx , o .wasmClient , contractAddress , unrealizedCancelsByTickIdRequest {UnrealizedCancels : unrealizedCancelsRequestPayload {TickIds : tickIDs }}, & unrealizedCancels ); err != nil {
@@ -70,6 +85,33 @@ func (o *orderbookClientImpl) GetTickUnrealizedCancels(ctx context.Context, cont
70
85
return unrealizedCancels .Ticks , nil
71
86
}
72
87
88
+ // FetchTickUnrealizedCancels implements OrderBookClient.
89
+ func (o * orderbookClientImpl ) FetchTickUnrealizedCancels (ctx context.Context , chunkSize int , contractAddress string , tickIDs []int64 ) ([]UnrealizedTickCancels , error ) {
90
+ allUnrealizedCancels := make ([]UnrealizedTickCancels , 0 , len (tickIDs ))
91
+
92
+ for i := 0 ; i < len (tickIDs ); i += chunkSize {
93
+ end := i + chunkSize
94
+ if end > len (tickIDs ) {
95
+ end = len (tickIDs )
96
+ }
97
+
98
+ currentTickIDs := tickIDs [i :end ]
99
+
100
+ unrealizedCancels , err := o .GetTickUnrealizedCancels (ctx , contractAddress , currentTickIDs )
101
+ if err != nil {
102
+ return nil , fmt .Errorf ("failed to fetch unrealized cancels for ticks %v: %w" , currentTickIDs , err )
103
+ }
104
+
105
+ allUnrealizedCancels = append (allUnrealizedCancels , unrealizedCancels ... )
106
+ }
107
+
108
+ if len (allUnrealizedCancels ) != len (tickIDs ) {
109
+ return nil , fmt .Errorf ("mismatch in number of unrealized cancels fetched: expected %d, got %d" , len (tickIDs ), len (allUnrealizedCancels ))
110
+ }
111
+
112
+ return allUnrealizedCancels , nil
113
+ }
114
+
73
115
// QueryTicks implements OrderBookClient.
74
116
func (o * orderbookClientImpl ) QueryTicks (ctx context.Context , contractAddress string , ticks []int64 ) ([]orderbookdomain.Tick , error ) {
75
117
var orderbookTicks queryTicksResponse
@@ -78,3 +120,30 @@ func (o *orderbookClientImpl) QueryTicks(ctx context.Context, contractAddress st
78
120
}
79
121
return orderbookTicks .Ticks , nil
80
122
}
123
+
124
+ // FetchTicks implements OrderBookClient.
125
+ func (o * orderbookClientImpl ) FetchTicks (ctx context.Context , chunkSize int , contractAddress string , tickIDs []int64 ) ([]orderbookdomain.Tick , error ) {
126
+ finalTickStates := make ([]orderbookdomain.Tick , 0 , len (tickIDs ))
127
+
128
+ for i := 0 ; i < len (tickIDs ); i += chunkSize {
129
+ end := i + chunkSize
130
+ if end > len (tickIDs ) {
131
+ end = len (tickIDs )
132
+ }
133
+
134
+ currentTickIDs := tickIDs [i :end ]
135
+
136
+ tickStates , err := o .QueryTicks (ctx , contractAddress , currentTickIDs )
137
+ if err != nil {
138
+ return nil , fmt .Errorf ("failed to fetch ticks for pool %s: %w" , contractAddress , err )
139
+ }
140
+
141
+ finalTickStates = append (finalTickStates , tickStates ... )
142
+ }
143
+
144
+ if len (finalTickStates ) != len (tickIDs ) {
145
+ return nil , fmt .Errorf ("mismatch in number of ticks fetched: expected %d, got %d" , len (tickIDs ), len (finalTickStates ))
146
+ }
147
+
148
+ return finalTickStates , nil
149
+ }
0 commit comments