3

I'd like to prevent a user from entering my dapp until they have registered my token with MetaMask.

I accomplish this using the code below, which is executed the moment the user is about to enter the dapp:

const wasAdded = await ethereum.request({
        method: "wallet_watchAsset",
        params: {
            type: "ERC20",
            options: {
                address: TOKEN_ADDRESS,
                symbol: TOKEN_SYMBOL,
                decimals: 18,
                image: "",
            },
        },
    });

If wasAdded returns false, I prevent the user from entering the dapp.

The problem is, this code will ALWAYS show a prompt to the user to register the token, even if it is already registered.

Is there any way of checking if a token is already registered before triggering this prompt?

If not, what are the alternatives? It's not feasible to show a pointless prompt every time the user launches the dapp.

2 Answers 2

2

There is no way to check if it is registered

Instead, you default to unregistered tokens for all users, and when the user first registers, you record them so they don't need to be displayed to the user the next time.

Or it can be displayed inconspicuously in case a user removes the token, and it can be re-registered.

2
  • 1
    Thanks. That's disappointing. I'd rather that MetaMask record this and I could query MetaMask, this way the state couldn't go out of sync. Aug 19, 2021 at 18:36
  • Unfortunately, you have to do it yourself
    – zmy
    Aug 21, 2021 at 2:33
-1

How to add unlisted tokens (custom tokens) in MetaMask

You can find Automatic token detection (Experimented at the point of writing this).

Your Answer

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

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