btc-wallet 0.3.22 → 0.3.24

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/esm/index.js CHANGED
@@ -2731,6 +2731,14 @@ var walletConfig = {
2731
2731
  walletUrl: "https://wallet-test.satoshibridge.top",
2732
2732
  bridgeUrl: "https://testnet.satoshibridge.top/"
2733
2733
  },
2734
+ private_mainnet: {
2735
+ base_url: "https://api.stg.satoshibridge.top",
2736
+ token: "nbtc.toalice.near",
2737
+ accountContractId: "acc.toalice.near",
2738
+ bridgeContractId: "brg.toalice.near",
2739
+ walletUrl: "https://wallet-stg.satoshibridge.top",
2740
+ bridgeUrl: "https://stg.satoshibridge.top/"
2741
+ },
2734
2742
  mainnet: {
2735
2743
  base_url: "https://api.mainnet.satoshibridge.top",
2736
2744
  token: "nbtc.toalice.near",
@@ -3200,10 +3208,9 @@ function getBtcRpcUrl() {
3200
3208
  return btcRpcUrls[network];
3201
3209
  });
3202
3210
  }
3203
- function getConfig(isDev) {
3211
+ function getConfig(env) {
3204
3212
  return __async(this, null, function* () {
3205
- const network = yield getNetwork();
3206
- return walletConfig[isDev ? "dev" : network];
3213
+ return walletConfig[env];
3207
3214
  });
3208
3215
  }
3209
3216
  function nearCall(contractId, methodName, args) {
@@ -3219,7 +3226,7 @@ function getAccountInfo(csna, accountContractId) {
3219
3226
  return accountInfo;
3220
3227
  });
3221
3228
  }
3222
- function checkGasTokenBalance(csna, gasToken, minAmount, isDev) {
3229
+ function checkGasTokenBalance(csna, gasToken, minAmount, env) {
3223
3230
  return __async(this, null, function* () {
3224
3231
  const amount = yield nearCall(gasToken, "ft_balance_of", { account_id: csna });
3225
3232
  console.log("gas token balance:", amount);
@@ -3228,17 +3235,17 @@ function checkGasTokenBalance(csna, gasToken, minAmount, isDev) {
3228
3235
  title: "Gas token balance is insufficient",
3229
3236
  message: "Please deposit gas token to continue, will open bridge website."
3230
3237
  });
3231
- const config = yield getConfig(isDev);
3238
+ const config = yield getConfig(env);
3232
3239
  window.open(config.bridgeUrl, "_blank");
3233
3240
  throw new Error("Gas token balance is insufficient");
3234
3241
  }
3235
3242
  });
3236
3243
  }
