@p2pdotme/sdk 1.2.0 → 1.2.1

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.
package/dist/react.cjs CHANGED
@@ -5442,7 +5442,8 @@ var OrderRoutingError = class extends SdkError {
5442
5442
  // src/orders/internal/routing/validation.ts
5443
5443
  var import_zod6 = require("zod");
5444
5444
  var ZodCircleScoreStateSchema = import_zod6.z.object({
5445
- activeMerchantsCount: import_zod6.z.coerce.number()
5445
+ activeMerchantsCount: import_zod6.z.coerce.number(),
5446
+ availableMerchantsCount: import_zod6.z.coerce.number()
5446
5447
  });
5447
5448
  var ZodCircleMetricsForRoutingSchema = import_zod6.z.object({
5448
5449
  circleScore: import_zod6.z.coerce.number(),
@@ -6024,8 +6025,9 @@ var import_neverthrow15 = require("neverthrow");
6024
6025
  var EPSILON = 0.25;
6025
6026
  var RECOVERY_SCALE = 0.3;
6026
6027
  var BOOTSTRAP_MAX_WEIGHT = 25;
6028
+ var BOOTSTRAP_RESERVE = 0.1;
6027
6029
  var MAX_VALIDATION_ATTEMPTS = 3;
6028
- function circleWeight(c) {
6030
+ function baseScore(c) {
6029
6031
  const score = c.metrics.circleScore;
6030
6032
  if (c.metrics.circleStatus === "paused") {
6031
6033
  return score * RECOVERY_SCALE;
@@ -6035,8 +6037,13 @@ function circleWeight(c) {
6035
6037
  }
6036
6038
  return score;
6037
6039
  }
6040
+ function circleWeight(c) {
6041
+ return baseScore(c) * c.metrics.scoreState.availableMerchantsCount;
6042
+ }
6038
6043
  function filterEligibleCircles(circles, orderCurrency) {
6039
- return circles.filter((c) => c.currency.toLowerCase() === orderCurrency.toLowerCase());
6044
+ return circles.filter(
6045
+ (c) => c.currency.toLowerCase() === orderCurrency.toLowerCase() && c.metrics.scoreState.availableMerchantsCount > 0
6046
+ );
6040
6047
  }
6041
6048
  function weightedRandomChoice(arr, weights) {
6042
6049
  const totalWeight = weights.reduce((sum, w) => sum + w, 0);
@@ -6056,8 +6063,14 @@ function selectCircle(eligible) {
6056
6063
  if (eligible.length === 0) {
6057
6064
  return null;
6058
6065
  }
6066
+ const bootstrapCircles = eligible.filter((c) => c.metrics.circleStatus === "bootstrap");
6059
6067
  const activeCircles = eligible.filter((c) => c.metrics.circleStatus === "active");
6060
- const isExplore = Math.random() < EPSILON;
6068
+ const r = Math.random();
6069
+ if (r < BOOTSTRAP_RESERVE && bootstrapCircles.length > 0) {
6070
+ const weights2 = bootstrapCircles.map(circleWeight);
6071
+ return weightedRandomChoice(bootstrapCircles, weights2);
6072
+ }
6073
+ const isExplore = r < BOOTSTRAP_RESERVE + EPSILON;
6061
6074
  if (isExplore) {
6062
6075
  const weights2 = eligible.map(circleWeight);
6063
6076
  return weightedRandomChoice(eligible, weights2);
@@ -6066,7 +6079,7 @@ function selectCircle(eligible) {
6066
6079
  const weights2 = eligible.map(circleWeight);
6067
6080
  return weightedRandomChoice(eligible, weights2);
6068
6081
  }
6069
- const weights = activeCircles.map((c) => c.metrics.circleScore);
6082
+ const weights = activeCircles.map(circleWeight);
6070
6083
  return weightedRandomChoice(activeCircles, weights);
6071
6084
  }
6072
6085
  function selectCircleForOrderAsync(circles, orderCurrency, validateCircle, logger = noopLogger) {
@@ -6151,6 +6164,7 @@ var CIRCLES_FOR_ROUTING_QUERY = (
6151
6164
  circleStatus
6152
6165
  scoreState {
6153
6166
  activeMerchantsCount
6167
+ availableMerchantsCount
6154
6168
  }
6155
6169
  }
6156
6170
  }
@@ -6177,11 +6191,11 @@ function getCirclesForRouting(subgraphUrl, currency, logger = noopLogger) {
6177
6191
  (message, cause, d) => new OrderRoutingError(message, { code: "VALIDATION_ERROR", cause, context: { data: d } })
6178
6192
  ).map((validated) => {
6179
6193
  const circles = validated.circles.filter(
6180
- (item) => Number(item.metrics.scoreState.activeMerchantsCount) > 0
6194
+ (item) => Number(item.metrics.scoreState.availableMerchantsCount) > 0
6181
6195
  );
6182
6196
  logger.info("fetched circles from subgraph", {
6183
6197
  total: validated.circles.length,
6184
- withActiveMerchants: circles.length,
6198
+ withAvailableMerchants: circles.length,
6185
6199
  circles
6186
6200
  });
6187
6201
  return circles;