Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to react on network and wallet changes #30

Open
csmuller opened this issue Jun 27, 2022 · 5 comments
Open

Add a way to react on network and wallet changes #30

csmuller opened this issue Jun 27, 2022 · 5 comments

Comments

@csmuller
Copy link

We need a functionality that allows the user of the wallet-adapt to react on network changes (e.g., changing from mainnet to testnet) and wallet changes. Currently the adapter simply disconnects the wallet on such changes.

It would be nice if the WALLET_CHANGED/NETWORK_CHANGED events would bubble up to the library user or the useWallet() method would return variables that hold such state. Something like:

const { address, wallet, connecting, walletChanged, networkChaged } = useWallet();
@csmuller
Copy link
Author

@alexsteixeira, @gsmachado please comment if my description is unclear.

@gsmachado
Copy link

I think that's fine. 👍

Basically, we need that a way to pass through the events from the specific wallet to the NWA SDK users. 😸

@mfbz
Copy link
Contributor

mfbz commented Jun 27, 2022

Yep, at the moment it disconnects everytime to act like walletconnect.
Anyway, I agree regarding the fact that it would be cool to have those events also if they won't work when connecting to walletconnect wallet.

@gsmachado
Copy link

hey @mfbz!
any timeline for this feature?! 😄

@mfbz
Copy link
Contributor

mfbz commented Aug 21, 2023

Heyy @gsmachado
Actually, considering @csmuller question I'm thinking about 2 ways of implementing this:

Case 1

No changes needed on the codebase. You implement directly the hooks needed.

walletChanged

...
const [currentWalletName, setCurrentWalletName] = useState<string | null>(null);
useEffect(()=> {
  if (wallet?.name !== currentWalletName) {
    setCurrentWalletName(wallet.name);
    onWalletChanged(currentWalletName);
  }
}, [wallet, currentWalletName]);

networkChanged

...
const [walletNetwork, setWalletNetwork] = useState<string | null>(null);
const fetchWalletNetwork = useCallback(async () => {
  try {
	  const result = await getNetworks();
	  if (result?.status === 'success') {
		  setWalletNetwork(result.data?.defaultNetwork || null);
	  }
  } catch (error) {
	  console.error(error);
  }
}, [getNetworks]);
useEffect(() => {
  if (connected) {
	  fetchWalletNetwork();
  } else {
	  setWalletNetwork(null);
  }
}, [connected, fetchWalletNetwork]);

Case 2:

Basically the same but in the library. Maybe with custom hooks separated from useWallet one to avoid having wallet network fetching happening also when not needed.

Thoughts about this? If you need them in a hurry I think it's better to go with Case 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants