@routstr/sdk 0.1.4 → 0.1.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.
- package/dist/client/index.js +45 -6
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +45 -6
- package/dist/client/index.mjs.map +1 -1
- package/dist/discovery/index.d.mts +3 -1
- package/dist/discovery/index.d.ts +3 -1
- package/dist/discovery/index.js +17 -13
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +17 -13
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/index.js +62 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -19
- package/dist/index.mjs.map +1 -1
- package/dist/wallet/index.js +3 -0
- package/dist/wallet/index.js.map +1 -1
- package/dist/wallet/index.mjs +3 -0
- package/dist/wallet/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/client/index.js
CHANGED
|
@@ -681,6 +681,9 @@ var BalanceManager = class {
|
|
|
681
681
|
fetchResult.token
|
|
682
682
|
);
|
|
683
683
|
const totalAmountMsat = receiveResult.unit === "msat" ? receiveResult.amount : receiveResult.amount * 1e3;
|
|
684
|
+
if (receiveResult.success) {
|
|
685
|
+
this.storageAdapter.removeApiKey(baseUrl);
|
|
686
|
+
}
|
|
684
687
|
return {
|
|
685
688
|
success: receiveResult.success,
|
|
686
689
|
refundedAmount: totalAmountMsat,
|
|
@@ -1761,7 +1764,7 @@ var ProviderManager = class {
|
|
|
1761
1764
|
};
|
|
1762
1765
|
|
|
1763
1766
|
// client/RoutstrClient.ts
|
|
1764
|
-
var TOPUP_MARGIN =
|
|
1767
|
+
var TOPUP_MARGIN = 1.2;
|
|
1765
1768
|
var RoutstrClient = class {
|
|
1766
1769
|
constructor(walletAdapter, storageAdapter, providerRegistry, alertLevel, mode = "xcashu") {
|
|
1767
1770
|
this.walletAdapter = walletAdapter;
|
|
@@ -2161,8 +2164,7 @@ var RoutstrClient = class {
|
|
|
2161
2164
|
);
|
|
2162
2165
|
}
|
|
2163
2166
|
}
|
|
2164
|
-
if (
|
|
2165
|
-
console.log("RESPONSFE ", responseBody);
|
|
2167
|
+
if (status === 402 && !tryNextProvider && (this.mode === "apikeys" || this.mode === "lazyrefund")) {
|
|
2166
2168
|
const topupResult = await this.balanceManager.topUp({
|
|
2167
2169
|
mintUrl,
|
|
2168
2170
|
baseUrl,
|
|
@@ -2205,6 +2207,41 @@ var RoutstrClient = class {
|
|
|
2205
2207
|
headers: this._withAuthHeader(params.baseHeaders, params.token)
|
|
2206
2208
|
});
|
|
2207
2209
|
}
|
|
2210
|
+
const isInsufficientBalance413 = status === 413 && responseBody?.includes("Insufficient balance");
|
|
2211
|
+
if (isInsufficientBalance413 && !tryNextProvider && this.mode === "apikeys") {
|
|
2212
|
+
let retryToken = params.token;
|
|
2213
|
+
try {
|
|
2214
|
+
const latestBalanceInfo = await this.balanceManager.getTokenBalance(
|
|
2215
|
+
params.token,
|
|
2216
|
+
baseUrl
|
|
2217
|
+
);
|
|
2218
|
+
const latestTokenBalance = latestBalanceInfo.unit === "msat" ? latestBalanceInfo.amount / 1e3 : latestBalanceInfo.amount;
|
|
2219
|
+
if (latestBalanceInfo.apiKey) {
|
|
2220
|
+
const storedApiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
|
|
2221
|
+
if (storedApiKeyEntry?.key !== latestBalanceInfo.apiKey) {
|
|
2222
|
+
if (storedApiKeyEntry) {
|
|
2223
|
+
this.storageAdapter.removeApiKey(baseUrl);
|
|
2224
|
+
}
|
|
2225
|
+
this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
|
|
2226
|
+
}
|
|
2227
|
+
retryToken = latestBalanceInfo.apiKey;
|
|
2228
|
+
}
|
|
2229
|
+
if (latestTokenBalance >= 0) {
|
|
2230
|
+
this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
|
|
2231
|
+
}
|
|
2232
|
+
} catch (error) {
|
|
2233
|
+
this._log(
|
|
2234
|
+
"WARN",
|
|
2235
|
+
`[RoutstrClient] _handleErrorResponse: Failed to refresh API key after 413 insufficient balance for ${baseUrl}`,
|
|
2236
|
+
error
|
|
2237
|
+
);
|
|
2238
|
+
}
|
|
2239
|
+
return this._makeRequest({
|
|
2240
|
+
...params,
|
|
2241
|
+
token: retryToken,
|
|
2242
|
+
headers: this._withAuthHeader(params.baseHeaders, retryToken)
|
|
2243
|
+
});
|
|
2244
|
+
}
|
|
2208
2245
|
if ((status === 401 || status === 403 || status === 413 || status === 400 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
|
|
2209
2246
|
this._log(
|
|
2210
2247
|
"DEBUG",
|
|
@@ -2265,8 +2302,6 @@ var RoutstrClient = class {
|
|
|
2265
2302
|
status,
|
|
2266
2303
|
refundResult.message ?? "Unknown error"
|
|
2267
2304
|
);
|
|
2268
|
-
} else {
|
|
2269
|
-
this.storageAdapter.removeApiKey(baseUrl);
|
|
2270
2305
|
}
|
|
2271
2306
|
}
|
|
2272
2307
|
}
|
|
@@ -2365,7 +2400,11 @@ var RoutstrClient = class {
|
|
|
2365
2400
|
baseUrl
|
|
2366
2401
|
);
|
|
2367
2402
|
const latestTokenBalance = latestBalanceInfo.unit === "msat" ? latestBalanceInfo.amount / 1e3 : latestBalanceInfo.amount;
|
|
2368
|
-
this.storageAdapter.
|
|
2403
|
+
const storedApiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
|
|
2404
|
+
if (storedApiKeyEntry?.key.startsWith("cashu") && latestBalanceInfo.apiKey) {
|
|
2405
|
+
this.storageAdapter.removeApiKey(baseUrl);
|
|
2406
|
+
this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
|
|
2407
|
+
}
|
|
2369
2408
|
this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
|
|
2370
2409
|
satsSpent = initialTokenBalance - latestTokenBalance;
|
|
2371
2410
|
} catch (e) {
|