Skip to content

Commit 68218f2

Browse files
authored
[Lightcone_V2.js] add grpc-web dependency (#252)
* m * m * modify proto structure * modify git ignore top level * modify git ignore * add test * fix bug && modify * temp commit * Add tslint-microsoft-contrib to devDependencies * Change the version to 0.0.1 * Add index.js to enable import * success access server * Fix merge conflicts in previours PR (#239) * small modify * [Protocol3] remove our own reputation token (#242) * [Protocol3] Misc small improvements + More tests (#241) * [Protocol3] WIP: maintenance mode changes * [Protocol3] WIP: maintenance mode improvements * [Protocol3] Fixed issue * [Protocol3] Improved docs * [Protocol3] Fixed doc * Update ExchangeMode.sol * Update ExchangeAdmins.sol * [Protocol3] Tests + fixes for improved maintenance mode * [Protocol3] Fixed misc tests * [Protocol3] Added block version * [Protocol3] Added support for data compression * [Protocol3] Removed protocol2-js dependency * [Protocol3] Fixed misc tests * [Protocol3] Renamed numElements to blockSize for consistency * [Protocol3] Optimized onchain decompression * [Protocol3] Improved LZ compressor + ring data transformation * [Protocol3] Misc small improvements * [Protocol3] Improved LZ compressor performance * [Protocol3] Fixed offchain withdrawal message signing * [Protocol3] Update ABI * [Protocol3] Updated generate_keys script for parameter rename * [Protocol3] Break long lines * [Protocol3] Misc small improvements + added misc tests * [Protocol3] Misc improvements/tests for same accounts in requests * [Protocol3] Fixed test * [Protocol3] Added reentrancy tests * [Protocol3] Small refactor * [Protocol3] Added misc ring settlement/block fee tests * [Protocol3] Removed 'only' from tests * [Protocol3] Added transfer tests * [Protocol3] Fixed testing check * [Protocol3] Small refactor (burn -> protocol fees) * [Protocol3] Cleaned up logging * [Protocol3] Fixed precission error in test check * [Protocol3] Added some more misc tests * [Protocol3] Fixed test check for shutdown mode * [Protocol3] Update backend doc * [Protocol3] Small refactoring * [Protocol3] Allow withdrawing the protocol fees directly to pfm * [Protocol3] Changed createOrUpdateAccount to update the Merkle tree * [Protocol3] Fixed compile warning * [Protocol3] Fixed test with AccountContract * [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 * fix migrate error by upgrading ganache-cli version (#249) * [Protocol3] rename ProtocolFeeManager to ProtocolFeeVault (#250) * [Protocol3] update ABI files (#251) * modify add grpc-web
1 parent 6577c44 commit 68218f2

File tree

9 files changed

+85
-45
lines changed

9 files changed

+85
-45
lines changed

packages/lightcone_v2.js/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"ethereumjs-util": "5.2.0",
3535
"ethereumjs-wallet": "0.6.0",
3636
"google-protobuf": "^3.9.0-rc.1",
37-
"grpc": "^1.21.1",
37+
"grpc": "^1.17.0",
38+
"grpc-web": "^0.4.0",
3839
"hdkey": "0.7.1",
3940
"nodeunit": "^0.11.3",
4041
"snarkjs": "0.1.11",
@@ -49,6 +50,9 @@
4950
"@types/pluralize": "0.0.29",
5051
"grpc-tools": "^1.6.6",
5152
"grpc_tools_node_protoc_ts": "^2.5.1",
53+
"browserify": "^16.2.2",
54+
"webpack": "^4.16.5",
55+
"webpack-cli": "^3.1.0",
5256
"mocha": "^5.2.0",
5357
"protobufjs": "^6.8.8",
5458
"rimraf": "^2.6.2",

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
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';
1+
import * as grpcWeb from 'grpc-web';
2+
import { DexServiceClient } from '..';
3+
import { GetNextOrderIdReq } from '..';
44
import { UInt32Value } from "google-protobuf/google/protobuf/wrappers_pb";
55

66
/* // https://github.com/grpc/grpc-node/issues/543#issuecomment-427487420
@@ -16,14 +16,14 @@ const client: GreeterClient = new GreeterClient('localhost:50051', credentials.c
1616
*/
1717

1818
// 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());
19+
export const client: DexServiceClient = new DexServiceClient('localhost:8980', null, null);
2020

2121
const param: GetNextOrderIdReq = new GetNextOrderIdReq();
2222
param.setAccountId(1);
2323
param.setTokenSellId(1);
2424

2525
async function example(): Promise<void> {
26-
client.getNextOrderId(param, (err: ServiceError | null, res: UInt32Value) => {
26+
client.getNextOrderId(param, (err: Error | null, res: UInt32Value) => {
2727
console.log(res.getValue())
2828
});
2929
}

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

Lines changed: 48 additions & 35 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';
3-
import {credentials, Metadata, ServiceError} from 'grpc';
43
import {Order} from '../proto_gen/data_order_pb';
54
import {io} from "../model/types";
65
import {
76
OffchainWithdrawalRequest,
87
OrderCancellationRequest
98
} from '../proto_gen/data_requests_pb';
10-
import { DexServiceClient } from '../proto_gen/service_dex_grpc_pb';
9+
import { DexServiceClient } from '..';
10+
import {Metadata} from "grpc";
1111
import {
1212
Account,
1313
CancelOrderRes,
@@ -35,13 +35,13 @@ import {
3535
* gRPC GrpcClientService Service
3636
*/
3737
class GrpcClientService {
38-
private readonly client: DexServiceClient = new DexServiceClient('18.179.197.168:5000', credentials.createInsecure()); // TODO: config
38+
private readonly client: DexServiceClient = new DexServiceClient('18.179.197.168:5000', null, null); // TODO: config
3939

40-
public async getDexConfigurations(metadata: Metadata = new Metadata()): Promise<DexConfigurations> {
40+
public async getDexConfigurations(): Promise<DexConfigurations> {
41+
const metadata = new Metadata();
4142
const empty: Empty = new Empty();
42-
4343
return new Promise<DexConfigurations>((resolve: Function, reject: Function): void => {
44-
this.client.getDexConfigurations(empty, metadata, (err: ServiceError | null, res: DexConfigurations) => {
44+
this.client.getDexConfigurations(empty, metadata, (err: Error | null, res: DexConfigurations) => {
4545
if (err) {
4646
return reject(err);
4747
}
@@ -50,12 +50,13 @@ class GrpcClientService {
5050
});
5151
}
5252

53-
public async getAccount(param: string, metadata: Metadata = new Metadata()): Promise<Account> {
53+
public async getAccount(param: string): Promise<Account> {
54+
const metadata = new Metadata();
5455
const address: StringValue = new StringValue();
5556
address.setValue(param);
5657

5758
return new Promise<Account>((resolve: Function, reject: Function): void => {
58-
this.client.getAccount(address, metadata, (err: ServiceError | null, res: Account) => {
59+
this.client.getAccount(address, metadata, (err: Error | null, res: Account) => {
5960
if (err) {
6061
return reject(err);
6162
}
@@ -64,12 +65,13 @@ class GrpcClientService {
6465
});
6566
}
6667

67-
public async getNonce(param: number, metadata: Metadata = new Metadata()): Promise<UInt32Value> {
68+
public async getNonce(param: number): Promise<UInt32Value> {
69+
const metadata = new Metadata();
6870
const accountId: UInt32Value = new UInt32Value();
6971
accountId.setValue(param);
7072

7173
return new Promise<UInt32Value>((resolve: Function, reject: Function): void => {
72-
this.client.getNonce(accountId, metadata, (err: ServiceError | null, res: UInt32Value) => {
74+
this.client.getNonce(accountId, metadata, (err: Error | null, res: UInt32Value) => {
7375
if (err) {
7476
return reject(err);
7577
}
@@ -78,9 +80,10 @@ class GrpcClientService {
7880
});
7981
}
8082

81-
public async getTokens(param: GetTokensReq, metadata: Metadata = new Metadata()): Promise<GetTokensRes> {
83+
public async getTokens(param: GetTokensReq): Promise<GetTokensRes> {
84+
const metadata = new Metadata();
8285
return new Promise<GetTokensRes>((resolve: Function, reject: Function): void => {
83-
this.client.getTokens(param, metadata, (err: ServiceError | null, res: GetTokensRes) => {
86+
this.client.getTokens(param, metadata, (err: Error | null, res: GetTokensRes) => {
8487
if (err) {
8588
return reject(err);
8689
}
@@ -89,9 +92,10 @@ class GrpcClientService {
8992
});
9093
}
9194

92-
public async getNextOrderId(param: GetNextOrderIdReq, metadata: Metadata = new Metadata()): Promise<UInt32Value> {
95+
public async getNextOrderId(param: GetNextOrderIdReq): Promise<UInt32Value> {
96+
const metadata = new Metadata();
9397
return new Promise<UInt32Value>((resolve: Function, reject: Function): void => {
94-
this.client.getNextOrderId(param, metadata, (err: ServiceError | null, res: UInt32Value) => {
98+
this.client.getNextOrderId(param, metadata, (err: Error | null, res: UInt32Value) => {
9599
if (err) {
96100
return reject(err);
97101
}
@@ -100,9 +104,10 @@ class GrpcClientService {
100104
});
101105
}
102106

103-
public async getMarkets(param: GetMarketsReq, metadata: Metadata = new Metadata()): Promise<GetMarketsRes> {
107+
public async getMarkets(param: GetMarketsReq): Promise<GetMarketsRes> {
108+
const metadata = new Metadata();
104109
return new Promise<GetMarketsRes>((resolve: Function, reject: Function): void => {
105-
this.client.getMarkets(param, metadata, (err: ServiceError | null, res: GetMarketsRes) => {
110+
this.client.getMarkets(param, metadata, (err: Error | null, res: GetMarketsRes) => {
106111
if (err) {
107112
return reject(err);
108113
}
@@ -111,9 +116,10 @@ class GrpcClientService {
111116
});
112117
}
113118

114-
public async getMarketFills(param: GetMarketFillsReq, metadata: Metadata = new Metadata()): Promise<GetFillsRes> {
119+
public async getMarketFills(param: GetMarketFillsReq): Promise<GetFillsRes> {
120+
const metadata = new Metadata();
115121
return new Promise<GetFillsRes>((resolve: Function, reject: Function): void => {
116-
this.client.getMarketFills(param, metadata, (err: ServiceError | null, res: GetFillsRes) => {
122+
this.client.getMarketFills(param, metadata, (err: Error | null, res: GetFillsRes) => {
117123
if (err) {
118124
return reject(err);
119125
}
@@ -122,9 +128,10 @@ class GrpcClientService {
122128
});
123129
}
124130

125-
public async getUserFills(param: GetUserFillsReq, metadata: Metadata = new Metadata()): Promise<GetFillsRes> {
131+
public async getUserFills(param: GetUserFillsReq): Promise<GetFillsRes> {
132+
const metadata = new Metadata();
126133
return new Promise<GetFillsRes>((resolve: Function, reject: Function): void => {
127-
this.client.getUserFills(param, metadata, (err: ServiceError | null, res: GetFillsRes) => {
134+
this.client.getUserFills(param, metadata, (err: Error | null, res: GetFillsRes) => {
128135
if (err) {
129136
return reject(err);
130137
}
@@ -133,9 +140,10 @@ class GrpcClientService {
133140
});
134141
}
135142

136-
public async getUserTransactions(param: GetUserTransactionsReq, metadata: Metadata = new Metadata()): Promise<GetUserTransactionsRes> {
143+
public async getUserTransactions(param: GetUserTransactionsReq): Promise<GetUserTransactionsRes> {
144+
const metadata = new Metadata();
137145
return new Promise<GetUserTransactionsRes>((resolve: Function, reject: Function): void => {
138-
this.client.getUserTransfers(param, metadata, (err: ServiceError | null, res: GetUserTransactionsRes) => {
146+
this.client.getUserTransfers(param, metadata, (err: Error | null, res: GetUserTransactionsRes) => {
139147
if (err) {
140148
return reject(err);
141149
}
@@ -144,9 +152,10 @@ class GrpcClientService {
144152
});
145153
}
146154

147-
public async getOrderBook(param: GetOrderBookReq, metadata: Metadata = new Metadata()): Promise<OrderBook> {
155+
public async getOrderBook(param: GetOrderBookReq): Promise<OrderBook> {
156+
const metadata = new Metadata();
148157
return new Promise<OrderBook>((resolve: Function, reject: Function): void => {
149-
this.client.getOrderBook(param, metadata, (err: ServiceError | null, res: OrderBook) => {
158+
this.client.getOrderBook(param, metadata, (err: Error | null, res: OrderBook) => {
150159
if (err) {
151160
return reject(err);
152161
}
@@ -155,9 +164,10 @@ class GrpcClientService {
155164
});
156165
}
157166

158-
public async getUserOrders(param: GetUserOrdersReq, metadata: Metadata = new Metadata()): Promise<GetUserOrdersRes> {
167+
public async getUserOrders(param: GetUserOrdersReq): Promise<GetUserOrdersRes> {
168+
const metadata = new Metadata();
159169
return new Promise<GetUserOrdersRes>((resolve: Function, reject: Function): void => {
160-
this.client.getUserOrders(param, metadata, (err: ServiceError | null, res: GetUserOrdersRes) => {
170+
this.client.getUserOrders(param, metadata, (err: Error | null, res: GetUserOrdersRes) => {
161171
if (err) {
162172
return reject(err);
163173
}
@@ -166,9 +176,10 @@ class GrpcClientService {
166176
});
167177
}
168178

169-
public async submitOrder(param: Order, metadata: Metadata = new Metadata()): Promise<SubmitOrderRes> {
179+
public async submitOrder(param: Order): Promise<SubmitOrderRes> {
180+
const metadata = new Metadata();
170181
return new Promise<SubmitOrderRes>((resolve: Function, reject: Function): void => {
171-
this.client.submitOrder(param, metadata, (err: ServiceError | null, res: SubmitOrderRes) => {
182+
this.client.submitOrder(param, metadata, (err: Error | null, res: SubmitOrderRes) => {
172183
if (err) {
173184
return reject(err);
174185
}
@@ -178,9 +189,10 @@ class GrpcClientService {
178189
}
179190

180191
// Cancel orders by marking them obsoleted in database, not to be included in blocks.
181-
public async cancelOrder(param: SimpleOrderCancellationReq, metadata: Metadata = new Metadata()): Promise<CancelOrderRes> {
192+
public async cancelOrder(param: SimpleOrderCancellationReq): Promise<CancelOrderRes> {
193+
const metadata = new Metadata();
182194
return new Promise<CancelOrderRes>((resolve: Function, reject: Function): void => {
183-
this.client.cancelOrder(param, metadata, (err: ServiceError | null, res: CancelOrderRes) => {
195+
this.client.cancelOrder(param, metadata, (err: Error | null, res: CancelOrderRes) => {
184196
if (err) {
185197
return reject(err);
186198
}
@@ -190,9 +202,10 @@ class GrpcClientService {
190202
}
191203

192204
// Submit an offchain order cancellation request, will make into blocks.
193-
public async submitOrderCancellation(param: OrderCancellationRequest, metadata: Metadata = new Metadata()): Promise<CancelOrderRes> {
205+
public async submitOrderCancellation(param: OrderCancellationRequest): Promise<CancelOrderRes> {
206+
const metadata = new Metadata();
194207
return new Promise<CancelOrderRes>((resolve: Function, reject: Function): void => {
195-
this.client.submitOrderCancellation(param, metadata, (err: ServiceError | null, res: CancelOrderRes) => {
208+
this.client.submitOrderCancellation(param, metadata, (err: Error | null, res: CancelOrderRes) => {
196209
if (err) {
197210
return reject(err);
198211
}
@@ -202,10 +215,10 @@ class GrpcClientService {
202215
}
203216

204217
// Submit an offchain withdrawal request, will make into blocks.
205-
public async submitOffchainWithdrawal(param: OffchainWithdrawalRequest,
206-
metadata: Metadata = new Metadata()): Promise<OffchainWithdrawalalRes> {
218+
public async submitOffchainWithdrawal(param: OffchainWithdrawalRequest): Promise<OffchainWithdrawalalRes> {
219+
const metadata = new Metadata();
207220
return new Promise<OffchainWithdrawalalRes>((resolve: Function, reject: Function): void => {
208-
this.client.submitOffchainWithdrawal(param, metadata, (err: ServiceError | null, res: OffchainWithdrawalalRes) => {
221+
this.client.submitOffchainWithdrawal(param, metadata, (err: Error | null, res: OffchainWithdrawalalRes) => {
209222
if (err) {
210223
return reject(err);
211224
}

packages/loopring_v3/contracts/impl/libexchange/ExchangeAccounts.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import "./ExchangeData.sol";
2727
/// @author Brecht Devos - <[email protected]>
2828
library ExchangeAccounts
2929
{
30+
using AddressUtil for address payable;
3031
using MathUint for uint;
3132
using ExchangeBalances for ExchangeData.State;
3233

packages/loopring_v3/contracts/impl/libexchange/ExchangeBlocks.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ library ExchangeBlocks
118118
),
119119
"INVALID_PROOF"
120120
);
121-
122121
// Mark the block as verified
123122
specifiedBlock.state = ExchangeData.BlockState.VERIFIED;
124123
emit BlockVerified(blockIdx);

packages/loopring_v3/contracts/impl/libexchange/ExchangeWithdrawals.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ library ExchangeWithdrawals
127127
require(msg.value >= S.withdrawalFeeETH, "INSUFFICIENT_FEE");
128128
// Send surplus of ETH back to the sender
129129
msg.sender.transferETH(msg.value.sub(S.withdrawalFeeETH), gasleft());
130-
131130
// Add the withdraw to the withdraw chain
132131
ExchangeData.Request storage prevRequest = S.withdrawalChain[S.withdrawalChain.length - 1];
133132
ExchangeData.Request memory request = ExchangeData.Request(

packages/loopring_v3/test/testExchangeBlocks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ contract("Exchange", (accounts: string[]) => {
1111
let exchangeId = 0;
1212
let exchange: any;
1313
let loopring: any;
14-
14+
1515
const createExchange = async (bSetupTestState: boolean = true) => {
1616
exchangeId = await exchangeTestUtil.createExchange(
1717
exchangeTestUtil.testContext.stateOwners[0],

packages/loopring_v3/test/testExchangeDepositWithdraw.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ contract("Exchange", (accounts: string[]) => {
142142
accountID: number, token: string,
143143
owner: string, uExpectedAmount: BN) => {
144144
const expectedAmount = roundToFloatValue(uExpectedAmount, constants.Float28Encoding);
145-
146145
const recipient = (accountID === 0) ? await loopring.protocolFeeVault() : owner;
147146
const balanceOwnerBefore = await exchangeTestUtil.getOnchainBalance(recipient, token);
148147
const balanceContractBefore = await exchangeTestUtil.getOnchainBalance(exchange.address, token);
@@ -360,6 +359,30 @@ contract("Exchange", (accounts: string[]) => {
360359
assert(newAccountID === accountID, "Account ID needs to remain the same");
361360
});
362361

362+
it("Update account", async () => {
363+
await createExchange();
364+
365+
const keyPair = exchangeTestUtil.getKeyPairEDDSA();
366+
const owner = exchangeTestUtil.testContext.orderOwners[0];
367+
368+
// The correct deposit fee expected by the contract
369+
const fees = await exchange.getFees();
370+
const accountCreationFee = fees._accountCreationFeeETH;
371+
const accountUpdateFee = fees._accountUpdateFeeETH;
372+
const depositFee = fees._depositFeeETH;
373+
374+
// Everything correct for account creation
375+
const totalCreationFee = depositFee.add(accountCreationFee);
376+
const accountID = await createOrUpdateAccountChecked(keyPair, owner, totalCreationFee, true);
377+
assert(accountID > 0);
378+
379+
// Update the keys
380+
const totalUpdateFee = depositFee.add(accountUpdateFee);
381+
const newKeyPair = exchangeTestUtil.getKeyPairEDDSA();
382+
const newAccountID = await createOrUpdateAccountChecked(newKeyPair, owner, totalUpdateFee, false);
383+
assert(newAccountID === accountID, "Account ID needs to remain the same");
384+
});
385+
363386
it("ERC20: Deposit", async () => {
364387
await createExchange();
365388

packages/loopring_v3/test/testExchangeRingSettlement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,7 @@ contract("Exchange", (accounts: string[]) => {
11891189
amountS: new BN(web3.utils.toWei("100", "ether")),
11901190
amountB: new BN(web3.utils.toWei("100", "ether")),
11911191
},
1192+
dualAuthBSignature: {Rx: "4564565564545", Ry: "456445648974", s: "445644894"},
11921193
};
11931194

11941195
await exchangeTestUtil.setupRing(ring);

0 commit comments

Comments
 (0)