@usherlabs/cex-broker 0.1.8 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -289979,13 +289979,16 @@ class Logger extends BaseLogger {
289979
289979
  // src/helpers/logger.ts
289980
289980
  var log = new Logger({
289981
289981
  type: "pretty",
289982
- stylePrettyLogs: false
289982
+ stylePrettyLogs: false,
289983
+ minLevel: process.env.LOG_LEVEL === "debug" ? 0 : 3
289983
289984
  });
289984
289985
 
289985
289986
  // src/helpers/index.ts
289986
289987
  function authenticateRequest(call, whitelistIps) {
289987
289988
  const clientIp = call.getPeer().split(":")[0];
289988
- if (!clientIp || !whitelistIps.includes(clientIp)) {
289989
+ if (whitelistIps.includes("*")) {
289990
+ return true;
289991
+ } else if (!clientIp || !whitelistIps.includes(clientIp)) {
289989
289992
  log.warn(`Blocked access from unauthorized IP: ${clientIp || "unknown"}`);
289990
289993
  return false;
289991
289994
  }
@@ -290013,6 +290016,9 @@ function createBroker(cex3, metadata, useVerity, verityProverUrl) {
290013
290016
  recvWindow: 60000
290014
290017
  }
290015
290018
  });
290019
+ if (process.env.CEX_BROKER_SANDBOX_MODE === "true") {
290020
+ exchange.setSandboxMode(true);
290021
+ }
290016
290022
  exchange.options.recvWindow = 60000;
290017
290023
  return exchange;
290018
290024
  }
@@ -290153,17 +290159,19 @@ var Action = {
290153
290159
  GetOrderDetails: 4,
290154
290160
  CancelOrder: 5,
290155
290161
  FetchBalance: 6,
290156
- FetchDepositAddresses: 7
290162
+ FetchBalances: 7,
290163
+ FetchDepositAddresses: 8
290157
290164
  };
290158
290165
 
290159
290166
  // src/proto/cex_broker/SubscriptionType.ts
290160
290167
  var SubscriptionType = {
290161
- ORDERBOOK: 0,
290162
- TRADES: 1,
290163
- TICKER: 2,
290164
- OHLCV: 3,
290165
- BALANCE: 4,
290166
- ORDERS: 5
290168
+ NO_ACTION: 0,
290169
+ ORDERBOOK: 1,
290170
+ TRADES: 2,
290171
+ TICKER: 3,
290172
+ OHLCV: 4,
290173
+ BALANCE: 5,
290174
+ ORDERS: 6
290167
290175
  };
290168
290176
 
290169
290177
  // src/server.ts
@@ -290180,6 +290188,11 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290180
290188
  const server = new grpc.Server;
