Skip to content

Commit e489257

Browse files
authored
[Lightcone_V2.js] Update GrpcClientService which can be used in web apps now (#255)
* Update GrpcClientService which can be used in web apps now * Run tslint on lightcone_v2.js * Update lint command in lightcone_v2.js * Fix import error in lightcone_v2.js * Update grpcClientService.ts * Update exchange.ts * Remove proto_gen
1 parent 68218f2 commit e489257

File tree

9 files changed

+236
-166
lines changed

9 files changed

+236
-166
lines changed

packages/lightcone_v2.js/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"scripts": {
99
"build:proto": "rimraf src/proto_gen && mkdir src/proto_gen && node src/lib/grpc/proto",
1010
"build": "npm run build:proto && rimraf dist && tsc",
11-
"lint": "tslint --project tsconfig.json ./src/**/*.ts",
11+
"lint": "tslint --project tsconfig.json ./src/**/*.ts -e \"**/proto_gen/**\" --fix",
1212
"client": "node dist/grpc/grpcClient.js",
1313
"test": "mocha -r ts-node/register --project tsconfig.json test/*.ts"
1414
},
@@ -34,8 +34,9 @@
3434
"ethereumjs-util": "5.2.0",
3535
"ethereumjs-wallet": "0.6.0",
3636
"google-protobuf": "^3.9.0-rc.1",
37-
"grpc": "^1.17.0",
38-
"grpc-web": "^0.4.0",
37+
"grpc": "^1.21.1",
38+
"grpc-loader": "^2.0.1",
39+
"grpc-web": "^1.0.4",
3940
"hdkey": "0.7.1",
4041
"nodeunit": "^0.11.3",
4142
"snarkjs": "0.1.11",

packages/lightcone_v2.js/src/grpc/grpcClient.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
// Disable gRPC node for now as the protoc command is different.
2+
/*
3+
import {credentials, Metadata, ServiceError} from 'grpc';
4+
import { DexServiceClient } from '../proto_gen/service_dex_grpc_pb';
5+
import { GetNextOrderIdReq } from '../proto_gen/service_dex_pb';
16
import * as grpcWeb from 'grpc-web';
27
import { DexServiceClient } from '..';
38
import { GetNextOrderIdReq } from '..';
49
import { UInt32Value } from "google-protobuf/google/protobuf/wrappers_pb";
10+
*/
511

612
/* // https://github.com/grpc/grpc-node/issues/543#issuecomment-427487420
713
const baseCred: ChannelCredentials = credentials.createSsl();
@@ -13,7 +19,6 @@ const authCred: CallCredentials = credentials.createFromMetadataGenerator((param
1319
callback(null, metadata);
1420
});
1521
const client: GreeterClient = new GreeterClient('localhost:50051', credentials.combineChannelCredentials(baseCred, authCred));
16-
*/
1722
1823
// 18.179.197.168:5000 is temperate gRPC server we hosted on AWS.
1924
export const client: DexServiceClient = new DexServiceClient('localhost:8980', null, null);
@@ -29,3 +34,4 @@ async function example(): Promise<void> {
2934
}
3035
3136
example().catch((err: Error) => console.log(err));
37+
*/
Lines changed: 90 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import {Empty} from 'google-protobuf/google/protobuf/empty_pb';
22
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';
37
import {Order} from '../proto_gen/data_order_pb';
4-
import {io} from "../model/types";
58
import {
69
OffchainWithdrawalRequest,
710
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';
1115
import {
1216
Account,
1317
CancelOrderRes,
@@ -29,19 +33,21 @@ import {
2933
OrderBook,
3034
SimpleOrderCancellationReq,
3135
SubmitOrderRes
32-
} from 'src/proto_gen/service_dex_pb';
36+
} from '../proto_gen/service_dex_pb';
37+
import { DexServiceClient } from '../proto_gen/Service_dexServiceClientPb';
3338

3439
/**
3540
* gRPC GrpcClientService Service
3641
*/
3742
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
3945

46+
// Verfied
4047
public async getDexConfigurations(): Promise<DexConfigurations> {
41-
const metadata = new Metadata();
4248
const empty: Empty = new Empty();
4349
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) => {
4551
if (err) {
4652
return reject(err);
4753
}
@@ -51,12 +57,11 @@ class GrpcClientService {
5157
}
5258

5359
public async getAccount(param: string): Promise<Account> {
54-
const metadata = new Metadata();
5560
const address: StringValue = new StringValue();
5661
address.setValue(param);
5762

5863
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) => {
6065
if (err) {
6166
return reject(err);
6267
}
@@ -66,12 +71,11 @@ class GrpcClientService {
6671
}
6772

6873
public async getNonce(param: number): Promise<UInt32Value> {
69-
const metadata = new Metadata();
7074
const accountId: UInt32Value = new UInt32Value();
7175
accountId.setValue(param);
7276

7377
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) => {
7579
if (err) {
7680
return reject(err);
7781
}
@@ -81,9 +85,15 @@ class GrpcClientService {
8185
}
8286

8387
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+
8595
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) => {
8797
if (err) {
8898
return reject(err);
8999
}
@@ -92,10 +102,13 @@ class GrpcClientService {
92102
});
93103
}
94104

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+
97110
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) => {
99112
if (err) {
100113
return reject(err);
101114
}
@@ -105,9 +118,14 @@ class GrpcClientService {
105118
}
106119

107120
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+
109127
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) => {
111129
if (err) {
112130
return reject(err);
113131
}
@@ -117,9 +135,12 @@ class GrpcClientService {
117135
}
118136

119137
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+
121142
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) => {
123144
if (err) {
124145
return reject(err);
125146
}
@@ -128,10 +149,18 @@ class GrpcClientService {
128149
});
129150
}
130151

152+
// FIXME: time out
131153
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+
133162
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) => {
135164
if (err) {
136165
return reject(err);
137166
}
@@ -140,10 +169,19 @@ class GrpcClientService {
140169
});
141170
}
142171

