btc-wallet 0.5.43-beta → 0.5.45-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/core/btcUtils.d.ts +20 -2
- package/dist/index.d.ts +0 -1
- package/dist/index.js +212 -228
- package/dist/index.js.map +3 -3
- package/dist/utils/satoshi.d.ts +0 -18
- package/esm/index.js +212 -228
- package/esm/index.js.map +3 -3
- package/package.json +1 -1
package/dist/utils/satoshi.d.ts
CHANGED
@@ -86,22 +86,4 @@ export declare function calculateGasStrategy({ csna, transactions, env, }: Calcu
|
|
86
86
|
useNearPayGas: boolean;
|
87
87
|
gasLimit: string;
|
88
88
|
}>;
|
89
|
-
interface CalculateWithdrawParams {
|
90
|
-
amount: string | number;
|
91
|
-
feeRate?: number;
|
92
|
-
csna: string;
|
93
|
-
btcAddress: string;
|
94
|
-
env: ENV;
|
95
|
-
}
|
96
|
-
interface CalculateWithdrawResult {
|
97
|
-
withdrawFee: number;
|
98
|
-
gasFee?: number;
|
99
|
-
inputs?: any[];
|
100
|
-
outputs?: any[];
|
101
|
-
fromAmount?: number;
|
102
|
-
receiveAmount?: string;
|
103
|
-
isError: boolean;
|
104
|
-
errorMsg?: string;
|
105
|
-
}
|
106
|
-
export declare function calculateWithdraw({ amount, feeRate, csna, btcAddress, env, }: CalculateWithdrawParams): Promise<CalculateWithdrawResult>;
|
107
89
|
export {};
|
package/esm/index.js
CHANGED
@@ -3142,7 +3142,6 @@ var state_default = {
|
|
3142
3142
|
};
|
3143
3143
|
|
3144
3144
|
// src/utils/satoshi.ts
|
3145
|
-
import coinselect from "coinselect";
|
3146
3145
|
function getNonce(url, accountId) {
|
3147
3146
|
return __async(this, null, function* () {
|
3148
3147
|
const { result_code, result_message, result_data } = yield request(
|
@@ -3259,22 +3258,6 @@ function getWhitelist(url) {
|
|
3259
3258
|
return data;
|
3260
3259
|
});
|
3261
3260
|
}
|
3262
|
-
function receiveWithdrawMsg(url, txHash) {
|
3263
|
-
return __async(this, null, function* () {
|
3264
|
-
const { result_code, result_message, result_data } = yield request(
|
3265
|
-
`${url}/v1/receiveWithdrawMsg`,
|
3266
|
-
{
|
3267
|
-
method: "POST",
|
3268
|
-
body: { txHash }
|
3269
|
-
}
|
3270
|
-
);
|
3271
|
-
console.log("receiveWithdrawMsg resp:", { result_code, result_message, result_data });
|
3272
|
-
if (result_code !== 0) {
|
3273
|
-
throw new Error(result_message);
|
3274
|
-
}
|
3275
|
-
return result_data;
|
3276
|
-
});
|
3277
|
-
}
|
3278
3261
|
function getAccountInfo(_0) {
|
3279
3262
|
return __async(this, arguments, function* ({ csna, env }) {
|
3280
3263
|
const config = getWalletConfig(env);
|
@@ -3600,196 +3583,10 @@ function getPredictedGasAmount(_0) {
|
|
3600
3583
|
return gasAmount.toString();
|
3601
3584
|
});
|
3602
3585
|
}
|
3603
|
-
function calculateWithdraw(_0) {
|
3604
|
-
return __async(this, arguments, function* ({
|
3605
|
-
amount,
|
3606
|
-
feeRate,
|
3607
|
-
csna,
|
3608
|
-
btcAddress,
|
3609
|
-
env
|
3610
|
-
}) {
|
3611
|
-
try {
|
3612
|
-
const config = getWalletConfig(env);
|
3613
|
-
const _feeRate = feeRate || (yield getBtcGasPrice());
|
3614
|
-
const gasLimit = yield calculateGasLimit({
|
3615
|
-
csna,
|
3616
|
-
transactions: [
|
3617
|
-
{
|
3618
|
-
signerId: "",
|
3619
|
-
receiverId: config.btcToken,
|
3620
|
-
actions: [
|
3621
|
-
{
|
3622
|
-
type: "FunctionCall",
|
3623
|
-
params: {
|
3624
|
-
methodName: "ft_transfer_call",
|
3625
|
-
args: {
|
3626
|
-
receiver_id: config.btcToken,
|
3627
|
-
amount: "100",
|
3628
|
-
msg: ""
|
3629
|
-
},
|
3630
|
-
gas: "300000000000000",
|
3631
|
-
deposit: "1"
|
3632
|
-
}
|
3633
|
-
}
|
3634
|
-
]
|
3635
|
-
}
|
3636
|
-
],
|
3637
|
-
env
|
3638
|
-
});
|
3639
|
-
let satoshis = Number(amount);
|
3640
|
-
if (Number(gasLimit) > 0) {
|
3641
|
-
satoshis = new Big(amount).minus(gasLimit).toNumber();
|
3642
|
-
}
|
3643
|
-
const brgConfig = yield getBridgeConfig({ env });
|
3644
|
-
const allUTXO = yield nearCallFunction(config.bridgeContractId, "get_utxos_paged", {}, { network: config.network });
|
3645
|
-
if (brgConfig.min_withdraw_amount) {
|
3646
|
-
if (Number(satoshis) < Number(brgConfig.min_withdraw_amount)) {
|
3647
|
-
return {
|
3648
|
-
withdrawFee: 0,
|
3649
|
-
isError: true,
|
3650
|
-
errorMsg: `Mini withdraw amount is ${Number(brgConfig.min_withdraw_amount) + Number(gasLimit)} sats`
|
3651
|
-
};
|
3652
|
-
}
|
3653
|
-
}
|
3654
|
-
const feePercent = Number(brgConfig.withdraw_bridge_fee.fee_rate) * Number(satoshis);
|
3655
|
-
const withdrawFee = feePercent > Number(brgConfig.withdraw_bridge_fee.fee_min) ? feePercent : Number(brgConfig.withdraw_bridge_fee.fee_min);
|
3656
|
-
const withdrawChangeAddress = brgConfig.change_address;
|
3657
|
-
const utxos = Object.keys(allUTXO).map((key) => {
|
3658
|
-
const txid = key.split("@");
|
3659
|
-
return {
|
3660
|
-
txid: txid[0],
|
3661
|
-
vout: allUTXO[key].vout,
|
3662
|
-
value: Number(allUTXO[key].balance),
|
3663
|
-
script: allUTXO[key].script
|
3664
|
-
};
|
3665
|
-
}).filter((utxo) => utxo.value > Number(brgConfig.min_change_amount));
|
3666
|
-
if (!utxos || utxos.length === 0) {
|
3667
|
-
return {
|
3668
|
-
withdrawFee,
|
3669
|
-
isError: true,
|
3670
|
-
errorMsg: "The network is busy, please try again later."
|
3671
|
-
};
|
3672
|
-
}
|
3673
|
-
const userSatoshis = Number(satoshis);
|
3674
|
-
const maxBtcFee = Number(brgConfig.max_btc_gas_fee);
|
3675
|
-
const { inputs, outputs, fee } = coinselect(
|
3676
|
-
utxos,
|
3677
|
-
[{ address: btcAddress, value: userSatoshis }],
|
3678
|
-
Math.ceil(_feeRate)
|
3679
|
-
);
|
3680
|
-
const newInputs = inputs;
|
3681
|
-
let newOutputs = outputs;
|
3682
|
-
let newFee = fee;
|
3683
|
-
if (!newOutputs || newOutputs.length === 0) {
|
3684
|
-
return {
|
3685
|
-
withdrawFee,
|
3686
|
-
isError: true,
|
3687
|
-
errorMsg: "The network is busy, please try again later."
|
3688
|
-
};
|
3689
|
-
}
|
3690
|
-
let userOutput, noUserOutput;
|
3691
|
-
for (let i = 0; i < newOutputs.length; i++) {
|
3692
|
-
const output = newOutputs[i];
|
3693
|
-
if (output.value.toString() === userSatoshis.toString()) {
|
3694
|
-
userOutput = output;
|
3695
|
-
} else {
|
3696
|
-
noUserOutput = output;
|
3697
|
-
}
|
3698
|
-
if (!output.address) {
|
3699
|
-
output.address = withdrawChangeAddress;
|
3700
|
-
}
|
3701
|
-
}
|
3702
|
-
let dis = 0;
|
3703
|
-
if (newFee > maxBtcFee) {
|
3704
|
-
dis = newFee - maxBtcFee;
|
3705
|
-
newFee = maxBtcFee;
|
3706
|
-
return {
|
3707
|
-
gasFee: newFee,
|
3708
|
-
withdrawFee,
|
3709
|
-
isError: true,
|
3710
|
-
errorMsg: "Gas exceeds maximum value"
|
3711
|
-
};
|
3712
|
-
}
|
3713
|
-
userOutput.value = new Big(userOutput.value).minus(newFee).minus(withdrawFee).toNumber();
|
3714
|
-
if (userOutput.value < 0) {
|
3715
|
-
return {
|
3716
|
-
gasFee: newFee,
|
3717
|
-
withdrawFee,
|
3718
|
-
isError: true,
|
3719
|
-
errorMsg: "Not enough gas"
|
3720
|
-
};
|
3721
|
-
}
|
3722
|
-
if (noUserOutput) {
|
3723
|
-
if (!noUserOutput.address) {
|
3724
|
-
noUserOutput.address = withdrawChangeAddress;
|
3725
|
-
}
|
3726
|
-
noUserOutput.value = new Big(noUserOutput.value).plus(newFee).plus(withdrawFee).plus(dis).toNumber();
|
3727
|
-
} else {
|
3728
|
-
noUserOutput = {
|
3729
|
-
address: withdrawChangeAddress,
|
3730
|
-
value: new Big(newFee).plus(withdrawFee).plus(dis).toNumber()
|
3731
|
-
};
|
3732
|
-
newOutputs.push(noUserOutput);
|
3733
|
-
}
|
3734
|
-
let minValue = Math.min(...newInputs.map((input) => input.value));
|
3735
|
-
let totalNoUserOutputValue = noUserOutput.value;
|
3736
|
-
while (totalNoUserOutputValue >= minValue && minValue > 0 && newInputs.length > 0) {
|
3737
|
-
totalNoUserOutputValue -= minValue;
|
3738
|
-
noUserOutput.value = totalNoUserOutputValue;
|
3739
|
-
const minValueIndex = newInputs.findIndex((input) => input.value === minValue);
|
3740
|
-
if (minValueIndex > -1) {
|
3741
|
-
newInputs.splice(minValueIndex, 1);
|
3742
|
-
}
|
3743
|
-
minValue = Math.min(...newInputs.map((input) => input.value));
|
3744
|
-
}
|
3745
|
-
let gasMore = 0;
|
3746
|
-
if (noUserOutput.value === 0) {
|
3747
|
-
newOutputs = newOutputs.filter((item) => item.value !== 0);
|
3748
|
-
} else if (noUserOutput.value < Number(brgConfig.min_change_amount)) {
|
3749
|
-
gasMore = Number(brgConfig.min_change_amount) - noUserOutput.value;
|
3750
|
-
userOutput.value -= gasMore;
|
3751
|
-
noUserOutput.value = Number(brgConfig.min_change_amount);
|
3752
|
-
}
|
3753
|
-
const insufficientOutput = newOutputs.some((item) => item.value < 0);
|
3754
|
-
if (insufficientOutput) {
|
3755
|
-
return {
|
3756
|
-
gasFee: newFee,
|
3757
|
-
withdrawFee,
|
3758
|
-
isError: true,
|
3759
|
-
errorMsg: "Not enough gas"
|
3760
|
-
};
|
3761
|
-
}
|
3762
|
-
const inputSum = newInputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
3763
|
-
const outputSum = newOutputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
3764
|
-
if (newFee + outputSum !== inputSum) {
|
3765
|
-
return {
|
3766
|
-
withdrawFee,
|
3767
|
-
isError: true,
|
3768
|
-
errorMsg: "Service busy, please try again later"
|
3769
|
-
};
|
3770
|
-
}
|
3771
|
-
return {
|
3772
|
-
withdrawFee: new Big(withdrawFee).plus(gasLimit).plus(gasMore).toNumber(),
|
3773
|
-
gasFee: new Big(newFee).toNumber(),
|
3774
|
-
inputs: newInputs,
|
3775
|
-
outputs: newOutputs,
|
3776
|
-
fromAmount: satoshis,
|
3777
|
-
receiveAmount: userOutput.value,
|
3778
|
-
isError: false
|
3779
|
-
};
|
3780
|
-
} catch (error) {
|
3781
|
-
return {
|
3782
|
-
withdrawFee: 0,
|
3783
|
-
isError: true,
|
3784
|
-
errorMsg: error.message
|
3785
|
-
};
|
3786
|
-
}
|
3787
|
-
});
|
3788
|
-
}
|
3789
3586
|
|
3790
3587
|
// src/core/btcUtils.ts
|
3791
3588
|
import * as bitcoin from "bitcoinjs-lib";
|
3792
|
-
import
|
3589
|
+
import coinselect from "coinselect";
|
3793
3590
|
import * as ecc from "@bitcoinerlab/secp256k1";
|
3794
3591
|
bitcoin.initEccLib(ecc);
|
3795
3592
|
var NEAR_STORAGE_DEPOSIT_AMOUNT = "1250000000000000000000";
|
@@ -3885,7 +3682,7 @@ function calculateGasFee(account, amount, feeRate) {
|
|
3885
3682
|
return __async(this, null, function* () {
|
3886
3683
|
const _feeRate = feeRate || (yield getBtcGasPrice());
|
3887
3684
|
const utxos = yield getBtcUtxos(account);
|
3888
|
-
const { fee } =
|
3685
|
+
const { fee } = coinselect(utxos, [{ address: account, value: amount }], Math.ceil(_feeRate));
|
3889
3686
|
console.log("calculateGasFee fee:", fee);
|
3890
3687
|
return fee;
|
3891
3688
|
});
|
@@ -3935,19 +3732,23 @@ function getDepositAmount(amount, option) {
|
|
3935
3732
|
const accountInfo = yield getAccountInfo({ csna, env });
|
3936
3733
|
const debtAction = yield checkGasTokenDebt(csna, env, false);
|
3937
3734
|
const repayAmount = (debtAction == null ? void 0 : debtAction.amount) || 0;
|
3735
|
+
const depositAmount = Number(amount);
|
3938
3736
|
const {
|
3939
3737
|
deposit_bridge_fee: { fee_min, fee_rate },
|
3940
3738
|
min_deposit_amount
|
3941
3739
|
} = yield getBridgeConfig({ env });
|
3942
|
-
const
|
3943
|
-
const protocolFee = Math.max(Number(fee_min), Number(depositAmount) * fee_rate);
|
3740
|
+
const protocolFee = Math.max(Number(fee_min), depositAmount * fee_rate);
|
3944
3741
|
const newAccountMinDepositAmount = !(accountInfo == null ? void 0 : accountInfo.nonce) && _newAccountMinDepositAmount ? NEW_ACCOUNT_MIN_DEPOSIT_AMOUNT : 0;
|
3945
|
-
|
3742
|
+
let receiveAmount = new Big2(depositAmount).minus(protocolFee).minus(repayAmount).round(0, Big2.roundDown).toNumber();
|
3743
|
+
receiveAmount = Math.max(receiveAmount, 0);
|
3946
3744
|
if (Number(newAccountMinDepositAmount) > 0 && receiveAmount < Number(newAccountMinDepositAmount)) {
|
3947
|
-
|
3948
|
-
`Receive amount (${receiveAmount}) is less than minimum required amount for new account (${newAccountMinDepositAmount})`
|
3745
|
+
console.error(
|
3746
|
+
`Receive amount (${receiveAmount}) is less than minimum required amount for new account (${newAccountMinDepositAmount} sat)`
|
3949
3747
|
);
|
3950
3748
|
}
|
3749
|
+
if (receiveAmount <= 0) {
|
3750
|
+
console.error(`Receive amount (${receiveAmount}) is less than 0`);
|
3751
|
+
}
|
3951
3752
|
return {
|
3952
3753
|
depositAmount,
|
3953
3754
|
receiveAmount,
|
@@ -4007,17 +3808,20 @@ function executeBTCDepositAndAction(_0) {
|
|
4007
3808
|
throw new Error("BTC Public Key is not available.");
|
4008
3809
|
}
|
4009
3810
|
if (!amount && !action) {
|
4010
|
-
throw new Error("amount or action is required");
|
3811
|
+
throw new Error("Deposit amount or action is required");
|
4011
3812
|
}
|
4012
3813
|
const csna = yield getCsnaAccountId(env);
|
4013
3814
|
const depositAmount = (_a = action ? action.amount : amount) != null ? _a : "0";
|
4014
3815
|
if (new Big2(depositAmount).lt(0)) {
|
4015
|
-
throw new Error("amount must be greater than 0");
|
3816
|
+
throw new Error("Deposit amount must be greater than 0");
|
4016
3817
|
}
|
4017
3818
|
const { receiveAmount, protocolFee, repayAmount } = yield getDepositAmount(depositAmount, {
|
4018
3819
|
env,
|
4019
3820
|
newAccountMinDepositAmount
|
4020
3821
|
});
|
3822
|
+
if (receiveAmount <= 0) {
|
3823
|
+
throw new Error("Invalid deposit amount, too small");
|
3824
|
+
}
|
4021
3825
|
const accountInfo = yield getAccountInfo({ csna, env });
|
4022
3826
|
const newActions = [];
|
4023
3827
|
const debtAction = yield checkGasTokenDebt(csna, env, false);
|
@@ -4225,6 +4029,201 @@ function getWithdrawTransaction(_0) {
|
|
4225
4029
|
return transaction;
|
4226
4030
|
});
|
4227
4031
|
}
|
4032
|
+
function calculateWithdraw(_0) {
|
4033
|
+
return __async(this, arguments, function* ({
|
4034
|
+
amount,
|
4035
|
+
feeRate: _feeRate,
|
4036
|
+
csna: _csna,
|
4037
|
+
btcAddress: _btcAddress,
|
4038
|
+
env
|
4039
|
+
}) {
|
4040
|
+
try {
|
4041
|
+
const config = getWalletConfig(env);
|
4042
|
+
let btcAddress = _btcAddress || getBtcProvider().account;
|
4043
|
+
if (!btcAddress) {
|
4044
|
+
yield getBtcProvider().autoConnect();
|
4045
|
+
btcAddress = getBtcProvider().account;
|
4046
|
+
if (!btcAddress) {
|
4047
|
+
throw new Error("BTC Account is not available.");
|
4048
|
+
}
|
4049
|
+
}
|
4050
|
+
const csna = _csna || (yield getCsnaAccountId(env));
|
4051
|
+
const feeRate = _feeRate || (yield getBtcGasPrice());
|
4052
|
+
const gasLimit = yield calculateGasLimit({
|
4053
|
+
csna,
|
4054
|
+
transactions: [
|
4055
|
+
{
|
4056
|
+
signerId: "",
|
4057
|
+
receiverId: config.btcToken,
|
4058
|
+
actions: [
|
4059
|
+
{
|
4060
|
+
type: "FunctionCall",
|
4061
|
+
params: {
|
4062
|
+
methodName: "ft_transfer_call",
|
4063
|
+
args: {
|
4064
|
+
receiver_id: config.btcToken,
|
4065
|
+
amount: "100",
|
4066
|
+
msg: ""
|
4067
|
+
},
|
4068
|
+
gas: "300000000000000",
|
4069
|
+
deposit: "1"
|
4070
|
+
}
|
4071
|
+
}
|
4072
|
+
]
|
4073
|
+
}
|
4074
|
+
],
|
4075
|
+
env
|
4076
|
+
});
|
4077
|
+
let satoshis = Number(amount);
|
4078
|
+
if (Number(gasLimit) > 0) {
|
4079
|
+
satoshis = new Big2(amount).minus(gasLimit).toNumber();
|
4080
|
+
}
|
4081
|
+
const brgConfig = yield getBridgeConfig({ env });
|
4082
|
+
const allUTXO = yield nearCallFunction(config.bridgeContractId, "get_utxos_paged", {}, { network: config.network });
|
4083
|
+
if (brgConfig.min_withdraw_amount) {
|
4084
|
+
if (Number(satoshis) < Number(brgConfig.min_withdraw_amount)) {
|
4085
|
+
return {
|
4086
|
+
withdrawFee: 0,
|
4087
|
+
isError: true,
|
4088
|
+
errorMsg: `Mini withdraw amount is ${Number(brgConfig.min_withdraw_amount) + Number(gasLimit)} sats`
|
4089
|
+
};
|
4090
|
+
}
|
4091
|
+
}
|
4092
|
+
const feePercent = Number(brgConfig.withdraw_bridge_fee.fee_rate) * Number(satoshis);
|
4093
|
+
const withdrawFee = feePercent > Number(brgConfig.withdraw_bridge_fee.fee_min) ? feePercent : Number(brgConfig.withdraw_bridge_fee.fee_min);
|
4094
|
+
const withdrawChangeAddress = brgConfig.change_address;
|
4095
|
+
const utxos = Object.keys(allUTXO).map((key) => {
|
4096
|
+
const txid = key.split("@");
|
4097
|
+
return {
|
4098
|
+
txid: txid[0],
|
4099
|
+
vout: allUTXO[key].vout,
|
4100
|
+
value: Number(allUTXO[key].balance),
|
4101
|
+
script: allUTXO[key].script
|
4102
|
+
};
|
4103
|
+
}).filter((utxo) => utxo.value > Number(brgConfig.min_change_amount));
|
4104
|
+
if (!utxos || utxos.length === 0) {
|
4105
|
+
return {
|
4106
|
+
withdrawFee,
|
4107
|
+
isError: true,
|
4108
|
+
errorMsg: "The network is busy, please try again later."
|
4109
|
+
};
|
4110
|
+
}
|
4111
|
+
const userSatoshis = Number(satoshis);
|
4112
|
+
const maxBtcFee = Number(brgConfig.max_btc_gas_fee);
|
4113
|
+
const { inputs, outputs, fee } = coinselect(
|
4114
|
+
utxos,
|
4115
|
+
[{ address: btcAddress, value: userSatoshis }],
|
4116
|
+
Math.ceil(feeRate)
|
4117
|
+
);
|
4118
|
+
const newInputs = inputs;
|
4119
|
+
let newOutputs = outputs;
|
4120
|
+
let newFee = fee;
|
4121
|
+
if (!newOutputs || newOutputs.length === 0) {
|
4122
|
+
return {
|
4123
|
+
withdrawFee,
|
4124
|
+
isError: true,
|
4125
|
+
errorMsg: "The network is busy, please try again later."
|
4126
|
+
};
|
4127
|
+
}
|
4128
|
+
let userOutput, noUserOutput;
|
4129
|
+
for (let i = 0; i < newOutputs.length; i++) {
|
4130
|
+
const output = newOutputs[i];
|
4131
|
+
if (output.value.toString() === userSatoshis.toString()) {
|
4132
|
+
userOutput = output;
|
4133
|
+
} else {
|
4134
|
+
noUserOutput = output;
|
4135
|
+
}
|
4136
|
+
if (!output.address) {
|
4137
|
+
output.address = withdrawChangeAddress;
|
4138
|
+
}
|
4139
|
+
}
|
4140
|
+
let dis = 0;
|
4141
|
+
if (newFee > maxBtcFee) {
|
4142
|
+
dis = newFee - maxBtcFee;
|
4143
|
+
newFee = maxBtcFee;
|
4144
|
+
return {
|
4145
|
+
gasFee: newFee,
|
4146
|
+
withdrawFee,
|
4147
|
+
isError: true,
|
4148
|
+
errorMsg: "Gas exceeds maximum value"
|
4149
|
+
};
|
4150
|
+
}
|
4151
|
+
userOutput.value = new Big2(userOutput.value).minus(newFee).minus(withdrawFee).toNumber();
|
4152
|
+
if (userOutput.value < 0) {
|
4153
|
+
return {
|
4154
|
+
gasFee: newFee,
|
4155
|
+
withdrawFee,
|
4156
|
+
isError: true,
|
4157
|
+
errorMsg: "Not enough gas"
|
4158
|
+
};
|
4159
|
+
}
|
4160
|
+
if (noUserOutput) {
|
4161
|
+
if (!noUserOutput.address) {
|
4162
|
+
noUserOutput.address = withdrawChangeAddress;
|
4163
|
+
}
|
4164
|
+
noUserOutput.value = new Big2(noUserOutput.value).plus(newFee).plus(withdrawFee).plus(dis).toNumber();
|
4165
|
+
} else {
|
4166
|
+
noUserOutput = {
|
4167
|
+
address: withdrawChangeAddress,
|
4168
|
+
value: new Big2(newFee).plus(withdrawFee).plus(dis).toNumber()
|
4169
|
+
};
|
4170
|
+
newOutputs.push(noUserOutput);
|
4171
|
+
}
|
4172
|
+
let minValue = Math.min(...newInputs.map((input) => input.value));
|
4173
|
+
let totalNoUserOutputValue = noUserOutput.value;
|
4174
|
+
while (totalNoUserOutputValue >= minValue && minValue > 0 && newInputs.length > 0) {
|
4175
|
+
totalNoUserOutputValue -= minValue;
|
4176
|
+
noUserOutput.value = totalNoUserOutputValue;
|
4177
|
+
const minValueIndex = newInputs.findIndex((input) => input.value === minValue);
|
4178
|
+
if (minValueIndex > -1) {
|
4179
|
+
newInputs.splice(minValueIndex, 1);
|
4180
|
+
}
|
4181
|
+
minValue = Math.min(...newInputs.map((input) => input.value));
|
4182
|
+
}
|
4183
|
+
let gasMore = 0;
|
4184
|
+
if (noUserOutput.value === 0) {
|
4185
|
+
newOutputs = newOutputs.filter((item) => item.value !== 0);
|
4186
|
+
} else if (noUserOutput.value < Number(brgConfig.min_change_amount)) {
|
4187
|
+
gasMore = Number(brgConfig.min_change_amount) - noUserOutput.value;
|
4188
|
+
userOutput.value -= gasMore;
|
4189
|
+
noUserOutput.value = Number(brgConfig.min_change_amount);
|
4190
|
+
}
|
4191
|
+
const insufficientOutput = newOutputs.some((item) => item.value < 0);
|
4192
|
+
if (insufficientOutput) {
|
4193
|
+
return {
|
4194
|
+
gasFee: newFee,
|
4195
|
+
withdrawFee,
|
4196
|
+
isError: true,
|
4197
|
+
errorMsg: "Not enough gas"
|
4198
|
+
};
|
4199
|
+
}
|
4200
|
+
const inputSum = newInputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
4201
|
+
const outputSum = newOutputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
4202
|
+
if (newFee + outputSum !== inputSum) {
|
4203
|
+
return {
|
4204
|
+
withdrawFee,
|
4205
|
+
isError: true,
|
4206
|
+
errorMsg: "Service busy, please try again later"
|
4207
|
+
};
|
4208
|
+
}
|
4209
|
+
return {
|
4210
|
+
withdrawFee: new Big2(withdrawFee).plus(gasLimit).plus(gasMore).toNumber(),
|
4211
|
+
gasFee: new Big2(newFee).toNumber(),
|
4212
|
+
inputs: newInputs,
|
4213
|
+
outputs: newOutputs,
|
4214
|
+
fromAmount: satoshis,
|
4215
|
+
receiveAmount: userOutput.value,
|
4216
|
+
isError: false
|
4217
|
+
};
|
4218
|
+
} catch (error) {
|
4219
|
+
return {
|
4220
|
+
withdrawFee: 0,
|
4221
|
+
isError: true,
|
4222
|
+
errorMsg: error.message
|
4223
|
+
};
|
4224
|
+
}
|
4225
|
+
});
|
4226
|
+
}
|
4228
4227
|
function uint8ArrayToHex(uint8Array) {
|
4229
4228
|
return Array.from(uint8Array).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
4230
4229
|
}
|
@@ -4866,7 +4865,7 @@ function getGroup(state) {
|
|
4866
4865
|
|
4867
4866
|
// src/index.ts
|
4868
4867
|
var getVersion = () => {
|
4869
|
-
return "0.5.
|
4868
|
+
return "0.5.45-beta";
|
4870
4869
|
};
|
4871
4870
|
if (typeof window !== "undefined") {
|
4872
4871
|
window.__BTC_WALLET_VERSION = getVersion();
|
@@ -4888,35 +4887,20 @@ export {
|
|
4888
4887
|
btcRpcUrls,
|
4889
4888
|
calculateGasFee,
|
4890
4889
|
calculateGasLimit,
|
4891
|
-
calculateGasStrategy,
|
4892
4890
|
calculateWithdraw,
|
4893
|
-
checkBridgeTransactionStatus,
|
4894
|
-
checkBtcTransactionStatus,
|
4895
|
-
checkGasTokenBalance,
|
4896
4891
|
checkGasTokenDebt,
|
4897
4892
|
checkSatoshiWhitelist,
|
4898
|
-
convertTransactionToTxHex,
|
4899
4893
|
estimateDepositAmount,
|
4900
4894
|
executeBTCDepositAndAction,
|
4901
|
-
getAccountInfo,
|
4902
|
-
getBridgeConfig,
|
4903
4895
|
getBtcBalance,
|
4904
4896
|
getBtcGasPrice,
|
4905
4897
|
getBtcUtxos,
|
4906
4898
|
getCsnaAccountId,
|
4907
4899
|
getDepositAmount,
|
4908
|
-
getNearNonce,
|
4909
|
-
getNonce,
|
4910
|
-
getTokenBalance,
|
4911
4900
|
getVersion,
|
4912
4901
|
getWalletConfig,
|
4913
|
-
getWhitelist,
|
4914
4902
|
getWithdrawTransaction,
|
4915
4903
|
nearRpcUrls,
|
4916
|
-
preReceiveDepositMsg,
|
4917
|
-
receiveDepositMsg,
|
4918
|
-
receiveTransaction,
|
4919
|
-
receiveWithdrawMsg,
|
4920
4904
|
sendBitcoin,
|
4921
4905
|
setupBTCWallet,
|
4922
4906
|
setupWalletSelectorModal,
|