@bsv/wallet-toolbox-client 2.6.4 → 2.6.5

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.
@@ -9986,16 +9986,15 @@ async function createActionCore(storage, auth, vargs, parent) {
9986
9986
  });
9987
9987
  const feeModel = validateStorageFeeModel(storage.feeModel);
9988
9988
  logger?.log(`validated fee model ${JSON.stringify(feeModel)}`);
9989
- const initialFundingPlan = await prepareFundingPlan(storage, [
9989
+ const initialFundingPlan = await prepareFundingPlanWithSendingFallback(storage, [
9990
9990
  userId,
9991
9991
  vargs,
9992
9992
  xinputs,
9993
9993
  xoutputs,
9994
9994
  changeBasket,
9995
9995
  noSendChangeIn,
9996
- feeModel,
9997
- parent
9998
- ]);
9996
+ feeModel
9997
+ ], parent);
9999
9998
  logger?.log(`planned funding from ${initialFundingPlan.availableChangeCount} change inputs`);
10000
9999
  const allocatedBeefPrefetch = startAllocatedChangeBeefPrefetch(storage, vargs, initialFundingPlan.selected, beef, parent);
10001
10000
  const storageBeefBytes = storageBeef.toBinary();
@@ -10595,8 +10594,7 @@ function makeFundingParams(vargs, xinputs, xoutputs, changeBasket, feeModel, ava
10595
10594
  };
10596
10595
  }
10597
10596
  async function prepareFundingPlan(storage, context) {
10598
- const [userId, vargs, xinputs, xoutputs, changeBasket, noSendChangeIn, feeModel, parent, trx] = context;
10599
- const excludeSending = !vargs.isDelayed;
10597
+ const [userId, vargs, xinputs, xoutputs, changeBasket, noSendChangeIn, feeModel, excludeSending, parent, trx] = context;
10600
10598
  const candidates = await traceStorageStep(storage, "wallet.storage.create_action.funding_candidates", parent, { "funding.exclude_sending": excludeSending }, async (span) => {
10601
10599
  const outputs = await storage.findAvailableManagedChangeInputCandidates(userId, changeBasket.basketId, excludeSending, trx);
10602
10600
  span?.end({ attributes: {
@@ -10645,10 +10643,30 @@ async function prepareFundingPlan(storage, context) {
10645
10643
  params,
10646
10644
  result,
10647
10645
  selected,
10648
- availableChangeCount: candidates.length
10646
+ availableChangeCount: candidates.length,
10647
+ excludeSending
10649
10648
  };
10650
10649
  });
10651
10650
  }
10651
+ async function prepareFundingPlanWithSendingFallback(storage, context, parent, trx) {
10652
+ const excludeSending = !context[1].isDelayed;
10653
+ try {
10654
+ return await prepareFundingPlan(storage, [
10655
+ ...context,
10656
+ excludeSending,
10657
+ parent,
10658
+ trx
10659
+ ]);
10660
+ } catch (error) {
10661
+ if (!excludeSending || !(error instanceof WERR_INSUFFICIENT_FUNDS)) throw error;
10662
+ return await prepareFundingPlan(storage, [
10663
+ ...context,
10664
+ false,
10665
+ parent,
10666
+ trx
10667
+ ]);
10668
+ }
10669
+ }
10652
10670
  async function claimFundingPlan(storage, request) {
10653
10671
  const [userId, basketId, excludeSending, transactionId, noSendChangeIn, plan, trx] = request;
10654
10672
  if (plan.selected.length === 0) return {
@@ -10731,7 +10749,7 @@ async function fundNewTransactionSdk(storage, userId, vargs, ctx, initialPlan, p
10731
10749
  const claim = await claimFundingPlan(storage, [
10732
10750
  userId,
10733
10751
  ctx.changeBasket.basketId,
10734
- !vargs.isDelayed,
10752
+ plan.excludeSending,
10735
10753
  ctx.transactionId,
10736
10754
  ctx.noSendChangeIn,
10737
10755
  plan,
@@ -10749,17 +10767,15 @@ async function fundNewTransactionSdk(storage, userId, vargs, ctx, initialPlan, p
10749
10767
  }
10750
10768
  if (claim.conflict === "noSendChange") throw new WERR_INVALID_PARAMETER("noSendChange", "outputs that remain spendable during action planning");
10751
10769
  retryCount++;
10752
- plan = await prepareFundingPlan(storage, [
10770
+ plan = await prepareFundingPlanWithSendingFallback(storage, [
10753
10771
  userId,
10754
10772
  vargs,
10755
10773
  ctx.xinputs,
10756
10774
  ctx.xoutputs,
10757
10775
  ctx.changeBasket,
10758
10776
  ctx.noSendChangeIn,
10759
- ctx.feeModel,
10760
- parent,
10761
- trx
10762
- ]);
10777
+ ctx.feeModel
10778
+ ], parent, trx);
10763
10779
  }
10764
10780
  throw new WERR_INVALID_OPERATION("wallet funding changed repeatedly during action planning; retry createAction");
10765
10781
  });