btc-wallet 0.3.15 → 0.3.17

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/esm/index.js CHANGED
@@ -2831,7 +2831,13 @@ function request(url, options) {
2831
2831
  if (options.shouldStopPolling(data)) {
2832
2832
  return data;
2833
2833
  }
2834
- throw new Error("Polling should continue");
2834
+ if (options.maxPollingAttempts && options.maxPollingAttempts > 0) {
2835
+ yield new Promise((resolve) => setTimeout(resolve, options.pollingInterval));
2836
+ return request(url, __spreadProps(__spreadValues({}, options), {
2837
+ maxPollingAttempts: options.maxPollingAttempts - 1
2838
+ }));
2839
+ }
2840
+ throw new Error("Polling failed: maximum attempts reached without meeting the condition");
2835
2841
  }
2836
2842
  if (cacheKey) {
2837
2843
  cache.set(cacheKey, { timestamp: Date.now(), data });
@@ -3174,6 +3180,7 @@ function nearCall(contractId, methodName, args) {
3174
3180
  function getAccountInfo(csna, accountContractId) {
3175
3181
  return __async(this, null, function* () {
3176
3182
  const accountInfo = yield nearCall(accountContractId, "get_account", { account_id: csna });
3183
+ console.log("get_account accountInfo:", accountInfo);
3177
3184
  return accountInfo;
3178
3185
  });
3179
3186
  }
@@ -3244,20 +3251,21 @@ function getBtcBalance() {
3244
3251
  }
3245
3252
  const btcRpcUrl = yield getBtcRpcUrl();
3246
3253
  const utxos = yield fetch(`${btcRpcUrl}/address/${account}/utxo`).then((res) => res.json());
3254
+ const btcDecimals = 8;
3247
3255
  const rawBalance = (utxos == null ? void 0 : utxos.reduce((acc, cur) => acc + cur.value, 0)) || 0;
3248
- const balance = rawBalance / __pow(10, 8);
3256
+ const balance = rawBalance / __pow(10, btcDecimals);
3249
3257
  const feeRate = yield getBtcGasPrice();
3250
3258
  const inputSize = ((utxos == null ? void 0 : utxos.length) || 0) * 66;
3251
3259
  const outputSize = 34;
3252
3260
  const overheadSize = 10;
3253
3261
  const estimatedTxSize = inputSize + outputSize + overheadSize;
3254
- const estimatedFee = estimatedTxSize * feeRate / __pow(10, 8);
3255
- console.log("estimated fee:", estimatedFee);
3256
- const availableBalance = Math.max(0, balance - estimatedFee);
3262
+ const estimatedFee = estimatedTxSize * feeRate;
3263
+ const availableRawBalance = (rawBalance - estimatedFee).toFixed(0);
3264
+ const availableBalance = new Big(availableRawBalance).div(__pow(10, btcDecimals)).round(btcDecimals, Big.roundDown).toNumber();
3257
3265
  return {
3258
3266
  rawBalance,
3259
3267
  balance,
3260
- availableBalance
3268
+ availableBalance: Math.max(availableBalance, 0)
3261
3269
  };
3262
3270
  });
3263
3271
  }
@@ -3286,7 +3294,8 @@ function getDepositAmount(amount, option) {
3286
3294
  );
3287
3295
  const depositAmount = (option == null ? void 0 : option.isEstimate) ? Number(amount) : Math.max(MINIMUM_DEPOSIT_AMOUNT + MINIMUM_DEPOSIT_AMOUNT_BASE, Number(amount));
3288
3296
  const fee = Math.max(Number(fee_min), Number(depositAmount) * fee_rate);
3289
- const receiveAmount = new Big(depositAmount).minus(fee).round(0, Big.roundDown).toNumber();
3297
+ const receiveAmount = new Big(depositAmount).minus(fee).minus(MINIMUM_DEPOSIT_AMOUNT_BASE).round(0, Big.roundDown).toNumber();
3298
+ console.log("getDepositAmount:", { depositAmount, receiveAmount, fee });
3290
3299
  return {
3291
3300
  depositAmount,
3292
3301
  receiveAmount: Math.max(receiveAmount, 0),
@@ -3330,7 +3339,7 @@ function executeBTCDepositAndAction(_0) {
3330
3339
  const accountInfo = yield getAccountInfo(csna, config.accountContractId);
3331
3340
  const newActions = [];
3332
3341
  const gasLimit = new Big(50).mul(__pow(10, 12)).toFixed(0);
3333
- const repayAction = yield checkGasTokenArrears(accountInfo.debt_info, isDev, false);
3342
+ const repayAction = yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, false);
3334
3343
  if (repayAction) {
3335
3344
  newActions.push(__spreadProps(__spreadValues({}, repayAction), {
3336
3345
  gas: gasLimit
@@ -3600,10 +3609,10 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3600
3609
  const accountId = state.getAccount();
3601
3610
  const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
3602
3611
  yield checkGasTokenBalance(accountId, currentConfig.token, isDev);
3603
- yield checkGasTokenArrears(accountInfo.debt_info, isDev, true);
3612
+ yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, true);
3604
3613
  const trans = [...params.transactions];
3605
3614
  console.log("raw trans:", trans);
3606
- const gasTokenBalance = accountInfo.gas_token[currentConfig.token] || "0";
3615
+ const gasTokenBalance = (accountInfo == null ? void 0 : accountInfo.gas_token[currentConfig.token]) || "0";
3607
3616
  const { transferGasTransaction, useNearPayGas, gasLimit } = yield calculateGasStrategy(
3608
3617
  gasTokenBalance,
3609
3618
  trans
@@ -3619,7 +3628,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3619
3628
  trans.map((transaction, index) => convertTransactionToTxHex(transaction, index))
3620
3629
  );
3621
3630
  const nonceFromApi = yield getNonce(currentConfig.base_url, accountId);
3622
- const nonce = Number(nonceFromApi) > Number(accountInfo.nonce) ? String(nonceFromApi) : String(accountInfo.nonce);
3631
+ const nonce = Number(nonceFromApi) > Number(accountInfo == null ? void 0 : accountInfo.nonce) ? String(nonceFromApi) : String(accountInfo == null ? void 0 : accountInfo.nonce);
3623
3632
  const intention = {
3624
3633
  chain_id: "397",
3625
3634
  csna: accountId,
@@ -3836,6 +3845,7 @@ function setupBTCWallet({
3836
3845
  syncLogOut = true,
3837
3846
  isDev = false
3838
3847
  } = {}) {
3848
+ console.log("\u26A1\uFE0F BTC Wallet Version:", getVersion());
3839
3849
  const btcWallet = () => __async(this, null, function* () {
3840
3850
  return {
3841
3851
  id: "btc-wallet",
@@ -3859,10 +3869,10 @@ function setupBTCWallet({
3859
3869
 
3860
3870
  // src/index.ts
3861
3871
  var getVersion = () => {
3862
- return "0.3.15";
3872
+ return "0.3.17";
3863
3873
  };
3864
3874
  if (typeof window !== "undefined") {
3865
- window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();
3875
+ window.__BTC_WALLET_VERSION = getVersion();
3866
3876
  }
3867
3877
  export {
3868
3878
  BaseConnector,