4

I am using @solana/wallet-adapter-react How can I check what network I am on? because I want to show an alert to users that they are on devnet and should switch to mainnet

2 Answers 2

4

You can't, the "standard" wallet doesn't report any state back to the adapter except the public key and the functions to send transactions.

Note that you can't even read the SOL balance from the wallet, even though phantom clearly has that info in the extension. You have to send a getBalance() request to the RPC.

This is annoying because if you send a tx and the user is on the wrong network, they get a very generic failure error about too long to approve the tx (at least on Phantom). It's also neat because you can read a wallet's balance on some network even if they are connected to another network.

2
  • wow So how do you suggest I go about my situation? Just hope that the user discovers he's on devnet and switches to mainnet? Jan 28, 2023 at 7:51
  • You can send a dummy "test" tx and see if it errors, but that's not a great solution because the user still has to sign it. Tbh this is rarely an issue because normal users do not generally switch to devnet. The setting to switch to devnet is hidden under "developer settings" in most wallets.
    – Whiteseal
    Jan 31, 2023 at 16:41
0

You can set the network you are currently on by using WalletAdapterNetwork

example

export const Wallet: FC = () => {
    // The network can be set to 'devnet', 'testnet', or 'mainnet-beta'.
    const network = WalletAdapterNetwork.Devnet;

    // You can also provide a custom RPC endpoint.
    const endpoint = useMemo(() => clusterApiUrl(network), [network]);

    ...
};

Taken from https://github.com/solana-labs/wallet-adapter/blob/master/APP.md

5
  • 1
    Have you tested this code? it always connects to same wallet regardless you are on testnet, devmet or main-beta
    – Yilmaz
    Jan 27, 2023 at 17:07
  • The cluster used for website != cluster used in your wallet. The website will use the assigned cluster. But the wallet might show a different cluster. Most Solana wallets implement a feature to change the cluster you want to view your balances for.
    – vicyyn
    Jan 27, 2023 at 17:16
  • I know how to set the network What I want to do is check what network the user's wallet is set to I noticed on opensea that an alert dialog is shown at the top of the page when I am on devnet. So I want to achieve that same thing here with solana wallets Jan 27, 2023 at 23:28
  • "The cluster used for website != cluster used in your wallet."... Oh so does this mean the user doesn't need to change the network to devnet? and as long I set the cluset to mainnet, he's gonna get charged from mainnet balance? Jan 27, 2023 at 23:30
  • 1
    @NooblantisDistrict even though you change the cluster in react, from devnet to mainnet-beta, the connected wallet is still the same on the phantom. I do not understand what is going on.
    – Yilmaz
    Jan 28, 2023 at 0:45

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.