btc-wallet 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.
@@ -5,3 +5,7 @@ export declare const defaultTokenIcon = "https://static.particle.network/token-l
5
5
  export declare const ipfsToSrc: (ipfs: string) => string;
6
6
  export declare const checkBTCVersion: (accountContracts: AAOptions["accountContracts"], accountContractKey: string, version: string) => boolean;
7
7
  export declare const delay: (ms: number) => Promise<unknown>;
8
+ export declare function retryOperation<T>(operation: () => Promise<T> | T, shouldStop: (result: T) => boolean, { maxRetries, delayMs, }?: {
9
+ maxRetries?: number;
10
+ delayMs?: number;
11
+ }): Promise<T>;
package/esm/index.js CHANGED
@@ -470,7 +470,7 @@ var _network2, _event2;
470
470
  var MagicEdenConnector = class extends BaseConnector {
471
471
  constructor() {
472
472
  super();
473
- __privateAdd(this, _network2, "Testnet");
473
+ __privateAdd(this, _network2, "Mainnet");
474
474
  __privateAdd(this, _event2, new EventEmitter2());
475
475
  this.metadata = {
476
476
  id: "magicEden",
@@ -1309,6 +1309,27 @@ var checkBTCVersion = (accountContracts, accountContractKey, version) => {
1309
1309
  return accountContracts[accountContractKey].some((item) => item.version === version);
1310
1310
  };
1311
1311
  var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
1312
+ function retryOperation(_0, _1) {
1313
+ return __async(this, arguments, function* (operation, shouldStop, {
1314
+ maxRetries = 3,
1315
+ delayMs = 1e3
1316
+ } = {}) {
1317
+ let retries = 0;
1318
+ while (retries <= maxRetries) {
1319
+ const result = yield operation();
1320
+ if (shouldStop(result)) {
1321
+ return result;
1322
+ }
1323
+ if (retries === maxRetries) {
1324
+ console.warn("Max retries reached");
1325
+ return result;
1326
+ }
1327
+ retries++;
1328
+ yield delay(delayMs);
1329
+ }
1330
+ throw new Error("Unexpected execution path");
1331
+ });
1332
+ }
1312
1333
 
1313
1334
  // src/utils/ethereumUtils.ts
1314
1335
  import {
@@ -2729,7 +2750,7 @@ function request(url, options) {
2729
2750
  body,
2730
2751
  method
2731
2752
  });
2732
- const retryCount2 = (_a = options == null ? void 0 : options.retryCount) != null ? _a : 1;
2753
+ const retryCount = (_a = options == null ? void 0 : options.retryCount) != null ? _a : 1;
2733
2754
  const controller = new AbortController();
2734
2755
  const timeout = (options == null ? void 0 : options.timeout) || 2e4;
2735
2756
  const timeoutId = setTimeout(() => controller.abort(), timeout);
@@ -2752,16 +2773,16 @@ function request(url, options) {
2752
2773
  return data;
2753
2774
  } catch (err) {
2754
2775
  console.error(err);
2755
- if (retryCount2 > 0) {
2756
- console.log(`Retrying... attempts left: ${retryCount2}`);
2757
- return request(url, __spreadProps(__spreadValues({}, options), { retryCount: retryCount2 - 1 }));
2776
+ if (retryCount > 0) {
2777
+ console.log(`Retrying... attempts left: ${retryCount}`);
2778
+ return request(url, __spreadProps(__spreadValues({}, options), { retryCount: retryCount - 1 }));
2758
2779
  } else if ((options == null ? void 0 : options.pollingInterval) && (options == null ? void 0 : options.maxPollingAttempts)) {
2759
2780
  if (options.maxPollingAttempts > 0) {
2760
2781
  console.log(`Polling... attempts left: ${options.maxPollingAttempts}`);
2761
2782
  yield new Promise((resolve) => setTimeout(resolve, options.pollingInterval));
2762
2783
  return request(url, __spreadProps(__spreadValues({}, options), {
2763
2784
  maxPollingAttempts: options.maxPollingAttempts - 1,
2764
- retryCount: retryCount2
2785
+ retryCount
2765
2786
  }));
2766
2787
  }
2767
2788
  }
@@ -2912,8 +2933,15 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
2912
2933
  }
2913
2934
  ];
2914
2935
  }
2915
- const btcAccount = yield btcContext.login();
2916
- const btcPublicKey = yield btcContext.getPublicKey();
2936
+ yield btcContext.login();
2937
+ const btcPublicKey = yield retryOperation(btcContext.getPublicKey, (res) => !!res, {
2938
+ maxRetries: 40,
2939
+ delayMs: 3e3
2940
+ });
2941
+ console.log("btcPublicKey:", btcPublicKey);
2942
+ if (!btcPublicKey) {
2943
+ throw new Error("No connected BTC wallet, please connect your BTC wallet first.");
2944
+ }
2917
2945
  const { nearTempAddress, nearTempPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
2918
2946
  return [
2919
2947
  {
@@ -3235,24 +3263,21 @@ function getBtcGasPrice() {
3235
3263
  }
3236
3264
  });
3237
3265
  }
3238
- var retryCount = 0;
3239
3266
  function getBtcBalance() {
3240
3267
  return __async(this, null, function* () {
3241
- const { account } = getBtcProvider();
3268
+ var _a;
3269
+ const { account } = yield retryOperation(getBtcProvider, (res2) => !!res2.account);
3242
3270
  if (!account) {
3243
- retryCount++;
3244
- if (retryCount > 3) {
3245
- throw new Error("BTC Account is not available.");
3246
- }
3247
- yield delay(1e3);
3248
- return getBtcBalance();
3271
+ console.error("BTC Account is not available.");
3272
+ return { rawBalance: 0, balance: 0 };
3249
3273
  }
3250
- retryCount = 0;
3251
3274
  const btcRpcUrl = yield getBtcRpcUrl();
3252
3275
  const res = yield fetch(`${btcRpcUrl}/address/${account}/utxo`).then((res2) => res2.json());
3253
- const rawBalance = res.reduce((acc, cur) => acc + cur.value, 0);
3276
+ const rawBalance = (_a = res.filter((item) => {
3277
+ var _a2;
3278
+ return (_a2 = item == null ? void 0 : item.status) == null ? void 0 : _a2.confirmed;
3279
+ })) == null ? void 0 : _a.reduce((acc, cur) => acc + cur.value, 0);
3254
3280
  const balance = rawBalance / __pow(10, 8);
3255
- console.log("btc balance:", balance);
3256
3281
  return { rawBalance, balance };
3257
3282
  });
3258
3283
  }
@@ -3294,7 +3319,7 @@ function executeBurrowSupply(_0) {
3294
3319
 
3295
3320
  // src/index.ts
3296
3321
  var getVersion = () => {
3297
- return "0.3.4";
3322
+ return "0.3.5";
3298
3323
  };
3299
3324
  if (typeof window !== "undefined") {
3300
3325
  window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();