btc-wallet 0.5.20-beta → 0.5.22-beta
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/core/btcUtils.d.ts +2 -1
- package/dist/index.js +240 -175
- package/dist/index.js.map +3 -3
- package/dist/utils/satoshi.d.ts +35 -0
- package/esm/index.js +240 -175
- package/esm/index.js.map +2 -2
- package/package.json +1 -1
package/dist/core/btcUtils.d.ts
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
import type { ENV } from '../config';
|
2
|
+
import { calculateGasLimit } from '../utils/satoshi';
|
2
3
|
import type { FinalExecutionOutcome, Transaction } from '@near-wallet-selector/core';
|
4
|
+
export { calculateGasLimit };
|
3
5
|
type CheckGasTokenDebtReturnType<T extends boolean> = T extends true ? void : {
|
4
6
|
receiver_id: string;
|
5
7
|
amount: string;
|
@@ -55,4 +57,3 @@ interface WithdrawParams {
|
|
55
57
|
env?: ENV;
|
56
58
|
}
|
57
59
|
export declare function getWithdrawTransaction({ amount, feeRate, env, }: WithdrawParams): Promise<Transaction>;
|
58
|
-
export {};
|
package/dist/index.js
CHANGED
@@ -24,6 +24,18 @@ var __spreadValues = (a, b) => {
|
|
24
24
|
return a;
|
25
25
|
};
|
26
26
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
27
|
+
var __objRest = (source, exclude) => {
|
28
|
+
var target = {};
|
29
|
+
for (var prop in source)
|
30
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
31
|
+
target[prop] = source[prop];
|
32
|
+
if (source != null && __getOwnPropSymbols)
|
33
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
34
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
35
|
+
target[prop] = source[prop];
|
36
|
+
}
|
37
|
+
return target;
|
38
|
+
};
|
27
39
|
var __export = (target, all) => {
|
28
40
|
for (var name in all)
|
29
41
|
__defProp(target, name, { get: all[name], enumerable: true });
|
@@ -92,6 +104,7 @@ __export(src_exports, {
|
|
92
104
|
WizzConnector: () => WizzConnector,
|
93
105
|
XverseConnector: () => XverseConnector,
|
94
106
|
btcRpcUrls: () => btcRpcUrls,
|
107
|
+
calculateGasLimit: () => calculateGasLimit,
|
95
108
|
checkGasTokenDebt: () => checkGasTokenDebt,
|
96
109
|
checkSatoshiWhitelist: () => checkSatoshiWhitelist,
|
97
110
|
estimateDepositAmount: () => estimateDepositAmount,
|
@@ -3013,7 +3026,6 @@ function request(url, options) {
|
|
3013
3026
|
}
|
3014
3027
|
return data;
|
3015
3028
|
} catch (err) {
|
3016
|
-
console.error(err);
|
3017
3029
|
if (retryCount > 0) {
|
3018
3030
|
console.log(`Retrying... attempts left: ${retryCount}`);
|
3019
3031
|
return request(url, __spreadProps(__spreadValues({}, options), { retryCount: retryCount - 1 }));
|
@@ -3027,6 +3039,7 @@ function request(url, options) {
|
|
3027
3039
|
}));
|
3028
3040
|
}
|
3029
3041
|
}
|
3042
|
+
console.error(err);
|
3030
3043
|
return Promise.reject(err);
|
3031
3044
|
}
|
3032
3045
|
});
|
@@ -3199,6 +3212,7 @@ var state_default = {
|
|
3199
3212
|
};
|
3200
3213
|
|
3201
3214
|
// src/utils/satoshi.ts
|
3215
|
+
var import_coinselect = __toESM(require("coinselect"), 1);
|
3202
3216
|
function getNonce(url, accountId) {
|
3203
3217
|
return __async(this, null, function* () {
|
3204
3218
|
const { result_code, result_message, result_data } = yield request(
|
@@ -3328,10 +3342,16 @@ function getAccountInfo(_0) {
|
|
3328
3342
|
).catch((error) => {
|
3329
3343
|
return void 0;
|
3330
3344
|
});
|
3331
|
-
console.log("get_account accountInfo:", accountInfo);
|
3332
3345
|
return accountInfo;
|
3333
3346
|
});
|
3334
3347
|
}
|
3348
|
+
function getBridgeConfig(_0) {
|
3349
|
+
return __async(this, arguments, function* ({ env }) {
|
3350
|
+
const config = getWalletConfig(env);
|
3351
|
+
const bridgeConfig = yield nearCallFunction(config.bridgeContractId, "get_config", {}, { network: config.network });
|
3352
|
+
return bridgeConfig;
|
3353
|
+
});
|
3354
|
+
}
|
3335
3355
|
function getTokenBalance(_0) {
|
3336
3356
|
return __async(this, arguments, function* ({
|
3337
3357
|
csna,
|
@@ -3630,11 +3650,196 @@ function getPredictedGasAmount(_0) {
|
|
3630
3650
|
return gasAmount.toString();
|
3631
3651
|
});
|
3632
3652
|
}
|
3653
|
+
function calculateWithdraw(_0) {
|
3654
|
+
return __async(this, arguments, function* ({
|
3655
|
+
amount,
|
3656
|
+
feeRate,
|
3657
|
+
csna,
|
3658
|
+
btcAddress,
|
3659
|
+
env
|
3660
|
+
}) {
|
3661
|
+
console.log("calculateWithdraw feeRate:", feeRate);
|
3662
|
+
try {
|
3663
|
+
const config = getWalletConfig(env);
|
3664
|
+
const gasLimit = yield calculateGasLimit({
|
3665
|
+
csna,
|
3666
|
+
transactions: [
|
3667
|
+
{
|
3668
|
+
signerId: "",
|
3669
|
+
receiverId: config.btcToken,
|
3670
|
+
actions: [
|
3671
|
+
{
|
3672
|
+
type: "FunctionCall",
|
3673
|
+
params: {
|
3674
|
+
methodName: "ft_transfer_call",
|
3675
|
+
args: {
|
3676
|
+
receiver_id: config.btcToken,
|
3677
|
+
amount: "100",
|
3678
|
+
msg: ""
|
3679
|
+
},
|
3680
|
+
gas: "300000000000000",
|
3681
|
+
deposit: "1"
|
3682
|
+
}
|
3683
|
+
}
|
3684
|
+
]
|
3685
|
+
}
|
3686
|
+
],
|
3687
|
+
env
|
3688
|
+
});
|
3689
|
+
let satoshis = Number(amount);
|
3690
|
+
if (Number(gasLimit) > 0) {
|
3691
|
+
satoshis = new import_big.default(amount).minus(gasLimit).toNumber();
|
3692
|
+
}
|
3693
|
+
const brgConfig = yield getBridgeConfig({ env });
|
3694
|
+
const allUTXO = yield nearCallFunction(config.bridgeContractId, "get_utxos_paged", {}, { network: config.network });
|
3695
|
+
if (brgConfig.min_withdraw_amount) {
|
3696
|
+
if (Number(satoshis) < Number(brgConfig.min_withdraw_amount)) {
|
3697
|
+
return {
|
3698
|
+
withdrawFee: 0,
|
3699
|
+
isError: true,
|
3700
|
+
errorMsg: "Mini withdraw amount is " + (Number(brgConfig.min_withdraw_amount) + Number(gasLimit))
|
3701
|
+
};
|
3702
|
+
}
|
3703
|
+
}
|
3704
|
+
const feePercent = Number(brgConfig.withdraw_bridge_fee.fee_rate) * Number(satoshis);
|
3705
|
+
const withdrawFee = feePercent > Number(brgConfig.withdraw_bridge_fee.fee_min) ? feePercent : Number(brgConfig.withdraw_bridge_fee.fee_min);
|
3706
|
+
const withdrawChangeAddress = brgConfig.change_address;
|
3707
|
+
const utxos = Object.keys(allUTXO).map((key) => {
|
3708
|
+
const txid = key.split("@");
|
3709
|
+
return {
|
3710
|
+
txid: txid[0],
|
3711
|
+
vout: allUTXO[key].vout,
|
3712
|
+
value: Number(allUTXO[key].balance),
|
3713
|
+
script: allUTXO[key].script
|
3714
|
+
};
|
3715
|
+
}).filter((utxo) => utxo.value > Number(brgConfig.min_change_amount));
|
3716
|
+
if (!utxos || utxos.length === 0) {
|
3717
|
+
return {
|
3718
|
+
withdrawFee,
|
3719
|
+
isError: true,
|
3720
|
+
errorMsg: "The network is busy, please try again later."
|
3721
|
+
};
|
3722
|
+
}
|
3723
|
+
const userSatoshis = Number(satoshis);
|
3724
|
+
const maxBtcFee = Number(brgConfig.max_btc_gas_fee);
|
3725
|
+
const { inputs, outputs, fee } = (0, import_coinselect.default)(
|
3726
|
+
utxos,
|
3727
|
+
[{ address: btcAddress, value: userSatoshis }],
|
3728
|
+
Math.ceil(feeRate || 0)
|
3729
|
+
);
|
3730
|
+
const newInputs = inputs;
|
3731
|
+
let newOutputs = outputs;
|
3732
|
+
let newFee = fee;
|
3733
|
+
if (!newOutputs || newOutputs.length === 0) {
|
3734
|
+
return {
|
3735
|
+
withdrawFee,
|
3736
|
+
isError: true,
|
3737
|
+
errorMsg: "The network is busy, please try again later."
|
3738
|
+
};
|
3739
|
+
}
|
3740
|
+
let userOutput, noUserOutput;
|
3741
|
+
for (let i = 0; i < newOutputs.length; i++) {
|
3742
|
+
const output = newOutputs[i];
|
3743
|
+
if (output.value.toString() === userSatoshis.toString()) {
|
3744
|
+
userOutput = output;
|
3745
|
+
} else {
|
3746
|
+
noUserOutput = output;
|
3747
|
+
}
|
3748
|
+
if (!output.address) {
|
3749
|
+
output.address = withdrawChangeAddress;
|
3750
|
+
}
|
3751
|
+
}
|
3752
|
+
let dis = 0;
|
3753
|
+
if (newFee > maxBtcFee) {
|
3754
|
+
dis = newFee - maxBtcFee;
|
3755
|
+
newFee = maxBtcFee;
|
3756
|
+
return {
|
3757
|
+
gasFee: newFee,
|
3758
|
+
withdrawFee,
|
3759
|
+
isError: true,
|
3760
|
+
errorMsg: "Gas exceeds maximum value"
|
3761
|
+
};
|
3762
|
+
}
|
3763
|
+
userOutput.value = new import_big.default(userOutput.value).minus(newFee).minus(withdrawFee).toNumber();
|
3764
|
+
if (userOutput.value < 0) {
|
3765
|
+
return {
|
3766
|
+
gasFee: newFee,
|
3767
|
+
withdrawFee,
|
3768
|
+
isError: true,
|
3769
|
+
errorMsg: "Not enough gas"
|
3770
|
+
};
|
3771
|
+
}
|
3772
|
+
if (noUserOutput) {
|
3773
|
+
if (!noUserOutput.address) {
|
3774
|
+
noUserOutput.address = withdrawChangeAddress;
|
3775
|
+
}
|
3776
|
+
noUserOutput.value = new import_big.default(noUserOutput.value).plus(newFee).plus(withdrawFee).plus(dis).toNumber();
|
3777
|
+
} else {
|
3778
|
+
noUserOutput = {
|
3779
|
+
address: withdrawChangeAddress,
|
3780
|
+
value: new import_big.default(newFee).plus(withdrawFee).plus(dis).toNumber()
|
3781
|
+
};
|
3782
|
+
newOutputs.push(noUserOutput);
|
3783
|
+
}
|
3784
|
+
let minValue = Math.min(...newInputs.map((input) => input.value));
|
3785
|
+
let totalNoUserOutputValue = noUserOutput.value;
|
3786
|
+
while (totalNoUserOutputValue >= minValue && minValue > 0 && newInputs.length > 0) {
|
3787
|
+
totalNoUserOutputValue -= minValue;
|
3788
|
+
noUserOutput.value = totalNoUserOutputValue;
|
3789
|
+
const minValueIndex = newInputs.findIndex((input) => input.value === minValue);
|
3790
|
+
if (minValueIndex > -1) {
|
3791
|
+
newInputs.splice(minValueIndex, 1);
|
3792
|
+
}
|
3793
|
+
minValue = Math.min(...newInputs.map((input) => input.value));
|
3794
|
+
}
|
3795
|
+
let gasMore = 0;
|
3796
|
+
if (noUserOutput.value === 0) {
|
3797
|
+
newOutputs = newOutputs.filter((item) => item.value !== 0);
|
3798
|
+
} else if (noUserOutput.value < Number(brgConfig.min_change_amount)) {
|
3799
|
+
gasMore = Number(brgConfig.min_change_amount) - noUserOutput.value;
|
3800
|
+
userOutput.value -= gasMore;
|
3801
|
+
noUserOutput.value = Number(brgConfig.min_change_amount);
|
3802
|
+
}
|
3803
|
+
const insufficientOutput = newOutputs.some((item) => item.value < 0);
|
3804
|
+
if (insufficientOutput) {
|
3805
|
+
return {
|
3806
|
+
gasFee: newFee,
|
3807
|
+
withdrawFee,
|
3808
|
+
isError: true,
|
3809
|
+
errorMsg: "Not enough gas"
|
3810
|
+
};
|
3811
|
+
}
|
3812
|
+
const inputSum = newInputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
3813
|
+
const outputSum = newOutputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
3814
|
+
if (newFee + outputSum !== inputSum) {
|
3815
|
+
return {
|
3816
|
+
withdrawFee,
|
3817
|
+
isError: true,
|
3818
|
+
errorMsg: "Service busy, please try again later"
|
3819
|
+
};
|
3820
|
+
}
|
3821
|
+
return {
|
3822
|
+
withdrawFee: new import_big.default(withdrawFee).plus(gasLimit).plus(gasMore).toNumber(),
|
3823
|
+
gasFee: new import_big.default(newFee).toNumber(),
|
3824
|
+
inputs: newInputs,
|
3825
|
+
outputs: newOutputs,
|
3826
|
+
fromAmount: satoshis,
|
3827
|
+
receiveAmount: userOutput.value,
|
3828
|
+
isError: false
|
3829
|
+
};
|
3830
|
+
} catch (error) {
|
3831
|
+
return {
|
3832
|
+
withdrawFee: 0,
|
3833
|
+
isError: true,
|
3834
|
+
errorMsg: error.message
|
3835
|
+
};
|
3836
|
+
}
|
3837
|
+
});
|
3838
|
+
}
|
3633
3839
|
|
3634
3840
|
// src/core/btcUtils.ts
|
3635
3841
|
var import_bitcoinjs_lib = __toESM(require("bitcoinjs-lib"), 1);
|
3636
3842
|
var ecc = __toESM(require("@bitcoinerlab/secp256k1"), 1);
|
3637
|
-
var import_coinselect = __toESM(require("coinselect"), 1);
|
3638
3843
|
import_bitcoinjs_lib.default.initEccLib(ecc);
|
3639
3844
|
var NEAR_STORAGE_DEPOSIT_AMOUNT = "1250000000000000000000";
|
3640
3845
|
var NBTC_STORAGE_DEPOSIT_AMOUNT = "3000";
|
@@ -3758,7 +3963,6 @@ function getDepositAmount(amount, option) {
|
|
3758
3963
|
var _a;
|
3759
3964
|
const env = (option == null ? void 0 : option.env) || "mainnet";
|
3760
3965
|
const _newAccountMinDepositAmount = (_a = option == null ? void 0 : option.newAccountMinDepositAmount) != null ? _a : true;
|
3761
|
-
const config = getWalletConfig(env);
|
3762
3966
|
const csna = yield getCsnaAccountId(env);
|
3763
3967
|
const accountInfo = yield getAccountInfo({ csna, env });
|
3764
3968
|
const debtAction = yield checkGasTokenDebt(csna, env, false);
|
@@ -3766,7 +3970,7 @@ function getDepositAmount(amount, option) {
|
|
3766
3970
|
const {
|
3767
3971
|
deposit_bridge_fee: { fee_min, fee_rate },
|
3768
3972
|
min_deposit_amount
|
3769
|
-
} = yield
|
3973
|
+
} = yield getBridgeConfig({ env });
|
3770
3974
|
const depositAmount = Math.max(Number(min_deposit_amount), Number(amount));
|
3771
3975
|
const protocolFee = Math.max(Number(fee_min), Number(depositAmount) * fee_rate);
|
3772
3976
|
const newAccountMinDepositAmount = !(accountInfo == null ? void 0 : accountInfo.nonce) && _newAccountMinDepositAmount ? NEW_ACCOUNT_MIN_DEPOSIT_AMOUNT : 0;
|
@@ -3958,190 +4162,52 @@ function getWithdrawTransaction(_0) {
|
|
3958
4162
|
feeRate,
|
3959
4163
|
env = "mainnet"
|
3960
4164
|
}) {
|
3961
|
-
|
4165
|
+
const config = getWalletConfig(env);
|
3962
4166
|
const provider = getBtcProvider();
|
3963
4167
|
const btcAddress = provider.account;
|
3964
|
-
const config = getWalletConfig(env);
|
3965
4168
|
const csna = yield getCsnaAccountId(env);
|
3966
|
-
const
|
3967
|
-
|
3968
|
-
|
3969
|
-
|
3970
|
-
}
|
3971
|
-
}
|
3972
|
-
const feePercent = Number(brgConfig.withdraw_bridge_fee.fee_rate) * Number(amount);
|
3973
|
-
const withdrawFee = feePercent > Number(brgConfig.withdraw_bridge_fee.fee_min) ? feePercent : Number(brgConfig.withdraw_bridge_fee.fee_min);
|
3974
|
-
console.log("Withdrawal Fee:", {
|
3975
|
-
feePercent,
|
3976
|
-
withdrawFee,
|
3977
|
-
minFee: brgConfig.withdraw_bridge_fee.fee_min
|
3978
|
-
});
|
3979
|
-
const gasLimit = yield calculateGasLimit({
|
4169
|
+
const _feeRate = feeRate || (yield getBtcGasPrice());
|
4170
|
+
const _a = yield calculateWithdraw({
|
4171
|
+
amount,
|
4172
|
+
feeRate: _feeRate,
|
3980
4173
|
csna,
|
3981
|
-
|
3982
|
-
{
|
3983
|
-
signerId: "",
|
3984
|
-
receiverId: config.btcToken,
|
3985
|
-
actions: [
|
3986
|
-
{
|
3987
|
-
type: "FunctionCall",
|
3988
|
-
params: {
|
3989
|
-
methodName: "ft_transfer_call",
|
3990
|
-
args: {
|
3991
|
-
receiver_id: config.btcToken,
|
3992
|
-
amount: "100",
|
3993
|
-
msg: ""
|
3994
|
-
},
|
3995
|
-
gas: "300000000000000",
|
3996
|
-
deposit: "1"
|
3997
|
-
}
|
3998
|
-
}
|
3999
|
-
]
|
4000
|
-
}
|
4001
|
-
],
|
4174
|
+
btcAddress,
|
4002
4175
|
env
|
4003
|
-
});
|
4004
|
-
|
4005
|
-
|
4006
|
-
console.log("All UTXOs:", allUTXO);
|
4007
|
-
if (!allUTXO || Object.keys(allUTXO).length === 0) {
|
4008
|
-
throw new Error("The network is busy, please try again later.");
|
4176
|
+
}), { inputs, outputs, isError, errorMsg } = _a, rest = __objRest(_a, ["inputs", "outputs", "isError", "errorMsg"]);
|
4177
|
+
if (isError || !inputs || !outputs) {
|
4178
|
+
throw new Error(errorMsg);
|
4009
4179
|
}
|
4010
|
-
|
4011
|
-
|
4012
|
-
|
4013
|
-
|
4014
|
-
|
4015
|
-
value: Number(allUTXO[key].balance),
|
4016
|
-
script: allUTXO[key].script
|
4017
|
-
};
|
4018
|
-
});
|
4019
|
-
console.log("Formatted UTXOs:", utxos);
|
4020
|
-
const _feeRate = feeRate || (yield getBtcGasPrice());
|
4021
|
-
console.log("Fee Rate:", _feeRate);
|
4022
|
-
const coinSelectResult = (0, import_coinselect.default)(
|
4023
|
-
utxos,
|
4024
|
-
[{ address: btcAddress, value: Number(finalAmount) }],
|
4025
|
-
Math.ceil(_feeRate)
|
4180
|
+
console.log("inputs:", JSON.stringify(inputs));
|
4181
|
+
console.log("outputs:", JSON.stringify(outputs));
|
4182
|
+
console.log("inputs - outputs = gas");
|
4183
|
+
console.log(
|
4184
|
+
`(${inputs.map((item) => item.value).join(" + ")}) - (${outputs.map((item) => item.value).join(" + ")}) = ${rest.gasFee}`
|
4026
4185
|
);
|
4027
|
-
console.log("Coinselect Result:", coinSelectResult);
|
4028
|
-
const { inputs, outputs, fee } = coinSelectResult;
|
4029
|
-
if (!outputs || !inputs) {
|
4030
|
-
throw new Error("The network is busy, please try again later.");
|
4031
|
-
}
|
4032
|
-
const maxBtcFee = Number(brgConfig.max_btc_gas_fee);
|
4033
|
-
const transactionFee = fee;
|
4034
|
-
console.log("Transaction Fee:", { transactionFee, maxBtcFee });
|
4035
|
-
if (transactionFee > maxBtcFee) {
|
4036
|
-
throw new Error("Gas exceeds maximum value");
|
4037
|
-
}
|
4038
|
-
let recipientOutput, changeOutput;
|
4039
|
-
for (let i = 0; i < outputs.length; i++) {
|
4040
|
-
const output = outputs[i];
|
4041
|
-
if (output.value.toString() === finalAmount.toString()) {
|
4042
|
-
recipientOutput = output;
|
4043
|
-
} else {
|
4044
|
-
changeOutput = output;
|
4045
|
-
}
|
4046
|
-
if (!output.address) {
|
4047
|
-
output.address = brgConfig.change_address;
|
4048
|
-
}
|
4049
|
-
}
|
4050
|
-
console.log("Initial Outputs:", { recipientOutput, changeOutput });
|
4051
|
-
recipientOutput.value = new import_big2.default(recipientOutput.value).minus(transactionFee).minus(withdrawFee).toNumber();
|
4052
|
-
if (changeOutput) {
|
4053
|
-
changeOutput.value = new import_big2.default(changeOutput.value).plus(transactionFee).plus(withdrawFee).toNumber();
|
4054
|
-
const remainingInputs = [...inputs];
|
4055
|
-
let smallestInput = Math.min.apply(
|
4056
|
-
null,
|
4057
|
-
remainingInputs.map((input) => input.value)
|
4058
|
-
);
|
4059
|
-
let remainingChangeAmount = changeOutput.value;
|
4060
|
-
console.log("Initial Change Processing:", { smallestInput, remainingChangeAmount });
|
4061
|
-
while (remainingChangeAmount >= smallestInput && smallestInput > 0 && remainingInputs.length > 0) {
|
4062
|
-
remainingChangeAmount -= smallestInput;
|
4063
|
-
changeOutput.value = remainingChangeAmount;
|
4064
|
-
const smallestInputIndex = remainingInputs.findIndex(
|
4065
|
-
(input) => input.value === smallestInput
|
4066
|
-
);
|
4067
|
-
if (smallestInputIndex > -1) {
|
4068
|
-
remainingInputs.splice(smallestInputIndex, 1);
|
4069
|
-
}
|
4070
|
-
smallestInput = Math.min.apply(
|
4071
|
-
null,
|
4072
|
-
remainingInputs.map((input) => input.value)
|
4073
|
-
);
|
4074
|
-
console.log("Change Processing Loop:", {
|
4075
|
-
remainingChangeAmount,
|
4076
|
-
smallestInput,
|
4077
|
-
remainingInputsCount: remainingInputs.length
|
4078
|
-
});
|
4079
|
-
}
|
4080
|
-
const minChangeAmount = Number(brgConfig.min_change_amount);
|
4081
|
-
let additionalFee = 0;
|
4082
|
-
console.log("Checking minimum change amount:", {
|
4083
|
-
changeValue: changeOutput.value,
|
4084
|
-
minChangeAmount
|
4085
|
-
});
|
4086
|
-
let finalOutputs = [...outputs];
|
4087
|
-
if (changeOutput.value === 0) {
|
4088
|
-
finalOutputs = finalOutputs.filter((output) => output.value !== 0);
|
4089
|
-
console.log("Removed zero-value change output", finalOutputs);
|
4090
|
-
} else if (changeOutput.value < minChangeAmount) {
|
4091
|
-
additionalFee = minChangeAmount - changeOutput.value;
|
4092
|
-
recipientOutput.value -= additionalFee;
|
4093
|
-
changeOutput.value = minChangeAmount;
|
4094
|
-
console.log("Adjusted for minimum change amount:", {
|
4095
|
-
additionalFee,
|
4096
|
-
newRecipientValue: recipientOutput.value,
|
4097
|
-
newChangeValue: changeOutput.value
|
4098
|
-
});
|
4099
|
-
}
|
4100
|
-
} else {
|
4101
|
-
changeOutput = {
|
4102
|
-
address: brgConfig.change_address,
|
4103
|
-
value: new import_big2.default(transactionFee).plus(withdrawFee).toNumber()
|
4104
|
-
};
|
4105
|
-
outputs.push(changeOutput);
|
4106
|
-
console.log("Created new change output:", changeOutput);
|
4107
|
-
}
|
4108
|
-
const insufficientOutput = outputs.some((item) => item.value < 0);
|
4109
|
-
if (insufficientOutput) {
|
4110
|
-
console.error("Negative output value detected");
|
4111
|
-
throw new Error("Not enough gas");
|
4112
|
-
}
|
4113
|
-
const inputSum = inputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
4114
|
-
const outputSum = outputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
4115
|
-
console.log("Balance verification:", { inputSum, outputSum, transactionFee });
|
4116
|
-
if (transactionFee + outputSum !== inputSum) {
|
4117
|
-
console.error("Balance mismatch:", { inputSum, outputSum, transactionFee });
|
4118
|
-
throw new Error("compute error");
|
4119
|
-
}
|
4120
4186
|
const network = yield getNetwork();
|
4121
4187
|
const btcNetwork = network === "mainnet" ? import_bitcoinjs_lib.default.networks.bitcoin : import_bitcoinjs_lib.default.networks.testnet;
|
4122
4188
|
const psbt = new import_bitcoinjs_lib.default.Psbt({ network: btcNetwork });
|
4123
4189
|
const btcRpcUrl = yield getBtcRpcUrl();
|
4124
|
-
|
4125
|
-
|
4126
|
-
|
4127
|
-
|
4128
|
-
|
4129
|
-
|
4130
|
-
|
4131
|
-
|
4132
|
-
|
4133
|
-
|
4134
|
-
|
4135
|
-
|
4136
|
-
|
4137
|
-
|
4190
|
+
Promise.all(
|
4191
|
+
inputs.map((input) => __async(this, null, function* () {
|
4192
|
+
const txData = yield fetch(`${btcRpcUrl}/tx/${input.txid}`).then((res) => res.json());
|
4193
|
+
const inputOptions = {
|
4194
|
+
hash: input.txid,
|
4195
|
+
index: input.vout,
|
4196
|
+
sequence: 4294967293,
|
4197
|
+
witnessUtxo: {
|
4198
|
+
script: Buffer.from(txData.vout[input.vout].scriptpubkey, "hex"),
|
4199
|
+
value: input.value
|
4200
|
+
}
|
4201
|
+
};
|
4202
|
+
psbt.addInput(inputOptions);
|
4203
|
+
}))
|
4204
|
+
);
|
4138
4205
|
outputs.forEach((output) => {
|
4139
4206
|
psbt.addOutput({
|
4140
4207
|
address: output.address,
|
4141
4208
|
value: output.value
|
4142
4209
|
});
|
4143
4210
|
});
|
4144
|
-
console.log("outputs:", JSON.stringify(outputs));
|
4145
4211
|
const _inputs = inputs.map((item) => {
|
4146
4212
|
return `${item.txid}:${item.vout}`;
|
4147
4213
|
});
|
@@ -4436,8 +4502,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4436
4502
|
signMessage,
|
4437
4503
|
isSignedIn,
|
4438
4504
|
signAndSendTransaction,
|
4439
|
-
signAndSendTransactions
|
4440
|
-
calculateGasLimit
|
4505
|
+
signAndSendTransactions
|
4441
4506
|
};
|
4442
4507
|
const env = metadata.env || options.network.networkId || "mainnet";
|
4443
4508
|
const currentConfig = getWalletConfig(env);
|
@@ -4726,7 +4791,7 @@ function setupBTCWallet({
|
|
4726
4791
|
|
4727
4792
|
// src/index.ts
|
4728
4793
|
var getVersion = () => {
|
4729
|
-
return "0.5.
|
4794
|
+
return "0.5.22-beta";
|
4730
4795
|
};
|
4731
4796
|
if (typeof window !== "undefined") {
|
4732
4797
|
window.__BTC_WALLET_VERSION = getVersion();
|