[SOLVED]In moralis, how to do this kind of 'Add Token'?

Original code like below, I want to use Moralis to re-structure.

const tokenImage = "https://xxx.com/free.ico";
      await window.ethereum.request({
        method: "wallet_watchAsset",
        params: {
          type: "ERC20",
          options: {
            address: tokenAddress,
            symbol: tokenSymbol,
            decimals: tokenDecimals,
            image: tokenImage,
          },
        },
      });

There doesn’t seem to be anything for that in the Moralis SDK.

My destination, is to add token to MetaMask, when user click ‘Add’ button.

And I am working along the direction of :

image

At this stage I think you’ll have to stick with using the watchAsset method. I can’t find a Moralis abstraction for it in the docs.

I also wonder, if it will be any conflict with moralis.

yarn add ethers

If you want to use ethers, you can do this: Web3 Provider - Moralis

1 Like

Following.

  const handleAddToken: MouseEventHandler<HTMLButtonElement> = async (e) => {
    // const {  Moralis } = useMoralis();

    const ethers = Moralis.web3Library;
    const web3 = await Moralis.enableWeb3({ provider: "metamask" });
    
    // await window.ethereum.request({
    //   method: "wallet_watchAsset",
    //   params: {
    //     type: "ERC20",
    //     options: {
    //       address: tokenAddress,
    //       symbol: tokenSymbol,
    //       decimals: tokenDecimals,
    //       image: tokenImage,
    //     },
    //   },
    // });
  };

It seems like there is some confusion here about what exactly are the minimum requirements to call the “Add Token” method mentioned. You don’t need either Ethers.js or web3.js to use the “wallet_watchAsset” method. If you have anything injecting a window.ethereum object on your browser (i.e. MetaMask wallet), you are good to go to fire up “wallet_watchAsset”, plus it won’t break anything related to Moralis SDK.

The only thing I would recommend doing is checking if there is a window.ethereum object available before proceeding with the watchAsset method, here is an example of how to do that:

1 Like