@txnlab/use-wallet 3.1.5 → 3.2.0

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/dist/index.cjs CHANGED
@@ -5972,10 +5972,11 @@ function setAccounts(store, { walletId, accounts }) {
5972
5972
  };
5973
5973
  });
5974
5974
  }
5975
- function setActiveNetwork(store, { networkId }) {
5975
+ function setActiveNetwork(store, { networkId, algodClient }) {
5976
5976
  store.setState((state) => ({
5977
5977
  ...state,
5978
- activeNetwork: networkId
5978
+ activeNetwork: networkId,
5979
+ algodClient
5979
5980
  }));
5980
5981
  }
5981
5982
  function isValidWalletId(walletId) {
@@ -6007,13 +6008,18 @@ function isValidState(state) {
6007
6008
  var WalletManager = class {
6008
6009
  _clients = /* @__PURE__ */ new Map();
6009
6010
  networkConfig;
6010
- algodClient;
6011
6011
  store;
6012
6012
  subscribe;
6013
6013
  constructor({ wallets = [], network = "testnet" /* TESTNET */, algod = {} } = {}) {
6014
- const initialState = this.loadPersistedState() || {
6014
+ this.networkConfig = this.initNetworkConfig(network, algod);
6015
+ const persistedState = this.loadPersistedState();
6016
+ const initialState = persistedState ? {
6017
+ ...persistedState,
6018
+ algodClient: this.createAlgodClient(this.networkConfig[persistedState.activeNetwork])
6019
+ } : {
6015
6020
  ...defaultState,
6016
- activeNetwork: network
6021
+ activeNetwork: network,
6022
+ algodClient: this.createAlgodClient(this.networkConfig[network])
6017
6023
  };
6018
6024
  this.store = new import_store13.Store(initialState, {
6019
6025
  onUpdate: () => this.savePersistedState()
@@ -6025,11 +6031,18 @@ var WalletManager = class {
6025
6031
  });
6026
6032
  return unsubscribe;
6027
6033
  };
6028
- this.networkConfig = this.initNetworkConfig(network, algod);
6029
- this.algodClient = this.createAlgodClient(this.networkConfig[network]);
6030
6034
  this.initializeWallets(wallets);
6031
6035
  }
6032
6036
  // ---------- Store ------------------------------------------------- //
6037
+ get algodClient() {
6038
+ return this.store.state.algodClient;
6039
+ }
6040
+ set algodClient(algodClient) {
6041
+ this.store.setState((state) => ({
6042
+ ...state,
6043
+ algodClient
6044
+ }));
6045
+ }
6033
6046
  loadPersistedState() {
6034
6047
  try {
6035
6048
  const serializedState = StorageAdapter.getItem(LOCAL_STORAGE_KEY);
@@ -6049,8 +6062,9 @@ var WalletManager = class {
6049
6062
  }
6050
6063
  savePersistedState() {
6051
6064
  try {
6052
- const state = this.store.state;
6053
- const serializedState = JSON.stringify(state);
6065
+ const { wallets, activeWallet, activeNetwork } = this.store.state;
6066
+ const persistedState = { wallets, activeWallet, activeNetwork };
6067
+ const serializedState = JSON.stringify(persistedState);
6054
6068
  StorageAdapter.setItem(LOCAL_STORAGE_KEY, serializedState);
6055
6069
  } catch (error) {
6056
6070
  console.error("[Store] Could not save state to local storage:", error);
@@ -6128,7 +6142,8 @@ var WalletManager = class {
6128
6142
  return networkConfig;
6129
6143
  }
6130
6144
  createAlgodClient(config) {
6131
- console.info(`[Manager] Creating Algodv2 client for ${this.activeNetwork}...`);
6145
+ if (this.store)
6146
+ console.info(`[Manager] Creating Algodv2 client for ${this.activeNetwork}...`);
6132
6147
  const { token = "", baseServer, port = "", headers = {} } = config;
6133
6148
  return new import_algosdk12.default.Algodv2(token, baseServer, port, headers);
6134
6149
  }
@@ -6139,8 +6154,8 @@ var WalletManager = class {
6139
6154
  if (this.activeNetwork === networkId) {
6140
6155
  return;
6141
6156
  }
6142
- setActiveNetwork(this.store, { networkId });
6143
- this.algodClient = this.createAlgodClient(this.networkConfig[networkId]);
6157
+ const algodClient = this.createAlgodClient(this.networkConfig[networkId]);
6158
+ setActiveNetwork(this.store, { networkId, algodClient });
6144
6159
  console.info(`[Manager] \u2705 Active network set to ${networkId}.`);
6145
6160
  };
6146
6161
  get activeNetwork() {