@routstr/sdk 0.2.3 → 0.2.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.
Files changed (38) hide show
  1. package/README.md +9 -0
  2. package/dist/client/index.d.mts +21 -8
  3. package/dist/client/index.d.ts +21 -8
  4. package/dist/client/index.js +1406 -69
  5. package/dist/client/index.js.map +1 -1
  6. package/dist/client/index.mjs +1406 -70
  7. package/dist/client/index.mjs.map +1 -1
  8. package/dist/discovery/index.d.mts +2 -2
  9. package/dist/discovery/index.d.ts +2 -2
  10. package/dist/discovery/index.js +1 -4
  11. package/dist/discovery/index.js.map +1 -1
  12. package/dist/discovery/index.mjs +1 -4
  13. package/dist/discovery/index.mjs.map +1 -1
  14. package/dist/index.d.mts +15 -19
  15. package/dist/index.d.ts +15 -19
  16. package/dist/index.js +2385 -1574
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.mjs +2380 -1575
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/{interfaces-DGdP8fQp.d.mts → interfaces-BWJJTCXO.d.mts} +1 -1
  21. package/dist/{interfaces-CC0LT9p9.d.ts → interfaces-BxDEka72.d.ts} +1 -1
  22. package/dist/{interfaces-B85Wx7ni.d.mts → interfaces-C6Dr6hKy.d.mts} +1 -1
  23. package/dist/{interfaces-BVNyAmKu.d.ts → interfaces-CluftN4z.d.ts} +1 -1
  24. package/dist/storage/index.d.mts +56 -34
  25. package/dist/storage/index.d.ts +56 -34
  26. package/dist/storage/index.js +500 -51
  27. package/dist/storage/index.js.map +1 -1
  28. package/dist/storage/index.mjs +497 -52
  29. package/dist/storage/index.mjs.map +1 -1
  30. package/dist/{types-BlHjmWRK.d.mts → types-BYj_8c5c.d.mts} +3 -0
  31. package/dist/{types-BlHjmWRK.d.ts → types-BYj_8c5c.d.ts} +3 -0
  32. package/dist/wallet/index.d.mts +9 -5
  33. package/dist/wallet/index.d.ts +9 -5
  34. package/dist/wallet/index.js +54 -22
  35. package/dist/wallet/index.js.map +1 -1
  36. package/dist/wallet/index.mjs +54 -22
  37. package/dist/wallet/index.mjs.map +1 -1
  38. package/package.json +1 -1
