@routstr/sdk 0.3.2 → 0.3.4
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/client/index.d.mts +9 -5
- package/dist/client/index.d.ts +9 -5
- package/dist/client/index.js +70 -131
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +70 -131
- package/dist/client/index.mjs.map +1 -1
- package/dist/discovery/index.d.mts +8 -2
- package/dist/discovery/index.d.ts +8 -2
- package/dist/discovery/index.js +27 -16
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +27 -16
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/index.d.mts +10 -8
- package/dist/index.d.ts +10 -8
- package/dist/index.js +141 -185
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +140 -186
- package/dist/index.mjs.map +1 -1
- package/dist/{interfaces-C5fLD3jB.d.mts → interfaces-Bp0Ngmqv.d.mts} +1 -1
- package/dist/{interfaces-BxDEka72.d.ts → interfaces-CIfd_phZ.d.ts} +1 -1
- package/dist/{interfaces-BWJJTCXO.d.mts → interfaces-Cxi8R4TT.d.mts} +1 -1
- package/dist/{interfaces-B62Rw-dd.d.ts → interfaces-D2FDCLyP.d.ts} +1 -1
- package/dist/storage/index.d.mts +9 -7
- package/dist/storage/index.d.ts +9 -7
- package/dist/storage/index.js +51 -34
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/index.mjs +51 -34
- package/dist/storage/index.mjs.map +1 -1
- package/dist/{store-h7m23ffq.d.ts → store-BD5zF9Hp.d.ts} +4 -4
- package/dist/{store-DGeLPv9E.d.mts → store-CBSyK2qg.d.mts} +4 -4
- package/dist/{types-BYj_8c5c.d.mts → types-DPQM6tIG.d.mts} +10 -1
- package/dist/{types-BYj_8c5c.d.ts → types-DPQM6tIG.d.ts} +10 -1
- package/dist/wallet/index.d.mts +8 -6
- package/dist/wallet/index.d.ts +8 -6
- package/dist/wallet/index.js +43 -52
- package/dist/wallet/index.js.map +1 -1
- package/dist/wallet/index.mjs +43 -52
- package/dist/wallet/index.mjs.map +1 -1
- package/package.json +3 -2
package/dist/wallet/index.mjs
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import { getDecodedToken } from '@cashu/cashu-ts';
|
|
2
2
|
|
|
3
|
+
// core/types.ts
|
|
4
|
+
function makeConsoleLogger(prefix) {
|
|
5
|
+
const fmt = (args) => prefix ? [prefix, ...args] : args;
|
|
6
|
+
return {
|
|
7
|
+
log: (...args) => console.log(...fmt(args)),
|
|
8
|
+
warn: (...args) => console.warn(...fmt(args)),
|
|
9
|
+
error: (...args) => console.error(...fmt(args)),
|
|
10
|
+
debug: (...args) => console.log(...fmt(args)),
|
|
11
|
+
child: (p) => makeConsoleLogger(prefix ? `${prefix}:${p}` : p)
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
var consoleLogger = makeConsoleLogger();
|
|
15
|
+
|
|
3
16
|
// core/errors.ts
|
|
4
17
|
var InsufficientBalanceError = class extends Error {
|
|
5
18
|
constructor(required, available, maxMintBalance = 0, maxMintUrl = "", customMessage) {
|
|
@@ -78,14 +91,16 @@ function selectMintWithBalance(balances, units, amount, excludeMints = []) {
|
|
|
78
91
|
return { selectedMintUrl: null, selectedMintBalance: 0 };
|
|
79
92
|
}
|
|
80
93
|
var CashuSpender = class {
|
|
81
|
-
constructor(walletAdapter, storageAdapter, _providerRegistry, balanceManager) {
|
|
94
|
+
constructor(walletAdapter, storageAdapter, _providerRegistry, balanceManager, logger) {
|
|
82
95
|
this.walletAdapter = walletAdapter;
|
|
83
96
|
this.storageAdapter = storageAdapter;
|
|
84
97
|
this._providerRegistry = _providerRegistry;
|
|
85
98
|
this.balanceManager = balanceManager;
|
|
99
|
+
this.logger = (logger ?? consoleLogger).child("CashuSpender");
|
|
86
100
|
}
|
|
87
101
|
_isBusy = false;
|
|
88
102
|
debugLevel = "WARN";
|
|
103
|
+
logger;
|
|
89
104
|
async receiveToken(token) {
|
|
90
105
|
try {
|
|
91
106
|
const result = await this.walletAdapter.receiveToken(token);
|
|
@@ -181,13 +196,13 @@ var CashuSpender = class {
|
|
|
181
196
|
if (levelPriority[level] >= levelPriority[this.debugLevel]) {
|
|
182
197
|
switch (level) {
|
|
183
198
|
case "DEBUG":
|
|
184
|
-
|
|
199
|
+
this.logger.log(...args);
|
|
185
200
|
break;
|
|
186
201
|
case "WARN":
|
|
187
|
-
|
|
202
|
+
this.logger.warn(...args);
|
|
188
203
|
break;
|
|
189
204
|
case "ERROR":
|
|
190
|
-
|
|
205
|
+
this.logger.error(...args);
|
|
191
206
|
break;
|
|
192
207
|
}
|
|
193
208
|
}
|
|
@@ -614,10 +629,11 @@ var CashuSpender = class {
|
|
|
614
629
|
|
|
615
630
|
// wallet/BalanceManager.ts
|
|
616
631
|
var BalanceManager = class _BalanceManager {
|
|
617
|
-
constructor(walletAdapter, storageAdapter, providerRegistry, cashuSpender) {
|
|
632
|
+
constructor(walletAdapter, storageAdapter, providerRegistry, cashuSpender, logger) {
|
|
618
633
|
this.walletAdapter = walletAdapter;
|
|
619
634
|
this.storageAdapter = storageAdapter;
|
|
620
635
|
this.providerRegistry = providerRegistry;
|
|
636
|
+
this.logger = (logger ?? consoleLogger).child("BalanceManager");
|
|
621
637
|
if (cashuSpender) {
|
|
622
638
|
this.cashuSpender = cashuSpender;
|
|
623
639
|
} else {
|
|
@@ -634,6 +650,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
634
650
|
providerWalletOps = /* @__PURE__ */ new Map();
|
|
635
651
|
/** Cooldown (ms) between opposite operations on the same provider */
|
|
636
652
|
static PROVIDER_WALLET_COOLDOWN_MS = 1e4;
|
|
653
|
+
logger;
|
|
637
654
|
/**
|
|
638
655
|
* Check whether a wallet operation (topup/refund) may run for a provider.
|
|
639
656
|
* Returns the reason when blocked.
|
|
@@ -708,7 +725,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
708
725
|
const { mintUrl, baseUrl, apiKey, forceRefund } = options;
|
|
709
726
|
const guard = this._canRunProviderWalletOperation(baseUrl, "refund");
|
|
710
727
|
if (!guard.allowed) {
|
|
711
|
-
|
|
728
|
+
this.logger.log(`Skipping refund for ${baseUrl} - ${guard.reason}`);
|
|
712
729
|
return { success: false, message: guard.reason };
|
|
713
730
|
}
|
|
714
731
|
this._beginProviderWalletOperation(baseUrl, "refund");
|
|
@@ -728,9 +745,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
728
745
|
if (apiKeyEntry?.lastUsed) {
|
|
729
746
|
const fiveMinutesAgo = Date.now() - 5 * 60 * 1e3;
|
|
730
747
|
if (apiKeyEntry.lastUsed > fiveMinutesAgo) {
|
|
731
|
-
|
|
732
|
-
`[BalanceManager] Skipping refund for ${baseUrl} - used ${Math.round((Date.now() - apiKeyEntry.lastUsed) / 1e3)}s ago`
|
|
733
|
-
);
|
|
748
|
+
this.logger.log(`Skipping refund for ${baseUrl} - used ${Math.round((Date.now() - apiKeyEntry.lastUsed) / 1e3)}s ago`);
|
|
734
749
|
return {
|
|
735
750
|
success: false,
|
|
736
751
|
message: "API key was used recently, skipping refund"
|
|
@@ -772,7 +787,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
772
787
|
requestId: fetchResult.requestId
|
|
773
788
|
};
|
|
774
789
|
} catch (error) {
|
|
775
|
-
|
|
790
|
+
this.logger.error("API key refund error", error);
|
|
776
791
|
return this._handleRefundError(error, mintUrl, fetchResult?.requestId);
|
|
777
792
|
}
|
|
778
793
|
}
|
|
@@ -824,7 +839,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
824
839
|
};
|
|
825
840
|
} catch (error) {
|
|
826
841
|
clearTimeout(timeoutId);
|
|
827
|
-
|
|
842
|
+
this.logger.error("fetchRefundToken fetch error", error);
|
|
828
843
|
if (error instanceof Error) {
|
|
829
844
|
if (error.name === "AbortError") {
|
|
830
845
|
return {
|
|
@@ -850,7 +865,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
850
865
|
const { mintUrl, baseUrl, amount, token: providedToken } = options;
|
|
851
866
|
const guard = this._canRunProviderWalletOperation(baseUrl, "topup");
|
|
852
867
|
if (!guard.allowed) {
|
|
853
|
-
|
|
868
|
+
this.logger.log(`Skipping topup for ${baseUrl} - ${guard.reason}`);
|
|
854
869
|
return { success: false, message: guard.reason };
|
|
855
870
|
}
|
|
856
871
|
this._beginProviderWalletOperation(baseUrl, "topup");
|
|
@@ -887,7 +902,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
887
902
|
cashuToken = tokenResult.token;
|
|
888
903
|
const topUpResult = await this._postTopUp(baseUrl, apiKey, cashuToken);
|
|
889
904
|
requestId = topUpResult.requestId;
|
|
890
|
-
|
|
905
|
+
this.logger.log("topUpResult:", topUpResult);
|
|
891
906
|
if (!topUpResult.success) {
|
|
892
907
|
await this._recoverFailedTopUp(cashuToken);
|
|
893
908
|
return {
|
|
@@ -903,10 +918,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
903
918
|
requestId
|
|
904
919
|
};
|
|
905
920
|
} catch (error) {
|
|
906
|
-
|
|
907
|
-
"DEBUG",
|
|
908
|
-
`[TopuPU] topup: Topup result for ${baseUrl}: error=${error}`
|
|
909
|
-
);
|
|
921
|
+
this.logger.log(`topup error for ${baseUrl}: ${error}`);
|
|
910
922
|
if (cashuToken) {
|
|
911
923
|
await this._recoverFailedTopUp(cashuToken);
|
|
912
924
|
}
|
|
@@ -923,13 +935,9 @@ var BalanceManager = class _BalanceManager {
|
|
|
923
935
|
p2pkPubkey
|
|
924
936
|
} = options;
|
|
925
937
|
const adjustedAmount = Math.ceil(amount);
|
|
926
|
-
|
|
927
|
-
`[BalanceManager.createProviderToken] Starting: baseUrl=${baseUrl}, mintUrl=${mintUrl}, amount=${amount}, adjustedAmount=${adjustedAmount}, retryCount=${retryCount}`
|
|
928
|
-
);
|
|
938
|
+
this.logger.log(`createProviderToken: baseUrl=${baseUrl} mintUrl=${mintUrl} amount=${amount} adjustedAmount=${adjustedAmount} retryCount=${retryCount}`);
|
|
929
939
|
if (!adjustedAmount || isNaN(adjustedAmount)) {
|
|
930
|
-
|
|
931
|
-
`[BalanceManager.createProviderToken] FAILURE: Invalid amount - amount=${amount}, adjustedAmount=${adjustedAmount}`
|
|
932
|
-
);
|
|
940
|
+
this.logger.error(`createProviderToken: invalid amount=${amount}`);
|
|
933
941
|
return { success: false, error: "Invalid top up amount" };
|
|
934
942
|
}
|
|
935
943
|
const balanceState = await this.getBalanceState();
|
|
@@ -960,9 +968,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
960
968
|
{ url: "", balance: 0 }
|
|
961
969
|
).url
|
|
962
970
|
);
|
|
963
|
-
|
|
964
|
-
`[BalanceManager.createProviderToken] FAILURE: Insufficient balance - required=${adjustedAmount}, available=${totalMintBalance + targetProviderBalance}, totalMintBalance=${totalMintBalance}, targetProviderBalance=${targetProviderBalance}, refundableProviderBalance=${refundableProviderBalance}`
|
|
965
|
-
);
|
|
971
|
+
this.logger.error(`createProviderToken: insufficient balance required=${adjustedAmount} available=${totalMintBalance + targetProviderBalance} totalMint=${totalMintBalance} targetProvider=${targetProviderBalance}`);
|
|
966
972
|
return { success: false, error: error.message };
|
|
967
973
|
}
|
|
968
974
|
const providerMints = baseUrl && this.providerRegistry ? this.providerRegistry.getProviderMints(baseUrl) : [];
|
|
@@ -998,9 +1004,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
998
1004
|
maxMintUrl = mintUrl2;
|
|
999
1005
|
}
|
|
1000
1006
|
}
|
|
1001
|
-
|
|
1002
|
-
`[BalanceManager.createProviderToken] FAILURE: No candidate mints found - requiredAmount=${requiredAmount}, totalMintBalance=${totalMintBalance}, maxBalance=${maxBalance}, maxMintUrl=${maxMintUrl}, providerMints=${JSON.stringify(providerMints)}`
|
|
1003
|
-
);
|
|
1007
|
+
this.logger.error(`createProviderToken: no candidate mints required=${requiredAmount} totalMint=${totalMintBalance} maxBalance=${maxBalance} maxMint=${maxMintUrl}`);
|
|
1004
1008
|
const error = new InsufficientBalanceError(
|
|
1005
1009
|
adjustedAmount,
|
|
1006
1010
|
totalMintBalance,
|
|
@@ -1012,17 +1016,13 @@ var BalanceManager = class _BalanceManager {
|
|
|
1012
1016
|
let lastError;
|
|
1013
1017
|
for (const candidateMint of candidates) {
|
|
1014
1018
|
try {
|
|
1015
|
-
|
|
1016
|
-
`[BalanceManager.createProviderToken] Attempting mint: ${candidateMint}, amount: ${requiredAmount}`
|
|
1017
|
-
);
|
|
1019
|
+
this.logger.log(`createProviderToken: attempting mint=${candidateMint} amount=${requiredAmount}`);
|
|
1018
1020
|
const token = await this.walletAdapter.sendToken(
|
|
1019
1021
|
candidateMint,
|
|
1020
1022
|
requiredAmount,
|
|
1021
1023
|
p2pkPubkey
|
|
1022
1024
|
);
|
|
1023
|
-
|
|
1024
|
-
`[BalanceManager.createProviderToken] SUCCESS: Token created from mint ${candidateMint}, all mint balances: ${JSON.stringify(Object.fromEntries(Object.entries(balances).map(([mint, balance]) => [mint, getBalanceInSats(balance, units[mint])])))}`
|
|
1025
|
-
);
|
|
1025
|
+
this.logger.log(`createProviderToken: success from mint=${candidateMint}`);
|
|
1026
1026
|
return {
|
|
1027
1027
|
success: true,
|
|
1028
1028
|
token,
|
|
@@ -1031,15 +1031,11 @@ var BalanceManager = class _BalanceManager {
|
|
|
1031
1031
|
};
|
|
1032
1032
|
} catch (error) {
|
|
1033
1033
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
1034
|
-
|
|
1035
|
-
`[BalanceManager.createProviderToken] FAILURE: Mint ${candidateMint} failed with error: ${errorMsg}`
|
|
1036
|
-
);
|
|
1034
|
+
this.logger.error(`createProviderToken: mint=${candidateMint} failed: ${errorMsg}`);
|
|
1037
1035
|
if (error instanceof Error) {
|
|
1038
1036
|
lastError = errorMsg;
|
|
1039
1037
|
if (isNetworkErrorMessage(error.message)) {
|
|
1040
|
-
|
|
1041
|
-
`[BalanceManager.createProviderToken] Network error from ${candidateMint}, trying next mint...`
|
|
1042
|
-
);
|
|
1038
|
+
this.logger.warn(`createProviderToken: network error from ${candidateMint}, trying next mint...`);
|
|
1043
1039
|
continue;
|
|
1044
1040
|
}
|
|
1045
1041
|
}
|
|
@@ -1049,9 +1045,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
1049
1045
|
};
|
|
1050
1046
|
}
|
|
1051
1047
|
}
|
|
1052
|
-
|
|
1053
|
-
`[BalanceManager.createProviderToken] FAILURE: All candidate mints exhausted - lastError=${lastError}, candidates=${JSON.stringify(candidates)}`
|
|
1054
|
-
);
|
|
1048
|
+
this.logger.error(`createProviderToken: all candidate mints exhausted lastError=${lastError}`);
|
|
1055
1049
|
return {
|
|
1056
1050
|
success: false,
|
|
1057
1051
|
error: lastError || "All candidate mints failed while creating top up token"
|
|
@@ -1166,7 +1160,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
1166
1160
|
return { success: true, requestId };
|
|
1167
1161
|
} catch (error) {
|
|
1168
1162
|
clearTimeout(timeoutId);
|
|
1169
|
-
|
|
1163
|
+
this.logger.error("_postTopUp fetch error", error);
|
|
1170
1164
|
if (error instanceof Error) {
|
|
1171
1165
|
if (error.name === "AbortError") {
|
|
1172
1166
|
return {
|
|
@@ -1192,10 +1186,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
1192
1186
|
try {
|
|
1193
1187
|
await this.cashuSpender.receiveToken(cashuToken);
|
|
1194
1188
|
} catch (error) {
|
|
1195
|
-
|
|
1196
|
-
"[BalanceManager._recoverFailedTopUp] Failed to recover token",
|
|
1197
|
-
error
|
|
1198
|
-
);
|
|
1189
|
+
this.logger.error("_recoverFailedTopUp: failed to recover token", error);
|
|
1199
1190
|
}
|
|
1200
1191
|
}
|
|
1201
1192
|
/**
|
|
@@ -1248,9 +1239,9 @@ var BalanceManager = class _BalanceManager {
|
|
|
1248
1239
|
apiKey: data.api_key
|
|
1249
1240
|
};
|
|
1250
1241
|
} else {
|
|
1251
|
-
|
|
1242
|
+
this.logger.warn(`getTokenBalance: status=${response.status}`);
|
|
1252
1243
|
const data = await response.json();
|
|
1253
|
-
|
|
1244
|
+
this.logger.warn("getTokenBalance: FAILED", data);
|
|
1254
1245
|
const isInvalidApiKey = response.status === 401 && data?.detail?.error?.code === "invalid_api_key" && data?.detail?.error?.message?.includes("proofs already spent");
|
|
1255
1246
|
return {
|
|
1256
1247
|
amount: -1,
|
|
@@ -1261,7 +1252,7 @@ var BalanceManager = class _BalanceManager {
|
|
|
1261
1252
|
};
|
|
1262
1253
|
}
|
|
1263
1254
|
} catch (error) {
|
|
1264
|
-
|
|
1255
|
+
this.logger.error("getTokenBalance error", error);
|
|
1265
1256
|
}
|
|
1266
1257
|
return { amount: -1, reserved: 0, unit: "sat", apiKey: "" };
|
|
1267
1258
|
}
|