290181
290189
  server.addService(cexNode.cex_service.service, {
290182
290190
  ExecuteAction: async (call, callback) => {
290191
+ log.info(`Request - ExecuteAction: ${JSON.stringify({
290192
+ action: call.request.action,
290193
+ cex: call.request.cex,
290194
+ symbol: call.request.symbol
290195
+ })}`);
290183
290196
  if (!authenticateRequest(call, whitelistIps)) {
290184
290197
  return callback({
290185
290198
  code: grpc.status.PERMISSION_DENIED,
@@ -290223,7 +290236,8 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290223
290236
  if (deposit) {
290224
290237
  log.info(`Amount ${value.amount} at ${value.transactionHash} . Paid to ${value.recipientAddress}`);
290225
290238
  return callback(null, {
290226
- result: useVerity ? broker.last_proof : JSON.stringify({ ...deposit })
290239
+ proof: broker.last_proof || "",
290240
+ result: JSON.stringify({ ...deposit })
290227
290241
  });
290228
290242
  }
290229
290243
  callback({
@@ -290264,7 +290278,8 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290264
290278
  });
290265
290279
  if (depositAddresses) {
290266
290280
  return callback(null, {
290267
- result: useVerity ? broker.last_proof : JSON.stringify({ ...depositAddresses })
290281
+ proof: broker.last_proof || "",
290282
+ result: JSON.stringify({ ...depositAddresses })
290268
290283
  });
290269
290284
  }
290270
290285
  callback({
@@ -290314,7 +290329,8 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290314
290329
  const transaction = await broker.withdraw(symbol, Number(transferValue.amount), transferValue.recipientAddress, undefined, { network: transferValue.chain });
290315
290330
  log.info(`Transfer Transfer: ${JSON.stringify(transaction)}`);
290316
290331
  callback(null, {
290317
- result: useVerity ? broker.last_proof : JSON.stringify({ ...transaction })
290332
+ proof: broker.last_proof || "",
290333
+ result: JSON.stringify({ ...transaction })
290318
290334
  });
290319
290335
  } catch (error) {
290320
290336
  log.error({ error });
@@ -290434,7 +290450,8 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290434
290450
  });
290435
290451
  const currencyBalance = balance[symbol];
290436
290452
  callback(null, {
290437
- result: useVerity ? broker.last_proof : JSON.stringify({
290453
+ proof: broker.last_proof || "",
290454
+ result: JSON.stringify({
290438
290455
  balance: currencyBalance || 0,
290439
290456
  currency: symbol
290440
290457
  })
@@ -290447,6 +290464,23 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290447
290464
  }, null);
290448
290465
  }
290449
290466
  break;
290467
+ case Action.FetchBalances:
290468
+ try {
290469
+ const balance = await broker.fetchFreeBalance({
290470
+ ...call.request.payload ?? {}
290471
+ });
290472
+ callback(null, {
290473
+ proof: broker.last_proof || "",
290474
+ result: JSON.stringify(balance)
290475
+ });
290476
+ } catch (error) {
290477
+ log.error(`Error fetching balance from ${cex3}:`, error);
290478
+ callback({
290479
+ code: grpc.status.INTERNAL,
290480
+ message: `Failed to fetch balance from ${cex3}`
290481
+ }, null);
290482
+ }
290483
+ break;
290450
290484
  default:
290451
290485
  return callback({
290452
290486
  code: grpc.status.INVALID_ARGUMENT,
@@ -290467,14 +290501,20 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290467
290501
  try {
290468
290502
  const request = call.request;
290469
290503
  const { cex: cex3, symbol, type: type2, options } = request;
290470
- if (!cex3 || !symbol || type2 === undefined) {
290504
+ const subscriptionType = type2 !== undefined ? type2 : SubscriptionType.ORDERBOOK;
290505
+ log.info(`Request - Subscribe: ${JSON.stringify({
290506
+ cex: request.cex,
290507
+ symbol: request.symbol,
290508
+ type: subscriptionType
290509
+ })}`);
290510
+ if (!cex3 || !symbol) {
290471
290511
  call.write({
290472
290512
  data: JSON.stringify({
290473
290513
  error: "cex, symbol, and type are required"
290474
290514
  }),
290475
290515
  timestamp: Date.now(),
290476
290516
  symbol: symbol || "",
290477
- type: type2 || SubscriptionType.ORDERBOOK
290517
+ type: subscriptionType
290478
290518
  });
290479
290519
  call.end();
290480
290520
  return;
@@ -290487,12 +290527,12 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290487
290527
  }),
290488
290528
  timestamp: Date.now(),
290489
290529
  symbol,
290490
- type: type2
290530
+ type: subscriptionType
290491
290531
  });
290492
290532
  call.end();
290493
290533
  return;
290494
290534
  }
290495
- switch (type2) {
290535
+ switch (subscriptionType) {
290496
290536
  case SubscriptionType.ORDERBOOK:
290497
290537
  try {
290498
290538
  while (true) {
@@ -290501,7 +290541,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290501
290541
  data: JSON.stringify(orderbook),
290502
290542
  timestamp: Date.now(),
290503
290543
  symbol,
290504
- type: type2
290544
+ type: subscriptionType
290505
290545
  });
290506
290546
  }
290507
290547
  } catch (error) {
@@ -290513,7 +290553,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290513
290553
  }),
290514
290554
  timestamp: Date.now(),
290515
290555
  symbol,
290516
- type: type2
290556
+ type: subscriptionType
290517
290557
  });
290518
290558
  }
290519
290559
  break;
@@ -290525,7 +290565,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290525
290565
  data: JSON.stringify(trades),
290526
290566
  timestamp: Date.now(),
290527
290567
  symbol,
290528
- type: type2
290568
+ type: subscriptionType
290529
290569
  });
290530
290570
  }
290531
290571
  } catch (error) {
@@ -290537,7 +290577,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290537
290577
  }),
290538
290578
  timestamp: Date.now(),
290539
290579
  symbol,
290540
- type: type2
290580
+ type: subscriptionType
290541
290581
  });
290542
290582
  }
290543
290583
  break;
@@ -290561,7 +290601,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290561
290601
  }),
