5

When building a Solana dApp, developers need to understand changes in both the active wallet for the current user and in changes to the selected network for that wallet. Changes to the active wallet can be detected through events or by comparing the active account public key. What events, listeners, or automated checks can be used to detect changes in the selected network for the current wallet if the selected account public key stays the same (i.e. on mainnet-beta and testnet)?


For reference, this question is centered around event detection across all wallets supported in @solana/wallet-adapter. Link to Repo: https://github.com/solana-labs/wallet-adapter

1
  • Do you have a piece of code that is not working? You will get better answers if you are more specific about what you have tried and the problems you are running into
    – sohrab
    Jul 14, 2022 at 11:21

1 Answer 1

5

I'll answer this as comprehensively as I can.

Unfortunately you may be displeased (as am I!) to know that this isn't generally possible.

Let's cover network changes first.

  • This issue (which is quite old) describes the desire for this feature.
  • This PR contains an attempt to implement this in Wallet Adapter.

The problem with this is that Wallet Adapter can only do what wallets can do. Wallets don't generally have a way to either

  • Set the network to use for sending things. A few wallets (currently -- Solflare, Glow, Sollet, Blocto) implement this, and their adapters try to support it, but it's uncommon and therefore can't be relied upon.
  • Detect when a wallet changes the network it's configured to use. No wallet adapter implements this. If any wallet supports it, it's unknown to me, and because it's uncommon, can't be relied upon.

Now, account changes.

Basically, the same points as above. Wallets don't generally expose APIs for switching active accounts to dapps, and they also don't generally emit events for when active accounts change, or when accounts are added or removed from wallets.

The short answer is that what you want to do cannot currently be done in a general fashion, but it is being actively worked on, by standardizing the interface of wallets and their events, and decoupling the network and account state of wallets from dapps.

I can't provide a timeline of when this will be done, but here's some more reading:

I'll be providing more updates over the next few weeks on the initial draft of a new Wallet Standard that aims to fix these issues and more.

2
  • 1
    Accepting this as the most comprehensive summary of the issue - hopefully this becomes a standard event we can listen to in future releases.
    – Wagg
    Jul 20, 2022 at 1:03
  • 1
    @Wagg the Phantom adapter has been updated: twitter.com/jordaaash/status/1559708025631301632 This still isn't generally possible, but it is possible for some wallets!
    – Jordan
    Aug 17, 2022 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.