btc-wallet 0.5.3-beta → 0.5.4-beta

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -4325,17 +4325,58 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4325
4325
  }
4326
4326
  function calculateGasStrategy(gasTokenBalance, transactions2) {
4327
4327
  return __async(this, null, function* () {
4328
+ var _a;
4328
4329
  const accountId = state.getAccount();
4330
+ const nearAccount = yield provider.query({
4331
+ request_type: "view_account",
4332
+ account_id: accountId,
4333
+ finality: "final"
4334
+ });
4335
+ const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
4336
+ console.log("available near balance:", availableBalance);
4337
+ console.log("available gas token balance:", gasTokenBalance);
4329
4338
  const convertTx = yield Promise.all(
4330
4339
  transactions2.map((transaction, index) => convertTransactionToTxHex(transaction, index))
4331
4340
  );
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);
4341
+ if (availableBalance > 0.5) {
4342
+ console.log("near balance is enough, get the protocol fee of each transaction");
4343
+ const gasTokens = yield nearCall2(
4344
+ currentConfig.accountContractId,
4345
+ "list_gas_token",
4346
+ { token_ids: [currentConfig.token] }
4347
+ );
4348
+ console.log("list_gas_token gas tokens:", gasTokens);
4349
+ const perTxFee = Math.max(
4350
+ Number(((_a = gasTokens[currentConfig.token]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
4351
+ 100
4352
+ );
4353
+ console.log("perTxFee:", perTxFee);
4354
+ const protocolFee = new import_big2.default(perTxFee || "0").mul(convertTx.length).toFixed(0);
4355
+ console.log("protocolFee:", protocolFee);
4356
+ if (new import_big2.default(gasTokenBalance).gte(protocolFee)) {
4357
+ console.log("use near pay gas and enough gas token balance");
4358
+ return { useNearPayGas: true, gasLimit: protocolFee };
4359
+ } else {
4360
+ console.log("use near pay gas and not enough gas token balance");
4361
+ const transferTx = yield createGasTokenTransfer(accountId, protocolFee);
4362
+ return recalculateGasWithTransfer(transferTx, convertTx, true, perTxFee.toString());
4363
+ }
4364
+ } else {
4365
+ console.log("near balance is not enough, predict the gas token amount required");
4366
+ const adjustedGas = yield getPredictedGasAmount(
4367
+ currentConfig.accountContractId,
4368
+ currentConfig.token,
4369
+ convertTx.map((t) => t.txHex)
4370
+ );
4371
+ if (new import_big2.default(gasTokenBalance).gte(adjustedGas)) {
4372
+ console.log("use gas token and gas token balance is enough");
4373
+ return { useNearPayGas: false, gasLimit: adjustedGas };
4374
+ } else {
4375
+ console.log("use gas token and gas token balance is not enough, need to transfer");
4376
+ const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
4377
+ return recalculateGasWithTransfer(transferTx, convertTx, false);
4378
+ }
4379
+ }
4339
4380
  });
4340
4381
  }
4341
4382
  function convertTransactionToTxHex(transaction, index = 0) {
@@ -4440,7 +4481,7 @@ function setupBTCWallet({
4440
4481
 
4441
4482
  // src/index.ts
4442
4483
  var getVersion = () => {
4443
- return "0.5.3-beta";
4484
+ return "0.5.4-beta";
4444
4485
  };
4445
4486
  if (typeof window !== "undefined") {
4446
4487
  window.__BTC_WALLET_VERSION = getVersion();