290562
290602
  timestamp: Date.now(),
290563
290603
  symbol,
290564
- type: type2
290604
+ type: subscriptionType
290565
290605
  });
290566
290606
  }
290567
290607
  break;
@@ -290586,7 +290626,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290586
290626
  }),
290587
290627
  timestamp: Date.now(),
290588
290628
  symbol,
290589
- type: type2
290629
+ type: subscriptionType
290590
290630
  });
290591
290631
  }
290592
290632
  break;
@@ -290610,7 +290650,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290610
290650
  }),
290611
290651
  timestamp: Date.now(),
290612
290652
  symbol,
290613
- type: type2
290653
+ type: subscriptionType
290614
290654
  });
290615
290655
  }
290616
290656
  break;
@@ -290634,7 +290674,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
290634
290674
  }),
290635
290675
  timestamp: Date.now(),
290636
290676
  symbol,
290637
- type: type2
290677
+ type: subscriptionType
290638
290678
  });
290639
290679
  }
290640
290680
  break;
@@ -290895,6 +290935,9 @@ class CEXBroker {
290895
290935
  recvWindow: 60000
290896
290936
  }
290897
290937
  });
290938
+ if (process.env.CEX_BROKER_SANDBOX_MODE === "true") {
290939
+ client.setSandboxMode(true);
290940
+ }
290898
290941
  this.brokers[broker] = {
290899
290942
  primary: client,
290900
290943
  secondaryBrokers
@@ -291041,7 +291084,7 @@ async function startBrokerCommand(policyPath, port, whitelistIps, url2) {
291041
291084
 
291042
291085
  // src/cli.ts
291043
291086
  var program2 = new Command;
291044
- program2.name("cex-broker").description("CLI to start the CEXBroker service").requiredOption("-p, --policy <path>", "Policy JSON file").option("--port <number>", "Port number (default: 8086)", "8086").option("-w", "--whitelist <addresses...>", "IPv4 address whitelist (space-separated list)").option("-vu", "--verityProverUrl <url>", "Verity Prover Url").action(async (options) => {
291087
+ program2.name("cex-broker").description("CLI to start the CEXBroker service").requiredOption("-p, --policy <path>", "Policy JSON file").option("--port <number>", "Port number (default: 8086)", "8086").option("-w, --whitelist <addresses...>", "IPv4 address whitelist (space-separated list)").option("--verityProverUrl <url>", "Verity Prover Url").action(async (options) => {
291045
291088
  try {
291046
291089
  if (options.whitelist) {
291047
291090
  const isValidIPv4 = (ip) => /^(\d{1,3}\.){3}\d{1,3}$/.test(ip) && ip.split(".").every((part) => Number(part) >= 0 && Number(part) <= 255);
package/dist/index.js CHANGED
@@ -285026,13 +285026,16 @@ class Logger extends BaseLogger {
285026
285026
  // src/helpers/logger.ts
285027
285027
  var log = new Logger({
285028
285028
  type: "pretty",
285029
- stylePrettyLogs: false
285029
+ stylePrettyLogs: false,
285030
+ minLevel: process.env.LOG_LEVEL === "debug" ? 0 : 3
285030
285031
  });
285031
285032
 
285032
285033
  // src/helpers/index.ts
285033
285034
  function authenticateRequest(call, whitelistIps) {
285034
285035
  const clientIp = call.getPeer().split(":")[0];
285035
- if (!clientIp || !whitelistIps.includes(clientIp)) {
285036
+ if (whitelistIps.includes("*")) {
285037
+ return true;
285038
+ } else if (!clientIp || !whitelistIps.includes(clientIp)) {
285036
285039
  log.warn(`Blocked access from unauthorized IP: ${clientIp || "unknown"}`);
285037
285040
  return false;
285038
285041
  }
@@ -285060,6 +285063,9 @@ function createBroker(cex3, metadata, useVerity, verityProverUrl) {
285060
285063
  recvWindow: 60000
285061
285064
  }
285062
285065
  });
285066
+ if (process.env.CEX_BROKER_SANDBOX_MODE === "true") {
285067
+ exchange.setSandboxMode(true);
285068
+ }
285063
285069
  exchange.options.recvWindow = 60000;
285064
285070
  return exchange;
285065
285071
  }
@@ -285200,17 +285206,19 @@ var Action = {
285200
285206
  GetOrderDetails: 4,
285201
285207
  CancelOrder: 5,
285202
285208
  FetchBalance: 6,
285203
- FetchDepositAddresses: 7
285209
+ FetchBalances: 7,
285210
+ FetchDepositAddresses: 8
285204
285211
  };
285205
285212
 
285206
285213
  // src/proto/cex_broker/SubscriptionType.ts
285207
285214
  var SubscriptionType = {
285208
- ORDERBOOK: 0,
285209
- TRADES: 1,
285210
- TICKER: 2,
285211
- OHLCV: 3,
285212
- BALANCE: 4,
285213
- ORDERS: 5
285215
+ NO_ACTION: 0,
285216
+ ORDERBOOK: 1,
285217
+ TRADES: 2,
285218
+ TICKER: 3,
285219
+ OHLCV: 4,
285220
+ BALANCE: 5,
285221
+ ORDERS: 6
285214
285222
  };
285215
285223
 
285216
285224
  // src/server.ts
@@ -285227,6 +285235,11 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285227
285235
  const server = new grpc.Server;
285228
285236
  server.addService(cexNode.cex_service.service, {
285229
285237
  ExecuteAction: async (call, callback) => {
285238
+ log.info(`Request - ExecuteAction: ${JSON.stringify({
285239
+ action: call.request.action,
285240
+ cex: call.request.cex,
285241
+ symbol: call.request.symbol
285242
+ })}`);
285230
285243
  if (!authenticateRequest(call, whitelistIps)) {
285231
285244
  return callback({
285232
285245
  code: grpc.status.PERMISSION_DENIED,
@@ -285270,7 +285283,8 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285270
285283
  if (deposit) {
285271
285284
  log.info(`Amount ${value.amount} at ${value.transactionHash} . Paid to ${value.recipientAddress}`);
285272
285285
  return callback(null, {
285273
- result: useVerity ? broker.last_proof : JSON.stringify({ ...deposit })
285286
+ proof: broker.last_proof || "",
285287
+ result: JSON.stringify({ ...deposit })
285274
285288
  });
285275
285289
  }
285276
285290
  callback({
@@ -285311,7 +285325,8 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285311
285325
  });
285312
285326
  if (depositAddresses) {
285313
285327
  return callback(null, {
285314
- result: useVerity ? broker.last_proof : JSON.stringify({ ...depositAddresses })
285328
+ proof: broker.last_proof || "",
285329
+ result: JSON.stringify({ ...depositAddresses })
285315
285330
  });
285316
285331
  }
285317
285332
  callback({
@@ -285361,7 +285376,8 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285361
285376
  const transaction = await broker.withdraw(symbol, Number(transferValue.amount), transferValue.recipientAddress, undefined, { network: transferValue.chain });
285362
285377
  log.info(`Transfer Transfer: ${JSON.stringify(transaction)}`);
285363
285378
  callback(null, {
285364
- result: useVerity ? broker.last_proof : JSON.stringify({ ...transaction })
285379
+ proof: broker.last_proof || "",
285380
+ result: JSON.stringify({ ...transaction })
285365
285381
  });
285366
285382
  } catch (error) {
285367
285383
  log.error({ error });
@@ -285481,7 +285497,8 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285481
285497
  });
285482
285498
  const currencyBalance = balance[symbol];
285483
285499
  callback(null, {
285484
- result: useVerity ? broker.last_proof : JSON.stringify({
285500
+ proof: broker.last_proof || "",
285501
+ result: JSON.stringify({
285485
285502
  balance: currencyBalance || 0,
285486
285503
  currency: symbol
285487
285504
  })
@@ -285494,6 +285511,23 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285494
285511
  }, null);
285495
285512
  }
285496
285513
  break;
285514
+ case Action.FetchBalances:
285515
+ try {
285516
+ const balance = await broker.fetchFreeBalance({
285517
+ ...call.request.payload ?? {}
285518
+ });
285519
+ callback(null, {
285520
+ proof: broker.last_proof || "",
285521
+ result: JSON.stringify(balance)
285522
+ });
285523
+ } catch (error) {
285524
+ log.error(`Error fetching balance from ${cex3}:`, error);
285525
+ callback({
285526
+ code: grpc.status.INTERNAL,
285527
+ message: `Failed to fetch balance from ${cex3}`
285528
+ }, null);
285529
+ }
285530
+ break;
285497
285531
  default:
285498
285532
  return callback({
285499
285533
  code: grpc.status.INVALID_ARGUMENT,
@@ -285514,14 +285548,20 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285514
285548
  try {
285515
285549
  const request = call.request;
285516
285550
  const { cex: cex3, symbol, type: type2, options } = request;
285517
- if (!cex3 || !symbol || type2 === undefined) {
285551
+ const subscriptionType = type2 !== undefined ? type2 : SubscriptionType.ORDERBOOK;
285552
+ log.info(`Request - Subscribe: ${JSON.stringify({
285553
+ cex: request.cex,
285554
+ symbol: request.symbol,
285555
+ type: subscriptionType
285556
+ })}`);
285557
+ if (!cex3 || !symbol) {
285518
285558
  call.write({
285519
285559
  data: JSON.stringify({
285520
285560
  error: "cex, symbol, and type are required"
285521
285561
  }),
285522
285562
  timestamp: Date.now(),
285523
285563
  symbol: symbol || "",
285524
- type: type2 || SubscriptionType.ORDERBOOK
285564
+ type: subscriptionType
285525
285565
  });
285526
285566
  call.end();
285527
285567
  return;
@@ -285534,12 +285574,12 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285534
285574
  }),
285535
285575
  timestamp: Date.now(),
285536
285576
  symbol,
285537
- type: type2
285577
+ type: subscriptionType
285538
285578
  });
285539
285579
  call.end();
285540
285580
  return;
285541
285581
  }
285542
- switch (type2) {
285582
+ switch (subscriptionType) {
285543
285583
  case SubscriptionType.ORDERBOOK:
285544
285584
  try {
285545
285585
  while (true) {
@@ -285548,7 +285588,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285548
285588
  data: JSON.stringify(orderbook),
285549
285589
  timestamp: Date.now(),
285550
285590
  symbol,
285551
- type: type2
285591
+ type: subscriptionType
285552
285592
  });
285553
285593
  }
285554
285594
  } catch (error) {
@@ -285560,7 +285600,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285560
285600
  }),
285561
285601
  timestamp: Date.now(),
285562
285602
  symbol,
285563
- type: type2
285603
+ type: subscriptionType
285564
285604
  });
285565
285605
  }
285566
285606
  break;
@@ -285572,7 +285612,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285572
285612
  data: JSON.stringify(trades),
285573
285613
  timestamp: Date.now(),
285574
285614
  symbol,
285575
- type: type2
285615
+ type: subscriptionType
285576
285616
  });
285577
285617
  }
285578
285618
  } catch (error) {
@@ -285584,7 +285624,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285584
285624
  }),
285585
285625
  timestamp: Date.now(),
285586
285626
  symbol,
285587
- type: type2
285627
+ type: subscriptionType
285588
285628
  });
285589
285629
  }
285590
285630
  break;
@@ -285608,7 +285648,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285608
285648
  }),
285609
285649
  timestamp: Date.now(),
285610
285650
  symbol,
285611
- type: type2
285651
+ type: subscriptionType
285612
285652
  });
