btc-wallet 0.5.44-beta → 0.5.46-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.
@@ -37,6 +37,7 @@ export declare function getDepositAmount(amount: string, option?: {
37
37
  protocolFee: number;
38
38
  repayAmount: string | number;
39
39
  newAccountMinDepositAmount: string | number;
40
+ minDepositAmount: number;
40
41
  }>;
41
42
  export declare function getCsnaAccountId(env: ENV): Promise<string>;
42
43
  interface ExecuteBTCDepositAndActionParams<T extends boolean = true> {
package/dist/index.js CHANGED
@@ -3678,6 +3678,9 @@ function getNetwork() {
3678
3678
  }
3679
3679
  });
3680
3680
  }
3681
+ function formatBtcAmount(amount) {
3682
+ return new import_big2.default(amount).div(__pow(10, 8)).toString();
3683
+ }
3681
3684
  function getBtcRpcUrl() {
3682
3685
  return __async(this, null, function* () {
3683
3686
  const network = yield getNetwork();
@@ -3709,15 +3712,20 @@ function checkGasTokenDebt(csna, env, autoDeposit) {
3709
3712
  };
3710
3713
  if (!autoDeposit)
3711
3714
  return action;
3715
+ console.log("checkGasTokenDebt action:", action);
3716
+ const { minDepositAmount } = yield getDepositAmount(action.amount, {
3717
+ env
3718
+ });
3719
+ const remainingAmount = new import_big2.default(minDepositAmount).minus(transferAmount).toNumber();
3712
3720
  const confirmed = yield Dialog.confirm({
3713
- title: hasDebtArrears ? "Has gas token arrears" : "Has relayer fee arrears",
3714
- message: hasDebtArrears ? "You have gas token arrears, please deposit gas token to continue." : "You have relayer fee arrears, please deposit relayer fee to continue."
3721
+ title: hasDebtArrears ? "Gas Token Arrears" : "Relayer Fee Arrears",
3722
+ message: hasDebtArrears ? `You have gas token arrears. Minimum deposit amount is ${formatBtcAmount(minDepositAmount)} BTC, of which ${formatBtcAmount(transferAmount)} BTC will be used to repay the debt, and the remaining ${formatBtcAmount(remainingAmount)} BTC will be credited to your account.` : `You have relayer fee arrears. Minimum deposit amount is ${formatBtcAmount(minDepositAmount)} BTC, of which ${formatBtcAmount(transferAmount)} BTC will be used for relayer fee, and the remaining ${formatBtcAmount(remainingAmount)} BTC will be credited to your account.`
3715
3723
  });
3716
3724
  if (confirmed) {
3717
- yield executeBTCDepositAndAction({ action, env });
3725
+ yield executeBTCDepositAndAction({ amount: minDepositAmount.toString(), action, env });
3718
3726
  yield Dialog.alert({
3719
- title: "Deposit success",
3720
- message: "Deposit success, will continue to execute transaction."
3727
+ title: "Deposit Success",
3728
+ message: `Deposit successful. ${formatBtcAmount(transferAmount)} BTC has been paid for ${hasDebtArrears ? "debt" : "relayer fee"}, and the remaining ${formatBtcAmount(remainingAmount)} BTC has been credited to your account. Transaction will continue.`
3721
3729
  });
3722
3730
  } else {
3723
3731
  throw new Error("Deposit failed, please deposit gas token first.");
@@ -3801,25 +3809,26 @@ function getDepositAmount(amount, option) {
3801
3809
  const accountInfo = yield getAccountInfo({ csna, env });
3802
3810
  const debtAction = yield checkGasTokenDebt(csna, env, false);
3803
3811
  const repayAmount = (debtAction == null ? void 0 : debtAction.amount) || 0;
3812
+ const depositAmount = Number(amount);
3804
3813
  const {
3805
3814
  deposit_bridge_fee: { fee_min, fee_rate },
3806
3815
  min_deposit_amount
3807
3816
  } = yield getBridgeConfig({ env });
3808
- const depositAmount = Math.max(Number(min_deposit_amount), Number(amount));
3809
- const protocolFee = Math.max(Number(fee_min), Number(depositAmount) * fee_rate);
3817
+ const protocolFee = Math.max(Number(fee_min), depositAmount * fee_rate);
3810
3818
  const newAccountMinDepositAmount = !(accountInfo == null ? void 0 : accountInfo.nonce) && _newAccountMinDepositAmount ? NEW_ACCOUNT_MIN_DEPOSIT_AMOUNT : 0;
3811
- const receiveAmount = new import_big2.default(depositAmount).minus(protocolFee).minus(repayAmount).round(0, import_big2.default.roundDown).toNumber();
3812
- if (Number(newAccountMinDepositAmount) > 0 && receiveAmount < Number(newAccountMinDepositAmount)) {
3813
- throw new Error(
3814
- `Receive amount (${receiveAmount}) is less than minimum required amount for new account (${newAccountMinDepositAmount})`
3815
- );
3816
- }
3819
+ let receiveAmount = new import_big2.default(depositAmount).minus(protocolFee).minus(repayAmount).round(0, import_big2.default.roundDown).toNumber();
3820
+ receiveAmount = Math.max(receiveAmount, 0);
3821
+ const minDepositAmount = new import_big2.default(min_deposit_amount || 0).plus(newAccountMinDepositAmount).plus(protocolFee).plus(repayAmount).round(0, import_big2.default.roundUp).toNumber();
3822
+ console.log(
3823
+ `minDepositAmount: ${minDepositAmount} = ${min_deposit_amount} + ${newAccountMinDepositAmount} + ${protocolFee} + ${repayAmount}`
3824
+ );
3817
3825
  return {
3818
3826
  depositAmount,
3819
3827
  receiveAmount,
3820
3828
  protocolFee,
3821
3829
  repayAmount,
3822
- newAccountMinDepositAmount
3830
+ newAccountMinDepositAmount,
3831
+ minDepositAmount
3823
3832
  };
3824
3833
  });
3825
3834
  }
@@ -3862,9 +3871,16 @@ function executeBTCDepositAndAction(_0) {
3862
3871
  newAccountMinDepositAmount,
3863
3872
  registerContractId
3864
3873
  }) {
3865
- var _a;
3866
3874
  try {
3867
- console.log("executeBTCDepositAndAction start", amount);
3875
+ console.log("executeBTCDepositAndAction start", {
3876
+ action,
3877
+ amount,
3878
+ feeRate,
3879
+ pollResult,
3880
+ registerDeposit,
3881
+ newAccountMinDepositAmount,
3882
+ registerContractId
3883
+ });
3868
3884
  checkDepositDisabledAddress();
3869
3885
  const { getPublicKey } = getBtcProvider();
3870
3886
  const config = getWalletConfig(env);
@@ -3873,17 +3889,26 @@ function executeBTCDepositAndAction(_0) {
3873
3889
  throw new Error("BTC Public Key is not available.");
3874
3890
  }
3875
3891
  if (!amount && !action) {
3876
- throw new Error("amount or action is required");
3892
+ throw new Error("Deposit amount or action is required");
3877
3893
  }
3878
3894
  const csna = yield getCsnaAccountId(env);
3879
- const depositAmount = (_a = action ? action.amount : amount) != null ? _a : "0";
3880
- if (new import_big2.default(depositAmount).lt(0)) {
3881
- throw new Error("amount must be greater than 0");
3895
+ const depositAmount = Number(amount || (action == null ? void 0 : action.amount) || 0);
3896
+ console.log("depositAmount", depositAmount);
3897
+ if (depositAmount <= 0) {
3898
+ throw new Error("Invalid deposit amount");
3899
+ }
3900
+ const { receiveAmount, protocolFee, repayAmount, minDepositAmount } = yield getDepositAmount(
3901
+ depositAmount.toString(),
3902
+ {
3903
+ env,
3904
+ newAccountMinDepositAmount
3905
+ }
3906
+ );
3907
+ if (depositAmount < minDepositAmount) {
3908
+ throw new Error(
3909
+ `Invalid deposit amount, must be greater than ${formatBtcAmount(minDepositAmount)} BTC`
3910
+ );
3882
3911
  }
3883
- const { receiveAmount, protocolFee, repayAmount } = yield getDepositAmount(depositAmount, {
3884
- env,
3885
- newAccountMinDepositAmount
3886
- });
3887
3912
  const accountInfo = yield getAccountInfo({ csna, env });
3888
3913
  const newActions = [];
3889
3914
  const debtAction = yield checkGasTokenDebt(csna, env, false);
@@ -3940,7 +3965,7 @@ function executeBTCDepositAndAction(_0) {
3940
3965
  postActions: postActionsStr,
3941
3966
  extraMsg: depositMsg.extra_msg
3942
3967
  });
3943
- const txHash = yield sendBitcoin(userDepositAddress, Number(depositAmount), _feeRate);
3968
+ const txHash = yield sendBitcoin(userDepositAddress, depositAmount, _feeRate);
3944
3969
  yield receiveDepositMsg(config.base_url, {
3945
3970
  btcPublicKey,
3946
3971
  txHash,
@@ -4147,7 +4172,7 @@ function calculateWithdraw(_0) {
4147
4172
  return {
4148
4173
  withdrawFee: 0,
4149
4174
  isError: true,
4150
- errorMsg: `Mini withdraw amount is ${Number(brgConfig.min_withdraw_amount) + Number(gasLimit)} sats`
4175
+ errorMsg: `Minimum withdraw amount is ${formatBtcAmount(Number(brgConfig.min_withdraw_amount) + Number(gasLimit))} BTC`
4151
4176
  };
4152
4177
  }
4153
4178
  }
@@ -4925,7 +4950,7 @@ function getGroup(state) {
4925
4950
 
4926
4951
  // src/index.ts
4927
4952
  var getVersion = () => {
4928
- return "0.5.44-beta";
4953
+ return "0.5.46-beta";
4929
4954
  };
4930
4955
  if (typeof window !== "undefined") {
4931
4956
  window.__BTC_WALLET_VERSION = getVersion();