React Moralis - mint NFT using useWeb3ExecuteFunction

In my react moralis app I have following imports:

import { Moralis } from 'moralis';
import { useMoralis, useMoralisWeb3Api, useNewMoralisObject, useMoralisCloudFunction, useWeb3ExecuteFunction } from 'react-moralis';
import { contractAbi } from '../abis/abi';

and following component:

const CreateNFT = () => {
        const { data, error, fetch, isFetching, isLoading } = useWeb3ExecuteFunction({
            abi: contractAbi,
            contractAddress: '0x0B41BA45e82d6A4F2e70EC5Bc6ce191798dC29A1',
            functionName: "mint",
            params: {
                _to: user.attributes.ethAddress,
                _mintAmount: 1
            },
        });

        return (<div>
            {error &&  <div>{JSON.stringify(error)}</div>}
            <button onClick={() => fetch()} disabled={isFetching}>Mint NFT</button>   
        </div>);
    }

In solidity, the mint function looks like this:

function mint(address _to, uint256 _mintAmount) public {
    uint256 supply = totalSupply();
    require(!paused);
    require(_mintAmount > 0);
    require(_mintAmount <= maxMintAmount);
    require(supply + _mintAmount <= maxSupply);

    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(_to, supply + i);
    }
  }

This returns an error, but the error’s array is empty. What could be the reason?

1 Like

I have tried We3API.native.runContractFunction but it didnt work for me.
I am currently using this:

const mint = async () => {
    let web3 = await Moralis.Web3.enable();
    const contract = new web3.eth.Contract(smartContractAbi, contractAddress);

    let receipt = await contract.methods
      .mint(1)
      .send({ from: user.attributes.ethAddress, value: 8620580000000000 })
      .then((response) => console.log(response))
      .catch((err) => setError(err.message));
  };

and this works for me, my smart contract mint function only takes one parameter which is _amount, you should also make sure you are authenticated, i have the following variables pulled from useMoralis()

const {
    Moralis,
    authenticate,
    isInitialized,
    isAuthenticated,
    user,
    logout,
  } = useMoralis();
1 Like

@nosh247, I think that sending value parameter was a recent fix to Moralis SDK, you could try again with latest Moralis SDK.

1 Like

@nosh247 thanks for this! What are you setting as value? If I want to make the minting free (outside of the gas fee ofc) then what value should I set?

I’m not sure, but try with 0 also your smart contract cost should be set to 0 also.

Thank you! I’ll check it out.

did you get a fix for this? it’s not working for me no matter what I try

      const {data, error, fetch, isFetching_}  = useWeb3ExecuteFunction({
        abi: _nftea,
        contractAddress:_contractTea.toString(),
        name:_contractTea.toString(),
        functionName: "mint",
        params: {
          quantity_: _mintForm.amount,
          redeems_: _mintForm.redeems,
          data_: w3.utils.asciiToHex(_mintForm.metadataurl_),
          ipfs_:  _mintForm.ipfs,
          payto_: _mintForm.payTo,
          splitwith_: _mintForm.splitwith,
          splitpercent_:_mintForm.split,
          redeemfrequency_:_mintForm.redeemfrequency
        },
      });

error
invalid address or ENS name (argument=“name”, value=0, code=INVALID_ARGUMENT, version=contracts/5.6.0)

Keep it to this thread please.