172+
// TODO: getUserTransactions and getUserTransfers are different
143173
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+
145183
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) => {
147185
if (err) {
148186
return reject(err);
149187
}
@@ -152,10 +190,15 @@ class GrpcClientService {
152190
});
153191
}
154192

193+
// FIXME: time out
155194
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+
157200
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) => {
159202
if (err) {
160203
return reject(err);
161204
}
@@ -164,10 +207,20 @@ class GrpcClientService {
164207
});
165208
}
166209

210+
// FIXME: time out
167211
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+
169222
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) => {
171224
if (err) {
172225
return reject(err);
173226
}
@@ -176,10 +229,10 @@ class GrpcClientService {
176229
});
177230
}
178231

232+
// TODO: not test in web
179233
public async submitOrder(param: Order): Promise<SubmitOrderRes> {
180-
const metadata = new Metadata();
181234
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) => {
183236
if (err) {
184237
return reject(err);
185238
}
@@ -188,11 +241,11 @@ class GrpcClientService {
188241
});
189242
}
190243

244+
// TODO: not test in web
191245
// Cancel orders by marking them obsoleted in database, not to be included in blocks.
192246
public async cancelOrder(param: SimpleOrderCancellationReq): Promise<CancelOrderRes> {
193-
const metadata = new Metadata();
194247
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) => {
196249
if (err) {
197250
return reject(err);
198251
}
@@ -201,11 +254,11 @@ class GrpcClientService {
201254
});
202255
}
203256

257+
// TODO: not test in web
204258
// Submit an offchain order cancellation request, will make into blocks.
205259
public async submitOrderCancellation(param: OrderCancellationRequest): Promise<CancelOrderRes> {
206-
const metadata = new Metadata();
207260
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) => {
209262
if (err) {
210263
return reject(err);
211264
}
@@ -214,11 +267,11 @@ class GrpcClientService {
214267
});
215268
}
216269

270+
// TODO: not test in web
217271
// Submit an offchain withdrawal request, will make into blocks.
218272
public async submitOffchainWithdrawal(param: OffchainWithdrawalRequest): Promise<OffchainWithdrawalalRes> {
219-
const metadata = new Metadata();
220273
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) => {
222275
if (err) {
223276
return reject(err);
224277
}
@@ -227,12 +280,6 @@ class GrpcClientService {
227280
});
228281
}
229282

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-
236283
}
237284

238285
export const grpcClientService: GrpcClientService = new GrpcClientService();

packages/lightcone_v2.js/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { GetNextOrderIdReq } from './proto_gen/service_dex_pb';
2-
import { DexServiceClient } from './proto_gen/service_dex_grpc_pb';
1+
import { grpcClientService } from './grpc/grpcClientService';
32

43
import common from './lib/wallet/common';
54
import ethereum from './lib/wallet/ethereum';
@@ -14,8 +13,7 @@ import Utils from './lib/wallet/common/utils';
1413

1514

1615
export {
17-
GetNextOrderIdReq,
18-
DexServiceClient,
16+
grpcClientService,
1917
common,
2018
ethereum,
2119
// WalletUtils,

packages/lightcone_v2.js/src/lib/grpc/proto.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ const PROTOC_GEN_TS_PATH = path.join(__dirname, '../../../node_modules/.bin/prot
1212

1313
rimraf.sync(`${MODEL_DIR}/*`);
1414

15+
shell.exec('grpc_tools_node_protoc '
16+
+ `--js_out="import_style=commonjs,binary:${MODEL_DIR}" `
17+
+ `--grpc-web_out="import_style=typescript,mode=grpcwebtext:${MODEL_DIR}" `
18+
+ `--proto_path ${PROTO_DIR} ${PROTO_DIR}/*.proto`);
19+
20+
/*
1521
shell.exec('grpc_tools_node_protoc '
1622
+ `--plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" `
1723
+ `--grpc_out="${MODEL_DIR}" `
1824
+ `--js_out="import_style=commonjs,binary:${MODEL_DIR}" `
1925
+ `--ts_out="${MODEL_DIR}" `
2026
+ `--proto_path ${PROTO_DIR} ${PROTO_DIR}/*.proto`);
27+
*/

0 commit comments

Comments
 (0)