@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
@@ -447,7 +447,7 @@ var CashuSpender = class {
447
447
  /**
448
448
  * Refund specific providers without retrying spend
449
449
  */
450
- async refundProviders(baseUrls, mintUrl, refundApiKeys = false) {
450
+ async refundProviders(baseUrls, mintUrl, refundApiKeys = false, forceRefund) {
451
451
  const results = [];
452
452
  const pendingDistribution = this.storageAdapter.getCachedTokenDistribution();
453
453
  const toRefund = pendingDistribution.filter(
@@ -497,7 +497,8 @@ var CashuSpender = class {
497
497
  const refundResult = await this.balanceManager.refundApiKey({
498
498
  mintUrl,
499
499
  baseUrl: apiKeyEntry.baseUrl,
500
- apiKey: apiKeyEntryFull.key
500
+ apiKey: apiKeyEntryFull.key,
501
+ forceRefund
501
502
  });
502
503
  if (refundResult.success) {
503
504
  this.storageAdapter.updateApiKeyBalance(apiKeyEntry.baseUrl, 0);
@@ -670,12 +671,29 @@ var BalanceManager = class {
670
671
  }
671
672
  /**
672
673
  * Refund API key balance - convert remaining API key balance to cashu token
674
+ * @param options - Refund options including forceRefund flag
675
+ * @returns Refund result
673
676
  */
674
677
  async refundApiKey(options) {
675
- const { mintUrl, baseUrl, apiKey } = options;
678
+ const { mintUrl, baseUrl, apiKey, forceRefund } = options;
676
679
  if (!apiKey) {
677
680
  return { success: false, message: "No API key to refund" };
678
681
  }
682
+ if (!forceRefund) {
683
+ const apiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
684
+ if (apiKeyEntry?.lastUsed) {
685
+ const fiveMinutesAgo = Date.now() - 5 * 60 * 1e3;
686
+ if (apiKeyEntry.lastUsed > fiveMinutesAgo) {
687
+ console.log(
688
+ `[BalanceManager] Skipping refund for ${baseUrl} - used ${Math.round((Date.now() - apiKeyEntry.lastUsed) / 1e3)}s ago`
689
+ );
690
+ return {
691
+ success: false,
692
+ message: "API key was used recently, skipping refund"
693
+ };
694
+ }
695
+ }
696
+ }
679
697
  let fetchResult;
680
698
  try {
681
699
  fetchResult = await this._fetchRefundTokenWithApiKey(baseUrl, apiKey);
@@ -847,9 +865,13 @@ var BalanceManager = class {
847
865
  p2pkPubkey
848
866
  } = options;
849
867
  const adjustedAmount = Math.ceil(amount);
850
- console.log(`[BalanceManager.createProviderToken] Starting: baseUrl=${baseUrl}, mintUrl=${mintUrl}, amount=${amount}, adjustedAmount=${adjustedAmount}, retryCount=${retryCount}`);
868
+ console.log(
869
+ `[BalanceManager.createProviderToken] Starting: baseUrl=${baseUrl}, mintUrl=${mintUrl}, amount=${amount}, adjustedAmount=${adjustedAmount}, retryCount=${retryCount}`
870
+ );
851
871
  if (!adjustedAmount || isNaN(adjustedAmount)) {
852
- console.error(`[BalanceManager.createProviderToken] FAILURE: Invalid amount - amount=${amount}, adjustedAmount=${adjustedAmount}`);
872
+ console.error(
873
+ `[BalanceManager.createProviderToken] FAILURE: Invalid amount - amount=${amount}, adjustedAmount=${adjustedAmount}`
874
+ );
853
875
  return { success: false, error: "Invalid top up amount" };
854
876
  }
855
877
  const balanceState = await this.getBalanceState();
@@ -863,8 +885,8 @@ var BalanceManager = class {
863
885
  const refundableProviderBalance = Object.entries(
864
886
  balanceState.providerBalances
865
887
  ).filter(([providerBaseUrl]) => providerBaseUrl !== baseUrl).reduce((sum, [, value]) => sum + value, 0);
866
- if (totalMintBalance + targetProviderBalance < adjustedAmount && totalMintBalance + targetProviderBalance + refundableProviderBalance >= adjustedAmount && retryCount < 1) {
867
- await this._refundOtherProvidersForTopUp(baseUrl, mintUrl);
888
+ if (totalMintBalance + targetProviderBalance < adjustedAmount && totalMintBalance + targetProviderBalance + refundableProviderBalance >= adjustedAmount && retryCount < 2) {
889
+ await this._refundOtherProvidersForTopUp(baseUrl, mintUrl, retryCount);
868
890
  return this.createProviderToken({
869
891
  ...options,
870
892
  retryCount: retryCount + 1
@@ -880,15 +902,11 @@ var BalanceManager = class {
880
902
  { url: "", balance: 0 }
881
903
  ).url
882
904
  );
883
- console.error(`[BalanceManager.createProviderToken] FAILURE: Insufficient balance - required=${adjustedAmount}, available=${totalMintBalance + targetProviderBalance}, totalMintBalance=${totalMintBalance}, targetProviderBalance=${targetProviderBalance}, refundableProviderBalance=${refundableProviderBalance}`);
905
+ console.error(
906
+ `[BalanceManager.createProviderToken] FAILURE: Insufficient balance - required=${adjustedAmount}, available=${totalMintBalance + targetProviderBalance}, totalMintBalance=${totalMintBalance}, targetProviderBalance=${targetProviderBalance}, refundableProviderBalance=${refundableProviderBalance}`
907
+ );
884
908
  return { success: false, error: error.message };
885
909
  }
886
- if (targetProviderBalance >= adjustedAmount) {
887
- return {
888
- success: true,
889
- amountSpent: 0
890
- };
891
- }
892
910
  const providerMints = baseUrl && this.providerRegistry ? this.providerRegistry.getProviderMints(baseUrl) : [];
893
911
  let requiredAmount = adjustedAmount;
894
912
  const supportedMintsOnly = providerMints.length > 0;
@@ -922,7 +940,9 @@ var BalanceManager = class {
922
940
  maxMintUrl = mintUrl2;
923
941
  }
924
942
  }
925
- console.error(`[BalanceManager.createProviderToken] FAILURE: No candidate mints found - requiredAmount=${requiredAmount}, totalMintBalance=${totalMintBalance}, maxBalance=${maxBalance}, maxMintUrl=${maxMintUrl}, providerMints=${JSON.stringify(providerMints)}`);
943
+ console.error(
944
+ `[BalanceManager.createProviderToken] FAILURE: No candidate mints found - requiredAmount=${requiredAmount}, totalMintBalance=${totalMintBalance}, maxBalance=${maxBalance}, maxMintUrl=${maxMintUrl}, providerMints=${JSON.stringify(providerMints)}`
945
+ );
926
946
  const error = new InsufficientBalanceError(
927
947
  adjustedAmount,
928
948
  totalMintBalance,
@@ -934,13 +954,17 @@ var BalanceManager = class {
934
954
  let lastError;
935
955
  for (const candidateMint of candidates) {
936
956
  try {
937
- console.log(`[BalanceManager.createProviderToken] Attempting mint: ${candidateMint}, amount: ${requiredAmount}`);
957
+ console.log(
958
+ `[BalanceManager.createProviderToken] Attempting mint: ${candidateMint}, amount: ${requiredAmount}`
959
+ );
938
960
  const token = await this.walletAdapter.sendToken(
939
961
  candidateMint,
940
962
  requiredAmount,
941
963
  p2pkPubkey
942
964
  );
943
- console.log(`[BalanceManager.createProviderToken] SUCCESS: Token created from mint ${candidateMint}`);
965
+ console.log(
966
+ `[BalanceManager.createProviderToken] SUCCESS: Token created from mint ${candidateMint}`
967
+ );
944
968
  return {
945
969
  success: true,
946
970
  token,
@@ -949,11 +973,15 @@ var BalanceManager = class {
949
973
  };
950
974
  } catch (error) {
951
975
  const errorMsg = error instanceof Error ? error.message : String(error);
952
- console.error(`[BalanceManager.createProviderToken] FAILURE: Mint ${candidateMint} failed with error: ${errorMsg}`);
976
+ console.error(
977
+ `[BalanceManager.createProviderToken] FAILURE: Mint ${candidateMint} failed with error: ${errorMsg}`
978
+ );
953
979
  if (error instanceof Error) {
954
980
  lastError = errorMsg;
955
981
  if (isNetworkErrorMessage(error.message)) {
956
- console.warn(`[BalanceManager.createProviderToken] Network error from ${candidateMint}, trying next mint...`);
982
+ console.warn(
983
+ `[BalanceManager.createProviderToken] Network error from ${candidateMint}, trying next mint...`
984
+ );
957
985
  continue;
958
986
  }
959
987
  }
@@ -963,7 +991,9 @@ var BalanceManager = class {
963
991
  };
964
992
  }
965
993
  }
966
- console.error(`[BalanceManager.createProviderToken] FAILURE: All candidate mints exhausted - lastError=${lastError}, candidates=${JSON.stringify(candidates)}`);
994
+ console.error(
995
+ `[BalanceManager.createProviderToken] FAILURE: All candidate mints exhausted - lastError=${lastError}, candidates=${JSON.stringify(candidates)}`
996
+ );
967
997
  return {
968
998
  success: false,
969
999
  error: lastError || "All candidate mints failed while creating top up token"
@@ -1009,9 +1039,10 @@ var BalanceManager = class {
1009
1039
  }
1010
1040
  return candidates;
1011
1041
  }
1012
- async _refundOtherProvidersForTopUp(baseUrl, mintUrl) {
1042
+ async _refundOtherProvidersForTopUp(baseUrl, mintUrl, retryCount) {
1013
1043
  const pendingDistribution = this.storageAdapter.getCachedTokenDistribution();
1014
1044
  const apiKeyDistribution = this.storageAdapter.getApiKeyDistribution();
1045
+ const forceRefund = retryCount >= 2;
1015
1046
  const toRefund = pendingDistribution.filter(
1016
1047
  (pending) => pending.baseUrl !== baseUrl
1017
1048
  );
@@ -1052,7 +1083,8 @@ var BalanceManager = class {
1052
1083
  const result = await this.refundApiKey({
1053
1084
  mintUrl,
1054
1085
  baseUrl: apiKeyEntry.baseUrl,
1055
- apiKey: fullApiKeyEntry.key
1086
+ apiKey: fullApiKeyEntry.key,
1087
+ forceRefund
1056
1088
  });
1057
1089
  return { baseUrl: apiKeyEntry.baseUrl, success: result.success };
1058
1090
  })