btc-wallet 0.3.15 → 0.3.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,8 +7,8 @@ export interface DebtInfo {
7
7
  export declare function getAccountInfo(csna: string, accountContractId: string): Promise<{
8
8
  nonce: string;
9
9
  gas_token: Record<string, string>;
10
- debt_info: DebtInfo;
11
- }>;
10
+ debt_info?: DebtInfo;
11
+ } | undefined>;
12
12
  export declare function checkGasTokenBalance(csna: string, gasToken: string, isDev: boolean): Promise<void>;
13
13
  type CheckGasTokenArrearsReturnType<T extends boolean> = T extends true ? void : {
14
14
  receiver_id: string;
package/dist/index.js CHANGED
@@ -2875,7 +2875,13 @@ function request(url, options) {
2875
2875
  if (options.shouldStopPolling(data)) {
2876
2876
  return data;
2877
2877
  }
2878
- throw new Error("Polling should continue");
2878
+ if (options.maxPollingAttempts && options.maxPollingAttempts > 0) {
2879
+ yield new Promise((resolve) => setTimeout(resolve, options.pollingInterval));
2880
+ return request(url, __spreadProps(__spreadValues({}, options), {
2881
+ maxPollingAttempts: options.maxPollingAttempts - 1
2882
+ }));
2883
+ }
2884
+ throw new Error("Polling failed: maximum attempts reached without meeting the condition");
2879
2885
  }
2880
2886
  if (cacheKey) {
2881
2887
  cache.set(cacheKey, { timestamp: Date.now(), data });
@@ -3218,6 +3224,7 @@ function nearCall(contractId, methodName, args) {
3218
3224
  function getAccountInfo(csna, accountContractId) {
3219
3225
  return __async(this, null, function* () {
3220
3226
  const accountInfo = yield nearCall(accountContractId, "get_account", { account_id: csna });
3227
+ console.log("get_account accountInfo:", accountInfo);
3221
3228
  return accountInfo;
3222
3229
  });
3223
3230
  }
@@ -3288,20 +3295,21 @@ function getBtcBalance() {
3288
3295
  }
3289
3296
  const btcRpcUrl = yield getBtcRpcUrl();
3290
3297
  const utxos = yield fetch(`${btcRpcUrl}/address/${account}/utxo`).then((res) => res.json());
3298
+ const btcDecimals = 8;
3291
3299
  const rawBalance = (utxos == null ? void 0 : utxos.reduce((acc, cur) => acc + cur.value, 0)) || 0;
3292
- const balance = rawBalance / __pow(10, 8);
3300
+ const balance = rawBalance / __pow(10, btcDecimals);
3293
3301
  const feeRate = yield getBtcGasPrice();
3294
3302
  const inputSize = ((utxos == null ? void 0 : utxos.length) || 0) * 66;
3295
3303
  const outputSize = 34;
3296
3304
  const overheadSize = 10;
3297
3305
  const estimatedTxSize = inputSize + outputSize + overheadSize;
3298
- const estimatedFee = estimatedTxSize * feeRate / __pow(10, 8);
3299
- console.log("estimated fee:", estimatedFee);
3300
- const availableBalance = Math.max(0, balance - estimatedFee);
3306
+ const estimatedFee = estimatedTxSize * feeRate;
3307
+ const availableRawBalance = (rawBalance - estimatedFee).toFixed(0);
3308
+ const availableBalance = new import_big.default(availableRawBalance).div(__pow(10, btcDecimals)).round(btcDecimals, import_big.default.roundDown).toNumber();
3301
3309
  return {
3302
3310
  rawBalance,
3303
3311
  balance,
3304
- availableBalance
3312
+ availableBalance: Math.max(availableBalance, 0)
3305
3313
  };
3306
3314
  });
3307
3315
  }
@@ -3330,7 +3338,8 @@ function getDepositAmount(amount, option) {
3330
3338
  );
