@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.mjs CHANGED
@@ -5397,7 +5397,8 @@ var OrderRoutingError = class extends SdkError {
5397
5397
  // src/orders/internal/routing/validation.ts
5398
5398
  import { z as z6 } from "zod";
5399
5399
  var ZodCircleScoreStateSchema = z6.object({
5400
- activeMerchantsCount: z6.coerce.number()
5400
+ activeMerchantsCount: z6.coerce.number(),
5401
+ availableMerchantsCount: z6.coerce.number()
5401
5402
  });
5402
5403
  var ZodCircleMetricsForRoutingSchema = z6.object({
5403
5404
  circleScore: z6.coerce.number(),
@@ -5979,8 +5980,9 @@ import { errAsync as errAsync3, okAsync as okAsync3 } from "neverthrow";
5979
5980
  var EPSILON = 0.25;
5980
5981
  var RECOVERY_SCALE = 0.3;
5981
5982
  var BOOTSTRAP_MAX_WEIGHT = 25;
5983
+ var BOOTSTRAP_RESERVE = 0.1;
5982
5984
  var MAX_VALIDATION_ATTEMPTS = 3;
5983
- function circleWeight(c) {
5985
+ function baseScore(c) {
5984
5986
  const score = c.metrics.circleScore;
5985
5987
  if (c.metrics.circleStatus === "paused") {
5986
5988
  return score * RECOVERY_SCALE;
@@ -5990,8 +5992,13 @@ function circleWeight(c) {
5990
5992
  }
5991
5993
  return score;
5992
5994
  }
5995
+ function circleWeight(c) {
5996
+ return baseScore(c) * c.metrics.scoreState.availableMerchantsCount;
5997
+ }
5993
5998
  function filterEligibleCircles(circles, orderCurrency) {
5994
- return circles.filter((c) => c.currency.toLowerCase() === orderCurrency.toLowerCase());
5999
+ return circles.filter(
6000
+ (c) => c.currency.toLowerCase() === orderCurrency.toLowerCase() && c.metrics.scoreState.availableMerchantsCount > 0
6001
+ );
5995
6002
  }
5996
6003
  function weightedRandomChoice(arr, weights) {
5997
6004
  const totalWeight = weights.reduce((sum, w) => sum + w, 0);
@@ -6011,8 +6018,14 @@ function selectCircle(eligible) {
6011
6018
  if (eligible.length === 0) {
6012
6019
  return null;
6013
6020
  }
6021
+ const bootstrapCircles = eligible.filter((c) => c.metrics.circleStatus === "bootstrap");
6014
6022
  const activeCircles = eligible.filter((c) => c.metrics.circleStatus === "active");
6015
- const isExplore = Math.random() < EPSILON;
6023
+ const r = Math.random();
6024
+ if (r < BOOTSTRAP_RESERVE && bootstrapCircles.length > 0) {
6025
+ const weights2 = bootstrapCircles.map(circleWeight);
6026
+ return weightedRandomChoice(bootstrapCircles, weights2);
6027
+ }
6028
+ const isExplore = r < BOOTSTRAP_RESERVE + EPSILON;
6016
6029
  if (isExplore) {
6017
6030
  const weights2 = eligible.map(circleWeight);
6018
6031
  return weightedRandomChoice(eligible, weights2);
@@ -6021,7 +6034,7 @@ function selectCircle(eligible) {
6021
6034
  const weights2 = eligible.map(circleWeight);
6022
6035
  return weightedRandomChoice(eligible, weights2);
6023
6036
  }
6024
- const weights = activeCircles.map((c) => c.metrics.circleScore);
6037
+ const weights = activeCircles.map(circleWeight);
6025
6038
  return weightedRandomChoice(activeCircles, weights);
6026
6039
  }
6027
6040
  function selectCircleForOrderAsync(circles, orderCurrency, validateCircle, logger = noopLogger) {
@@ -6106,6 +6119,7 @@ var CIRCLES_FOR_ROUTING_QUERY = (
6106
6119
  circleStatus
6107
6120
  scoreState {
6108
6121
  activeMerchantsCount
6122
+ availableMerchantsCount
6109
6123
  }
6110
6124
  }
6111
6125
  }
@@ -6132,11 +6146,11 @@ function getCirclesForRouting(subgraphUrl, currency, logger = noopLogger) {
6132
6146
  (message, cause, d) => new OrderRoutingError(message, { code: "VALIDATION_ERROR", cause, context: { data: d } })
6133
6147
  ).map((validated) => {
6134
6148
  const circles = validated.circles.filter(
6135
- (item) => Number(item.metrics.scoreState.activeMerchantsCount) > 0
6149
+ (item) => Number(item.metrics.scoreState.availableMerchantsCount) > 0
6136
6150
  );
6137
6151
  logger.info("fetched circles from subgraph", {
6138
6152
  total: validated.circles.length,
6139
- withActiveMerchants: circles.length,
6153
+ withAvailableMerchants: circles.length,
6140
6154
  circles
6141
6155
  });
6142
6156
  return circles;