3237
- function checkGasTokenArrears(debtInfo, isDev, autoDeposit) {
3244
+ function checkGasTokenArrears(debtInfo, env, autoDeposit) {
3238
3245
  return __async(this, null, function* () {
3239
3246
  if (!debtInfo)
3240
3247
  return;
3241
- const config = yield getConfig(isDev);
3248
+ const config = yield getConfig(env);
3242
3249
  const transferAmount = debtInfo.transfer_amount;
3243
3250
  console.log("get_account debtInfo:", debtInfo);
3244
3251
  const action = {
@@ -3253,7 +3260,7 @@ function checkGasTokenArrears(debtInfo, isDev, autoDeposit) {
3253
3260
  message: "You have gas token arrears, please deposit gas token to continue."
3254
3261
  });
3255
3262
  if (confirmed) {
3256
- yield executeBTCDepositAndAction({ action, isDev });
3263
+ yield executeBTCDepositAndAction({ action, env });
3257
3264
  yield Dialog.alert({
3258
3265
  title: "Deposit success",
3259
3266
  message: "Deposit success, will continue to execute transaction."
@@ -3263,6 +3270,14 @@ function checkGasTokenArrears(debtInfo, isDev, autoDeposit) {
3263
3270
  }
3264
3271
  });
3265
3272
  }
3273
+ function queryGasTokenArrears(env) {
3274
+ return __async(this, null, function* () {
3275
+ const config = yield getConfig(env);
3276
+ const csna = yield getCsnaAccountId(env);
3277
+ const accountInfo = yield getAccountInfo(csna, config.accountContractId);
3278
+ return accountInfo == null ? void 0 : accountInfo.debt_info;
3279
+ });
3280
+ }
3266
3281
  function getBtcGasPrice() {
3267
3282
  return __async(this, null, function* () {
3268
3283
  const network = yield getNetwork();
@@ -3319,7 +3334,7 @@ function estimateDepositAmount(amount, option) {
3319
3334
  }
3320
3335
  function getDepositAmount(amount, option) {
3321
3336
  return __async(this, null, function* () {
3322
- const config = yield getConfig((option == null ? void 0 : option.isDev) || false);
3337
+ const config = yield getConfig((option == null ? void 0 : option.env) || "mainnet");
3323
3338
  const {
3324
3339
  deposit_bridge_fee: { fee_min, fee_rate }
3325
3340
  } = yield nearCall(
@@ -3338,6 +3353,21 @@ function getDepositAmount(amount, option) {
3338
3353
  };
3339
3354
  });
3340
3355
  }
3356
+ function getCsnaAccountId(env) {
3357
+ return __async(this, null, function* () {
3358
+ const config = yield getConfig(env);
3359
+ const { getPublicKey } = getBtcProvider();
3360
+ const btcPublicKey = yield getPublicKey();
3361
+ const csna = yield nearCall(
3362
+ config.accountContractId,
3363
+ "get_chain_signature_near_account_id",
3364
+ {
3365
+ btc_public_key: btcPublicKey
3366
+ }
3367
+ );
3368
+ return csna;
3369
+ });
3370
+ }
3341
3371
  function executeBTCDepositAndAction(_0) {
3342
3372
  return __async(this, arguments, function* ({
3343
3373
  action,
@@ -3345,12 +3375,12 @@ function executeBTCDepositAndAction(_0) {
3345
3375
  feeRate,
3346
3376
  fixedAmount = true,
3347
3377
  pollResult = true,
3348
- isDev = false
3378
+ env = "mainnet"
3349
3379
  }) {
3350
3380
  var _a;
3351
3381
  try {
3352
3382
  const { getPublicKey } = getBtcProvider();
3353
- const config = yield getConfig(isDev);
3383
+ const config = yield getConfig(env);
3354
3384
  const btcPublicKey = yield getPublicKey();
3355
3385
  if (!btcPublicKey) {
3356
3386
  throw new Error("BTC Public Key is not available.");
@@ -3358,34 +3388,35 @@ function executeBTCDepositAndAction(_0) {
3358
3388
  if (!amount && !action) {
3359
3389
  throw new Error("amount or action is required");
3360
3390
  }
3361
- const csna = yield nearCall(
3362
- config.accountContractId,
3363
- "get_chain_signature_near_account_id",
3364
- {
3365
- btc_public_key: btcPublicKey
3366
- }
3367
- );
3391
+ const csna = yield getCsnaAccountId(env);
3368
3392
  const rawDepositAmount = (_a = action ? action.amount : amount) != null ? _a : "0";
3369
3393
  if (new Big(rawDepositAmount).lt(0)) {
3370
3394
  throw new Error("amount must be greater than 0");
3371
3395
  }
3372
3396
  const { depositAmount, receiveAmount } = yield getDepositAmount(rawDepositAmount, {
3373
- isDev
3397
+ env
3374
3398
  });
3375
3399
  const accountInfo = yield getAccountInfo(csna, config.accountContractId);
3376
3400
  const newActions = [];
3377
3401
  const gasLimit = new Big(50).mul(__pow(10, 12)).toFixed(0);
3378
- const repayAction = yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, false);
3402
+ const repayAction = yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, env, false);
3379
3403
  if (repayAction) {
3380
3404
  newActions.push(__spreadProps(__spreadValues({}, repayAction), {
3381
3405
  gas: gasLimit
3382
3406
  }));
3383
3407
  }
3384
- if (action) {
3385
- newActions.push(__spreadProps(__spreadValues({}, action), {
3386
- amount: (repayAction == null ? void 0 : repayAction.amount) && !fixedAmount ? new Big(receiveAmount).minus(repayAction.amount).toString() : receiveAmount.toString(),
3387
- gas: gasLimit
3388
- }));
3408
+ if (action || !action && new Big((accountInfo == null ? void 0 : accountInfo.gas_token[config.token]) || 0).lt(MINIMUM_DEPOSIT_AMOUNT_BASE)) {
3409
+ newActions.push(
3410
+ action ? __spreadProps(__spreadValues({}, action), {
3411
+ amount: (repayAction == null ? void 0 : repayAction.amount) && !fixedAmount ? new Big(receiveAmount).minus(repayAction.amount).toString() : receiveAmount.toString(),
3412
+ gas: gasLimit
3413
+ }) : {
3414
+ receiver_id: config.accountContractId,
3415
+ amount: MINIMUM_DEPOSIT_AMOUNT_BASE.toString(),
3416
+ msg: JSON.stringify("Deposit"),
3417
+ gas: gasLimit
3418
+ }
3419
+ );
3389
3420
  }
3390
3421
  const depositMsg = {
3391
3422
  recipient_id: csna,
@@ -3492,7 +3523,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3492
3523
  id,
3493
3524
  provider
3494
3525
  }) {
3495
- var _a;
3496
3526
  const wallet = {
3497
3527
  signIn,
3498
3528
  signOut,
@@ -3503,9 +3533,9 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3503
3533
  signAndSendTransaction,
3504
3534
  signAndSendTransactions
3505
3535
  };
3506
- const isDev = (_a = "isDev" in metadata && metadata.isDev) != null ? _a : false;
3507
- const currentConfig = isDev ? walletConfig.dev : walletConfig[options.network.networkId];
3508
- const walletNetwork = isDev ? "dev" : options.network.networkId;
3536
+ const env = options.network.networkId || metadata.env || "mainnet";
3537
+ const currentConfig = walletConfig[env];
3538
+ const walletNetwork = ["mainnet", "private_mainnet"].includes(env) ? "mainnet" : "testnet";
3509
3539
  yield initBtcContext();
3510
3540
  function setupBtcContextListeners() {
3511
3541
  return __async(this, null, function* () {
@@ -3575,21 +3605,17 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3575
3605
  }
3576
3606
  function getNearAccountByBtcPublicKey(btcPublicKey) {
3577
3607
  return __async(this, null, function* () {
3578
- const nearAddress = yield nearCall2(
3579
- currentConfig.accountContractId,
3580
- "get_chain_signature_near_account_id",
3581
- { btc_public_key: btcPublicKey }
3582
- );
3608
+ const csna = yield getCsnaAccountId(env);
3583
3609
  const nearPublicKey = yield nearCall2(
3584
3610
  currentConfig.accountContractId,
3585
3611
  "get_chain_signature_near_account_public_key",
3586
3612
  { btc_public_key: btcPublicKey }
3587
3613
  );
3588
- state.saveAccount(nearAddress);
3614
+ state.saveAccount(csna);
3589
3615
  state.savePublicKey(nearPublicKey);
3590
3616
  state.saveBtcPublicKey(btcPublicKey);
3591
3617
  return {
3592
- nearAddress,
3618
+ nearAddress: csna,
3593
3619
  nearPublicKey
3594
3620
  };
3595
3621
  });
@@ -3669,7 +3695,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3669
3695
  const btcContext = window.btcContext;
3670
3696
  const accountId = state.getAccount();
3671
3697
  const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
3672
- yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, true);
3698
+ yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, env, true);
3673
3699
  const trans = [...params.transactions];
3674
3700
  console.log("raw trans:", trans);
3675
3701
  const gasTokenBalance = (accountInfo == null ? void 0 : accountInfo.gas_token[currentConfig.token]) || "0";
@@ -3680,7 +3706,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3680
3706
  console.log("transferGasTransaction:", transferGasTransaction);
3681
3707
  console.log("useNearPayGas:", useNearPayGas);
3682
3708
  console.log("gasLimit:", gasLimit);
3683
- yield checkGasTokenBalance(accountId, currentConfig.token, gasLimit, isDev);
3709
+ yield checkGasTokenBalance(accountId, currentConfig.token, gasLimit, env);
3684
3710
  if (transferGasTransaction) {
3685
3711
  trans.unshift(transferGasTransaction);
3686
3712
  }
@@ -3766,7 +3792,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3766
3792
  }
3767
3793
  function calculateGasStrategy(gasTokenBalance, transactions2) {
3768
3794
  return __async(this, null, function* () {
3769
- var _a2;
3795
+ var _a;
3770
3796
  const accountId = state.getAccount();
3771
3797
  const nearAccount = yield provider.query({
3772
3798
  request_type: "view_account",
@@ -3788,7 +3814,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3788
3814
  );
3789
3815
  console.log("list_gas_token gas tokens:", gasTokens);
3790
3816
  const perTxFee = Math.max(
3791
- Number(((_a2 = gasTokens[currentConfig.token]) == null ? void 0 : _a2.per_tx_protocol_fee) || 0),
3817
+ Number(((_a = gasTokens[currentConfig.token]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
3792
3818
  100
3793
3819
  );
3794
3820
  console.log("perTxFee:", perTxFee);
@@ -3879,7 +3905,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3879
3905
  const btcNetwork = yield btcContext.getNetwork();
3880
3906
  console.log("btcNetwork:", btcNetwork, network);
3881
3907
  const networkMap = {
3882
- livenet: ["mainnet"],
3908
+ livenet: ["mainnet", "private_mainnet"],
3883
3909
  testnet: ["testnet", "dev"]
3884
3910
  };
3885
3911
  if (!networkMap[btcNetwork].includes(network)) {
@@ -3902,7 +3928,7 @@ function setupBTCWallet({
3902
3928
  deprecated = false,
3903
3929
  autoConnect = true,
3904
3930
  syncLogOut = true,
3905
- isDev = false
3931
+ env = "mainnet"
3906
3932
  } = {}) {
3907
3933
  console.log("\u26A1\uFE0F BTC Wallet Version:", getVersion());
3908
3934
  const btcWallet = () => __async(this, null, function* () {
@@ -3918,7 +3944,7 @@ function setupBTCWallet({
3918
3944
  available: true,
3919
3945
  autoConnect,
3920
3946
  syncLogOut,
3921
- isDev
3947
+ env
3922
3948
  },
3923
3949
  init: BTCWallet
3924
3950
  };
@@ -3928,7 +3954,7 @@ function setupBTCWallet({
3928
3954
 
3929
3955
  // src/index.ts
3930
3956
  var getVersion = () => {
3931
- return "0.3.22";
3957
+ return "0.3.24";
3932
3958
  };
3933
3959
  if (typeof window !== "undefined") {
3934
3960
  window.__BTC_WALLET_VERSION = getVersion();
@@ -3953,8 +3979,10 @@ export {
3953
3979
  getAccountInfo,
3954
3980
  getBtcBalance,
3955
3981
  getBtcGasPrice,
3982
+ getCsnaAccountId,
3956
3983
  getDepositAmount,
3957
3984
  getVersion,
3985
+ queryGasTokenArrears,
3958
3986
  sendBitcoin,
3959
3987
  setupBTCWallet,
3960
3988
  useAccountContract,