@@ -445,7 +445,7 @@ var CashuSpender = class {
445
445
  /**
446
446
  * Refund specific providers without retrying spend
447
447
  */
448
- async refundProviders(baseUrls, mintUrl, refundApiKeys = false) {
448
+ async refundProviders(baseUrls, mintUrl, refundApiKeys = false, forceRefund) {
449
449
  const results = [];
450
450
  const pendingDistribution = this.storageAdapter.getCachedTokenDistribution();
451
451
  const toRefund = pendingDistribution.filter(
@@ -495,7 +495,8 @@ var CashuSpender = class {
495
495
  const refundResult = await this.balanceManager.refundApiKey({
496
496
  mintUrl,
497
497
  baseUrl: apiKeyEntry.baseUrl,
498
- apiKey: apiKeyEntryFull.key
498
+ apiKey: apiKeyEntryFull.key,
499
+ forceRefund
499
500
  });
500
501
  if (refundResult.success) {
501
502
  this.storageAdapter.updateApiKeyBalance(apiKeyEntry.baseUrl, 0);
@@ -668,12 +669,29 @@ var BalanceManager = class {
668
669
  }
669
670
  /**
670
671
  * Refund API key balance - convert remaining API key balance to cashu token
672
+ * @param options - Refund options including forceRefund flag
673
+ * @returns Refund result
671
674
  */
672
675
  async refundApiKey(options) {
673
- const { mintUrl, baseUrl, apiKey } = options;
676
+ const { mintUrl, baseUrl, apiKey, forceRefund } = options;
674
677
  if (!apiKey) {
675
678
  return { success: false, message: "No API key to refund" };
676
679
  }
680
+ if (!forceRefund) {
681
+ const apiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
682
+ if (apiKeyEntry?.lastUsed) {
683
+ const fiveMinutesAgo = Date.now() - 5 * 60 * 1e3;
684
+ if (apiKeyEntry.lastUsed > fiveMinutesAgo) {
685
+ console.log(
686
+ `[BalanceManager] Skipping refund for ${baseUrl} - used ${Math.round((Date.now() - apiKeyEntry.lastUsed) / 1e3)}s ago`
687
+ );
688
+ return {
689
+ success: false,
690
+ message: "API key was used recently, skipping refund"
691
+ };
692
+ }
693
+ }
694
+ }
677
695
  let fetchResult;
678
696
  try {
679
697
  fetchResult = await this._fetchRefundTokenWithApiKey(baseUrl, apiKey);
@@ -845,9 +863,13 @@ var BalanceManager = class {
845
863
  p2pkPubkey
846
864
  } = options;
847
865
  const adjustedAmount = Math.ceil(amount);
848
- console.log(`[BalanceManager.createProviderToken] Starting: baseUrl=${baseUrl}, mintUrl=${mintUrl}, amount=${amount}, adjustedAmount=${adjustedAmount}, retryCount=${retryCount}`);
866
+ console.log(
867
+ `[BalanceManager.createProviderToken] Starting: baseUrl=${baseUrl}, mintUrl=${mintUrl}, amount=${amount}, adjustedAmount=${adjustedAmount}, retryCount=${retryCount}`
868
+ );
849
869
  if (!adjustedAmount || isNaN(adjustedAmount)) {
850
- console.error(`[BalanceManager.createProviderToken] FAILURE: Invalid amount - amount=${amount}, adjustedAmount=${adjustedAmount}`);
870
+ console.error(
871
+ `[BalanceManager.createProviderToken] FAILURE: Invalid amount - amount=${amount}, adjustedAmount=${adjustedAmount}`
872
+ );
851
873
  return { success: false, error: "Invalid top up amount" };
852
874
  }
853
875
  const balanceState = await this.getBalanceState();
@@ -861,8 +883,8 @@ var BalanceManager = class {
861
883
  const refundableProviderBalance = Object.entries(
862
884
  balanceState.providerBalances
863
885
  ).filter(([providerBaseUrl]) => providerBaseUrl !== baseUrl).reduce((sum, [, value]) => sum + value, 0);
864
- if (totalMintBalance + targetProviderBalance < adjustedAmount && totalMintBalance + targetProviderBalance + refundableProviderBalance >= adjustedAmount && retryCount < 1) {
865
- await this._refundOtherProvidersForTopUp(baseUrl, mintUrl);
886
+ if (totalMintBalance + targetProviderBalance < adjustedAmount && totalMintBalance + targetProviderBalance + refundableProviderBalance >= adjustedAmount && retryCount < 2) {
887
+ await this._refundOtherProvidersForTopUp(baseUrl, mintUrl, retryCount);
866
888
  return this.createProviderToken({
867
889
  ...options,
868
890
  retryCount: retryCount + 1
@@ -878,15 +900,11 @@ var BalanceManager = class {
878
900
  { url: "", balance: 0 }
879
901
  ).url
880
902
  );
881
- console.error(`[BalanceManager.createProviderToken] FAILURE: Insufficient balance - required=${adjustedAmount}, available=${totalMintBalance + targetProviderBalance}, totalMintBalance=${totalMintBalance}, targetProviderBalance=${targetProviderBalance}, refundableProviderBalance=${refundableProviderBalance}`);
903
+ console.error(
904
+ `[BalanceManager.createProviderToken] FAILURE: Insufficient balance - required=${adjustedAmount}, available=${totalMintBalance + targetProviderBalance}, totalMintBalance=${totalMintBalance}, targetProviderBalance=${targetProviderBalance}, refundableProviderBalance=${refundableProviderBalance}`
905
+ );
882
906
  return { success: false, error: error.message };
883
907
  }
884
- if (targetProviderBalance >= adjustedAmount) {
885
- return {
886
- success: true,
887
- amountSpent: 0
888
- };
889
- }
890
908
  const providerMints = baseUrl && this.providerRegistry ? this.providerRegistry.getProviderMints(baseUrl) : [];
891
909
  let requiredAmount = adjustedAmount;
892
910
  const supportedMintsOnly = providerMints.length > 0;
@@ -920,7 +938,9 @@ var BalanceManager = class {
920
938
  maxMintUrl = mintUrl2;
921
939
  }
922
940
  }
923
- console.error(`[BalanceManager.createProviderToken] FAILURE: No candidate mints found - requiredAmount=${requiredAmount}, totalMintBalance=${totalMintBalance}, maxBalance=${maxBalance}, maxMintUrl=${maxMintUrl}, providerMints=${JSON.stringify(providerMints)}`);
941
+ console.error(
942
+ `[BalanceManager.createProviderToken] FAILURE: No candidate mints found - requiredAmount=${requiredAmount}, totalMintBalance=${totalMintBalance}, maxBalance=${maxBalance}, maxMintUrl=${maxMintUrl}, providerMints=${JSON.stringify(providerMints)}`
943
+ );
924
944
  const error = new InsufficientBalanceError(
925
945
  adjustedAmount,
926
946
  totalMintBalance,
@@ -932,13 +952,17 @@ var BalanceManager = class {
932
952
  let lastError;
933
953
  for (const candidateMint of candidates) {
934
954
  try {
935
- console.log(`[BalanceManager.createProviderToken] Attempting mint: ${candidateMint}, amount: ${requiredAmount}`);
955
+ console.log(
956
+ `[BalanceManager.createProviderToken] Attempting mint: ${candidateMint}, amount: ${requiredAmount}`
957
+ );
936
958
  const token = await this.walletAdapter.sendToken(
937
959
  candidateMint,
938
960
  requiredAmount,
939
961
  p2pkPubkey
940
962
  );
941
- console.log(`[BalanceManager.createProviderToken] SUCCESS: Token created from mint ${candidateMint}`);
963
+ console.log(
964
+ `[BalanceManager.createProviderToken] SUCCESS: Token created from mint ${candidateMint}`
965
+ );
942
966
  return {
943
967
  success: true,
944
968
  token,
@@ -947,11 +971,15 @@ var BalanceManager = class {
947
971
  };
948
972
  } catch (error) {
949
973
  const errorMsg = error instanceof Error ? error.message : String(error);
950
- console.error(`[BalanceManager.createProviderToken] FAILURE: Mint ${candidateMint} failed with error: ${errorMsg}`);
974
+ console.error(
975
+ `[BalanceManager.createProviderToken] FAILURE: Mint ${candidateMint} failed with error: ${errorMsg}`
976
+ );
951
977
  if (error instanceof Error) {
952
978
  lastError = errorMsg;
953
979
  if (isNetworkErrorMessage(error.message)) {
954
- console.warn(`[BalanceManager.createProviderToken] Network error from ${candidateMint}, trying next mint...`);
980
+ console.warn(
981
+ `[BalanceManager.createProviderToken] Network error from ${candidateMint}, trying next mint...`
982
+ );
955
983
  continue;
956
984
  }
957
985
  }
@@ -961,7 +989,9 @@ var BalanceManager = class {
961
989
  };
962
990
  }
963
991
  }
964
- console.error(`[BalanceManager.createProviderToken] FAILURE: All candidate mints exhausted - lastError=${lastError}, candidates=${JSON.stringify(candidates)}`);
992
+ console.error(
993
+ `[BalanceManager.createProviderToken] FAILURE: All candidate mints exhausted - lastError=${lastError}, candidates=${JSON.stringify(candidates)}`
994
+ );
965
995
  return {
966
996
  success: false,
967
997
  error: lastError || "All candidate mints failed while creating top up token"
@@ -1007,9 +1037,10 @@ var BalanceManager = class {
1007
1037
  }
1008
1038
  return candidates;
1009
1039
  }
1010
- async _refundOtherProvidersForTopUp(baseUrl, mintUrl) {
1040
+ async _refundOtherProvidersForTopUp(baseUrl, mintUrl, retryCount) {
1011
1041
  const pendingDistribution = this.storageAdapter.getCachedTokenDistribution();
1012
1042
  const apiKeyDistribution = this.storageAdapter.getApiKeyDistribution();
1043
+ const forceRefund = retryCount >= 2;
1013
1044
  const toRefund = pendingDistribution.filter(
1014
1045
  (pending) => pending.baseUrl !== baseUrl
1015
1046
  );
@@ -1050,7 +1081,8 @@ var BalanceManager = class {
1050
1081
  const result = await this.refundApiKey({
1051
1082
  mintUrl,
1052
1083
  baseUrl: apiKeyEntry.baseUrl,
1053
- apiKey: fullApiKeyEntry.key
1084
+ apiKey: fullApiKeyEntry.key,
1085
+ forceRefund
1054
1086
  });
1055
1087
  return { baseUrl: apiKeyEntry.baseUrl, success: result.success };
1056
1088
  })