btc-wallet 0.5.3-beta → 0.5.4-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
@@ -4275,17 +4275,58 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4275
4275
  }
4276
4276
  function calculateGasStrategy(gasTokenBalance, transactions2) {
4277
4277
  return __async(this, null, function* () {
4278
+ var _a;
4278
4279
  const accountId = state.getAccount();
4280
+ const nearAccount = yield provider.query({
4281
+ request_type: "view_account",
4282
+ account_id: accountId,
4283
+ finality: "final"
4284
+ });
4285
+ const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
4286
+ console.log("available near balance:", availableBalance);
4287
+ console.log("available gas token balance:", gasTokenBalance);
4279
4288
  const convertTx = yield Promise.all(
4280
4289
  transactions2.map((transaction, index) => convertTransactionToTxHex(transaction, index))
4281
4290
  );
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);
4291
+ if (availableBalance > 0.5) {
4292
+ console.log("near balance is enough, get the protocol fee of each transaction");
4293
+ const gasTokens = yield nearCall2(
4294
+ currentConfig.accountContractId,
4295
+ "list_gas_token",
4296
+ { token_ids: [currentConfig.token] }
4297
+ );
4298
+ console.log("list_gas_token gas tokens:", gasTokens);
4299
+ const perTxFee = Math.max(
4300
+ Number(((_a = gasTokens[currentConfig.token]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
4301
+ 100
4302
+ );
4303
+ console.log("perTxFee:", perTxFee);
4304
+ const protocolFee = new Big2(perTxFee || "0").mul(convertTx.length).toFixed(0);
4305
+ console.log("protocolFee:", protocolFee);
4306
+ if (new Big2(gasTokenBalance).gte(protocolFee)) {
4307
+ console.log("use near pay gas and enough gas token balance");
4308
+ return { useNearPayGas: true, gasLimit: protocolFee };
4309
+ } else {
4310
+ console.log("use near pay gas and not enough gas token balance");
4311
+ const transferTx = yield createGasTokenTransfer(accountId, protocolFee);
4312
+ return recalculateGasWithTransfer(transferTx, convertTx, true, perTxFee.toString());
4313
+ }
4314
+ } else {
4315
+ console.log("near balance is not enough, predict the gas token amount required");
4316
+ const adjustedGas = yield getPredictedGasAmount(
4317
+ currentConfig.accountContractId,
4318
+ currentConfig.token,
4319
+ convertTx.map((t) => t.txHex)
4320
+ );
4321
+ if (new Big2(gasTokenBalance).gte(adjustedGas)) {
4322
+ console.log("use gas token and gas token balance is enough");
4323
+ return { useNearPayGas: false, gasLimit: adjustedGas };
4324
+ } else {
4325
+ console.log("use gas token and gas token balance is not enough, need to transfer");
4326
+ const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
4327
+ return recalculateGasWithTransfer(transferTx, convertTx, false);
4328
+ }
4329
+ }
4289
4330
  });
4290
4331
  }
4291
4332
  function convertTransactionToTxHex(transaction, index = 0) {
@@ -4390,7 +4431,7 @@ function setupBTCWallet({
4390
4431
 
4391
4432
  // src/index.ts
4392
4433
  var getVersion = () => {
4393
- return "0.5.3-beta";
4434
+ return "0.5.4-beta";
4394
4435
  };
4395
4436
  if (typeof window !== "undefined") {
4396
4437
  window.__BTC_WALLET_VERSION = getVersion();