285613
285653
  }
285614
285654
  break;
@@ -285633,7 +285673,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285633
285673
  }),
285634
285674
  timestamp: Date.now(),
285635
285675
  symbol,
285636
- type: type2
285676
+ type: subscriptionType
285637
285677
  });
285638
285678
  }
285639
285679
  break;
@@ -285657,7 +285697,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285657
285697
  }),
285658
285698
  timestamp: Date.now(),
285659
285699
  symbol,
285660
- type: type2
285700
+ type: subscriptionType
285661
285701
  });
285662
285702
  }
285663
285703
  break;
@@ -285681,7 +285721,7 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl) {
285681
285721
  }),
285682
285722
  timestamp: Date.now(),
285683
285723
  symbol,
285684
- type: type2
285724
+ type: subscriptionType
285685
285725
  });
285686
285726
  }
285687
285727
  break;
@@ -285942,6 +285982,9 @@ class CEXBroker {
285942
285982
  recvWindow: 60000
285943
285983
  }
285944
285984
  });
285985
+ if (process.env.CEX_BROKER_SANDBOX_MODE === "true") {
285986
+ client.setSandboxMode(true);
285987
+ }
285945
285988
  this.brokers[broker] = {
285946
285989
  primary: client,
285947
285990
  secondaryBrokers
@@ -286082,4 +286125,4 @@ export {
286082
286125
  CEXBroker as default
286083
286126
  };
286084
286127
 
286085
- //# debugId=18C1DCDB521AE71364756E2164756E21
286128
+ //# debugId=DE92A15E05E8EF9F64756E2164756E21