btc-wallet 0.5.43-beta → 0.5.44-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.
@@ -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 coinselect2 from "coinselect";
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 } = coinselect2(utxos, [{ address: account, value: amount }], Math.ceil(_feeRate));
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
  });
@@ -4225,6 +4022,201 @@ function getWithdrawTransaction(_0) {
4225
4022
  return transaction;
4226
4023
  });
4227
4024
  }
4025
+ function calculateWithdraw(_0) {
4026
+ return __async(this, arguments, function* ({
4027
+ amount,
4028
+ feeRate: _feeRate,
4029
+ csna: _csna,
4030
+ btcAddress: _btcAddress,
4031
+ env
4032
+ }) {
4033
+ try {
4034
+ const config = getWalletConfig(env);
4035
+ let btcAddress = _btcAddress || getBtcProvider().account;
4036
+ if (!btcAddress) {
4037
+ yield getBtcProvider().autoConnect();
4038
+ btcAddress = getBtcProvider().account;
4039
+ if (!btcAddress) {
4040
+ throw new Error("BTC Account is not available.");
4041
+ }
4042
+ }
4043
+ const csna = _csna || (yield getCsnaAccountId(env));
4044
+ const feeRate = _feeRate || (yield getBtcGasPrice());
4045
+ const gasLimit = yield calculateGasLimit({
4046
+ csna,
4047
+ transactions: [
4048
+ {
4049
+ signerId: "",
4050
+ receiverId: config.btcToken,
4051
+ actions: [
4052
+ {
4053
+ type: "FunctionCall",
4054
+ params: {
4055
+ methodName: "ft_transfer_call",
4056
+ args: {
4057
+ receiver_id: config.btcToken,
4058
+ amount: "100",
4059
+ msg: ""
4060
+ },
4061
+ gas: "300000000000000",
4062
+ deposit: "1"
4063
+ }
4064
+ }
4065
+ ]
4066
+ }
4067
+ ],
4068
+ env
4069
+ });
4070
+ let satoshis = Number(amount);
4071
+ if (Number(gasLimit) > 0) {
4072
+ satoshis = new Big2(amount).minus(gasLimit).toNumber();
4073
+ }
4074
+ const brgConfig = yield getBridgeConfig({ env });
4075
+ const allUTXO = yield nearCallFunction(config.bridgeContractId, "get_utxos_paged", {}, { network: config.network });
4076
+ if (brgConfig.min_withdraw_amount) {
4077
+ if (Number(satoshis) < Number(brgConfig.min_withdraw_amount)) {
4078
+ return {
4079
+ withdrawFee: 0,
4080
+ isError: true,
4081
+ errorMsg: `Mini withdraw amount is ${Number(brgConfig.min_withdraw_amount) + Number(gasLimit)} sats`
4082
+ };
4083
+ }
4084
+ }
4085
+ const feePercent = Number(brgConfig.withdraw_bridge_fee.fee_rate) * Number(satoshis);
4086
+ const withdrawFee = feePercent > Number(brgConfig.withdraw_bridge_fee.fee_min) ? feePercent : Number(brgConfig.withdraw_bridge_fee.fee_min);
4087
+ const withdrawChangeAddress = brgConfig.change_address;
4088
+ const utxos = Object.keys(allUTXO).map((key) => {
4089
+ const txid = key.split("@");
4090
+ return {
4091
+ txid: txid[0],
4092
+ vout: allUTXO[key].vout,
4093
+ value: Number(allUTXO[key].balance),
4094
+ script: allUTXO[key].script
4095
+ };
4096
+ }).filter((utxo) => utxo.value > Number(brgConfig.min_change_amount));
4097
+ if (!utxos || utxos.length === 0) {
4098
+ return {
4099
+ withdrawFee,
4100
+ isError: true,
4101
+ errorMsg: "The network is busy, please try again later."
4102
+ };
4103
+ }
4104
+ const userSatoshis = Number(satoshis);
4105
+ const maxBtcFee = Number(brgConfig.max_btc_gas_fee);
4106
+ const { inputs, outputs, fee } = coinselect(
4107
+ utxos,
4108
+ [{ address: btcAddress, value: userSatoshis }],
4109
+ Math.ceil(feeRate)
4110
+ );
4111
+ const newInputs = inputs;
4112
+ let newOutputs = outputs;
4113
+ let newFee = fee;
4114
+ if (!newOutputs || newOutputs.length === 0) {
4115
+ return {
4116
+ withdrawFee,
4117
+ isError: true,
4118
+ errorMsg: "The network is busy, please try again later."
4119
+ };
4120
+ }
4121
+ let userOutput, noUserOutput;
4122
+ for (let i = 0; i < newOutputs.length; i++) {
4123
+ const output = newOutputs[i];
4124
+ if (output.value.toString() === userSatoshis.toString()) {
4125
+ userOutput = output;
4126
+ } else {
4127
+ noUserOutput = output;
4128
+ }
4129
+ if (!output.address) {
4130
+ output.address = withdrawChangeAddress;
4131
+ }
4132
+ }
4133
+ let dis = 0;
4134
+ if (newFee > maxBtcFee) {
4135
+ dis = newFee - maxBtcFee;
4136
+ newFee = maxBtcFee;
4137
+ return {
4138
+ gasFee: newFee,
4139
+ withdrawFee,
4140
+ isError: true,
4141
+ errorMsg: "Gas exceeds maximum value"
4142
+ };
4143
+ }
4144
+ userOutput.value = new Big2(userOutput.value).minus(newFee).minus(withdrawFee).toNumber();
4145
+ if (userOutput.value < 0) {
4146
+ return {
4147
+ gasFee: newFee,
4148
+ withdrawFee,
4149
+ isError: true,
4150
+ errorMsg: "Not enough gas"
4151
+ };
4152
+ }
4153
+ if (noUserOutput) {
4154
+ if (!noUserOutput.address) {
4155
+ noUserOutput.address = withdrawChangeAddress;
4156
+ }
4157
+ noUserOutput.value = new Big2(noUserOutput.value).plus(newFee).plus(withdrawFee).plus(dis).toNumber();
4158
+ } else {
4159
+ noUserOutput = {
4160
+ address: withdrawChangeAddress,
4161
+ value: new Big2(newFee).plus(withdrawFee).plus(dis).toNumber()
4162
+ };
4163
+ newOutputs.push(noUserOutput);
4164
+ }
4165
+ let minValue = Math.min(...newInputs.map((input) => input.value));
4166
+ let totalNoUserOutputValue = noUserOutput.value;
4167
+ while (totalNoUserOutputValue >= minValue && minValue > 0 && newInputs.length > 0) {
4168
+ totalNoUserOutputValue -= minValue;
4169
+ noUserOutput.value = totalNoUserOutputValue;
4170
+ const minValueIndex = newInputs.findIndex((input) => input.value === minValue);
4171
+ if (minValueIndex > -1) {
4172
+ newInputs.splice(minValueIndex, 1);
4173
+ }
4174
+ minValue = Math.min(...newInputs.map((input) => input.value));
4175
+ }
4176
+ let gasMore = 0;
4177
+ if (noUserOutput.value === 0) {
4178
+ newOutputs = newOutputs.filter((item) => item.value !== 0);
4179
+ } else if (noUserOutput.value < Number(brgConfig.min_change_amount)) {
4180
+ gasMore = Number(brgConfig.min_change_amount) - noUserOutput.value;
4181
+ userOutput.value -= gasMore;
4182
+ noUserOutput.value = Number(brgConfig.min_change_amount);
4183
+ }
4184
+ const insufficientOutput = newOutputs.some((item) => item.value < 0);
4185
+ if (insufficientOutput) {
4186
+ return {
4187
+ gasFee: newFee,
4188
+ withdrawFee,
4189
+ isError: true,
4190
+ errorMsg: "Not enough gas"
4191
+ };
4192
+ }
4193
+ const inputSum = newInputs.reduce((sum, cur) => sum + Number(cur.value), 0);
4194
+ const outputSum = newOutputs.reduce((sum, cur) => sum + Number(cur.value), 0);
4195
+ if (newFee + outputSum !== inputSum) {
4196
+ return {
4197
+ withdrawFee,
4198
+ isError: true,
4199
+ errorMsg: "Service busy, please try again later"
4200
+ };
4201
+ }
4202
+ return {
4203
+ withdrawFee: new Big2(withdrawFee).plus(gasLimit).plus(gasMore).toNumber(),
4204
+ gasFee: new Big2(newFee).toNumber(),
4205
+ inputs: newInputs,
4206
+ outputs: newOutputs,
4207
+ fromAmount: satoshis,
4208
+ receiveAmount: userOutput.value,
4209
+ isError: false
4210
+ };
4211
+ } catch (error) {
4212
+ return {
4213
+ withdrawFee: 0,
4214
+ isError: true,
4215
+ errorMsg: error.message
4216
+ };
4217
+ }
4218
+ });
4219
+ }
4228
4220
  function uint8ArrayToHex(uint8Array) {
4229
4221
  return Array.from(uint8Array).map((byte) => byte.toString(16).padStart(2, "0")).join("");
4230
4222
  }
