btc-wallet 0.3.17 → 0.3.18

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
@@ -85,6 +85,7 @@ var InjectedConnector = class extends BaseConnector {
85
85
  requestAccounts() {
86
86
  return __async(this, null, function* () {
87
87
  const accounts = yield this.getProviderOrThrow().requestAccounts();
88
+ console.log("network:", yield this.getNetwork());
88
89
  console.log("\u{1F680} ~ InjectedConnector ~ requestAccounts ~ accounts:", accounts);
89
90
  return accounts;
90
91
  });
@@ -446,6 +447,7 @@ var XverseConnector = class extends BaseConnector {
446
447
  });
447
448
  }
448
449
  };
450
+ console.log("\u{1F680} ~ XverseConnector ~ sendBitcoin ~ sendBtcOptions:", sendBtcOptions);
449
451
  sendBtcTransaction(sendBtcOptions).catch((e) => reject(e));
450
452
  });
451
453
  return result;
@@ -2446,12 +2448,22 @@ function InitBtcWalletSelectorContext() {
2446
2448
  return null;
2447
2449
  }
2448
2450
  function useBtcWalletSelector() {
2449
- const { openConnectModal, openConnectModalAsync, disconnect, requestDirectAccount } = useConnectModal();
2450
- const { accounts, sendBitcoin: sendBitcoin2, getPublicKey, provider, signMessage, connector, getNetwork: getNetwork2 } = useBTCProvider();
2451
+ const { openConnectModal, disconnect, requestDirectAccount } = useConnectModal();
2452
+ const {
2453
+ accounts,
2454
+ sendBitcoin: sendBitcoin2,
2455
+ getPublicKey,
2456
+ provider,
2457
+ signMessage,
2458
+ connector,
2459
+ getNetwork: getNetwork2,
2460
+ switchNetwork
2461
+ } = useBTCProvider();
2451
2462
  const publicKey = useRef(null);
2452
2463
  const signMessageFn = useRef(null);
2453
2464
  const connectorRef = useRef(null);
2454
2465
  const context = useContext2(WalletSelectorContext);
2466
+ const isLoggingIn = useRef(false);
2455
2467
  useEffect6(() => {
2456
2468
  if (provider) {
2457
2469
  getPublicKey().then((res) => {
@@ -2486,7 +2498,27 @@ function useBtcWalletSelector() {
2486
2498
  login: () => __async(this, null, function* () {
2487
2499
  const account = accounts == null ? void 0 : accounts[0];
2488
2500
  if (!account) {
2489
- openConnectModal == null ? void 0 : openConnectModal();
2501
+ if (isLoggingIn.current) {
2502
+ return null;
2503
+ }
2504
+ try {
2505
+ isLoggingIn.current = true;
2506
+ openConnectModal == null ? void 0 : openConnectModal();
2507
+ const account1 = yield retryOperation(
2508
+ () => window.btcContext.account,
2509
+ (res) => !!res,
2510
+ {
2511
+ maxRetries: 100,
2512
+ delayMs: 1e3
2513
+ }
2514
+ );
2515
+ if (!account1) {
2516
+ throw new Error("Failed to get account");
2517
+ }
2518
+ return account1;
2519
+ } finally {
2520
+ isLoggingIn.current = false;
2521
+ }
2490
2522
  }
2491
2523
  return account;
2492
2524
  }),
@@ -2513,6 +2545,10 @@ function useBtcWalletSelector() {
2513
2545
  return context;
2514
2546
  },
2515
2547
  getNetwork: getNetwork2,
2548
+ switchNetwork: (network) => __async(this, null, function* () {
2549
+ console.log("switchNetwork:", network);
2550
+ yield switchNetwork(network);
2551
+ }),
2516
2552
  sendBitcoin: sendBitcoin2
2517
2553
  };
2518
2554
  }, [
@@ -2520,6 +2556,7 @@ function useBtcWalletSelector() {
2520
2556
  context,
2521
2557
  disconnect,
2522
2558
  getNetwork2,
2559
+ switchNetwork,
2523
2560
  openConnectModal,
2524
2561
  requestDirectAccount,
2525
2562
  sendBitcoin2
@@ -3184,11 +3221,11 @@ function getAccountInfo(csna, accountContractId) {
3184
3221
  return accountInfo;
3185
3222
  });
3186
3223
  }
3187
- function checkGasTokenBalance(csna, gasToken, isDev) {
3224
+ function checkGasTokenBalance(csna, gasToken, minAmount, isDev) {
3188
3225
  return __async(this, null, function* () {
3189
3226
  const amount = yield nearCall(gasToken, "ft_balance_of", { account_id: csna });
3190
3227
  console.log("gas token balance:", amount);
3191
- if (new Big(amount).lte(MINIMUM_DEPOSIT_AMOUNT_BASE)) {
3228
+ if (new Big(amount).lt(minAmount)) {
3192
3229
  yield Dialog.confirm({
3193
3230
  title: "Gas token balance is insufficient",
3194
3231
  message: "Please deposit gas token to continue, will open bridge website."
@@ -3444,7 +3481,6 @@ var state = {
3444
3481
  return window.localStorage.getItem("btc-wallet-btc-publickey");
3445
3482
  }
3446
3483
  };
3447
- var inter = null;
3448
3484
  var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3449
3485
  metadata,
3450
3486
  options,
@@ -3467,41 +3503,49 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3467
3503
  const isDev = (_a = "isDev" in metadata && metadata.isDev) != null ? _a : false;
3468
3504
  const currentConfig = isDev ? walletConfig.dev : walletConfig[options.network.networkId];
3469
3505
  const walletNetwork = isDev ? "dev" : options.network.networkId;
3470
- initWalletButton(walletNetwork, wallet);
3471
- if (!inter) {
3472
- inter = setInterval(() => __async(void 0, null, function* () {
3473
- const btcContext = window.btcContext;
3474
- if (btcContext) {
3475
- clearInterval(inter);
3476
- const context = btcContext.getContext();
3477
- context.on("updatePublicKey", (btcPublicKey) => __async(void 0, null, function* () {
3478
- const { nearTempAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3479
- removeWalletButton();
3480
- initWalletButton(walletNetwork, wallet);
3481
- emitter.emit("accountsChanged", {
3482
- accounts: [
3483
- {
3484
- accountId: nearTempAddress
3485
- }
3486
- ]
3487
- });
3488
- }));
3489
- context.on("btcLoginError", (e) => __async(void 0, null, function* () {
3490
- emitter.emit("accountsChanged", {
3491
- accounts: []
3492
- });
3493
- }));
3494
- context.on("btcLogOut", (e) => __async(void 0, null, function* () {
3495
- emitter.emit("accountsChanged", {
3496
- accounts: []
3497
- });
3498
- }));
3499
- if ("autoConnect" in metadata && metadata.autoConnect && localStorage.getItem("near-wallet-selector:selectedWalletId") === '"btc-wallet"') {
3500
- yield btcContext.autoConnect();
3501
- }
3502
- clearInterval(inter);
3506
+ yield initBtcContext();
3507
+ function setupBtcContextListeners(btcContext) {
3508
+ return __async(this, null, function* () {
3509
+ const context = btcContext.getContext();
3510
+ context.on("updatePublicKey", (btcPublicKey) => __async(this, null, function* () {
3511
+ const { nearTempAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3512
+ removeWalletButton();
3513
+ initConnected(walletNetwork, wallet);
3514
+ emitter.emit("accountsChanged", {
3515
+ accounts: [{ accountId: nearTempAddress }]
3516
+ });
3517
+ }));
3518
+ context.on("btcLoginError", () => __async(this, null, function* () {
3519
+ emitter.emit("accountsChanged", { accounts: [] });
3520
+ }));
3521
+ context.on("btcLogOut", () => __async(this, null, function* () {
3522
+ emitter.emit("accountsChanged", { accounts: [] });
3523
+ }));
3524
+ if ("autoConnect" in metadata && metadata.autoConnect && localStorage.getItem("near-wallet-selector:selectedWalletId") === '"btc-wallet"') {
3525
+ yield btcContext.autoConnect();
3503
3526
  }
3504
- }), 500);
3527
+ });
3528
+ }
3529
+ function initBtcContext() {
3530
+ return __async(this, null, function* () {
3531
+ console.log("initBtcContext");
3532
+ const btcContext = yield retryOperation(
3533
+ () => __async(this, null, function* () {
3534
+ const ctx = window.btcContext;
3535
+ if (!ctx) {
3536
+ throw new Error("btcContext not found");
3537
+ }
3538
+ return ctx;
3539
+ }),
3540
+ (res) => !!res,
3541
+ {
3542
+ maxRetries: 10,
3543
+ delayMs: 500
3544
+ }
3545
+ );
3546
+ yield setupBtcContextListeners(btcContext);
3547
+ return btcContext;
3548
+ });
3505
3549
  }
3506
3550
  function nearCall2(contractId, methodName, args) {
3507
3551
  return __async(this, null, function* () {
@@ -3531,28 +3575,20 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3531
3575
  }
3532
3576
  function signIn(_02) {
3533
3577
  return __async(this, arguments, function* ({ contractId, methodNames }) {
3578
+ const btcContext = window.btcContext;
3534
3579
  const accountId = state.getAccount();
3535
3580
  const publicKey = state.getPublicKey();
3536
- const btcContext = window.btcContext;
3537
- initWalletButton(walletNetwork, wallet);
3538
- if (accountId && publicKey) {
3539
- return [
3540
- {
3541
- accountId,
3542
- publicKey
3543
- }
3544
- ];
3581
+ console.log("isLogin:", accountId && publicKey);
3582
+ if (!accountId || !publicKey) {
3583
+ yield btcContext.login();
3545
3584
  }
3546
- yield btcContext.login();
3547
- const btcPublicKey = yield retryOperation(btcContext.getPublicKey, (res) => !!res, {
3548
- maxRetries: 40,
3549
- delayMs: 3e3
3550
- });
3585
+ const btcPublicKey = yield btcContext.getPublicKey();
3551
3586
  console.log("btcPublicKey:", btcPublicKey);
3552
3587
  if (!btcPublicKey) {
3553
3588
  throw new Error("No connected BTC wallet, please connect your BTC wallet first.");
3554
3589
  }
3555
3590
  const { nearTempAddress, nearTempPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3591
+ initConnected(walletNetwork, wallet);
3556
3592
  return [
3557
3593
  {
3558
3594
  accountId: nearTempAddress,
@@ -3571,6 +3607,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3571
3607
  const btcContext = window.btcContext;
3572
3608
  if (metadata.syncLogOut) {
3573
3609
  btcContext.logout();
3610
+ yield delay(300);
3574
3611
  }
3575
3612
  state.clear();
3576
3613
  window.localStorage.removeItem("near-wallet-selector:selectedWalletId");
@@ -3608,7 +3645,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3608
3645
  const btcContext = window.btcContext;
3609
3646
  const accountId = state.getAccount();
3610
3647
  const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
3611
- yield checkGasTokenBalance(accountId, currentConfig.token, isDev);
3612
3648
  yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, true);
3613
3649
  const trans = [...params.transactions];
3614
3650
  console.log("raw trans:", trans);
@@ -3620,6 +3656,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3620
3656
  console.log("transferGasTransaction:", transferGasTransaction);
3621
3657
  console.log("useNearPayGas:", useNearPayGas);
3622
3658
  console.log("gasLimit:", gasLimit);
3659
+ yield checkGasTokenBalance(accountId, currentConfig.token, gasLimit, isDev);
3623
3660
  if (transferGasTransaction) {
3624
3661
  trans.unshift(transferGasTransaction);
3625
3662
  }
@@ -3810,7 +3847,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3810
3847
  return { txBytes, txHex, hash };
3811
3848
  });
3812
3849
  }
3813
- function initWalletButton(network, wallet2) {
3850
+ function initConnected(network, wallet2) {
3814
3851
  return __async(this, null, function* () {
3815
3852
  const checkAndSetupWalletButton = () => {
3816
3853
  const accountId = state.getAccount();
@@ -3869,7 +3906,7 @@ function setupBTCWallet({
3869
3906
 
3870
3907
  // src/index.ts
3871
3908
  var getVersion = () => {
3872
- return "0.3.17";
3909
+ return "0.3.18";
3873
3910
  };
3874
3911
  if (typeof window !== "undefined") {
3875
3912
  window.__BTC_WALLET_VERSION = getVersion();