@usherlabs/cex-broker 0.2.8 → 0.2.9

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.
@@ -313683,7 +313683,13 @@ function loadPolicy(policyPath) {
313683
313683
  const withdrawRuleEntrySchema = import_joi.default.object({
313684
313684
  exchange: import_joi.default.string().required(),
313685
313685
  network: import_joi.default.string().required(),
313686
- whitelist: import_joi.default.array().items(import_joi.default.string()).required()
313686
+ whitelist: import_joi.default.array().items(import_joi.default.string()).required(),
313687
+ coins: import_joi.default.array().items(import_joi.default.string()).optional()
313688
+ });
313689
+ const depositRuleEntrySchema = import_joi.default.object({
313690
+ exchange: import_joi.default.string().required(),
313691
+ network: import_joi.default.string().required(),
313692
+ coins: import_joi.default.array().items(import_joi.default.string()).optional()
313687
313693
  });
313688
313694
  const orderRuleSchema = import_joi.default.object({
313689
313695
  markets: import_joi.default.array().items(import_joi.default.string()).required(),
@@ -313698,7 +313704,9 @@ function loadPolicy(policyPath) {
313698
313704
  withdraw: import_joi.default.object({
313699
313705
  rule: import_joi.default.array().items(withdrawRuleEntrySchema).min(1).required()
313700
313706
  }).required(),
313701
- deposit: import_joi.default.object().pattern(import_joi.default.string(), import_joi.default.valid(null)).required(),
313707
+ deposit: import_joi.default.object({
313708
+ rule: import_joi.default.array().items(depositRuleEntrySchema).optional()
313709
+ }).required(),
313702
313710
  order: import_joi.default.object({
313703
313711
  rule: orderRuleSchema.required()
313704
313712
  }).required()
@@ -313722,9 +313730,25 @@ function normalizePolicyConfig(policy) {
313722
313730
  ...rule,
313723
313731
  exchange: rule.exchange.trim().toUpperCase(),
313724
313732
  network: rule.network.trim().toUpperCase(),
313725
- whitelist: rule.whitelist.map((address) => address.trim().toLowerCase())
313733
+ whitelist: rule.whitelist.map((address) => address.trim().toLowerCase()),
313734
+ ...rule.coins && {
313735
+ coins: rule.coins.map((c) => c.trim().toUpperCase())
313736
+ }
313726
313737
  }))
313727
313738
  },
313739
+ deposit: {
313740
+ ...policy.deposit,
313741
+ ...policy.deposit.rule && {
313742
+ rule: policy.deposit.rule.map((rule) => ({
313743
+ ...rule,
313744
+ exchange: rule.exchange.trim().toUpperCase(),
313745
+ network: rule.network.trim().toUpperCase(),
313746
+ ...rule.coins && {
313747
+ coins: rule.coins.map((c) => c.trim().toUpperCase())
313748
+ }
313749
+ }))
313750
+ }
313751
+ },
313728
313752
  order: {
313729
313753
  ...policy.order,
313730
313754
  rule: {
@@ -313751,7 +313775,24 @@ function getWithdrawRulePriority(rule, exchange, network) {
313751
313775
  }
313752
313776
  return 1;
313753
313777
  }
313754
- function validateWithdraw(policy, exchange, network, recipientAddress, _amount, _ticker) {
313778
+ function getDepositRulePriority(rule, exchange, network) {
313779
+ const exchangeMatch = rule.exchange === exchange || rule.exchange === "*";
313780
+ const networkMatch = rule.network === network || rule.network === "*";
313781
+ if (!exchangeMatch || !networkMatch) {
313782
+ return 0;
313783
+ }
313784
+ if (rule.exchange === exchange && rule.network === network) {
313785
+ return 4;
313786
+ }
313787
+ if (rule.exchange === exchange && rule.network === "*") {
313788
+ return 3;
313789
+ }
313790
+ if (rule.exchange === "*" && rule.network === network) {
313791
+ return 2;
313792
+ }
313793
+ return 1;
313794
+ }
313795
+ function validateWithdraw(policy, exchange, network, recipientAddress, _amount, ticker) {
313755
313796
  const normalizedPolicy = normalizePolicyConfig(policy);
313756
313797
  const exchangeNorm = exchange.trim().toUpperCase();
313757
313798
  const networkNorm = network.trim().toUpperCase();
@@ -313773,6 +313814,16 @@ function validateWithdraw(policy, exchange, network, recipientAddress, _amount,
313773
313814
  error: `Address ${recipientAddress} is not whitelisted for withdrawals`
313774
313815
  };
313775
313816
  }
313817
+ const coins = withdrawRule.coins;
313818
+ if (coins && coins.length > 0 && !coins.includes("*")) {
313819
+ const tickerNorm = ticker.trim().toUpperCase();
313820
+ if (!coins.includes(tickerNorm)) {
313821
+ return {
313822
+ valid: false,
313823
+ error: `Token ${tickerNorm} is not allowed for withdrawals on ${exchangeNorm}:${networkNorm}. Allowed: [${coins.join(", ")}]`
313824
+ };
313825
+ }
313826
+ }
313776
313827
  return { valid: true };
313777
313828
  }
313778
313829
  function normalizeAccountSelector(selector2, metadata, defaultSelector) {
@@ -313970,6 +314021,35 @@ async function resolveOrderExecution(policy, broker, cex3, fromToken, toToken, a
313970
314021
  matchedPatterns
313971
314022
  };
313972
314023
  }
314024
+ function validateDeposit(policy, exchange, network, ticker) {
314025
+ const normalizedPolicy = normalizePolicyConfig(policy);
314026
+ if (!normalizedPolicy.deposit.rule || normalizedPolicy.deposit.rule.length === 0) {
314027
+ return { valid: true };
314028
+ }
314029
+ const exchangeNorm = exchange.trim().toUpperCase();
314030
+ const networkNorm = network.trim().toUpperCase();
314031
+ const tickerNorm = ticker.trim().toUpperCase();
314032
+ const matchingRules = normalizedPolicy.deposit.rule.map((rule) => ({
314033
+ rule,
314034
+ priority: getDepositRulePriority(rule, exchangeNorm, networkNorm)
314035
+ })).filter((r) => r.priority > 0).sort((a, b2) => b2.priority - a.priority);
314036
+ const depositRule = matchingRules[0]?.rule;
314037
+ if (!depositRule) {
314038
+ return {
314039
+ valid: false,
314040
+ error: `Deposits not allowed for ${exchangeNorm}:${networkNorm}`
314041
+ };
314042
+ }
314043
+ if (depositRule.coins && depositRule.coins.length > 0 && !depositRule.coins.includes("*")) {
314044
+ if (!depositRule.coins.includes(tickerNorm)) {
314045
+ return {
314046
+ valid: false,
314047
+ error: `Token ${tickerNorm} not allowed for deposit on ${exchangeNorm}:${networkNorm}. Allowed: [${depositRule.coins.join(", ")}]`
314048
+ };
314049
+ }
314050
+ }
314051
+ return { valid: true };
314052
+ }
313973
314053
 
313974
314054
  // src/helpers/otel.ts
313975
314055
  var import_api_logs2 = __toESM(require_src7(), 1);
@@ -328405,6 +328485,13 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl, ot
328405
328485
  }, null);
