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/dist/index.js +47 -18
- package/dist/index.js.map +2 -2
- package/esm/index.js +47 -18
- package/esm/index.js.map +2 -2
- package/package.json +1 -1
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 =
|
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
|
-
|
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
|
3645
|
-
const
|
3646
|
-
if (
|
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
|
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
|
-
|
3653
|
+
recipientOutput = output;
|
3654
3654
|
} else {
|
3655
|
-
|
3655
|
+
changeOutput = output;
|
3656
3656
|
}
|
3657
3657
|
if (!output.address) {
|
3658
|
-
output.address =
|
3658
|
+
output.address = changeAddress;
|
3659
3659
|
}
|
3660
3660
|
}
|
3661
|
-
|
3662
|
-
if (
|
3663
|
-
|
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
|
-
|
3666
|
-
address:
|
3667
|
-
value: new import_big.default(
|
3694
|
+
changeOutput = {
|
3695
|
+
address: changeAddress,
|
3696
|
+
value: new import_big.default(transactionFee).plus(withdrawFee).toNumber()
|
3668
3697
|
};
|
3669
|
-
outputs.push(
|
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 (
|
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.
|
4587
|
+
return "0.5.13-beta";
|
4559
4588
|
};
|
4560
4589
|
if (typeof window !== "undefined") {
|
4561
4590
|
window.__BTC_WALLET_VERSION = getVersion();
|