btc-wallet 0.5.3-beta → 0.5.5-beta

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
@@ -3405,6 +3405,9 @@ function getCsnaAccountId(env) {
3405
3405
  const config = yield getConfig(env);
3406
3406
  const { getPublicKey } = getBtcProvider();
3407
3407
  const btcPublicKey = yield getPublicKey();
3408
+ if (!btcPublicKey) {
3409
+ throw new Error("BTC Public Key is not available.");
3410
+ }
3408
3411
  const csna = yield nearCall(
3409
3412
  config.accountContractId,
3410
3413
  "get_chain_signature_near_account_id",
@@ -3523,7 +3526,7 @@ function executeBTCDepositAndAction(_0) {
3523
3526
  }
3524
3527
  function checkSatoshiWhitelist(btcAccountId, env = "mainnet") {
3525
3528
  return __async(this, null, function* () {
3526
- if (env !== "private_mainnet")
3529
+ if (env !== "mainnet")
3527
3530
  return;
3528
3531
  const hasShownNotice = localStorage.getItem("btc-wallet-private-mainnet-notice");
3529
3532
  if (!hasShownNotice) {
@@ -4046,6 +4049,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4046
4049
  yield checkBtcNetwork(walletNetwork);
4047
4050
  if (!state.isValid()) {
4048
4051
  state.clear();
4052
+ console.log("setupBtcContextListeners clear");
4049
4053
  }
4050
4054
  validateWalletState();
4051
4055
  const btcContext = window.btcContext;
@@ -4068,6 +4072,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4068
4072
  context.on("updatePublicKey", (btcPublicKey) => __async(this, null, function* () {
4069
4073
  console.log("updatePublicKey");
4070
4074
  state.clear();
4075
+ console.log("updatePublicKey clear");
4071
4076
  try {
4072
4077
  const { nearAddress, nearPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
4073
4078
  if (!nearAddress || !nearPublicKey) {
@@ -4079,8 +4084,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4079
4084
  yield handleConnectionUpdate();
4080
4085
  } catch (error) {
4081
4086
  console.error("Error updating public key:", error);
4082
- state.clear();
4083
- emitter.emit("accountsChanged", { accounts: [] });
4084
4087
  }
4085
4088
  }));
4086
4089
  context.on("btcLoginError", () => __async(this, null, function* () {
@@ -4325,17 +4328,58 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4325
4328
  }
4326
4329
  function calculateGasStrategy(gasTokenBalance, transactions2) {
4327
4330
  return __async(this, null, function* () {
4331
+ var _a;
4328
4332
  const accountId = state.getAccount();
4333
+ const nearAccount = yield provider.query({
4334
+ request_type: "view_account",
4335
+ account_id: accountId,
4336
+ finality: "final"
4337
+ });
4338
+ const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
4339
+ console.log("available near balance:", availableBalance);
4340
+ console.log("available gas token balance:", gasTokenBalance);
4329
4341
  const convertTx = yield Promise.all(
4330
4342
  transactions2.map((transaction, index) => convertTransactionToTxHex(transaction, index))
4331
4343
  );
4332
- const adjustedGas = yield getPredictedGasAmount(
4333
- currentConfig.accountContractId,
4334
- currentConfig.token,
4335
- convertTx.map((t) => t.txHex)
4336
- );
4337
- const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
4338
- return recalculateGasWithTransfer(transferTx, convertTx, false);
4344
+ if (availableBalance > 0.5) {
4345
+ console.log("near balance is enough, get the protocol fee of each transaction");
4346
+ const gasTokens = yield nearCall2(
4347
+ currentConfig.accountContractId,
4348
+ "list_gas_token",
4349
+ { token_ids: [currentConfig.token] }
4350
+ );
4351
+ console.log("list_gas_token gas tokens:", gasTokens);
4352
+ const perTxFee = Math.max(
4353
+ Number(((_a = gasTokens[currentConfig.token]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
4354
+ 100
4355
+ );
4356
+ console.log("perTxFee:", perTxFee);
4357
+ const protocolFee = new import_big2.default(perTxFee || "0").mul(convertTx.length).toFixed(0);
4358
+ console.log("protocolFee:", protocolFee);
4359
+ if (new import_big2.default(gasTokenBalance).gte(protocolFee)) {
4360
+ console.log("use near pay gas and enough gas token balance");
4361
+ return { useNearPayGas: true, gasLimit: protocolFee };
4362
+ } else {
4363
+ console.log("use near pay gas and not enough gas token balance");
4364
+ const transferTx = yield createGasTokenTransfer(accountId, protocolFee);
4365
+ return recalculateGasWithTransfer(transferTx, convertTx, true, perTxFee.toString());
4366
+ }
4367
+ } else {
4368
+ console.log("near balance is not enough, predict the gas token amount required");
4369
+ const adjustedGas = yield getPredictedGasAmount(
4370
+ currentConfig.accountContractId,
4371
+ currentConfig.token,
4372
+ convertTx.map((t) => t.txHex)
4373
+ );
4374
+ if (new import_big2.default(gasTokenBalance).gte(adjustedGas)) {
4375
+ console.log("use gas token and gas token balance is enough");
4376
+ return { useNearPayGas: false, gasLimit: adjustedGas };
4377
+ } else {
4378
+ console.log("use gas token and gas token balance is not enough, need to transfer");
4379
+ const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
4380
+ return recalculateGasWithTransfer(transferTx, convertTx, false);
4381
+ }
4382
+ }
4339
4383
  });
4340
4384
  }
4341
4385
  function convertTransactionToTxHex(transaction, index = 0) {
@@ -4440,7 +4484,7 @@ function setupBTCWallet({
4440
4484
 
4441
4485
  // src/index.ts
4442
4486
  var getVersion = () => {
4443
- return "0.5.3-beta";
4487
+ return "0.5.5-beta";
4444
4488
  };
4445
4489
  if (typeof window !== "undefined") {
4446
4490
  window.__BTC_WALLET_VERSION = getVersion();