MetaMask API – How to Detect If Token Already Registered?

javascriptmetamasktokens

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.

Best Answer

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.

Related Topic