btc-wallet 0.5.12-beta → 0.5.13-beta

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -3608,7 +3608,7 @@ function getWithdrawTransaction(_0) {
3608
3608
  env = "mainnet"
3609
3609
  }) {
3610
3610
  const provider = getBtcProvider();
3611
- const btcAddress = yield provider.account;
3611
+ const btcAddress = provider.account;
3612
3612
  const config = yield getConfig(env);
3613
3613
  const brgConfig = yield nearCall(config.bridgeContractId, "get_config", {});
3614
3614
  if (brgConfig.min_withdraw_amount) {
@@ -3632,7 +3632,7 @@ function getWithdrawTransaction(_0) {
3632
3632
  };
3633
3633
  });
3634
3634
  const _feeRate = feeRate || (yield getBtcGasPrice());
3635
- const { inputs, outputs, fee } = (0, import_coinselect.default)(
3635
+ let { inputs, outputs, fee } = (0, import_coinselect.default)(
3636
3636
  utxos,
3637
3637
  [{ address: btcAddress, value: Number(amount) }],
3638
3638
  Math.ceil(_feeRate)
@@ -3641,32 +3641,61 @@ function getWithdrawTransaction(_0) {
3641
3641
  throw new Error("The network is busy, please try again later.");
3642
3642
  }
3643
3643
  const maxBtcFee = Number(brgConfig.max_btc_gas_fee);
3644
- const newFee = fee;
3645
- const withdrawChangeAddress = brgConfig.change_address;
3646
- if (newFee > maxBtcFee) {
3644
+ const transactionFee = fee;
3645
+ const changeAddress = brgConfig.change_address;
3646
+ if (transactionFee > maxBtcFee) {
3647
3647
  throw new Error("Gas exceeds maximum value");
3648
3648
  }
3649
- let userOutput, noUserOutput;
3649
+ let recipientOutput, changeOutput;
3650
3650
  for (let i = 0; i < outputs.length; i++) {
3651
3651
  const output = outputs[i];
3652
3652
  if (output.value.toString() === amount.toString()) {
3653
- userOutput = output;
3653
+ recipientOutput = output;
3654
3654
  } else {
3655
- noUserOutput = output;
3655
+ changeOutput = output;
3656
3656
  }
3657
3657
  if (!output.address) {
3658
- output.address = withdrawChangeAddress;
3658
+ output.address = changeAddress;
3659
3659
  }
3660
3660
  }
3661
- userOutput.value = new import_big.default(userOutput.value).minus(newFee).minus(withdrawFee).toNumber();
3662
- if (noUserOutput) {
3663
- noUserOutput.value = new import_big.default(noUserOutput.value).plus(newFee).plus(withdrawFee).toNumber();
3661
+ recipientOutput.value = new import_big.default(recipientOutput.value).minus(transactionFee).minus(withdrawFee).toNumber();
3662
+ if (changeOutput) {
3663
+ changeOutput.value = new import_big.default(changeOutput.value).plus(transactionFee).plus(withdrawFee).toNumber();
3664
+ const remainingInputs = [...inputs];
3665
+ let smallestInput = Math.min.apply(
3666
+ null,
3667
+ remainingInputs.map((input) => input.value)
3668
+ );
3669
+ let remainingChangeAmount = changeOutput.value;
3670
+ while (remainingChangeAmount >= smallestInput && smallestInput > 0 && remainingInputs.length > 0) {
3671
+ remainingChangeAmount -= smallestInput;
3672
+ changeOutput.value = remainingChangeAmount;
3673
+ const smallestInputIndex = remainingInputs.findIndex(
3674
+ (input) => input.value === smallestInput
3675
+ );
3676
+ if (smallestInputIndex > -1) {
3677
+ remainingInputs.splice(smallestInputIndex, 1);
3678
+ }
3679
+ smallestInput = Math.min.apply(
3680
+ null,
3681
+ remainingInputs.map((input) => input.value)
3682
+ );
3683
+ }
3684
+ const minChangeAmount = Number(brgConfig.min_change_amount);
3685
+ let additionalFee = 0;
3686
+ if (changeOutput.value === 0) {
3687
+ outputs = outputs.filter((item) => item.value !== 0);
3688
+ } else if (changeOutput.value < minChangeAmount) {
3689
+ additionalFee = minChangeAmount - changeOutput.value;
3690
+ recipientOutput.value -= additionalFee;
3691
+ changeOutput.value = minChangeAmount;
3692
+ }
3664
3693
  } else {
3665
- noUserOutput = {
3666
- address: withdrawChangeAddress,
3667
- value: new import_big.default(newFee).plus(withdrawFee).toNumber()
3694
+ changeOutput = {
3695
+ address: changeAddress,
3696
+ value: new import_big.default(transactionFee).plus(withdrawFee).toNumber()
3668
3697
  };
3669
- outputs.push(noUserOutput);
3698
+ outputs.push(changeOutput);
3670
3699
  }
3671
3700
  const insufficientOutput = outputs.some((item) => item.value < 0);
3672
3701
  if (insufficientOutput) {
@@ -3674,7 +3703,7 @@ function getWithdrawTransaction(_0) {
3674
3703
  }
3675
3704
  const inputSum = inputs.reduce((sum, cur) => sum + Number(cur.value), 0);
3676
3705
  const outputSum = outputs.reduce((sum, cur) => sum + Number(cur.value), 0);
3677
- if (newFee + outputSum !== inputSum) {
3706
+ if (transactionFee + outputSum !== inputSum) {
3678
3707
  throw new Error("compute error");
3679
3708
  }
3680
3709
  const network = yield getNetwork();
@@ -4555,7 +4584,7 @@ function setupBTCWallet({
4555
4584
 
4556
4585
  // src/index.ts
4557
4586
  var getVersion = () => {
4558
- return "0.5.12-beta";
4587
+ return "0.5.13-beta";
4559
4588
  };
4560
4589
  if (typeof window !== "undefined") {
4561
4590
  window.__BTC_WALLET_VERSION = getVersion();