@routstr/sdk 0.3.4 → 0.3.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.
@@ -576,19 +576,50 @@ var CashuSpender = class {
576
576
  apiKeyEntry.baseUrl
577
577
  );
578
578
  if (apiKeyEntryFull && this.balanceManager) {
579
+ try {
580
+ const balanceResult = await this.balanceManager.getTokenBalance(
581
+ apiKeyEntryFull.key,
582
+ apiKeyEntry.baseUrl
583
+ );
584
+ if (balanceResult.isInvalidApiKey) {
585
+ this.storageAdapter.removeApiKey(apiKeyEntry.baseUrl);
586
+ results.push({
587
+ baseUrl: apiKeyEntry.baseUrl,
588
+ success: true
589
+ });
590
+ continue;
591
+ }
592
+ if (balanceResult.amount >= 0) {
593
+ const balanceSat = balanceResult.unit === "msat" ? Math.floor(balanceResult.amount / 1e3) : balanceResult.amount;
594
+ this.storageAdapter.updateApiKeyBalance(
595
+ apiKeyEntry.baseUrl,
596
+ balanceSat
597
+ );
598
+ }
599
+ } catch {
600
+ }
601
+ const refreshedEntry = this.storageAdapter.getApiKey(
602
+ apiKeyEntry.baseUrl
603
+ );
604
+ if (!refreshedEntry) continue;
579
605
  const refundResult = await this.balanceManager.refundApiKey({
580
606
  mintUrl,
581
607
  baseUrl: apiKeyEntry.baseUrl,
582
- apiKey: apiKeyEntryFull.key,
608
+ apiKey: refreshedEntry.key,
583
609
  forceRefund
584
610
  });
585
611
  if (refundResult.success) {
586
612
  this.storageAdapter.removeApiKey(apiKeyEntry.baseUrl);
587
613
  } else {
588
- this.storageAdapter.updateApiKeyBalance(
589
- apiKeyEntry.baseUrl,
590
- apiKeyEntry.amount
614
+ const currentEntry = this.storageAdapter.getApiKey(
615
+ apiKeyEntry.baseUrl
591
616
  );
617
+ if (currentEntry) {
618
+ this.storageAdapter.updateApiKeyBalance(
619
+ apiKeyEntry.baseUrl,
620
+ currentEntry.balance
621
+ );
622
+ }
592
623
  }
593
624
  results.push({
594
625
  baseUrl: apiKeyEntry.baseUrl,
@@ -797,7 +828,8 @@ var BalanceManager = class _BalanceManager {
797
828
  };
798
829
  }
799
830
  if (fetchResult.error === "No balance to refund") {
800
- return { success: false, message: "No balance to refund" };
831
+ this.storageAdapter.removeApiKey(baseUrl);
832
+ return { success: true, message: "No balance to refund, key cleaned up" };
801
833
  }
802
834
  const receiveResult = await this.cashuSpender.receiveToken(
803
835
  fetchResult.token