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/core/btcUtils.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import type { ENV } from '../config';
|
2
|
-
import {
|
2
|
+
import { calculateGasLimit } from '../utils/satoshi';
|
3
3
|
import type { FinalExecutionOutcome, Transaction } from '@near-wallet-selector/core';
|
4
|
-
export { calculateGasLimit
|
4
|
+
export { calculateGasLimit };
|
5
5
|
type CheckGasTokenDebtReturnType<T extends boolean> = T extends true ? void : {
|
6
6
|
receiver_id: string;
|
7
7
|
amount: string;
|
@@ -69,3 +69,21 @@ interface WithdrawParams {
|
|
69
69
|
env?: ENV;
|
70
70
|
}
|
71
71
|
export declare function getWithdrawTransaction({ amount, feeRate, csna, btcAddress, env, }: WithdrawParams): Promise<Transaction>;
|
72
|
+
interface CalculateWithdrawParams {
|
73
|
+
amount: string | number;
|
74
|
+
feeRate?: number;
|
75
|
+
csna?: string;
|
76
|
+
btcAddress?: string;
|
77
|
+
env: ENV;
|
78
|
+
}
|
79
|
+
interface CalculateWithdrawResult {
|
80
|
+
withdrawFee: number;
|
81
|
+
gasFee?: number;
|
82
|
+
inputs?: any[];
|
83
|
+
outputs?: any[];
|
84
|
+
fromAmount?: number;
|
85
|
+
receiveAmount?: string;
|
86
|
+
isError: boolean;
|
87
|
+
errorMsg?: string;
|
88
|
+
}
|
89
|
+
export declare function calculateWithdraw({ amount, feeRate: _feeRate, csna: _csna, btcAddress: _btcAddress, env, }: CalculateWithdrawParams): Promise<CalculateWithdrawResult>;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -94,35 +94,20 @@ __export(src_exports, {
|
|
94
94
|
btcRpcUrls: () => btcRpcUrls,
|
95
95
|
calculateGasFee: () => calculateGasFee,
|
96
96
|
calculateGasLimit: () => calculateGasLimit,
|
97
|
-
calculateGasStrategy: () => calculateGasStrategy,
|
98
97
|
calculateWithdraw: () => calculateWithdraw,
|
99
|
-
checkBridgeTransactionStatus: () => checkBridgeTransactionStatus,
|
100
|
-
checkBtcTransactionStatus: () => checkBtcTransactionStatus,
|
101
|
-
checkGasTokenBalance: () => checkGasTokenBalance,
|
102
98
|
checkGasTokenDebt: () => checkGasTokenDebt,
|
103
99
|
checkSatoshiWhitelist: () => checkSatoshiWhitelist,
|
104
|
-
convertTransactionToTxHex: () => convertTransactionToTxHex,
|
105
100
|
estimateDepositAmount: () => estimateDepositAmount,
|
106
101
|
executeBTCDepositAndAction: () => executeBTCDepositAndAction,
|
107
|
-
getAccountInfo: () => getAccountInfo,
|
108
|
-
getBridgeConfig: () => getBridgeConfig,
|
109
102
|
getBtcBalance: () => getBtcBalance,
|
110
103
|
getBtcGasPrice: () => getBtcGasPrice,
|
111
104
|
getBtcUtxos: () => getBtcUtxos,
|
112
105
|
getCsnaAccountId: () => getCsnaAccountId,
|
113
106
|
getDepositAmount: () => getDepositAmount,
|
114
|
-
getNearNonce: () => getNearNonce,
|
115
|
-
getNonce: () => getNonce,
|
116
|
-
getTokenBalance: () => getTokenBalance,
|
117
107
|
getVersion: () => getVersion,
|
118
108
|
getWalletConfig: () => getWalletConfig,
|
119
|
-
getWhitelist: () => getWhitelist,
|
120
109
|
getWithdrawTransaction: () => getWithdrawTransaction,
|
121
110
|
nearRpcUrls: () => nearRpcUrls,
|
122
|
-
preReceiveDepositMsg: () => preReceiveDepositMsg,
|
123
|
-
receiveDepositMsg: () => receiveDepositMsg,
|
124
|
-
receiveTransaction: () => receiveTransaction,
|
125
|
-
receiveWithdrawMsg: () => receiveWithdrawMsg,
|
126
111
|
sendBitcoin: () => sendBitcoin,
|
127
112
|
setupBTCWallet: () => setupBTCWallet,
|
128
113
|
setupWalletSelectorModal: () => setupWalletSelectorModal,
|
@@ -3226,7 +3211,6 @@ var state_default = {
|
|
3226
3211
|
};
|
3227
3212
|
|
3228
3213
|
// src/utils/satoshi.ts
|
3229
|
-
var import_coinselect = __toESM(require("coinselect"), 1);
|
3230
3214
|
function getNonce(url, accountId) {
|
3231
3215
|
return __async(this, null, function* () {
|
3232
3216
|
const { result_code, result_message, result_data } = yield request(
|
@@ -3343,22 +3327,6 @@ function getWhitelist(url) {
|
|
3343
3327
|
return data;
|
3344
3328
|
});
|
3345
3329
|
}
|
3346
|
-
function receiveWithdrawMsg(url, txHash) {
|
3347
|
-
return __async(this, null, function* () {
|
3348
|
-
const { result_code, result_message, result_data } = yield request(
|
3349
|
-
`${url}/v1/receiveWithdrawMsg`,
|
3350
|
-
{
|
3351
|
-
method: "POST",
|
3352
|
-
body: { txHash }
|
3353
|
-
}
|
3354
|
-
);
|
3355
|
-
console.log("receiveWithdrawMsg resp:", { result_code, result_message, result_data });
|
3356
|
-
if (result_code !== 0) {
|
3357
|
-
throw new Error(result_message);
|
3358
|
-
}
|
3359
|
-
return result_data;
|
3360
|
-
});
|
3361
|
-
}
|
3362
3330
|
function getAccountInfo(_0) {
|
3363
3331
|
return __async(this, arguments, function* ({ csna, env }) {
|
3364
3332
|
const config = getWalletConfig(env);
|
@@ -3684,196 +3652,10 @@ function getPredictedGasAmount(_0) {
|
|
3684
3652
|
return gasAmount.toString();
|
3685
3653
|
});
|
3686
3654
|
}
|
3687
|
-
function calculateWithdraw(_0) {
|
3688
|
-
return __async(this, arguments, function* ({
|
3689
|
-
amount,
|
3690
|
-
feeRate,
|
3691
|
-
csna,
|
3692
|
-
btcAddress,
|
3693
|
-
env
|
3694
|
-
}) {
|
3695
|
-
try {
|
3696
|
-
const config = getWalletConfig(env);
|
3697
|
-
const _feeRate = feeRate || (yield getBtcGasPrice());
|
3698
|
-
const gasLimit = yield calculateGasLimit({
|
3699
|
-
csna,
|
3700
|
-
transactions: [
|
3701
|
-
{
|
3702
|
-
signerId: "",
|
3703
|
-
receiverId: config.btcToken,
|
3704
|
-
actions: [
|
3705
|
-
{
|
3706
|
-
type: "FunctionCall",
|
3707
|
-
params: {
|
3708
|
-
methodName: "ft_transfer_call",
|
3709
|
-
args: {
|
3710
|
-
receiver_id: config.btcToken,
|
3711
|
-
amount: "100",
|
3712
|
-
msg: ""
|
3713
|
-
},
|
3714
|
-
gas: "300000000000000",
|
3715
|
-
deposit: "1"
|
3716
|
-
}
|
3717
|
-
}
|
3718
|
-
]
|
3719
|
-
}
|
3720
|
-
],
|
3721
|
-
env
|
3722
|
-
});
|
3723
|
-
let satoshis = Number(amount);
|
3724
|
-
if (Number(gasLimit) > 0) {
|
3725
|
-
satoshis = new import_big.default(amount).minus(gasLimit).toNumber();
|
3726
|
-
}
|
3727
|
-
const brgConfig = yield getBridgeConfig({ env });
|
3728
|
-
const allUTXO = yield nearCallFunction(config.bridgeContractId, "get_utxos_paged", {}, { network: config.network });
|
3729
|
-
if (brgConfig.min_withdraw_amount) {
|
3730
|
-
if (Number(satoshis) < Number(brgConfig.min_withdraw_amount)) {
|
3731
|
-
return {
|
3732
|
-
withdrawFee: 0,
|
3733
|
-
isError: true,
|
3734
|
-
errorMsg: `Mini withdraw amount is ${Number(brgConfig.min_withdraw_amount) + Number(gasLimit)} sats`
|
3735
|
-
};
|
3736
|
-
}
|
3737
|
-
}
|
3738
|
-
const feePercent = Number(brgConfig.withdraw_bridge_fee.fee_rate) * Number(satoshis);
|
3739
|
-
const withdrawFee = feePercent > Number(brgConfig.withdraw_bridge_fee.fee_min) ? feePercent : Number(brgConfig.withdraw_bridge_fee.fee_min);
|
3740
|
-
const withdrawChangeAddress = brgConfig.change_address;
|
3741
|
-
const utxos = Object.keys(allUTXO).map((key) => {
|
3742
|
-
const txid = key.split("@");
|
3743
|
-
return {
|
3744
|
-
txid: txid[0],
|
3745
|
-
vout: allUTXO[key].vout,
|
3746
|
-
value: Number(allUTXO[key].balance),
|
3747
|
-
script: allUTXO[key].script
|
3748
|
-
};
|
3749
|
-
}).filter((utxo) => utxo.value > Number(brgConfig.min_change_amount));
|
3750
|
-
if (!utxos || utxos.length === 0) {
|
3751
|
-
return {
|
3752
|
-
withdrawFee,
|
3753
|
-
isError: true,
|
3754
|
-
errorMsg: "The network is busy, please try again later."
|
3755
|
-
};
|
3756
|
-
}
|
3757
|
-
const userSatoshis = Number(satoshis);
|
3758
|
-
const maxBtcFee = Number(brgConfig.max_btc_gas_fee);
|
3759
|
-
const { inputs, outputs, fee } = (0, import_coinselect.default)(
|
3760
|
-
utxos,
|
3761
|
-
[{ address: btcAddress, value: userSatoshis }],
|
3762
|
-
Math.ceil(_feeRate)
|
3763
|
-
);
|
3764
|
-
const newInputs = inputs;
|
3765
|
-
let newOutputs = outputs;
|
3766
|
-
let newFee = fee;
|
3767
|
-
if (!newOutputs || newOutputs.length === 0) {
|
3768
|
-
return {
|
3769
|
-
withdrawFee,
|
3770
|
-
isError: true,
|
3771
|
-
errorMsg: "The network is busy, please try again later."
|
3772
|
-
};
|
3773
|
-
}
|
3774
|
-
let userOutput, noUserOutput;
|
3775
|
-
for (let i = 0; i < newOutputs.length; i++) {
|
3776
|
-
const output = newOutputs[i];
|
3777
|
-
if (output.value.toString() === userSatoshis.toString()) {
|
3778
|
-
userOutput = output;
|
3779
|
-
} else {
|
3780
|
-
noUserOutput = output;
|
3781
|
-
}
|
3782
|
-
if (!output.address) {
|
3783
|
-
output.address = withdrawChangeAddress;
|
3784
|
-
}
|
3785
|
-
}
|
3786
|
-
let dis = 0;
|
3787
|
-
if (newFee > maxBtcFee) {
|
3788
|
-
dis = newFee - maxBtcFee;
|
3789
|
-
newFee = maxBtcFee;
|
3790
|
-
return {
|
3791
|
-
gasFee: newFee,
|
3792
|
-
withdrawFee,
|
3793
|
-
isError: true,
|
3794
|
-
errorMsg: "Gas exceeds maximum value"
|
3795
|
-
};
|
3796
|
-
}
|
3797
|
-
userOutput.value = new import_big.default(userOutput.value).minus(newFee).minus(withdrawFee).toNumber();
|
3798
|
-
if (userOutput.value < 0) {
|
3799
|
-
return {
|
3800
|
-
gasFee: newFee,
|
3801
|
-
withdrawFee,
|
3802
|
-
isError: true,
|
3803
|
-
errorMsg: "Not enough gas"
|
3804
|
-
};
|
3805
|
-
}
|
3806
|
-
if (noUserOutput) {
|
3807
|
-
if (!noUserOutput.address) {
|
3808
|
-
noUserOutput.address = withdrawChangeAddress;
|
3809
|
-
}
|
3810
|
-
noUserOutput.value = new import_big.default(noUserOutput.value).plus(newFee).plus(withdrawFee).plus(dis).toNumber();
|
3811
|
-
} else {
|
3812
|
-
noUserOutput = {
|
3813
|
-
address: withdrawChangeAddress,
|
3814
|
-
value: new import_big.default(newFee).plus(withdrawFee).plus(dis).toNumber()
|
3815
|
-
};
|
3816
|
-
newOutputs.push(noUserOutput);
|
3817
|
-
}
|
3818
|
-
let minValue = Math.min(...newInputs.map((input) => input.value));
|
3819
|
-
let totalNoUserOutputValue = noUserOutput.value;
|
3820
|
-
while (totalNoUserOutputValue >= minValue && minValue > 0 && newInputs.length > 0) {
|
3821
|
-
totalNoUserOutputValue -= minValue;
|
3822
|
-
noUserOutput.value = totalNoUserOutputValue;
|
3823
|
-
const minValueIndex = newInputs.findIndex((input) => input.value === minValue);
|
3824
|
-
if (minValueIndex > -1) {
|
3825
|
-
newInputs.splice(minValueIndex, 1);
|
3826
|
-
}
|
3827
|
-
minValue = Math.min(...newInputs.map((input) => input.value));
|
3828
|
-
}
|
3829
|
-
let gasMore = 0;
|
3830
|
-
if (noUserOutput.value === 0) {
|
3831
|
-
newOutputs = newOutputs.filter((item) => item.value !== 0);
|
3832
|
-
} else if (noUserOutput.value < Number(brgConfig.min_change_amount)) {
|
3833
|
-
gasMore = Number(brgConfig.min_change_amount) - noUserOutput.value;
|
3834
|
-
userOutput.value -= gasMore;
|
3835
|
-
noUserOutput.value = Number(brgConfig.min_change_amount);
|
3836
|
-
}
|
3837
|
-
const insufficientOutput = newOutputs.some((item) => item.value < 0);
|
3838
|
-
if (insufficientOutput) {
|
3839
|
-
return {
|
3840
|
-
gasFee: newFee,
|
3841
|
-
withdrawFee,
|
3842
|
-
isError: true,
|
3843
|
-
errorMsg: "Not enough gas"
|
3844
|
-
};
|
3845
|
-
}
|
3846
|
-
const inputSum = newInputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
3847
|
-
const outputSum = newOutputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
3848
|
-
if (newFee + outputSum !== inputSum) {
|
3849
|
-
return {
|
3850
|
-
withdrawFee,
|
3851
|
-
isError: true,
|
3852
|
-
errorMsg: "Service busy, please try again later"
|
3853
|
-
};
|
3854
|
-
}
|
3855
|
-
return {
|
3856
|
-
withdrawFee: new import_big.default(withdrawFee).plus(gasLimit).plus(gasMore).toNumber(),
|
3857
|
-
gasFee: new import_big.default(newFee).toNumber(),
|
3858
|
-
inputs: newInputs,
|
3859
|
-
outputs: newOutputs,
|
3860
|
-
fromAmount: satoshis,
|
3861
|
-
receiveAmount: userOutput.value,
|
3862
|
-
isError: false
|
3863
|
-
};
|
3864
|
-
} catch (error) {
|
3865
|
-
return {
|
3866
|
-
withdrawFee: 0,
|
3867
|
-
isError: true,
|
3868
|
-
errorMsg: error.message
|
3869
|
-
};
|
3870
|
-
}
|
3871
|
-
});
|
3872
|
-
}
|
3873
3655
|
|
3874
3656
|
// src/core/btcUtils.ts
|
3875
3657
|
var bitcoin = __toESM(require("bitcoinjs-lib"), 1);
|
3876
|
-
var
|
3658
|
+
var import_coinselect = __toESM(require("coinselect"), 1);
|
3877
3659
|
var ecc = __toESM(require("@bitcoinerlab/secp256k1"), 1);
|
3878
3660
|
bitcoin.initEccLib(ecc);
|
3879
3661
|
var NEAR_STORAGE_DEPOSIT_AMOUNT = "1250000000000000000000";
|
@@ -3969,7 +3751,7 @@ function calculateGasFee(account, amount, feeRate) {
|
|
3969
3751
|
return __async(this, null, function* () {
|
3970
3752
|
const _feeRate = feeRate || (yield getBtcGasPrice());
|
3971
3753
|
const utxos = yield getBtcUtxos(account);
|
3972
|
-
const { fee } = (0,
|
3754
|
+
const { fee } = (0, import_coinselect.default)(utxos, [{ address: account, value: amount }], Math.ceil(_feeRate));
|
3973
3755
|
console.log("calculateGasFee fee:", fee);
|
3974
3756
|
return fee;
|
3975
3757
|
});
|
@@ -4019,19 +3801,23 @@ function getDepositAmount(amount, option) {
|
|
4019
3801
|
const accountInfo = yield getAccountInfo({ csna, env });
|
4020
3802
|
const debtAction = yield checkGasTokenDebt(csna, env, false);
|
4021
3803
|
const repayAmount = (debtAction == null ? void 0 : debtAction.amount) || 0;
|
3804
|
+
const depositAmount = Number(amount);
|
4022
3805
|
const {
|
4023
3806
|
deposit_bridge_fee: { fee_min, fee_rate },
|
4024
3807
|
min_deposit_amount
|
4025
3808
|
} = yield getBridgeConfig({ env });
|
4026
|
-
const
|
4027
|
-
const protocolFee = Math.max(Number(fee_min), Number(depositAmount) * fee_rate);
|
3809
|
+
const protocolFee = Math.max(Number(fee_min), depositAmount * fee_rate);
|
4028
3810
|
const newAccountMinDepositAmount = !(accountInfo == null ? void 0 : accountInfo.nonce) && _newAccountMinDepositAmount ? NEW_ACCOUNT_MIN_DEPOSIT_AMOUNT : 0;
|
4029
|
-
|
3811
|
+
let receiveAmount = new import_big2.default(depositAmount).minus(protocolFee).minus(repayAmount).round(0, import_big2.default.roundDown).toNumber();
|
3812
|
+
receiveAmount = Math.max(receiveAmount, 0);
|
4030
3813
|
if (Number(newAccountMinDepositAmount) > 0 && receiveAmount < Number(newAccountMinDepositAmount)) {
|
4031
|
-
|
4032
|
-
`Receive amount (${receiveAmount}) is less than minimum required amount for new account (${newAccountMinDepositAmount})`
|
3814
|
+
console.error(
|
3815
|
+
`Receive amount (${receiveAmount}) is less than minimum required amount for new account (${newAccountMinDepositAmount} sat)`
|
4033
3816
|
);
|
4034
3817
|
}
|
3818
|
+
if (receiveAmount <= 0) {
|
3819
|
+
console.error(`Receive amount (${receiveAmount}) is less than 0`);
|
3820
|
+
}
|
4035
3821
|
return {
|
4036
3822
|
depositAmount,
|
4037
3823
|
receiveAmount,
|
@@ -4091,17 +3877,20 @@ function executeBTCDepositAndAction(_0) {
|
|
4091
3877
|
throw new Error("BTC Public Key is not available.");
|
4092
3878
|
}
|
4093
3879
|
if (!amount && !action) {
|
4094
|
-
throw new Error("amount or action is required");
|
3880
|
+
throw new Error("Deposit amount or action is required");
|
4095
3881
|
}
|
4096
3882
|
const csna = yield getCsnaAccountId(env);
|
4097
3883
|
const depositAmount = (_a = action ? action.amount : amount) != null ? _a : "0";
|
4098
3884
|
if (new import_big2.default(depositAmount).lt(0)) {
|
4099
|
-
throw new Error("amount must be greater than 0");
|
3885
|
+
throw new Error("Deposit amount must be greater than 0");
|
4100
3886
|
}
|
4101
3887
|
const { receiveAmount, protocolFee, repayAmount } = yield getDepositAmount(depositAmount, {
|
4102
3888
|
env,
|
4103
3889
|
newAccountMinDepositAmount
|
4104
3890
|
});
|
3891
|
+
if (receiveAmount <= 0) {
|
3892
|
+
throw new Error("Invalid deposit amount, too small");
|
3893
|
+
}
|
4105
3894
|
const accountInfo = yield getAccountInfo({ csna, env });
|
4106
3895
|
const newActions = [];
|
4107
3896
|
const debtAction = yield checkGasTokenDebt(csna, env, false);
|
@@ -4309,6 +4098,201 @@ function getWithdrawTransaction(_0) {
|
|
4309
4098
|
return transaction;
|
4310
4099
|
});
|
4311
4100
|
}
|
4101
|
+
function calculateWithdraw(_0) {
|
4102
|
+
return __async(this, arguments, function* ({
|
4103
|
+
amount,
|
4104
|
+
feeRate: _feeRate,
|
4105
|
+
csna: _csna,
|
4106
|
+
btcAddress: _btcAddress,
|
4107
|
+
env
|
4108
|
+
}) {
|
4109
|
+
try {
|
4110
|
+
const config = getWalletConfig(env);
|
4111
|
+
let btcAddress = _btcAddress || getBtcProvider().account;
|
4112
|
+
if (!btcAddress) {
|
4113
|
+
yield getBtcProvider().autoConnect();
|
4114
|
+
btcAddress = getBtcProvider().account;
|
4115
|
+
if (!btcAddress) {
|
4116
|
+
throw new Error("BTC Account is not available.");
|
4117
|
+
}
|
4118
|
+
}
|
4119
|
+
const csna = _csna || (yield getCsnaAccountId(env));
|
4120
|
+
const feeRate = _feeRate || (yield getBtcGasPrice());
|
4121
|
+
const gasLimit = yield calculateGasLimit({
|
4122
|
+
csna,
|
4123
|
+
transactions: [
|
4124
|
+
{
|
4125
|
+
signerId: "",
|
4126
|
+
receiverId: config.btcToken,
|
4127
|
+
actions: [
|
4128
|
+
{
|
4129
|
+
type: "FunctionCall",
|
4130
|
+
params: {
|
4131
|
+
methodName: "ft_transfer_call",
|
4132
|
+
args: {
|
4133
|
+
receiver_id: config.btcToken,
|
4134
|
+
amount: "100",
|
4135
|
+
msg: ""
|
4136
|
+
},
|
4137
|
+
gas: "300000000000000",
|
4138
|
+
deposit: "1"
|
4139
|
+
}
|
4140
|
+
}
|
4141
|
+
]
|
4142
|
+
}
|
4143
|
+
],
|
4144
|
+
env
|
4145
|
+
});
|
4146
|
+
let satoshis = Number(amount);
|
4147
|
+
if (Number(gasLimit) > 0) {
|
4148
|
+
satoshis = new import_big2.default(amount).minus(gasLimit).toNumber();
|
4149
|
+
}
|
4150
|
+
const brgConfig = yield getBridgeConfig({ env });
|
4151
|
+
const allUTXO = yield nearCallFunction(config.bridgeContractId, "get_utxos_paged", {}, { network: config.network });
|
4152
|
+
if (brgConfig.min_withdraw_amount) {
|
4153
|
+
if (Number(satoshis) < Number(brgConfig.min_withdraw_amount)) {
|
4154
|
+
return {
|
4155
|
+
withdrawFee: 0,
|
4156
|
+
isError: true,
|
4157
|
+
errorMsg: `Mini withdraw amount is ${Number(brgConfig.min_withdraw_amount) + Number(gasLimit)} sats`
|
4158
|
+
};
|
4159
|
+
}
|
4160
|
+
}
|
4161
|
+
const feePercent = Number(brgConfig.withdraw_bridge_fee.fee_rate) * Number(satoshis);
|
4162
|
+
const withdrawFee = feePercent > Number(brgConfig.withdraw_bridge_fee.fee_min) ? feePercent : Number(brgConfig.withdraw_bridge_fee.fee_min);
|
4163
|
+
const withdrawChangeAddress = brgConfig.change_address;
|
4164
|
+
const utxos = Object.keys(allUTXO).map((key) => {
|
4165
|
+
const txid = key.split("@");
|
4166
|
+
return {
|
4167
|
+
txid: txid[0],
|
4168
|
+
vout: allUTXO[key].vout,
|
4169
|
+
value: Number(allUTXO[key].balance),
|
4170
|
+
script: allUTXO[key].script
|
4171
|
+
};
|
4172
|
+
}).filter((utxo) => utxo.value > Number(brgConfig.min_change_amount));
|
4173
|
+
if (!utxos || utxos.length === 0) {
|
4174
|
+
return {
|
4175
|
+
withdrawFee,
|
4176
|
+
isError: true,
|
4177
|
+
errorMsg: "The network is busy, please try again later."
|
4178
|
+
};
|
4179
|
+
}
|
4180
|
+
const userSatoshis = Number(satoshis);
|
4181
|
+
const maxBtcFee = Number(brgConfig.max_btc_gas_fee);
|
4182
|
+
const { inputs, outputs, fee } = (0, import_coinselect.default)(
|
4183
|
+
utxos,
|
4184
|
+
[{ address: btcAddress, value: userSatoshis }],
|
4185
|
+
Math.ceil(feeRate)
|
4186
|
+
);
|
4187
|
+
const newInputs = inputs;
|
4188
|
+
let newOutputs = outputs;
|
4189
|
+
let newFee = fee;
|
4190
|
+
if (!newOutputs || newOutputs.length === 0) {
|
4191
|
+
return {
|
4192
|
+
withdrawFee,
|
4193
|
+
isError: true,
|
4194
|
+
errorMsg: "The network is busy, please try again later."
|
4195
|
+
};
|
4196
|
+
}
|
4197
|
+
let userOutput, noUserOutput;
|
4198
|
+
for (let i = 0; i < newOutputs.length; i++) {
|
4199
|
+
const output = newOutputs[i];
|
4200
|
+
if (output.value.toString() === userSatoshis.toString()) {
|
4201
|
+
userOutput = output;
|
4202
|
+
} else {
|
4203
|
+
noUserOutput = output;
|
4204
|
+
}
|
4205
|
+
if (!output.address) {
|
4206
|
+
output.address = withdrawChangeAddress;
|
4207
|
+
}
|
4208
|
+
}
|
4209
|
+
let dis = 0;
|
4210
|
+
if (newFee > maxBtcFee) {
|
4211
|
+
dis = newFee - maxBtcFee;
|
4212
|
+
newFee = maxBtcFee;
|
4213
|
+
return {
|
4214
|
+
gasFee: newFee,
|
4215
|
+
withdrawFee,
|
4216
|
+
isError: true,
|
4217
|
+
errorMsg: "Gas exceeds maximum value"
|
4218
|
+
};
|
4219
|
+
}
|
4220
|
+
userOutput.value = new import_big2.default(userOutput.value).minus(newFee).minus(withdrawFee).toNumber();
|
4221
|
+
if (userOutput.value < 0) {
|
4222
|
+
return {
|
4223
|
+
gasFee: newFee,
|
4224
|
+
withdrawFee,
|
4225
|
+
isError: true,
|
4226
|
+
errorMsg: "Not enough gas"
|
4227
|
+
};
|
4228
|
+
}
|
4229
|
+
if (noUserOutput) {
|
4230
|
+
if (!noUserOutput.address) {
|
4231
|
+
noUserOutput.address = withdrawChangeAddress;
|
4232
|
+
}
|
4233
|
+
noUserOutput.value = new import_big2.default(noUserOutput.value).plus(newFee).plus(withdrawFee).plus(dis).toNumber();
|
4234
|
+
} else {
|
4235
|
+
noUserOutput = {
|
4236
|
+
address: withdrawChangeAddress,
|
4237
|
+
value: new import_big2.default(newFee).plus(withdrawFee).plus(dis).toNumber()
|
4238
|
+
};
|
4239
|
+
newOutputs.push(noUserOutput);
|
4240
|
+
}
|
4241
|
+
let minValue = Math.min(...newInputs.map((input) => input.value));
|
4242
|
+
let totalNoUserOutputValue = noUserOutput.value;
|
4243
|
+
while (totalNoUserOutputValue >= minValue && minValue > 0 && newInputs.length > 0) {
|
4244
|
+
totalNoUserOutputValue -= minValue;
|
4245
|
+
noUserOutput.value = totalNoUserOutputValue;
|
4246
|
+
const minValueIndex = newInputs.findIndex((input) => input.value === minValue);
|
4247
|
+
if (minValueIndex > -1) {
|
4248
|
+
newInputs.splice(minValueIndex, 1);
|
4249
|
+
}
|
4250
|
+
minValue = Math.min(...newInputs.map((input) => input.value));
|
4251
|
+
}
|
4252
|
+
let gasMore = 0;
|
4253
|
+
if (noUserOutput.value === 0) {
|
4254
|
+
newOutputs = newOutputs.filter((item) => item.value !== 0);
|
4255
|
+
} else if (noUserOutput.value < Number(brgConfig.min_change_amount)) {
|
4256
|
+
gasMore = Number(brgConfig.min_change_amount) - noUserOutput.value;
|
4257
|
+
userOutput.value -= gasMore;
|
4258
|
+
noUserOutput.value = Number(brgConfig.min_change_amount);
|
4259
|
+
}
|
4260
|
+
const insufficientOutput = newOutputs.some((item) => item.value < 0);
|
4261
|
+
if (insufficientOutput) {
|
4262
|
+
return {
|
4263
|
+
gasFee: newFee,
|
4264
|
+
withdrawFee,
|
4265
|
+
isError: true,
|
4266
|
+
errorMsg: "Not enough gas"
|
4267
|
+
};
|
4268
|
+
}
|
4269
|
+
const inputSum = newInputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
4270
|
+
const outputSum = newOutputs.reduce((sum, cur) => sum + Number(cur.value), 0);
|
4271
|
+
if (newFee + outputSum !== inputSum) {
|
4272
|
+
return {
|
4273
|
+
withdrawFee,
|
4274
|
+
isError: true,
|
4275
|
+
errorMsg: "Service busy, please try again later"
|
4276
|
+
};
|
4277
|
+
}
|
4278
|
+
return {
|
4279
|
+
withdrawFee: new import_big2.default(withdrawFee).plus(gasLimit).plus(gasMore).toNumber(),
|
4280
|
+
gasFee: new import_big2.default(newFee).toNumber(),
|
4281
|
+
inputs: newInputs,
|
4282
|
+
outputs: newOutputs,
|
4283
|
+
fromAmount: satoshis,
|
4284
|
+
receiveAmount: userOutput.value,
|
4285
|
+
isError: false
|
4286
|
+
};
|
4287
|
+
} catch (error) {
|
4288
|
+
return {
|
4289
|
+
withdrawFee: 0,
|
4290
|
+
isError: true,
|
4291
|
+
errorMsg: error.message
|
4292
|
+
};
|
4293
|
+
}
|
4294
|
+
});
|
4295
|
+
}
|
4312
4296
|
function uint8ArrayToHex(uint8Array) {
|
4313
4297
|
return Array.from(uint8Array).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
4314
4298
|
}
|
@@ -4948,7 +4932,7 @@ function getGroup(state) {
|
|
4948
4932
|
|
4949
4933
|
// src/index.ts
|
4950
4934
|
var getVersion = () => {
|
4951
|
-
return "0.5.
|
4935
|
+
return "0.5.45-beta";
|
4952
4936
|
};
|
4953
4937
|
if (typeof window !== "undefined") {
|
4954
4938
|
window.__BTC_WALLET_VERSION = getVersion();
|