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