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