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