3331
3339
  const depositAmount = (option == null ? void 0 : option.isEstimate) ? Number(amount) : Math.max(MINIMUM_DEPOSIT_AMOUNT + MINIMUM_DEPOSIT_AMOUNT_BASE, Number(amount));
3332
3340
  const fee = Math.max(Number(fee_min), Number(depositAmount) * fee_rate);
3333
- const receiveAmount = new import_big.default(depositAmount).minus(fee).round(0, import_big.default.roundDown).toNumber();
3341
+ const receiveAmount = new import_big.default(depositAmount).minus(fee).minus(MINIMUM_DEPOSIT_AMOUNT_BASE).round(0, import_big.default.roundDown).toNumber();
3342
+ console.log("getDepositAmount:", { depositAmount, receiveAmount, fee });
3334
3343
  return {
3335
3344
  depositAmount,
3336
3345
  receiveAmount: Math.max(receiveAmount, 0),
@@ -3374,7 +3383,7 @@ function executeBTCDepositAndAction(_0) {
3374
3383
  const accountInfo = yield getAccountInfo(csna, config.accountContractId);
3375
3384
  const newActions = [];
3376
3385
  const gasLimit = new import_big.default(50).mul(__pow(10, 12)).toFixed(0);
3377
- const repayAction = yield checkGasTokenArrears(accountInfo.debt_info, isDev, false);
3386
+ const repayAction = yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, false);
3378
3387
  if (repayAction) {
3379
3388
  newActions.push(__spreadProps(__spreadValues({}, repayAction), {
3380
3389
  gas: gasLimit
@@ -3644,10 +3653,10 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3644
3653
  const accountId = state.getAccount();
3645
3654
  const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
3646
3655
  yield checkGasTokenBalance(accountId, currentConfig.token, isDev);
3647
- yield checkGasTokenArrears(accountInfo.debt_info, isDev, true);
3656
+ yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, true);
3648
3657
  const trans = [...params.transactions];
3649
3658
  console.log("raw trans:", trans);
3650
- const gasTokenBalance = accountInfo.gas_token[currentConfig.token] || "0";
3659
+ const gasTokenBalance = (accountInfo == null ? void 0 : accountInfo.gas_token[currentConfig.token]) || "0";
3651
3660
  const { transferGasTransaction, useNearPayGas, gasLimit } = yield calculateGasStrategy(
3652
3661
  gasTokenBalance,
3653
3662
  trans
@@ -3663,7 +3672,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3663
3672
  trans.map((transaction, index) => convertTransactionToTxHex(transaction, index))
3664
3673
  );
3665
3674
  const nonceFromApi = yield getNonce(currentConfig.base_url, accountId);
3666
- const nonce = Number(nonceFromApi) > Number(accountInfo.nonce) ? String(nonceFromApi) : String(accountInfo.nonce);
3675
+ const nonce = Number(nonceFromApi) > Number(accountInfo == null ? void 0 : accountInfo.nonce) ? String(nonceFromApi) : String(accountInfo == null ? void 0 : accountInfo.nonce);
3667
3676
  const intention = {
3668
3677
  chain_id: "397",
3669
3678
  csna: accountId,
@@ -3880,6 +3889,7 @@ function setupBTCWallet({
3880
3889
  syncLogOut = true,
3881
3890
  isDev = false
3882
3891
  } = {}) {
3892
+ console.log("\u26A1\uFE0F BTC Wallet Version:", getVersion());
3883
3893
  const btcWallet = () => __async(this, null, function* () {
3884
3894
  return {
3885
3895
  id: "btc-wallet",
@@ -3903,9 +3913,9 @@ function setupBTCWallet({
3903
3913
 
3904
3914
  // src/index.ts
3905
3915
  var getVersion = () => {
3906
- return "0.3.15";
3916
+ return "0.3.17";
3907
3917
  };
3908
3918
  if (typeof window !== "undefined") {
3909
- window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();
3919
+ window.__BTC_WALLET_VERSION = getVersion();
3910
3920
  }
3911
3921
  //# sourceMappingURL=index.js.map