@routstr/sdk 0.2.7 → 0.2.9

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.
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { RelayPool, onlyEvents } from 'applesauce-relay';
2
2
  import { EventStore } from 'applesauce-core';
3
3
  import { tap } from 'rxjs';
4
+ import { getDecodedToken } from '@cashu/cashu-ts';
4
5
  import { createStore } from 'zustand/vanilla';
5
6
  import { Transform, Readable } from 'stream';
6
7
 
@@ -164,7 +165,7 @@ var ModelManager = class _ModelManager {
164
165
  const DEFAULT_RELAYS = [
165
166
  "wss://relay.primal.net",
166
167
  "wss://nos.lol",
167
- "wss://relay.routstr.com"
168
+ "wss://relay.damus.io"
168
169
  ];
169
170
  const pool = new RelayPool();
170
171
  const localEventStore = new EventStore();
@@ -733,8 +734,6 @@ function selectMintWithBalance(balances, units, amount, excludeMints = []) {
733
734
  }
734
735
  return { selectedMintUrl: null, selectedMintBalance: 0 };
735
736
  }
736
-
737
- // wallet/CashuSpender.ts
738
737
  var CashuSpender = class {
739
738
  constructor(walletAdapter, storageAdapter, _providerRegistry, balanceManager) {
740
739
  this.walletAdapter = walletAdapter;
@@ -745,23 +744,43 @@ var CashuSpender = class {
745
744
  _isBusy = false;
746
745
  debugLevel = "WARN";
747
746
  async receiveToken(token) {
748
- const result = await this.walletAdapter.receiveToken(token);
749
- if (!result.success && result.message?.includes("Failed to fetch mint")) {
750
- const cachedTokens = this.storageAdapter.getCachedReceiveTokens();
751
- const existingIndex = cachedTokens.findIndex((t) => t.token === token);
752
- if (existingIndex === -1) {
753
- this.storageAdapter.setCachedReceiveTokens([
754
- ...cachedTokens,
755
- {
756
- token,
757
- amount: result.amount,
758
- unit: result.unit,
759
- createdAt: Date.now()
760
- }
761
- ]);
747
+ try {
748
+ const result = await this.walletAdapter.receiveToken(token);
749
+ return result;
750
+ } catch (error) {
751
+ const errorMessage = error instanceof Error ? error.message : String(error);
752
+ if (errorMessage.includes("Failed to fetch mint")) {
753
+ const cachedTokens = this.storageAdapter.getCachedReceiveTokens();
754
+ const existingIndex = cachedTokens.findIndex((t) => t.token === token);
755
+ if (existingIndex === -1) {
756
+ const { amount: amount2, unit: unit2 } = this._decodeTokenAmount(token);
757
+ this.storageAdapter.setCachedReceiveTokens([
758
+ ...cachedTokens,
759
+ {
760
+ token,
761
+ amount: amount2,
762
+ unit: unit2,
763
+ createdAt: Date.now()
764
+ }
765
+ ]);
766
+ }
762
767
  }
768
+ const { amount, unit } = this._decodeTokenAmount(token);
769
+ return { success: false, amount, unit, message: errorMessage };
770
+ }
771
+ }
772
+ _decodeTokenAmount(token) {
773
+ try {
774
+ const decoded = getDecodedToken(token);
775
+ const amount = decoded.proofs.reduce(
776
+ (acc, proof) => acc + proof.amount,
777
+ 0
778
+ );
779
+ const unit = decoded.unit || "sat";
780
+ return { amount, unit };
781
+ } catch {
782
+ return { amount: 0, unit: "sat" };
763
783
  }
764
- return result;
765
784
  }
766
785
  async _getBalanceState() {
767
786
  if (this.balanceManager) {
@@ -1136,7 +1155,7 @@ var CashuSpender = class {
1136
1155
  });
1137
1156
  this._log(
1138
1157
  "DEBUG",
1139
- `[CashuSpender] refundXcashuTokens: Failed to receive refund token for ${baseUrl}, incremented tryCount to ${newTryCount}`
1158
+ `[CashuSpender] refundXcashuTokens: Failed to receive refund token for ${baseUrl}, incremented tryCount to ${newTryCount}: ${receiveResult.message}`
1140
1159
  );
1141
1160
  }
1142
1161
  } catch (error) {
@@ -1351,6 +1370,7 @@ var BalanceManager = class {
1351
1370
  return {
1352
1371
  success: receiveResult.success,
1353
1372
  refundedAmount: totalAmountMsat,
1373
+ message: receiveResult.message,
1354
1374
  requestId: fetchResult.requestId
1355
1375
  };
1356
1376
  } catch (error) {
@@ -4981,19 +5001,19 @@ var RoutstrClient = class {
4981
5001
  `[RoutstrClient] _handleErrorResponse: Attempting to receive/restore token for ${baseUrl}`
4982
5002
  );
4983
5003
  if (params.token.startsWith("cashu")) {
4984
- const tryReceiveTokenResult = await this.cashuSpender.receiveToken(
5004
+ const receiveResult = await this.cashuSpender.receiveToken(
4985
5005
  params.token
4986
5006
  );
4987
- if (tryReceiveTokenResult.success) {
5007
+ if (receiveResult.success) {
4988
5008
  this._log(
4989
5009
  "DEBUG",
4990
- `[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${tryReceiveTokenResult.amount}`
5010
+ `[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${receiveResult.amount}`
4991
5011
  );
4992
5012
  tryNextProvider = true;
4993
5013
  } else {
4994
5014
  this._log(
4995
5015
  "DEBUG",
4996
- `[RoutstrClient] _handleErrorResponse: Failed to receive token. `
5016
+ `[RoutstrClient] _handleErrorResponse: Failed to receive token: ${receiveResult.message}`
4997
5017
  );
4998
5018
  }
4999
5019
  }
@@ -5003,23 +5023,18 @@ var RoutstrClient = class {
5003
5023
  "DEBUG",
5004
5024
  `[RoutstrClient] _handleErrorResponse: Attempting to receive xcashu refund token, preview=${xCashuRefundToken.substring(0, 20)}...`
5005
5025
  );
5006
- try {
5007
- const receiveResult = await this.cashuSpender.receiveToken(xCashuRefundToken);
5008
- if (receiveResult.success) {
5009
- this._log(
5010
- "DEBUG",
5011
- `[RoutstrClient] _handleErrorResponse: xcashu refund received, amount=${receiveResult.amount}`
5012
- );
5013
- tryNextProvider = true;
5014
- } else
5015
- throw new ProviderError(
5016
- baseUrl,
5017
- status,
5018
- "xcashu refund failed",
5019
- requestId
5020
- );
5021
- } catch (error) {
5022
- this._log("ERROR", "[xcashu] Failed to receive refund token:", error);
5026
+ const receiveResult = await this.cashuSpender.receiveToken(xCashuRefundToken);
5027
+ if (receiveResult.success) {
5028
+ this._log(
5029
+ "DEBUG",
5030
+ `[RoutstrClient] _handleErrorResponse: xcashu refund received, amount=${receiveResult.amount}`
5031
+ );
5032
+ tryNextProvider = true;
5033
+ } else {
5034
+ this._log(
5035
+ "ERROR",
5036
+ `[xcashu] Failed to receive refund token: ${receiveResult.message}`
5037
+ );
5023
5038
  throw new ProviderError(
5024
5039
  baseUrl,
5025
5040
  status,
@@ -5312,14 +5327,15 @@ var RoutstrClient = class {
5312
5327
  if (this.mode === "xcashu" && response) {
5313
5328
  const refundToken = response.headers.get("x-cashu") ?? void 0;
5314
5329
  if (refundToken) {
5315
- try {
5316
- const receiveResult = await this.cashuSpender.receiveToken(refundToken);
5317
- if (receiveResult.success) {
5318
- this.storageAdapter.removeXcashuToken(baseUrl, token);
5319
- satsSpent = initialTokenBalance - receiveResult.amount * (receiveResult.unit == "sat" ? 1 : 1e3);
5320
- }
5321
- } catch (error) {
5322
- this._log("ERROR", "[xcashu] Failed to receive refund token:", error);
5330
+ const receiveResult = await this.cashuSpender.receiveToken(refundToken);
5331
+ if (receiveResult.success) {
5332
+ this.storageAdapter.removeXcashuToken(baseUrl, token);
5333
+ satsSpent = initialTokenBalance - receiveResult.amount * (receiveResult.unit == "sat" ? 1 : 1e3);
5334
+ } else {
5335
+ this._log(
5336
+ "ERROR",
5337
+ `[xcashu] Failed to receive refund token: ${receiveResult.message}`
5338
+ );
5323
5339
  }
5324
5340
  }
5325
5341
  } else if (this.mode === "apikeys") {
@@ -5360,12 +5376,6 @@ var RoutstrClient = class {
5360
5376
  clientApiKey
5361
5377
  });
5362
5378
  (async () => {
5363
- try {
5364
- const xcashuResults = await this.cashuSpender.refundXcashuTokens(mintUrl);
5365
- this._log("DEBUG", "Refund xcashu tokens results:", xcashuResults);
5366
- } catch (error) {
5367
- this._log("ERROR", "Failed to refund providers:", error);
5368
- }
5369
5379
  })();
5370
5380
  return satsSpent;
5371
5381
  }
@@ -5571,18 +5581,18 @@ var RoutstrClient = class {
5571
5581
  this.storageAdapter.setApiKey(baseUrl, spendResult2.token);
5572
5582
  } catch (error) {
5573
5583
  if (error instanceof Error && error.message.includes("ApiKey already exists")) {
5574
- const tryReceiveTokenResult = await this.cashuSpender.receiveToken(
5584
+ const receiveResult = await this.cashuSpender.receiveToken(
5575
5585
  spendResult2.token
5576
5586
  );
5577
- if (tryReceiveTokenResult.success) {
5587
+ if (receiveResult.success) {
5578
5588
  this._log(
5579
5589
  "DEBUG",
5580
- `[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${tryReceiveTokenResult.amount}`
5590
+ `[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${receiveResult.amount}`
5581
5591
  );
5582
5592
  } else {
5583
5593
  this._log(
5584
5594
  "DEBUG",
5585
- `[RoutstrClient] _handleErrorResponse: Token restore failed or not needed`
5595
+ `[RoutstrClient] _handleErrorResponse: Token restore failed: ${receiveResult.message}`
5586
5596
  );
5587
5597
  }
5588
5598
  this._log(