@routstr/sdk 0.1.5 → 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.
@@ -2162,8 +2162,7 @@ var RoutstrClient = class {
2162
2162
  );
2163
2163
  }
2164
2164
  }
2165
- if ((status === 402 || status === 413 && responseBody?.includes("Insufficient balance")) && !tryNextProvider && (this.mode === "apikeys" || this.mode === "lazyrefund")) {
2166
- console.log("RESPONSFE ", responseBody);
2165
+ if (status === 402 && !tryNextProvider && (this.mode === "apikeys" || this.mode === "lazyrefund")) {
2167
2166
  const topupResult = await this.balanceManager.topUp({
2168
2167
  mintUrl,
2169
2168
  baseUrl,
@@ -2206,6 +2205,41 @@ var RoutstrClient = class {
2206
2205
  headers: this._withAuthHeader(params.baseHeaders, params.token)
2207
2206
  });
2208
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
+ }
2209
2243
  if ((status === 401 || status === 403 || status === 413 || status === 400 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
2210
2244
  this._log(
2211
2245
  "DEBUG",