@@ -4866,7 +4858,7 @@ function getGroup(state) {
4866
4858
 
4867
4859
  // src/index.ts
4868
4860
  var getVersion = () => {
4869
- return "0.5.43-beta";
4861
+ return "0.5.44-beta";
4870
4862
  };
4871
4863
  if (typeof window !== "undefined") {
4872
4864
  window.__BTC_WALLET_VERSION = getVersion();
@@ -4888,35 +4880,20 @@ export {
4888
4880
  btcRpcUrls,
4889
4881
  calculateGasFee,
4890
4882
  calculateGasLimit,
4891
- calculateGasStrategy,
4892
4883
  calculateWithdraw,
4893
- checkBridgeTransactionStatus,
4894
- checkBtcTransactionStatus,
4895
- checkGasTokenBalance,
4896
4884
  checkGasTokenDebt,
4897
4885
  checkSatoshiWhitelist,
4898
- convertTransactionToTxHex,
4899
4886
  estimateDepositAmount,
4900
4887
  executeBTCDepositAndAction,
4901
- getAccountInfo,
4902
- getBridgeConfig,
4903
4888
  getBtcBalance,
4904
4889
  getBtcGasPrice,
4905
4890
  getBtcUtxos,
4906
4891
  getCsnaAccountId,
4907
4892
  getDepositAmount,
4908
- getNearNonce,
4909
- getNonce,
4910
- getTokenBalance,
4911
4893
  getVersion,
4912
4894
  getWalletConfig,
4913
- getWhitelist,
4914
4895
  getWithdrawTransaction,
4915
4896
  nearRpcUrls,
4916
- preReceiveDepositMsg,
4917
- receiveDepositMsg,
4918
- receiveTransaction,
4919
- receiveWithdrawMsg,
4920
4897
  sendBitcoin,
4921
4898
  setupBTCWallet,
4922
4899
  setupWalletSelectorModal,