Skip to content

Commit fd5e234

Browse files
xiaowheatiwbrhwfy
authored andcommitted
[Lightcone_V2.js] Make lightcone_v2.js to be imported in react web app (#243)
* Point DexService to aws hosted service * Make lightcone_v2.js to import in react web app * Update grpcClient.ts * [Lightcone_V2.js] Migrate Lightcone v2 Javascript to Typescript (#244) * Migrate lightcone v2 js to typescript (Step 1) * Solve typesript errors in walletAccount.ts * Resolve all errors during migration from Javascript to Typescript. * Move tests to typescript in lightcone_v2.js * Add resolveJsonModule to true. Update index.js which is the entrypoint of the package. * Add GetNextOrderIdReq to test importing grpc . Reenable test/grpc.ts * Update grpcClient
1 parent 020697c commit fd5e234

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+343
-275
lines changed

packages/lightcone_v2.js/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Javascript library for Lightcone 2.0
22

3-
1. `npm install` to install dependencies
4-
2. `npm run build:proto` to generate models from protobuffer
5-
3. `npm run build` to build the project
3+
* `npm install` to install dependencies
4+
* `npm run build:proto` to generate models from protobuffer
5+
* `npm run build` to build the project
6+
* `npm run test` to run tests
7+
8+
# Entrypoint of library
9+
`src/index.ts`

packages/lightcone_v2.js/globals.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ declare module 'snarkjs';
88
declare var web3: any;
99
declare var artifacts: any;
1010
declare var contract: any;
11-
declare var before: any;
12-
declare var beforeEach: any;
13-
declare var describe: any;
14-
declare var it: any;
11+
// FIXME: conflicts to node_modules/@types/mocha/index.d.ts
12+
// declare var before: any;
13+
// declare var beforeEach: any;
14+
// declare var describe: any;
15+
// declare var it: any;
1516
declare var assert: any;

packages/lightcone_v2.js/index.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/lightcone_v2.js/lib/wallet/config/data.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/lightcone_v2.js/package.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"test": "test"
77
},
88
"scripts": {
9-
"build:proto": "rimraf proto_gen && mkdir proto_gen && node lib/grpc/proto",
9+
"build:proto": "rimraf src/proto_gen && mkdir src/proto_gen && node src/lib/grpc/proto",
1010
"build": "npm run build:proto && rimraf dist && tsc",
1111
"lint": "tslint --project tsconfig.json ./src/**/*.ts",
12-
"client": "node dist/src/grpc/grpcClient.js",
13-
"test": "mocha"
12+
"client": "node dist/grpc/grpcClient.js",
13+
"test": "mocha -r ts-node/register --project tsconfig.json test/*.ts"
1414
},
1515
"keywords": [
1616
"ethereum",
@@ -21,34 +21,39 @@
2121
"type": "git",
2222
"url": "[email protected]:Loopring/loopr-app-sdk.git"
2323
},
24-
"main": "index.js",
24+
"main": "dist/index.js",
2525
"license": "ISC",
2626
"dependencies": {
2727
"async-validator": "1.8.2",
28+
"bignumber.js": "^9.0.0",
2829
"bip39": "2.5.0",
2930
"blake-hash": "^1.1.0",
3031
"bn.js": "^4.11.8",
3132
"ethereumjs-abi": "0.6.5",
3233
"ethereumjs-tx": "1.3.3",
3334
"ethereumjs-util": "5.2.0",
3435
"ethereumjs-wallet": "0.6.0",
35-
"grpc": "^1.17.0",
36-
"google-protobuf": "^3.6.1",
36+
"google-protobuf": "^3.9.0-rc.1",
37+
"grpc": "^1.21.1",
3738
"hdkey": "0.7.1",
3839
"nodeunit": "^0.11.3",
3940
"snarkjs": "0.1.11",
4041
"socket.io-client": "2.0.4",
41-
"web3": "^1.0.0-beta.36"
42+
"web3": "^1.0.0-beta.36",
43+
"whatwg-fetch": "^3.0.0"
4244
},
4345
"devDependencies": {
46+
"@types/chai": "^4.1.7",
4447
"@types/google-protobuf": "^3.2.7",
45-
"ts-node": "^7.0.1",
48+
"@types/mocha": "^5.2.7",
49+
"@types/pluralize": "0.0.29",
4650
"grpc-tools": "^1.6.6",
4751
"grpc_tools_node_protoc_ts": "^2.5.1",
4852
"mocha": "^5.2.0",
4953
"protobufjs": "^6.8.8",
5054
"rimraf": "^2.6.2",
5155
"shelljs": "^0.8.3",
56+
"ts-node": "^7.0.1",
5257
"tslint": "^5.12.0",
5358
"tslint-microsoft-contrib": "^6.2.0",
5459
"types-bn": "0.0.1",

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {credentials, Metadata, ServiceError} from 'grpc';
2-
import { DexServiceClient } from '../../proto_gen/service_dex_grpc_pb';
3-
import { GetNextOrderIdReq } from '../../proto_gen/service_dex_pb';
2+
import { DexServiceClient } from '../proto_gen/service_dex_grpc_pb';
3+
import { GetNextOrderIdReq } from '../proto_gen/service_dex_pb';
44
import { UInt32Value } from "google-protobuf/google/protobuf/wrappers_pb";
55

66
/* // https://github.com/grpc/grpc-node/issues/543#issuecomment-427487420
@@ -14,21 +14,14 @@ const authCred: CallCredentials = credentials.createFromMetadataGenerator((param
1414
});
1515
const client: GreeterClient = new GreeterClient('localhost:50051', credentials.combineChannelCredentials(baseCred, authCred));
1616
*/
17-
const client: DexServiceClient = new DexServiceClient('localhost:8980', credentials.createInsecure());
1817

19-
let argv: string = 'world';
20-
if (process.argv.length >= 3) {
21-
argv = process.argv[2];
22-
}
18+
// 18.179.197.168:5000 is temperate gRPC server we hosted on AWS.
19+
export const client: DexServiceClient = new DexServiceClient('18.179.197.168:5000', credentials.createInsecure());
2320

2421
const param: GetNextOrderIdReq = new GetNextOrderIdReq();
2522
param.setAccountId(1);
2623
param.setTokenSellId(1);
2724

28-
const metadata: Metadata = new Metadata();
29-
metadata.add('foo', 'bar1');
30-
metadata.add('foo', 'bar2');
31-
3225
async function example(): Promise<void> {
3326
client.getNextOrderId(param, (err: ServiceError | null, res: UInt32Value) => {
3427
console.log(res.getValue())

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {Empty} from 'google-protobuf/google/protobuf/empty_pb';
22
import {StringValue, UInt32Value} from 'google-protobuf/google/protobuf/wrappers_pb';
33
import {credentials, Metadata, ServiceError} from 'grpc';
4-
import {Order} from 'proto_gen/data_order_pb';
4+
import {Order} from '../proto_gen/data_order_pb';
55
import {io} from "../model/types";
66
import {
77
OffchainWithdrawalRequest,
88
OrderCancellationRequest
9-
} from 'proto_gen/data_requests_pb';
10-
import {DexServiceClient} from '../../proto_gen/service_dex_grpc_pb';
9+
} from '../proto_gen/data_requests_pb';
10+
import { DexServiceClient } from '../proto_gen/service_dex_grpc_pb';
1111
import {
1212
Account,
1313
CancelOrderRes,
@@ -29,14 +29,13 @@ import {
2929
OrderBook,
3030
SimpleOrderCancellationReq,
3131
SubmitOrderRes
32-
} from 'proto_gen/service_dex_pb';
32+
} from 'src/proto_gen/service_dex_pb';
3333

3434
/**
3535
* gRPC GrpcClientService Service
3636
*/
3737
class GrpcClientService {
38-
39-
private readonly client: DexServiceClient = new DexServiceClient('127.0.0.1:59480', credentials.createInsecure()); // TODO: config
38+
private readonly client: DexServiceClient = new DexServiceClient('18.179.197.168:5000', credentials.createInsecure()); // TODO: config
4039

4140
public async getDexConfigurations(metadata: Metadata = new Metadata()): Promise<DexConfigurations> {
4241
const empty: Empty = new Empty();

packages/lightcone_v2.js/src/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { GetNextOrderIdReq } from './proto_gen/service_dex_pb';
2+
import { DexServiceClient } from './proto_gen/service_dex_grpc_pb';
3+
4+
import common from './lib/wallet/common';
5+
import ethereum from './lib/wallet/ethereum';
6+
// Looks like we don't need WalletUtils
7+
// It also causes an error
8+
// "export 'default' (reexported as 'WalletUtils')
9+
// was not found in './WalletUtils'
10+
// import WalletUtils from './WalletUtils';
11+
import ContractUtils from './lib/wallet/ethereum/contracts/Contracts';
12+
import EthRpcUtils from './lib/wallet/ethereum/eth';
13+
import Utils from './lib/wallet/common/utils';
14+
15+
16+
export {
17+
GetNextOrderIdReq,
18+
DexServiceClient,
19+
common,
20+
ethereum,
21+
// WalletUtils,
22+
ContractUtils,
23+
EthRpcUtils,
24+
Utils
25+
};

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// proto.js is to generate proto_gen, not used in any typescript.
2+
13
const path = require('path');
24
const shell = require('shelljs');
35
const rimraf = require('rimraf');
@@ -6,7 +8,7 @@ process.env.PATH += (path.delimiter + path.join(process.cwd(), 'node_modules', '
68

79
const PROTO_DIR = path.join(__dirname, '../../proto_src');
810
const MODEL_DIR = path.join(__dirname, '../../proto_gen');
9-
const PROTOC_GEN_TS_PATH = path.join(__dirname, '../../node_modules/.bin/protoc-gen-ts');
11+
const PROTOC_GEN_TS_PATH = path.join(__dirname, '../../../node_modules/.bin/protoc-gen-ts');
1012

1113
rimraf.sync(`${MODEL_DIR}/*`);
1214

packages/lightcone_v2.js/lib/sign/babyjub.js renamed to packages/lightcone_v2.js/src/lib/sign/babyjub.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const bn128 = require("snarkjs").bn128;
2-
const bigInt = require("snarkjs").bigInt;
1+
import { bn128, bigInt } from 'snarkjs'
32

43
exports.addPoint = addPoint;
54
exports.mulPointEscalar = mulPointEscalar;

0 commit comments

Comments
 (0)