328406
328486
  }
328407
328487
  const fetchDepositAddresses = parsedPayload.data;
328488
+ const depositValidation = validateDeposit(policy, cex3, fetchDepositAddresses.chain, symbol2);
328489
+ if (!depositValidation.valid) {
328490
+ return wrappedCallback({
328491
+ code: grpc.status.PERMISSION_DENIED,
328492
+ message: depositValidation.error
328493
+ }, null);
328494
+ }
328408
328495
  try {
328409
328496
  const depositAddresses = broker.has.fetchDepositAddress === true ? [
328410
328497
  await broker.fetchDepositAddress(symbol2, {
@@ -43,7 +43,7 @@ export declare function selectBrokerAccount(brokers: BrokerPoolEntry | undefined
43
43
  */
44
44
  export declare function loadPolicy(policyPath: string): PolicyConfig;
45
45
  export declare function normalizePolicyConfig(policy: PolicyConfig): PolicyConfig;
46
- export declare function validateWithdraw(policy: PolicyConfig, exchange: string, network: string, recipientAddress: string, _amount: number, _ticker: string): {
46
+ export declare function validateWithdraw(policy: PolicyConfig, exchange: string, network: string, recipientAddress: string, _amount: number, ticker: string): {
47
47
  valid: boolean;
48
48
  error?: string;
49
49
  };
@@ -78,10 +78,7 @@ type OrderExecutionResolution = {
78
78
  matchedPatterns?: string[];
79
79
  };
80
80
  export declare function resolveOrderExecution(policy: PolicyConfig, broker: Exchange, cex: string, fromToken: string, toToken: string, amount: number, price: number): Promise<OrderExecutionResolution>;
81
- /**
82
- * Validates deposit request (currently empty but can be extended)
83
- */
84
- export declare function validateDeposit(_policy: PolicyConfig, _chain: string, _amount: number): {
81
+ export declare function validateDeposit(policy: PolicyConfig, exchange: string, network: string, ticker: string): {
85
82
  valid: boolean;
86
83
  error?: string;
87
84
  };
package/dist/index.js CHANGED
@@ -272936,7 +272936,13 @@ function loadPolicy(policyPath) {
272936
272936
  const withdrawRuleEntrySchema = import_joi.default.object({
272937
272937
  exchange: import_joi.default.string().required(),
272938
272938
  network: import_joi.default.string().required(),
272939
- whitelist: import_joi.default.array().items(import_joi.default.string()).required()
272939
+ whitelist: import_joi.default.array().items(import_joi.default.string()).required(),
272940
+ coins: import_joi.default.array().items(import_joi.default.string()).optional()
272941
+ });
272942
+ const depositRuleEntrySchema = import_joi.default.object({
272943
+ exchange: import_joi.default.string().required(),
272944
+ network: import_joi.default.string().required(),
272945
+ coins: import_joi.default.array().items(import_joi.default.string()).optional()
272940
272946
  });
272941
272947
  const orderRuleSchema = import_joi.default.object({
272942
272948
  markets: import_joi.default.array().items(import_joi.default.string()).required(),
@@ -272951,7 +272957,9 @@ function loadPolicy(policyPath) {
272951
272957
  withdraw: import_joi.default.object({
272952
272958
  rule: import_joi.default.array().items(withdrawRuleEntrySchema).min(1).required()
272953
272959
  }).required(),
272954
- deposit: import_joi.default.object().pattern(import_joi.default.string(), import_joi.default.valid(null)).required(),
272960
+ deposit: import_joi.default.object({
272961
+ rule: import_joi.default.array().items(depositRuleEntrySchema).optional()
272962
+ }).required(),
272955
272963
  order: import_joi.default.object({
272956
272964
  rule: orderRuleSchema.required()
272957
272965
  }).required()
@@ -272975,9 +272983,25 @@ function normalizePolicyConfig(policy) {
272975
272983
  ...rule,
272976
272984
  exchange: rule.exchange.trim().toUpperCase(),
272977
272985
  network: rule.network.trim().toUpperCase(),
272978
- whitelist: rule.whitelist.map((address) => address.trim().toLowerCase())
272986
+ whitelist: rule.whitelist.map((address) => address.trim().toLowerCase()),
272987
+ ...rule.coins && {
272988
+ coins: rule.coins.map((c) => c.trim().toUpperCase())
272989
+ }
272979
272990
  }))
272980
272991
  },
272992
+ deposit: {
272993
+ ...policy.deposit,
272994
+ ...policy.deposit.rule && {
272995
+ rule: policy.deposit.rule.map((rule) => ({
272996
+ ...rule,
272997
+ exchange: rule.exchange.trim().toUpperCase(),
272998
+ network: rule.network.trim().toUpperCase(),
272999
+ ...rule.coins && {
273000
+ coins: rule.coins.map((c) => c.trim().toUpperCase())
273001
+ }
273002
+ }))
273003
+ }
273004
+ },
272981
273005
  order: {
272982
273006
  ...policy.order,
272983
273007
  rule: {
@@ -273004,7 +273028,24 @@ function getWithdrawRulePriority(rule, exchange, network) {
273004
273028
  }
273005
273029
  return 1;
273006
273030
  }
273007
- function validateWithdraw(policy, exchange, network, recipientAddress, _amount, _ticker) {
273031
+ function getDepositRulePriority(rule, exchange, network) {
273032
+ const exchangeMatch = rule.exchange === exchange || rule.exchange === "*";
273033
+ const networkMatch = rule.network === network || rule.network === "*";
273034
+ if (!exchangeMatch || !networkMatch) {
273035
+ return 0;
273036
+ }
273037
+ if (rule.exchange === exchange && rule.network === network) {
273038
+ return 4;
273039
+ }
273040
+ if (rule.exchange === exchange && rule.network === "*") {
273041
+ return 3;
273042
+ }
273043
+ if (rule.exchange === "*" && rule.network === network) {
273044
+ return 2;
273045
+ }
273046
+ return 1;
273047
+ }
273048
+ function validateWithdraw(policy, exchange, network, recipientAddress, _amount, ticker) {
273008
273049
  const normalizedPolicy = normalizePolicyConfig(policy);
273009
273050
  const exchangeNorm = exchange.trim().toUpperCase();
273010
273051
  const networkNorm = network.trim().toUpperCase();
@@ -273026,6 +273067,16 @@ function validateWithdraw(policy, exchange, network, recipientAddress, _amount,
273026
273067
  error: `Address ${recipientAddress} is not whitelisted for withdrawals`
273027
273068
  };
273028
273069
  }
273070
+ const coins = withdrawRule.coins;
273071
+ if (coins && coins.length > 0 && !coins.includes("*")) {
273072
+ const tickerNorm = ticker.trim().toUpperCase();
273073
+ if (!coins.includes(tickerNorm)) {
273074
+ return {
273075
+ valid: false,
273076
+ error: `Token ${tickerNorm} is not allowed for withdrawals on ${exchangeNorm}:${networkNorm}. Allowed: [${coins.join(", ")}]`
273077
+ };
273078
+ }
273079
+ }
273029
273080
  return { valid: true };
273030
273081
  }
273031
273082
  function normalizeAccountSelector(selector2, metadata, defaultSelector) {
@@ -273223,6 +273274,35 @@ async function resolveOrderExecution(policy, broker, cex3, fromToken, toToken, a
273223
273274
  matchedPatterns
273224
273275
  };
273225
273276
  }
273277
+ function validateDeposit(policy, exchange, network, ticker) {
273278
+ const normalizedPolicy = normalizePolicyConfig(policy);
273279
+ if (!normalizedPolicy.deposit.rule || normalizedPolicy.deposit.rule.length === 0) {
273280
+ return { valid: true };
273281
+ }
273282
+ const exchangeNorm = exchange.trim().toUpperCase();
273283
+ const networkNorm = network.trim().toUpperCase();
273284
+ const tickerNorm = ticker.trim().toUpperCase();
273285
+ const matchingRules = normalizedPolicy.deposit.rule.map((rule) => ({
273286
+ rule,
273287
+ priority: getDepositRulePriority(rule, exchangeNorm, networkNorm)
273288
+ })).filter((r) => r.priority > 0).sort((a, b2) => b2.priority - a.priority);
273289
+ const depositRule = matchingRules[0]?.rule;
273290
+ if (!depositRule) {
273291
+ return {
273292
+ valid: false,
273293
+ error: `Deposits not allowed for ${exchangeNorm}:${networkNorm}`
273294
+ };
273295
+ }
273296
+ if (depositRule.coins && depositRule.coins.length > 0 && !depositRule.coins.includes("*")) {
273297
+ if (!depositRule.coins.includes(tickerNorm)) {
273298
+ return {
273299
+ valid: false,
273300
+ error: `Token ${tickerNorm} not allowed for deposit on ${exchangeNorm}:${networkNorm}. Allowed: [${depositRule.coins.join(", ")}]`
273301
+ };
273302
+ }
273303
+ }
273304
+ return { valid: true };
273305
+ }
273226
273306
 
273227
273307
  // node_modules/@opentelemetry/api-logs/build/esm/types/LogRecord.js
273228
273308
  var SeverityNumber2;
@@ -291358,6 +291438,13 @@ function getServer(policy, brokers, whitelistIps, useVerity, verityProverUrl, ot
291358
291438
  }, null);
291359
291439
  }
291360
291440
  const fetchDepositAddresses = parsedPayload.data;
291441
+ const depositValidation = validateDeposit(policy, cex3, fetchDepositAddresses.chain, symbol2);
291442
+ if (!depositValidation.valid) {
291443
+ return wrappedCallback({
291444
+ code: grpc.status.PERMISSION_DENIED,
291445
+ message: depositValidation.error
291446
+ }, null);
291447
+ }
291361
291448
  try {
291362
291449
  const depositAddresses = broker.has.fetchDepositAddress === true ? [
291363
291450
  await broker.fetchDepositAddress(symbol2, {
@@ -292185,4 +292272,4 @@ export {
292185
292272
  CEXBroker as default
292186
292273
  };
292187
292274
 
292188
- //# debugId=317BFF4A1E45296164756E2164756E21
292275
+ //# debugId=D692BFF7F06F00CD64756E2164756E21