btc-wallet 0.5.12-beta → 0.5.13-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
@@ -3558,7 +3558,7 @@ function getWithdrawTransaction(_0) {
3558
3558
  env = "mainnet"
3559
3559
  }) {
3560
3560
  const provider = getBtcProvider();
3561
- const btcAddress = yield provider.account;
3561
+ const btcAddress = provider.account;
3562
3562
  const config = yield getConfig(env);
3563
3563
  const brgConfig = yield nearCall(config.bridgeContractId, "get_config", {});
3564
3564
  if (brgConfig.min_withdraw_amount) {
@@ -3582,7 +3582,7 @@ function getWithdrawTransaction(_0) {
3582
3582
  };
3583
3583
  });
3584
3584
  const _feeRate = feeRate || (yield getBtcGasPrice());
3585
- const { inputs, outputs, fee } = coinselect(
3585
+ let { inputs, outputs, fee } = coinselect(
3586
3586
  utxos,
3587
3587
  [{ address: btcAddress, value: Number(amount) }],
3588
3588
  Math.ceil(_feeRate)
@@ -3591,32 +3591,61 @@ function getWithdrawTransaction(_0) {
3591
3591
  throw new Error("The network is busy, please try again later.");
3592
3592
  }
3593
3593
  const maxBtcFee = Number(brgConfig.max_btc_gas_fee);
3594
- const newFee = fee;
3595
- const withdrawChangeAddress = brgConfig.change_address;
3596
- if (newFee > maxBtcFee) {
3594
+ const transactionFee = fee;
3595
+ const changeAddress = brgConfig.change_address;
3596
+ if (transactionFee > maxBtcFee) {
3597
3597
  throw new Error("Gas exceeds maximum value");
3598
3598
  }
3599
- let userOutput, noUserOutput;
3599
+ let recipientOutput, changeOutput;
3600
3600
  for (let i = 0; i < outputs.length; i++) {
3601
3601
  const output = outputs[i];
3602
3602
  if (output.value.toString() === amount.toString()) {
3603
- userOutput = output;
3603
+ recipientOutput = output;
3604
3604
  } else {
3605
- noUserOutput = output;
3605
+ changeOutput = output;
3606
3606
  }
3607
3607
  if (!output.address) {
3608
- output.address = withdrawChangeAddress;
3608
+ output.address = changeAddress;
3609
3609
  }
3610
3610
  }
3611
- userOutput.value = new Big(userOutput.value).minus(newFee).minus(withdrawFee).toNumber();
3612
- if (noUserOutput) {
3613
- noUserOutput.value = new Big(noUserOutput.value).plus(newFee).plus(withdrawFee).toNumber();
3611
+ recipientOutput.value = new Big(recipientOutput.value).minus(transactionFee).minus(withdrawFee).toNumber();
3612
+ if (changeOutput) {
3613
+ changeOutput.value = new Big(changeOutput.value).plus(transactionFee).plus(withdrawFee).toNumber();
3614
+ const remainingInputs = [...inputs];
3615
+ let smallestInput = Math.min.apply(
3616
+ null,
3617
+ remainingInputs.map((input) => input.value)
3618
+ );
3619
+ let remainingChangeAmount = changeOutput.value;
3620
+ while (remainingChangeAmount >= smallestInput && smallestInput > 0 && remainingInputs.length > 0) {
3621
+ remainingChangeAmount -= smallestInput;
3622
+ changeOutput.value = remainingChangeAmount;
3623
+ const smallestInputIndex = remainingInputs.findIndex(
3624
+ (input) => input.value === smallestInput
3625
+ );
3626
+ if (smallestInputIndex > -1) {
3627
+ remainingInputs.splice(smallestInputIndex, 1);
3628
+ }
3629
+ smallestInput = Math.min.apply(
3630
+ null,
3631
+ remainingInputs.map((input) => input.value)
3632
+ );
3633
+ }
3634
+ const minChangeAmount = Number(brgConfig.min_change_amount);
3635
+ let additionalFee = 0;
3636
+ if (changeOutput.value === 0) {
3637
+ outputs = outputs.filter((item) => item.value !== 0);
3638
+ } else if (changeOutput.value < minChangeAmount) {
3639
+ additionalFee = minChangeAmount - changeOutput.value;
3640
+ recipientOutput.value -= additionalFee;
3641
+ changeOutput.value = minChangeAmount;
3642
+ }
3614
3643
  } else {
3615
- noUserOutput = {
3616
- address: withdrawChangeAddress,
3617
- value: new Big(newFee).plus(withdrawFee).toNumber()
3644
+ changeOutput = {
3645
+ address: changeAddress,
3646
+ value: new Big(transactionFee).plus(withdrawFee).toNumber()
3618
3647
  };
3619
- outputs.push(noUserOutput);
3648
+ outputs.push(changeOutput);
3620
3649
  }
3621
3650
  const insufficientOutput = outputs.some((item) => item.value < 0);
3622
3651
  if (insufficientOutput) {
@@ -3624,7 +3653,7 @@ function getWithdrawTransaction(_0) {
3624
3653
  }
3625
3654
  const inputSum = inputs.reduce((sum, cur) => sum + Number(cur.value), 0);
3626
3655
  const outputSum = outputs.reduce((sum, cur) => sum + Number(cur.value), 0);
3627
- if (newFee + outputSum !== inputSum) {
3656
+ if (transactionFee + outputSum !== inputSum) {
3628
3657
  throw new Error("compute error");
3629
3658
  }
3630
3659
  const network = yield getNetwork();
@@ -4505,7 +4534,7 @@ function setupBTCWallet({
4505
4534
 
4506
4535
  // src/index.ts
4507
4536
  var getVersion = () => {
4508
- return "0.5.12-beta";
4537
+ return "0.5.13-beta";
4509
4538
  };
4510
4539
  if (typeof window !== "undefined") {
4511
4540
  window.__BTC_WALLET_VERSION = getVersion();