How to add a NFT to metamask wallet with a RPC method

We have a way to add an ERC20 token to the metamask wallet from our dapps with a RPC method:

 const addTokenToMetamask = async () => {
      if (typeof window.ethereum !== "undefined") {
        
        const tokenAddress = "0x;
        const tokenSymbol = "TOKEN";
        const tokenDecimals = 18;
        const tokenImage =
          "https://raw.githubusercontent.com/logo.png";
        
  
        try {
          // wasAdded is a boolean. Like any RPC method, an error may be thrown.
          const wasAdded = await window.ethereum.request({
            method: "wallet_watchAsset",
            params: {
              type: "ERC20", // Initially only supports ERC20, but eventually more!
              options: {
                address: tokenAddress, // The address that the token is at.
                symbol: tokenSymbol, // A ticker symbol or shorthand, up to 5 chars.
                decimals: tokenDecimals, // The number of decimals in the token
                image: tokenImage, // A string url of the token logo
              },
            },
          });
        } catch (error) {
          console.log(error);
        }
      }
    };

As NFTs can be added from the v10.30 metamask version of the browser, I was wondering if there was any RPC method to add nfts to the metamask from our dapps. If not, how would you do it?

1 Like

Try this:

params: {
    type: "ERC1046",
    options: {
        address: tokenAddress, // string (the hexadecimal address of the token contract)
        chainId: 1 // number (if empty, defaults to the current chain ID)
    }
}

But this insertion does not indicate the nft contract and the id NFT.
How will it work?

ERC-1046:

  • Extends ERC-20 with an ERC-721-like tokenURI
  • Extends ERC-721 and ERC-1155 with interoperability

I'm assuming that MetaMask can then read the required information from the provided contract address.

image

I believe that there is a separate branch of MetaMask implementing this feature (as part of EIP-747).
Perhaps try to import that branch instead.

I use the version available for the browser.
Since I wanted it to be a client-side feature.
If it's not available, that's fine.
The client is instructed to do it manually.

By 'try to import that branch instead', I meant for you to see if that version was available for client-side.