@routstr/sdk 0.3.4 → 0.3.5

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.js CHANGED
@@ -1221,19 +1221,50 @@ var CashuSpender = class {
1221
1221
  apiKeyEntry.baseUrl
1222
1222
  );
1223
1223
  if (apiKeyEntryFull && this.balanceManager) {
1224
+ try {
1225
+ const balanceResult = await this.balanceManager.getTokenBalance(
1226
+ apiKeyEntryFull.key,
1227
+ apiKeyEntry.baseUrl
1228
+ );
1229
+ if (balanceResult.isInvalidApiKey) {
1230
+ this.storageAdapter.removeApiKey(apiKeyEntry.baseUrl);
1231
+ results.push({
1232
+ baseUrl: apiKeyEntry.baseUrl,
1233
+ success: true
1234
+ });
1235
+ continue;
1236
+ }
1237
+ if (balanceResult.amount >= 0) {
1238
+ const balanceSat = balanceResult.unit === "msat" ? Math.floor(balanceResult.amount / 1e3) : balanceResult.amount;
1239
+ this.storageAdapter.updateApiKeyBalance(
1240
+ apiKeyEntry.baseUrl,
1241
+ balanceSat
1242
+ );
1243
+ }
1244
+ } catch {
1245
+ }
1246
+ const refreshedEntry = this.storageAdapter.getApiKey(
1247
+ apiKeyEntry.baseUrl
1248
+ );
1249
+ if (!refreshedEntry) continue;
1224
1250
  const refundResult = await this.balanceManager.refundApiKey({
1225
1251
  mintUrl,
1226
1252
  baseUrl: apiKeyEntry.baseUrl,
1227
- apiKey: apiKeyEntryFull.key,
1253
+ apiKey: refreshedEntry.key,
1228
1254
  forceRefund
1229
1255
  });
1230
1256
  if (refundResult.success) {
1231
1257
  this.storageAdapter.removeApiKey(apiKeyEntry.baseUrl);
1232
1258
  } else {
1233
- this.storageAdapter.updateApiKeyBalance(
1234
- apiKeyEntry.baseUrl,
1235
- apiKeyEntry.amount
1259
+ const currentEntry = this.storageAdapter.getApiKey(
1260
+ apiKeyEntry.baseUrl
1236
1261
  );
1262
+ if (currentEntry) {
1263
+ this.storageAdapter.updateApiKeyBalance(
1264
+ apiKeyEntry.baseUrl,
1265
+ currentEntry.balance
1266
+ );
1267
+ }
1237
1268
  }
1238
1269
  results.push({
1239
1270
  baseUrl: apiKeyEntry.baseUrl,
@@ -1442,7 +1473,8 @@ var BalanceManager = class _BalanceManager {
1442
1473
  };
1443
1474
  }
1444
1475
  if (fetchResult.error === "No balance to refund") {
1445
- return { success: false, message: "No balance to refund" };
1476
+ this.storageAdapter.removeApiKey(baseUrl);
1477
+ return { success: true, message: "No balance to refund, key cleaned up" };
1446
1478
  }
1447
1479
  const receiveResult = await this.cashuSpender.receiveToken(
1448
1480
  fetchResult.token
@@ -4255,9 +4287,7 @@ var createStorageAdapterFromStore = (store) => ({
4255
4287
  const distributionMap = {};
4256
4288
  for (const entry of apiKeys) {
4257
4289
  const sum = entry.balance || 0;
4258
- if (sum > 0) {
4259
- distributionMap[entry.baseUrl] = (distributionMap[entry.baseUrl] || 0) + sum;
4260
- }
4290
+ distributionMap[entry.baseUrl] = (distributionMap[entry.baseUrl] || 0) + sum;
4261
4291
  }
4262
4292
  return Object.entries(distributionMap).map(([baseUrl, amt]) => ({ baseUrl, amount: amt })).sort((a, b) => b.amount - a.amount);
4263
4293
  },