@routstr/sdk 0.3.4 → 0.3.6

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,10 +1,10 @@
1
1
  import { M as Model, S as SdkLogger, a as Message, T as TransactionHistory, m as StreamingResult } from '../types-DPQM6tIG.mjs';
2
2
  import { P as ProviderRegistry, W as WalletAdapter, S as StorageAdapter, a as StreamingCallbacks } from '../interfaces-Bp0Ngmqv.mjs';
3
- import { S as SdkStore, U as UsageTrackingDriver } from '../store-CBSyK2qg.mjs';
3
+ import { S as SdkStore, U as UsageTrackingDriver } from '../store-C4FyyOnO.mjs';
4
4
  import { CashuSpender, BalanceManager } from '../wallet/index.mjs';
5
5
  import { Transform } from 'stream';
6
6
  import 'zustand/vanilla';
7
- import '../interfaces-Cxi8R4TT.mjs';
7
+ import '../interfaces-Cqkt41QR.mjs';
8
8
 
9
9
  /**
10
10
  * ProviderManager - Handles provider selection and failover logic
@@ -1,10 +1,10 @@
1
1
  import { M as Model, S as SdkLogger, a as Message, T as TransactionHistory, m as StreamingResult } from '../types-DPQM6tIG.js';
2
2
  import { P as ProviderRegistry, W as WalletAdapter, S as StorageAdapter, a as StreamingCallbacks } from '../interfaces-D2FDCLyP.js';
3
- import { S as SdkStore, U as UsageTrackingDriver } from '../store-BD5zF9Hp.js';
3
+ import { S as SdkStore, U as UsageTrackingDriver } from '../store-BFUGGr_v.js';
4
4
  import { CashuSpender, BalanceManager } from '../wallet/index.js';
5
5
  import { Transform } from 'stream';
6
6
  import 'zustand/vanilla';
7
- import '../interfaces-CIfd_phZ.js';
7
+ import '../interfaces-D9qI1ym6.js';
8
8
 
9
9
  /**
10
10
  * ProviderManager - Handles provider selection and failover logic
@@ -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
@@ -3261,7 +3293,10 @@ var hydrateStoreFromDriver = async (store, driver) => {
3261
3293
  driver.getItem(SDK_STORAGE_KEYS.CLIENT_IDS, []),
3262
3294
  driver.getItem(SDK_STORAGE_KEYS.FAILED_PROVIDERS, []),
3263
3295
  driver.getItem(SDK_STORAGE_KEYS.LAST_FAILED, {}),
3264
- driver.getItem(SDK_STORAGE_KEYS.PROVIDERS_ON_COOLDOWN, [])
3296
+ driver.getItem(
3297
+ SDK_STORAGE_KEYS.PROVIDERS_ON_COOLDOWN,
3298
+ []
3299
+ )
3265
3300
  ]);
3266
3301
  const modelsFromAllProviders = Object.fromEntries(
3267
3302
  Object.entries(rawModels).map(([baseUrl, models]) => [
@@ -3329,7 +3364,9 @@ var hydrateStoreFromDriver = async (store, driver) => {
3329
3364
  createdAt: entry.createdAt ?? Date.now(),
3330
3365
  lastUsed: entry.lastUsed ?? null
3331
3366
  }));
3332
- const failedProviders = rawFailedProviders.map((url) => normalizeBaseUrl5(url));
3367
+ const failedProviders = rawFailedProviders.map(
3368
+ (url) => normalizeBaseUrl5(url)
3369
+ );
3333
3370
  const lastFailed = Object.fromEntries(
3334
3371
  Object.entries(rawLastFailed).map(([baseUrl, timestamp]) => [
3335
3372
  normalizeBaseUrl5(baseUrl),