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/esm/index.js CHANGED
@@ -3355,6 +3355,9 @@ function getCsnaAccountId(env) {
3355
3355
  const config = yield getConfig(env);
3356
3356
  const { getPublicKey } = getBtcProvider();
3357
3357
  const btcPublicKey = yield getPublicKey();
3358
+ if (!btcPublicKey) {
3359
+ throw new Error("BTC Public Key is not available.");
3360
+ }
3358
3361
  const csna = yield nearCall(
3359
3362
  config.accountContractId,
3360
3363
  "get_chain_signature_near_account_id",
@@ -3473,7 +3476,7 @@ function executeBTCDepositAndAction(_0) {
3473
3476
  }
3474
3477
  function checkSatoshiWhitelist(btcAccountId, env = "mainnet") {
3475
3478
  return __async(this, null, function* () {
3476
- if (env !== "private_mainnet")
3479
+ if (env !== "mainnet")
3477
3480
  return;
3478
3481
  const hasShownNotice = localStorage.getItem("btc-wallet-private-mainnet-notice");
3479
3482
  if (!hasShownNotice) {
@@ -3996,6 +3999,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3996
3999
  yield checkBtcNetwork(walletNetwork);
3997
4000
  if (!state.isValid()) {
3998
4001
  state.clear();
4002
+ console.log("setupBtcContextListeners clear");
3999
4003
  }
4000
4004
  validateWalletState();
4001
4005
  const btcContext = window.btcContext;
@@ -4018,6 +4022,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4018
4022
  context.on("updatePublicKey", (btcPublicKey) => __async(this, null, function* () {
4019
4023
  console.log("updatePublicKey");
4020
4024
  state.clear();
4025
+ console.log("updatePublicKey clear");
4021
4026
  try {
4022
4027
  const { nearAddress, nearPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
4023
4028
  if (!nearAddress || !nearPublicKey) {
@@ -4029,8 +4034,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4029
4034
  yield handleConnectionUpdate();
4030
4035
  } catch (error) {
4031
4036
  console.error("Error updating public key:", error);
4032
- state.clear();
4033
- emitter.emit("accountsChanged", { accounts: [] });
4034
4037
  }
4035
4038
  }));
4036
4039
  context.on("btcLoginError", () => __async(this, null, function* () {
@@ -4275,17 +4278,58 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4275
4278
  }
4276
4279
  function calculateGasStrategy(gasTokenBalance, transactions2) {
4277
4280
  return __async(this, null, function* () {
4281
+ var _a;
4278
4282
  const accountId = state.getAccount();
4283
+ const nearAccount = yield provider.query({
4284
+ request_type: "view_account",
4285
+ account_id: accountId,
4286
+ finality: "final"
4287
+ });
4288
+ const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
4289
+ console.log("available near balance:", availableBalance);
4290
+ console.log("available gas token balance:", gasTokenBalance);
4279
4291
  const convertTx = yield Promise.all(
4280
4292
  transactions2.map((transaction, index) => convertTransactionToTxHex(transaction, index))
4281
4293
  );
4282
- const adjustedGas = yield getPredictedGasAmount(
4283
- currentConfig.accountContractId,
4284
- currentConfig.token,
4285
- convertTx.map((t) => t.txHex)
4286
- );
4287
- const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
4288
- return recalculateGasWithTransfer(transferTx, convertTx, false);
4294
+ if (availableBalance > 0.5) {
4295
+ console.log("near balance is enough, get the protocol fee of each transaction");
4296
+ const gasTokens = yield nearCall2(
4297
+ currentConfig.accountContractId,
4298
+ "list_gas_token",
4299
+ { token_ids: [currentConfig.token] }
4300
+ );
4301
+ console.log("list_gas_token gas tokens:", gasTokens);
4302
+ const perTxFee = Math.max(
4303
+ Number(((_a = gasTokens[currentConfig.token]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
4304
+ 100
4305
+ );
4306
+ console.log("perTxFee:", perTxFee);
4307
+ const protocolFee = new Big2(perTxFee || "0").mul(convertTx.length).toFixed(0);
4308
+ console.log("protocolFee:", protocolFee);
4309
+ if (new Big2(gasTokenBalance).gte(protocolFee)) {
4310
+ console.log("use near pay gas and enough gas token balance");
4311
+ return { useNearPayGas: true, gasLimit: protocolFee };
4312
+ } else {
4313
+ console.log("use near pay gas and not enough gas token balance");
4314
+ const transferTx = yield createGasTokenTransfer(accountId, protocolFee);
4315
+ return recalculateGasWithTransfer(transferTx, convertTx, true, perTxFee.toString());
4316
+ }
4317
+ } else {
4318
+ console.log("near balance is not enough, predict the gas token amount required");
4319
+ const adjustedGas = yield getPredictedGasAmount(
4320
+ currentConfig.accountContractId,
4321
+ currentConfig.token,
4322
+ convertTx.map((t) => t.txHex)
4323
+ );
4324
+ if (new Big2(gasTokenBalance).gte(adjustedGas)) {
4325
+ console.log("use gas token and gas token balance is enough");
4326
+ return { useNearPayGas: false, gasLimit: adjustedGas };
4327
+ } else {
4328
+ console.log("use gas token and gas token balance is not enough, need to transfer");
4329
+ const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
4330
+ return recalculateGasWithTransfer(transferTx, convertTx, false);
4331
+ }
4332
+ }
4289
4333
  });
4290
4334
  }
4291
4335
  function convertTransactionToTxHex(transaction, index = 0) {
@@ -4390,7 +4434,7 @@ function setupBTCWallet({
4390
4434
 
4391
4435
  // src/index.ts
4392
4436
  var getVersion = () => {
4393
- return "0.5.3-beta";
4437
+ return "0.5.5-beta";
4394
4438
  };
4395
4439
  if (typeof window !== "undefined") {
4396
4440
  window.__BTC_WALLET_VERSION = getVersion();