btc-wallet 0.3.22 → 0.3.24

Sign up to get free protection for your applications and to get access to all the features.
package/dist/config.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export declare const walletConfig: Record<string, {
1
+ export type ENV = 'mainnet' | 'testnet' | 'private_mainnet' | 'dev';
2
+ export declare const walletConfig: Record<ENV, {
2
3
  base_url: string;
3
4
  token: string;
4
5
  accountContractId: string;
@@ -1,3 +1,4 @@
1
+ import type { ENV } from '../config';
1
2
  import type { FinalExecutionOutcome } from '@near-wallet-selector/core';
2
3
  export interface DebtInfo {
3
4
  gas_token_id: string;
@@ -10,13 +11,14 @@ export declare function getAccountInfo(csna: string, accountContractId: string):
10
11
  gas_token: Record<string, string>;
11
12
  debt_info?: DebtInfo;
12
13
  } | undefined>;
13
- export declare function checkGasTokenBalance(csna: string, gasToken: string, minAmount: string, isDev: boolean): Promise<void>;
14
+ export declare function checkGasTokenBalance(csna: string, gasToken: string, minAmount: string, env: ENV): Promise<void>;
14
15
  type CheckGasTokenArrearsReturnType<T extends boolean> = T extends true ? void : {
15
16
  receiver_id: string;
16
17
  amount: string;
17
18
  msg: string;
18
19
  } | undefined;
19
- export declare function checkGasTokenArrears<T extends boolean>(debtInfo: DebtInfo | undefined, isDev: boolean, autoDeposit?: T): Promise<CheckGasTokenArrearsReturnType<T>>;
20
+ export declare function checkGasTokenArrears<T extends boolean>(debtInfo: DebtInfo | undefined, env: ENV, autoDeposit?: T): Promise<CheckGasTokenArrearsReturnType<T>>;
21
+ export declare function queryGasTokenArrears(env: ENV): Promise<DebtInfo | undefined>;
20
22
  export declare function getBtcGasPrice(): Promise<number>;
21
23
  export declare function getBtcBalance(): Promise<{
22
24
  rawBalance: number;
@@ -31,16 +33,17 @@ export declare function getBtcBalance(): Promise<{
31
33
  }>;
32
34
  export declare function sendBitcoin(address: string, amount: number, feeRate: number): Promise<string>;
33
35
  export declare function estimateDepositAmount(amount: string, option?: {
34
- isDev?: boolean;
36
+ env?: ENV;
35
37
  }): Promise<number>;
36
38
  export declare function getDepositAmount(amount: string, option?: {
37
39
  isEstimate?: boolean;
38
- isDev?: boolean;
40
+ env?: ENV;
39
41
  }): Promise<{
40
42
  depositAmount: number;
41
43
  receiveAmount: number;
42
44
  fee: number;
43
45
  }>;
46
+ export declare function getCsnaAccountId(env: ENV): Promise<string>;
44
47
  interface ExecuteBTCDepositAndActionParams<T extends boolean = true> {
45
48
  action?: {
46
49
  receiver_id: string;
@@ -50,12 +53,12 @@ interface ExecuteBTCDepositAndActionParams<T extends boolean = true> {
50
53
  amount?: string;
51
54
  feeRate?: number;
52
55
  fixedAmount?: boolean;
53
- isDev?: boolean;
56
+ env?: ENV;
54
57
  pollResult?: T;
55
58
  }
56
59
  /**
57
60
  * @param T - if true, return the poll result, otherwise return the btcTxHash
58
61
  */
59
62
  type ExecuteBTCDepositAndActionReturn<T extends boolean> = T extends true ? FinalExecutionOutcome[] : string;
60
- export declare function executeBTCDepositAndAction<T extends boolean = true>({ action, amount, feeRate, fixedAmount, pollResult, isDev, }: ExecuteBTCDepositAndActionParams<T>): Promise<ExecuteBTCDepositAndActionReturn<T>>;
63
+ export declare function executeBTCDepositAndAction<T extends boolean = true>({ action, amount, feeRate, fixedAmount, pollResult, env, }: ExecuteBTCDepositAndActionParams<T>): Promise<ExecuteBTCDepositAndActionReturn<T>>;
61
64
  export {};
@@ -1,5 +1,6 @@
1
1
  import type { InjectedWallet, WalletModuleFactory } from '@near-wallet-selector/core';
2
2
  import type { useBtcWalletSelector } from './btcWalletSelectorContext';
3
+ import type { ENV } from '../config';
3
4
  declare global {
4
5
  interface Window {
5
6
  btcContext: ReturnType<typeof useBtcWalletSelector>;
@@ -10,9 +11,9 @@ interface BTCWalletParams {
10
11
  deprecated?: boolean;
11
12
  autoConnect?: boolean;
12
13
  syncLogOut?: boolean;
13
- isDev?: boolean;
14
+ env?: ENV;
14
15
  }
15
- export declare function setupBTCWallet({ iconUrl, deprecated, autoConnect, syncLogOut, isDev, }?: BTCWalletParams | undefined): WalletModuleFactory<InjectedWallet>;
16
+ export declare function setupBTCWallet({ iconUrl, deprecated, autoConnect, syncLogOut, env, }?: BTCWalletParams | undefined): WalletModuleFactory<InjectedWallet>;
16
17
  declare const _default: {
17
18
  setupBTCWallet: typeof setupBTCWallet;
18
19
  };
package/dist/index.js CHANGED
@@ -97,8 +97,10 @@ __export(src_exports, {
97
97
  getAccountInfo: () => getAccountInfo,
98
98
  getBtcBalance: () => getBtcBalance,
99
99
  getBtcGasPrice: () => getBtcGasPrice,
100
+ getCsnaAccountId: () => getCsnaAccountId,
100
101
  getDepositAmount: () => getDepositAmount,
101
102
  getVersion: () => getVersion,
103
+ queryGasTokenArrears: () => queryGasTokenArrears,
102
104
  sendBitcoin: () => sendBitcoin,
103
105
  setupBTCWallet: () => setupBTCWallet,
104
106
  useAccountContract: () => useAccountContract,
@@ -2775,6 +2777,14 @@ var walletConfig = {
2775
2777
  walletUrl: "https://wallet-test.satoshibridge.top",
2776
2778
  bridgeUrl: "https://testnet.satoshibridge.top/"
2777
2779
  },
2780
+ private_mainnet: {
2781
+ base_url: "https://api.stg.satoshibridge.top",
2782
+ token: "nbtc.toalice.near",
2783
+ accountContractId: "acc.toalice.near",
2784
+ bridgeContractId: "brg.toalice.near",
2785
+ walletUrl: "https://wallet-stg.satoshibridge.top",
2786
+ bridgeUrl: "https://stg.satoshibridge.top/"
2787
+ },
2778
2788
  mainnet: {
2779
2789
  base_url: "https://api.mainnet.satoshibridge.top",
2780
2790
  token: "nbtc.toalice.near",
@@ -3244,10 +3254,9 @@ function getBtcRpcUrl() {
3244
3254
  return btcRpcUrls[network];
3245
3255
  });
3246
3256
  }
3247
- function getConfig(isDev) {
3257
+ function getConfig(env) {
3248
3258
  return __async(this, null, function* () {
3249
- const network = yield getNetwork();
3250
- return walletConfig[isDev ? "dev" : network];
3259
+ return walletConfig[env];
3251
3260
  });
3252
3261
  }
3253
3262
  function nearCall(contractId, methodName, args) {
@@ -3263,7 +3272,7 @@ function getAccountInfo(csna, accountContractId) {
3263
3272
  return accountInfo;
3264
3273
  });
3265
3274
  }
3266
- function checkGasTokenBalance(csna, gasToken, minAmount, isDev) {
3275
+ function checkGasTokenBalance(csna, gasToken, minAmount, env) {
3267
3276
  return __async(this, null, function* () {
3268
3277
  const amount = yield nearCall(gasToken, "ft_balance_of", { account_id: csna });
3269
3278
  console.log("gas token balance:", amount);
@@ -3272,17 +3281,17 @@ function checkGasTokenBalance(csna, gasToken, minAmount, isDev) {
3272
3281
  title: "Gas token balance is insufficient",
3273
3282
  message: "Please deposit gas token to continue, will open bridge website."
3274
3283
  });
3275
- const config = yield getConfig(isDev);
3284
+ const config = yield getConfig(env);
3276
3285
  window.open(config.bridgeUrl, "_blank");
3277
3286
  throw new Error("Gas token balance is insufficient");
3278
3287
  }
3279
3288
  });
3280
3289
  }
3281
- function checkGasTokenArrears(debtInfo, isDev, autoDeposit) {
3290
+ function checkGasTokenArrears(debtInfo, env, autoDeposit) {
3282
3291
  return __async(this, null, function* () {
3283
3292
  if (!debtInfo)
3284
3293
  return;
3285
- const config = yield getConfig(isDev);
3294
+ const config = yield getConfig(env);
3286
3295
  const transferAmount = debtInfo.transfer_amount;
3287
3296
  console.log("get_account debtInfo:", debtInfo);
3288
3297
  const action = {
@@ -3297,7 +3306,7 @@ function checkGasTokenArrears(debtInfo, isDev, autoDeposit) {
3297
3306
  message: "You have gas token arrears, please deposit gas token to continue."
3298
3307
  });
3299
3308
  if (confirmed) {
3300
- yield executeBTCDepositAndAction({ action, isDev });
3309
+ yield executeBTCDepositAndAction({ action, env });
3301
3310
  yield Dialog.alert({
3302
3311
  title: "Deposit success",
3303
3312
  message: "Deposit success, will continue to execute transaction."
@@ -3307,6 +3316,14 @@ function checkGasTokenArrears(debtInfo, isDev, autoDeposit) {
3307
3316
  }
3308
3317
  });
3309
3318
  }
3319
+ function queryGasTokenArrears(env) {
3320
+ return __async(this, null, function* () {
3321
+ const config = yield getConfig(env);
3322
+ const csna = yield getCsnaAccountId(env);
3323
+ const accountInfo = yield getAccountInfo(csna, config.accountContractId);
3324
+ return accountInfo == null ? void 0 : accountInfo.debt_info;
3325
+ });
3326
+ }
3310
3327
  function getBtcGasPrice() {
3311
3328
  return __async(this, null, function* () {
3312
3329
  const network = yield getNetwork();
@@ -3363,7 +3380,7 @@ function estimateDepositAmount(amount, option) {
3363
3380
  }
3364
3381
  function getDepositAmount(amount, option) {
3365
3382
  return __async(this, null, function* () {
3366
- const config = yield getConfig((option == null ? void 0 : option.isDev) || false);
3383
+ const config = yield getConfig((option == null ? void 0 : option.env) || "mainnet");
3367
3384
  const {
3368
3385
  deposit_bridge_fee: { fee_min, fee_rate }
3369
3386
  } = yield nearCall(
@@ -3382,6 +3399,21 @@ function getDepositAmount(amount, option) {
3382
3399
  };
3383
3400
  });
3384
3401
  }
3402
+ function getCsnaAccountId(env) {
3403
+ return __async(this, null, function* () {
3404
+ const config = yield getConfig(env);
3405
+ const { getPublicKey } = getBtcProvider();
3406
+ const btcPublicKey = yield getPublicKey();
3407
+ const csna = yield nearCall(
3408
+ config.accountContractId,
3409
+ "get_chain_signature_near_account_id",
3410
+ {
3411
+ btc_public_key: btcPublicKey
3412
+ }
3413
+ );
3414
+ return csna;
3415
+ });
3416
+ }
3385
3417
  function executeBTCDepositAndAction(_0) {
3386
3418
  return __async(this, arguments, function* ({
3387
3419
  action,
@@ -3389,12 +3421,12 @@ function executeBTCDepositAndAction(_0) {
3389
3421
  feeRate,
3390
3422
  fixedAmount = true,
3391
3423
  pollResult = true,
3392
- isDev = false
3424
+ env = "mainnet"
3393
3425
  }) {
3394
3426
  var _a;
3395
3427
  try {
3396
3428
  const { getPublicKey } = getBtcProvider();
3397
- const config = yield getConfig(isDev);
3429
+ const config = yield getConfig(env);
3398
3430
  const btcPublicKey = yield getPublicKey();
3399
3431
  if (!btcPublicKey) {
3400
3432
  throw new Error("BTC Public Key is not available.");
@@ -3402,34 +3434,35 @@ function executeBTCDepositAndAction(_0) {
3402
3434
  if (!amount && !action) {
3403
3435
  throw new Error("amount or action is required");
3404
3436
  }
3405
- const csna = yield nearCall(
3406
- config.accountContractId,
3407
- "get_chain_signature_near_account_id",
3408
- {
3409
- btc_public_key: btcPublicKey
3410
- }
3411
- );
3437
+ const csna = yield getCsnaAccountId(env);
3412
3438
  const rawDepositAmount = (_a = action ? action.amount : amount) != null ? _a : "0";
3413
3439
  if (new import_big.default(rawDepositAmount).lt(0)) {
3414
3440
  throw new Error("amount must be greater than 0");
3415
3441
  }
3416
3442
  const { depositAmount, receiveAmount } = yield getDepositAmount(rawDepositAmount, {
3417
- isDev
3443
+ env
3418
3444
  });
3419
3445
  const accountInfo = yield getAccountInfo(csna, config.accountContractId);
3420
3446
  const newActions = [];
3421
3447
  const gasLimit = new import_big.default(50).mul(__pow(10, 12)).toFixed(0);
3422
- const repayAction = yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, false);
3448
+ const repayAction = yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, env, false);
3423
3449
  if (repayAction) {
3424
3450
  newActions.push(__spreadProps(__spreadValues({}, repayAction), {
3425
3451
  gas: gasLimit
3426
3452
  }));
3427
3453
  }
3428
- if (action) {
3429
- newActions.push(__spreadProps(__spreadValues({}, action), {
3430
- amount: (repayAction == null ? void 0 : repayAction.amount) && !fixedAmount ? new import_big.default(receiveAmount).minus(repayAction.amount).toString() : receiveAmount.toString(),
3431
- gas: gasLimit
3432
- }));
3454
+ if (action || !action && new import_big.default((accountInfo == null ? void 0 : accountInfo.gas_token[config.token]) || 0).lt(MINIMUM_DEPOSIT_AMOUNT_BASE)) {
3455
+ newActions.push(
3456
+ action ? __spreadProps(__spreadValues({}, action), {
3457
+ amount: (repayAction == null ? void 0 : repayAction.amount) && !fixedAmount ? new import_big.default(receiveAmount).minus(repayAction.amount).toString() : receiveAmount.toString(),
3458
+ gas: gasLimit
3459
+ }) : {
3460
+ receiver_id: config.accountContractId,
3461
+ amount: MINIMUM_DEPOSIT_AMOUNT_BASE.toString(),
3462
+ msg: JSON.stringify("Deposit"),
3463
+ gas: gasLimit
3464
+ }
3465
+ );
3433
3466
  }
3434
3467
  const depositMsg = {
3435
3468
  recipient_id: csna,
@@ -3536,7 +3569,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3536
3569
  id,
3537
3570
  provider
3538
3571
  }) {
3539
- var _a;
3540
3572
  const wallet = {
3541
3573
  signIn,
3542
3574
  signOut,
@@ -3547,9 +3579,9 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3547
3579
  signAndSendTransaction,
3548
3580
  signAndSendTransactions
3549
3581
  };
3550
- const isDev = (_a = "isDev" in metadata && metadata.isDev) != null ? _a : false;
3551
- const currentConfig = isDev ? walletConfig.dev : walletConfig[options.network.networkId];
3552
- const walletNetwork = isDev ? "dev" : options.network.networkId;
3582
+ const env = options.network.networkId || metadata.env || "mainnet";
3583
+ const currentConfig = walletConfig[env];
3584
+ const walletNetwork = ["mainnet", "private_mainnet"].includes(env) ? "mainnet" : "testnet";
3553
3585
  yield initBtcContext();
3554
3586
  function setupBtcContextListeners() {
3555
3587
  return __async(this, null, function* () {
@@ -3619,21 +3651,17 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3619
3651
  }
3620
3652
  function getNearAccountByBtcPublicKey(btcPublicKey) {
3621
3653
  return __async(this, null, function* () {
3622
- const nearAddress = yield nearCall2(
3623
- currentConfig.accountContractId,
3624
- "get_chain_signature_near_account_id",
3625
- { btc_public_key: btcPublicKey }
3626
- );
3654
+ const csna = yield getCsnaAccountId(env);
3627
3655
  const nearPublicKey = yield nearCall2(
3628
3656
  currentConfig.accountContractId,
3629
3657
  "get_chain_signature_near_account_public_key",
3630
3658
  { btc_public_key: btcPublicKey }
3631
3659
  );
3632
- state.saveAccount(nearAddress);
3660
+ state.saveAccount(csna);
3633
3661
  state.savePublicKey(nearPublicKey);
3634
3662
  state.saveBtcPublicKey(btcPublicKey);
3635
3663
  return {
3636
- nearAddress,
3664
+ nearAddress: csna,
3637
3665
  nearPublicKey
3638
3666
  };
3639
3667
  });
@@ -3713,7 +3741,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3713
3741
  const btcContext = window.btcContext;
3714
3742
  const accountId = state.getAccount();
3715
3743
  const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
3716
- yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, true);
3744
+ yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, env, true);
3717
3745
  const trans = [...params.transactions];
3718
3746
  console.log("raw trans:", trans);
3719
3747
  const gasTokenBalance = (accountInfo == null ? void 0 : accountInfo.gas_token[currentConfig.token]) || "0";
@@ -3724,7 +3752,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3724
3752
  console.log("transferGasTransaction:", transferGasTransaction);
3725
3753
  console.log("useNearPayGas:", useNearPayGas);
3726
3754
  console.log("gasLimit:", gasLimit);
3727
- yield checkGasTokenBalance(accountId, currentConfig.token, gasLimit, isDev);
3755
+ yield checkGasTokenBalance(accountId, currentConfig.token, gasLimit, env);
3728
3756
  if (transferGasTransaction) {
3729
3757
  trans.unshift(transferGasTransaction);
3730
3758
  }
@@ -3810,7 +3838,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3810
3838
  }
3811
3839
  function calculateGasStrategy(gasTokenBalance, transactions2) {
3812
3840
  return __async(this, null, function* () {
3813
- var _a2;
3841
+ var _a;
3814
3842
  const accountId = state.getAccount();
3815
3843
  const nearAccount = yield provider.query({
3816
3844
  request_type: "view_account",
@@ -3832,7 +3860,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3832
3860
  );
3833
3861
  console.log("list_gas_token gas tokens:", gasTokens);
3834
3862
  const perTxFee = Math.max(
3835
- Number(((_a2 = gasTokens[currentConfig.token]) == null ? void 0 : _a2.per_tx_protocol_fee) || 0),
3863
+ Number(((_a = gasTokens[currentConfig.token]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
3836
3864
  100
3837
3865
  );
3838
3866
  console.log("perTxFee:", perTxFee);
@@ -3923,7 +3951,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3923
3951
  const btcNetwork = yield btcContext.getNetwork();
3924
3952
  console.log("btcNetwork:", btcNetwork, network);
3925
3953
  const networkMap = {
3926
- livenet: ["mainnet"],
3954
+ livenet: ["mainnet", "private_mainnet"],
3927
3955
  testnet: ["testnet", "dev"]
3928
3956
  };
3929
3957
  if (!networkMap[btcNetwork].includes(network)) {
@@ -3946,7 +3974,7 @@ function setupBTCWallet({
3946
3974
  deprecated = false,
3947
3975
  autoConnect = true,
3948
3976
  syncLogOut = true,
3949
- isDev = false
3977
+ env = "mainnet"
3950
3978
  } = {}) {
3951
3979
  console.log("\u26A1\uFE0F BTC Wallet Version:", getVersion());
3952
3980
  const btcWallet = () => __async(this, null, function* () {
@@ -3962,7 +3990,7 @@ function setupBTCWallet({
3962
3990
  available: true,
3963
3991
  autoConnect,
3964
3992
  syncLogOut,
3965
- isDev
3993
+ env
3966
3994
  },
3967
3995
  init: BTCWallet
3968
3996
  };
@@ -3972,7 +4000,7 @@ function setupBTCWallet({
3972
4000
 
3973
4001
  // src/index.ts
3974
4002
  var getVersion = () => {
3975
- return "0.3.22";
4003
+ return "0.3.24";
3976
4004
  };
3977
4005
  if (typeof window !== "undefined") {
3978
4006
  window.__BTC_WALLET_VERSION = getVersion();