@routstr/sdk 0.1.8 → 0.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.
@@ -1,8 +1,8 @@
1
1
  // core/errors.ts
2
2
  var InsufficientBalanceError = class extends Error {
3
- constructor(required, available, maxMintBalance = 0, maxMintUrl = "") {
3
+ constructor(required, available, maxMintBalance = 0, maxMintUrl = "", customMessage) {
4
4
  super(
5
- `Insufficient balance: need ${required} sats, have ${available} sats available. ` + (maxMintBalance > 0 ? `Largest mint balance: ${maxMintBalance} sats from ${maxMintUrl}` : "")
5
+ customMessage ?? `Insufficient balance: need ${required} sats, have ${available} sats available. ` + (maxMintBalance > 0 ? `Largest mint balance: ${maxMintBalance} sats from ${maxMintUrl}` : "")
6
6
  );
7
7
  this.required = required;
8
8
  this.available = available;
@@ -827,6 +827,10 @@ var BalanceManager = class {
827
827
  requestId
828
828
  };
829
829
  } catch (error) {
830
+ console.log(
831
+ "DEBUG",
832
+ `[TopuPU] topup: Topup result for ${baseUrl}: error=${error}`
833
+ );
830
834
  if (cashuToken) {
831
835
  await this._recoverFailedTopUp(cashuToken);
832
836
  }
@@ -853,16 +857,35 @@ var BalanceManager = class {
853
857
  (sum, value) => sum + value,
854
858
  0
855
859
  );
860
+ const targetProviderBalance = balanceState.providerBalances[baseUrl] || 0;
856
861
  const refundableProviderBalance = Object.entries(
857
862
  balanceState.providerBalances
858
863
  ).filter(([providerBaseUrl]) => providerBaseUrl !== baseUrl).reduce((sum, [, value]) => sum + value, 0);
859
- if (totalMintBalance < adjustedAmount && totalMintBalance + refundableProviderBalance >= adjustedAmount && retryCount < 1) {
864
+ if (totalMintBalance + targetProviderBalance < adjustedAmount && totalMintBalance + targetProviderBalance + refundableProviderBalance >= adjustedAmount && retryCount < 1) {
860
865
  await this._refundOtherProvidersForTopUp(baseUrl, mintUrl);
861
866
  return this.createProviderToken({
862
867
  ...options,
863
868
  retryCount: retryCount + 1
864
869
  });
865
870
  }
871
+ if (totalMintBalance + targetProviderBalance < adjustedAmount) {
872
+ const error = new InsufficientBalanceError(
873
+ adjustedAmount,
874
+ totalMintBalance + targetProviderBalance,
875
+ totalMintBalance,
876
+ Object.entries(balanceState.mintBalances).reduce(
877
+ (max, [url, balance]) => balance > max.balance ? { url, balance } : max,
878
+ { url: "", balance: 0 }
879
+ ).url
880
+ );
881
+ return { success: false, error: error.message };
882
+ }
883
+ if (targetProviderBalance >= adjustedAmount) {
884
+ return {
885
+ success: true,
886
+ amountSpent: 0
887
+ };
888
+ }
866
889
  const providerMints = baseUrl && this.providerRegistry ? this.providerRegistry.getProviderMints(baseUrl) : [];
867
890
  let requiredAmount = adjustedAmount;
868
891
  const supportedMintsOnly = providerMints.length > 0;
@@ -2268,7 +2291,7 @@ var RoutstrClient = class {
2268
2291
  "DEBUG",
2269
2292
  `[RoutstrClient] _handleErrorResponse: Insufficient balance, need=${required}, have=${available}`
2270
2293
  );
2271
- throw new InsufficientBalanceError(required, available);
2294
+ throw new InsufficientBalanceError(required, available, 0, "", message);
2272
2295
  } else {
2273
2296
  this._log(
2274
2297
  "DEBUG",