1
1
import { Empty } from 'google-protobuf/google/protobuf/empty_pb' ;
2
2
import { StringValue , UInt32Value } from 'google-protobuf/google/protobuf/wrappers_pb' ;
3
+
4
+ import * as grpcWeb from 'grpc-web' ;
5
+
6
+ import { io } from '../model/types' ;
3
7
import { Order } from '../proto_gen/data_order_pb' ;
4
- import { io } from "../model/types" ;
5
8
import {
6
9
OffchainWithdrawalRequest ,
7
10
OrderCancellationRequest
8
- } from '../proto_gen/data_requests_pb' ;
9
- import { DexServiceClient } from '..' ;
10
- import { Metadata } from "grpc" ;
11
+ } from '../proto_gen/data_requests_pb.d' ;
12
+ import {
13
+ CursorPaging
14
+ } from '../proto_gen/data_types_pb' ;
11
15
import {
12
16
Account ,
13
17
CancelOrderRes ,
@@ -29,19 +33,21 @@ import {
29
33
OrderBook ,
30
34
SimpleOrderCancellationReq ,
31
35
SubmitOrderRes
32
- } from 'src/proto_gen/service_dex_pb' ;
36
+ } from '../proto_gen/service_dex_pb' ;
37
+ import { DexServiceClient } from '../proto_gen/Service_dexServiceClientPb' ;
33
38
34
39
/**
35
40
* gRPC GrpcClientService Service
36
41
*/
37
42
class GrpcClientService {
38
- private readonly client : DexServiceClient = new DexServiceClient ( '18.179.197.168:5000' , null , null ) ; // TODO: config
43
+ // TODO: use localhost for debug
44
+ private readonly client = new DexServiceClient ( 'http://0.0.0.0:5000' , null , null ) ; // TODO: config
39
45
46
+ // Verfied
40
47
public async getDexConfigurations ( ) : Promise < DexConfigurations > {
41
- const metadata = new Metadata ( ) ;
42
48
const empty : Empty = new Empty ( ) ;
43
49
return new Promise < DexConfigurations > ( ( resolve : Function , reject : Function ) : void => {
44
- this . client . getDexConfigurations ( empty , metadata , ( err : Error | null , res : DexConfigurations ) => {
50
+ this . client . getDexConfigurations ( empty , null , ( err : grpcWeb . Error , res : DexConfigurations ) => {
45
51
if ( err ) {
46
52
return reject ( err ) ;
47
53
}
@@ -51,12 +57,11 @@ class GrpcClientService {
51
57
}
52
58
53
59
public async getAccount ( param : string ) : Promise < Account > {
54
- const metadata = new Metadata ( ) ;
55
60
const address : StringValue = new StringValue ( ) ;
56
61
address . setValue ( param ) ;
57
62
58
63
return new Promise < Account > ( ( resolve : Function , reject : Function ) : void => {
59
- this . client . getAccount ( address , metadata , ( err : Error | null , res : Account ) => {
64
+ this . client . getAccount ( address , null , ( err : grpcWeb . Error , res : Account ) => {
60
65
if ( err ) {
61
66
return reject ( err ) ;
62
67
}
@@ -66,12 +71,11 @@ class GrpcClientService {
66
71
}
67
72
68
73
public async getNonce ( param : number ) : Promise < UInt32Value > {
69
- const metadata = new Metadata ( ) ;
70
74
const accountId : UInt32Value = new UInt32Value ( ) ;
71
75
accountId . setValue ( param ) ;
72
76
73
77
return new Promise < UInt32Value > ( ( resolve : Function , reject : Function ) : void => {
74
- this . client . getNonce ( accountId , metadata , ( err : Error | null , res : UInt32Value ) => {
78
+ this . client . getNonce ( accountId , null , ( err : grpcWeb . Error , res : UInt32Value ) => {
75
79
if ( err ) {
76
80
return reject ( err ) ;
77
81
}
@@ -81,9 +85,15 @@ class GrpcClientService {
81
85
}
82
86
83
87
public async getTokens ( param : GetTokensReq ) : Promise < GetTokensRes > {
84
- const metadata = new Metadata ( ) ;
88
+ const req : GetTokensReq = new GetTokensReq ( ) ;
89
+ req . setRequireMetadata ( true ) ;
90
+ req . setRequireInfo ( true ) ;
91
+ req . setRequirePrice ( true ) ;
92
+ req . setQuoteCurrencyForPrice ( 'hello' ) ;
93
+ req . setTokensList ( [ 'LRC' , 'ETH' ] ) ;
94
+
85
95
return new Promise < GetTokensRes > ( ( resolve : Function , reject : Function ) : void => {
86
- this . client . getTokens ( param , metadata , ( err : Error | null , res : GetTokensRes ) => {
96
+ this . client . getTokens ( req , null , ( err : grpcWeb . Error , res : GetTokensRes ) => {
87
97
if ( err ) {
88
98
return reject ( err ) ;
89
99
}
@@ -92,10 +102,13 @@ class GrpcClientService {
92
102
} ) ;
93
103
}
94
104
95
- public async getNextOrderId ( param : GetNextOrderIdReq ) : Promise < UInt32Value > {
96
- const metadata = new Metadata ( ) ;
105
+ public async getNextOrderId ( accountId : number , tokenSellId : number ) : Promise < UInt32Value > {
106
+ const req : GetNextOrderIdReq = new GetNextOrderIdReq ( ) ;
107
+ req . setAccountId ( accountId ) ;
108
+ req . setTokenSellId ( tokenSellId ) ;
109
+
97
110
return new Promise < UInt32Value > ( ( resolve : Function , reject : Function ) : void => {
98
- this . client . getNextOrderId ( param , metadata , ( err : Error | null , res : UInt32Value ) => {
111
+ this . client . getNextOrderId ( req , null , ( err : grpcWeb . Error , res : UInt32Value ) => {
99
112
if ( err ) {
100
113
return reject ( err ) ;
101
114
}
@@ -105,9 +118,14 @@ class GrpcClientService {
105
118
}
106
119
107
120
public async getMarkets ( param : GetMarketsReq ) : Promise < GetMarketsRes > {
108
- const metadata = new Metadata ( ) ;
121
+ const req : GetMarketsReq = new GetMarketsReq ( ) ;
122
+ req . setRequireMetadata ( true ) ;
123
+ req . setRequireTicker ( true ) ;
124
+ req . setQueryLoopringTicker ( true ) ;
125
+ req . setMarketIdList ( [ 100 ] ) ;
126
+
109
127
return new Promise < GetMarketsRes > ( ( resolve : Function , reject : Function ) : void => {
110
- this . client . getMarkets ( param , metadata , ( err : Error | null , res : GetMarketsRes ) => {
128
+ this . client . getMarkets ( req , null , ( err : grpcWeb . Error , res : GetMarketsRes ) => {
111
129
if ( err ) {
112
130
return reject ( err ) ;
113
131
}
@@ -117,9 +135,12 @@ class GrpcClientService {
117
135
}
118
136
119
137
public async getMarketFills ( param : GetMarketFillsReq ) : Promise < GetFillsRes > {
120
- const metadata = new Metadata ( ) ;
138
+ const req : GetMarketFillsReq = new GetMarketFillsReq ( ) ;
139
+ req . setMarketId ( 100 ) ;
140
+ req . setNum ( 100 ) ;
141
+
121
142
return new Promise < GetFillsRes > ( ( resolve : Function , reject : Function ) : void => {
122
- this . client . getMarketFills ( param , metadata , ( err : Error | null , res : GetFillsRes ) => {
143
+ this . client . getMarketFills ( req , null , ( err : grpcWeb . Error , res : GetFillsRes ) => {
123
144
if ( err ) {
124
145
return reject ( err ) ;
125
146
}
@@ -128,10 +149,18 @@ class GrpcClientService {
128
149
} ) ;
129
150
}
130
151
152
+ // FIXME: time out
131
153
public async getUserFills ( param : GetUserFillsReq ) : Promise < GetFillsRes > {
132
- const metadata = new Metadata ( ) ;
154
+ const req = new GetUserFillsReq ( ) ;
155
+ req . setAccountId ( 100 ) ;
156
+ req . setOrderUuid ( 100 ) ;
157
+ const paging = new CursorPaging ( ) ;
158
+ paging . setNum ( 100 ) ;
159
+ paging . setCursor ( 0 ) ;
160
+ req . setPaging ( paging ) ;
161
+
133
162
return new Promise < GetFillsRes > ( ( resolve : Function , reject : Function ) : void => {
134
- this . client . getUserFills ( param , metadata , ( err : Error | null , res : GetFillsRes ) => {
163
+ this . client . getUserFills ( req , null , ( err : grpcWeb . Error , res : GetFillsRes ) => {
135
164
if ( err ) {
136
165
return reject ( err ) ;
137
166
}
@@ -140,10 +169,19 @@ class GrpcClientService {
140
169
} ) ;
141
170
}
142
171
172
+ // TODO: getUserTransactions and getUserTransfers are different
143
173
public async getUserTransactions ( param : GetUserTransactionsReq ) : Promise < GetUserTransactionsRes > {
144
- const metadata = new Metadata ( ) ;
174
+ const req = new GetUserTransactionsReq ( ) ;
175
+ req . setAccountId ( 100 ) ;
176
+ req . setTokenId ( 100 ) ;
177
+ req . setTransactionType ( 'TX_TYPE_DEPOSIT' ) ;
178
+ const paging = new CursorPaging ( ) ;
179
+ paging . setNum ( 100 ) ;
180
+ paging . setCursor ( 0 ) ;
181
+ req . setPaging ( paging ) ;
182
+
145
183
return new Promise < GetUserTransactionsRes > ( ( resolve : Function , reject : Function ) : void => {
146
- this . client . getUserTransfers ( param , metadata , ( err : Error | null , res : GetUserTransactionsRes ) => {
184
+ this . client . getUserTransfers ( req , null , ( err : grpcWeb . Error , res : GetUserTransactionsRes ) => {
147
185
if ( err ) {
148
186
return reject ( err ) ;
149
187
}
@@ -152,10 +190,15 @@ class GrpcClientService {
152
190
} ) ;
153
191
}
154
192
193
+ // FIXME: time out
155
194
public async getOrderBook ( param : GetOrderBookReq ) : Promise < OrderBook > {
156
- const metadata = new Metadata ( ) ;
195
+ const req = new GetOrderBookReq ( ) ;
196
+ req . setMarketId ( 100 ) ;
197
+ req . setAggregationLevel ( 6 ) ;
198
+ req . setFiatSymbol ( 'USD' ) ;
199
+
157
200
return new Promise < OrderBook > ( ( resolve : Function , reject : Function ) : void => {
158
- this . client . getOrderBook ( param , metadata , ( err : Error | null , res : OrderBook ) => {
201
+ this . client . getOrderBook ( req , null , ( err : grpcWeb . Error , res : OrderBook ) => {
159
202
if ( err ) {
160
203
return reject ( err ) ;
161
204
}
@@ -164,10 +207,20 @@ class GrpcClientService {
164
207
} ) ;
165
208
}
166
209
210
+ // FIXME: time out
167
211
public async getUserOrders ( param : GetUserOrdersReq ) : Promise < GetUserOrdersRes > {
168
- const metadata = new Metadata ( ) ;
212
+ const req = new GetUserOrdersReq ( ) ;
213
+ req . setAccountId ( 100 ) ;
214
+ req . setBaseTokenId ( 'b3b79b8d-0c7e-4142-9694-eb7df51969de' ) ;
215
+ req . setQuoteTokenId ( '738156aa-9f51-4e1f-96dd-c02712ea7e7a' ) ;
216
+ const paging = new CursorPaging ( ) ;
217
+ paging . setNum ( 100 ) ;
218
+ paging . setCursor ( 0 ) ;
219
+ req . setPaging ( paging ) ;
220
+ req . setStatusesList ( [ 'Hello' , 'World' ] ) ;
221
+
169
222
return new Promise < GetUserOrdersRes > ( ( resolve : Function , reject : Function ) : void => {
170
- this . client . getUserOrders ( param , metadata , ( err : Error | null , res : GetUserOrdersRes ) => {
223
+ this . client . getUserOrders ( req , null , ( err : grpcWeb . Error , res : GetUserOrdersRes ) => {
171
224
if ( err ) {
172
225
return reject ( err ) ;
173
226
}
@@ -176,10 +229,10 @@ class GrpcClientService {
176
229
} ) ;
177
230
}
178
231
232
+ // TODO: not test in web
179
233
public async submitOrder ( param : Order ) : Promise < SubmitOrderRes > {
180
- const metadata = new Metadata ( ) ;
181
234
return new Promise < SubmitOrderRes > ( ( resolve : Function , reject : Function ) : void => {
182
- this . client . submitOrder ( param , metadata , ( err : Error | null , res : SubmitOrderRes ) => {
235
+ this . client . submitOrder ( param , null , ( err : grpcWeb . Error , res : SubmitOrderRes ) => {
183
236
if ( err ) {
184
237
return reject ( err ) ;
185
238
}
@@ -188,11 +241,11 @@ class GrpcClientService {
188
241
} ) ;
189
242
}
190
243
244
+ // TODO: not test in web
191
245
// Cancel orders by marking them obsoleted in database, not to be included in blocks.
192
246
public async cancelOrder ( param : SimpleOrderCancellationReq ) : Promise < CancelOrderRes > {
193
- const metadata = new Metadata ( ) ;
194
247
return new Promise < CancelOrderRes > ( ( resolve : Function , reject : Function ) : void => {
195
- this . client . cancelOrder ( param , metadata , ( err : Error | null , res : CancelOrderRes ) => {
248
+ this . client . cancelOrder ( param , null , ( err : grpcWeb . Error , res : CancelOrderRes ) => {
196
249
if ( err ) {
197
250
return reject ( err ) ;
198
251
}
@@ -201,11 +254,11 @@ class GrpcClientService {
201
254
} ) ;
202
255
}
203
256
257
+ // TODO: not test in web
204
258
// Submit an offchain order cancellation request, will make into blocks.
205
259
public async submitOrderCancellation ( param : OrderCancellationRequest ) : Promise < CancelOrderRes > {
206
- const metadata = new Metadata ( ) ;
207
260
return new Promise < CancelOrderRes > ( ( resolve : Function , reject : Function ) : void => {
208
- this . client . submitOrderCancellation ( param , metadata , ( err : Error | null , res : CancelOrderRes ) => {
261
+ this . client . submitOrderCancellation ( param , null , ( err : grpcWeb . Error , res : CancelOrderRes ) => {
209
262
if ( err ) {
210
263
return reject ( err ) ;
211
264
}
@@ -214,11 +267,11 @@ class GrpcClientService {
214
267
} ) ;
215
268
}
216
269
270
+ // TODO: not test in web
217
271
// Submit an offchain withdrawal request, will make into blocks.
218
272
public async submitOffchainWithdrawal ( param : OffchainWithdrawalRequest ) : Promise < OffchainWithdrawalalRes > {
219
- const metadata = new Metadata ( ) ;
220
273
return new Promise < OffchainWithdrawalalRes > ( ( resolve : Function , reject : Function ) : void => {
221
- this . client . submitOffchainWithdrawal ( param , metadata , ( err : Error | null , res : OffchainWithdrawalalRes ) => {
274
+ this . client . submitOffchainWithdrawal ( param , null , ( err : grpcWeb . Error , res : OffchainWithdrawalalRes ) => {
222
275
if ( err ) {
223
276
return reject ( err ) ;
224
277
}
@@ -227,12 +280,6 @@ class GrpcClientService {
227
280
} ) ;
228
281
}
229
282
230
- public socketExample ( ) {
231
- const socket = io . connect ( "localhost" ) ; // TODO: config server ip
232
- socket . on ( "news" , ( data : any ) => alert ( data ) ) ;
233
- socket . emit ( "news" , "hello" ) ;
234
- }
235
-
236
283
}
237
284
238
285
export const grpcClientService : GrpcClientService = new GrpcClientService ( ) ;
0 commit comments