NFT Minting Problem

Hey guys, Iā€™ve been building my first dApp with Moralis and React and right now Iā€™m stuck on my minting function. Iā€™ve been reading the forum but I could not find a solution, and trust me I tried a lot of stuff. Iā€™m attaching my code, my solidity contract, my abi, my nft object Iā€™m trying to mint and the error Iā€™m getting, so you can have all the information neededā€¦ Really hope ope someone can help me out!! Cheers

Solidity Contract:

contract ZimCollectables is ERC721URIStorage {
  using Counters for Counters.Counter;
  Counters.Counter private _tokenIds;

  constructor() ERC721('ZimCollectables', 'ZCOL') {}

  function mintToken(address recipient, string memory tokenURI) public returns (uint256) {
    _tokenIds.increment();
    uint256 newItemId = _tokenIds.current();
    _safeMint(recipient, newItemId);
    _setTokenURI(newItemId, tokenURI);
    return newItemId;
  }
}

NFT object to be minted (nft):

{
  "id": "30",
  "title": "Happy Corgi",
  "photo": "https://cdn.pixabay.com/photo/2019/08/19/07/45/dog-4415649_1280.jpg",
  "price": "1750",
  "category": "pet",
}

ABI:

  const abi = {
    inputs: [
      {
        internalType: 'address',
        name: 'recipient',
        type: 'address',
      },
      {
        internalType: 'string',
        name: 'tokenURI',
        type: 'string',
      },
    ],
    name: 'mintToken',
    outputs: [
      {
        internalType: 'uint256',
        name: '',
        type: 'uint256',
      },
    ],
    stateMutability: 'nonpayable',
    type: 'function',
  };

Minting Function:

  const MintNFT = async nft => {
    const options = {
      contractAddress: contractAddress,
      functionName: 'mintToken',
      abi: abi,
      params: {
        recipient: user.attributes.ethAddress,
        tokenURI: nft,
      },
    };
    const receipt = await Moralis.executeFunction(options);
    console.log(receipt)
  };

Error:

PLEASEEE HELPPPP MEEE!

1 Like

https://docs.moralis.io/moralis-server/web3/web3#executefunction

you may need to use a syntax like abi = [{}] and not abi = {}

here you can try first with a string instead of that nft object

1 Like

Hey!! So I wrapped the abi in [] and the nft in JSON.stringify and now metamask is opening and connectingā€¦.

Thereā€™s one issue now. The transaction is being sent from the user address to the contract address and not the other way aroundā€¦ would you happen to know why?

that is what it happens by default, that is how you send a transaction, from the user address

1 Like

Thanks you for all the responses! I still got a question! The purpose of the minting is when the users clicks the button, the contracts creates the NFT and the ownership of the NFT should be of the user. Does that makes sense? How should I proceed? Thanks again

users click a button to mint, that button generates a transaction for a chain, usually MetaMask popups to sign that transaction, after the transaction is signed it is sent automatically to a blockchain to be mined, it can take seconds until the transaction is included in a blockchain block, and after the transaction is executed successfully in a blockchain block then the mint has finished

1 Like

whoever will mintā€¦ the caller of the mint function is by default owner of that nft

1 Like

hey i m getting this ``error that min qty is required how i pass the quantity ?

moralis.js:28005 You are not using the latest version of the SDK. Please update it as soon as possible to enjoy the newest features. Most recent version: 0.0.178

moralis.js:5570 Moralis.enable() is deprecated and will be removed, use Moralis.enableWeb3() instead.

inpage.js:1 MetaMask: The event ā€˜closeā€™ is deprecated and may be removed in the future. Please use ā€˜disconnectā€™ instead. For more information, see: https://eips.ethereum.org/EIPS/eip-1193#disconnect

moralis.js:28005 You are not using the latest version of the SDK. Please update it as soon as possible to enjoy the newest features. Most recent version: 0.0.178

moralis.js:5570 Moralis.enable() is deprecated and will be removed, use Moralis.enableWeb3() instead.

mint.html:42 Uncaught (in promise)

  1. [ā€™_mintQty is requiredā€™]

mint.html:42 Uncaught (in promise)

  1. [ā€™_mintQty is requiredā€™]

AMD MY CODE IS

  <script>   
const abi= [
{
inputs: [
{
internalType: "uint256",
name: "_mintQty",
type: "uint256"
}
],
name: "publicSalesMint",
outputs: [ ],
stateMutability: "payable",
type: "function"
},
];
const serverUrl = "https://orkt02z9jtrj.usemoralis.com:2053/server";
    const appId ="S3Dbn8eVyKx0yByHsh6b3RTJUPI2gRJwk6KlBLmC";
   
    async function mint () {
       await  Moralis.start({ serverUrl, appId });
       window.web3 = await Moralis.Web3.enable();
      
const options = {
      contractAddress: 0xA4eACdf4aF749060a22AEfE06337cB9FB96d45fB,
      functionName: "publicSalesMint",
      abi: abi,
      params: _mintQty =5
      
    };
    const oi = await Moralis.executeFunction(options);
    console.log(oi);
  };
  mint();
  document.getElementById("hi").onclick= mint();
    
 
</script>

https://docs.moralis.io/moralis-server/web3/web3#executefunction
you have an example there in the documentation

you can use the latest Morslis sdk version, and some warning can be ignored

https://docs.moralis.io/moralis-server/getting-started/connect-the-sdk#creating-an-empty-page

i read the documentation but i dont know why my metamask pop up is not opening as i do the right code plus my minting function have error on browsers console that

mint.html:43 Uncaught (in promise) [ā€™_mintQty is requiredā€™]
mint @ mint.html:43
await in mint (async)
(anonymous) @ mint.html:44
mint.html:43 Uncaught (in promise) [ā€™_mintQty is requiredā€™]0: "_mintQty is required"length: 1[[Prototype]]: Array(0)
mint @ mint.html:43
await in mint (async)
(anonymous) @ mint.html:45

 <script>   
const abi= [
{
inputs: [
{
internalType: "uint256",
name: "_mintQty",
type: "uint256"
}
],
name: "publicSalesMint",
outputs: [ ],
stateMutability: "payable",
type: "function"
},
];
const serverUrl = "https://orkt02z9jtrj.usemoralis.com:2053/server";
    const appId ="S3Dbn8eVyKx0yByHsh6b3RTJUPI2gRJwk6KlBLmC";
   
    async function mint () {
       await  Moralis.start({ serverUrl, appId });
       const web3 = await Moralis.enableWeb3();

      
const options = {
      contractAddress: 0xA4eACdf4aF749060a22AEfE06337cB9FB96d45fB,
      functionName: "publicSalesMint",
      abi: abi,
      params: _mintQty= "5"
      
      
    };
    const oi = await Moralis.executeFunction(options);
    console.log(oi);
  };
mint();
  document.getElementById("hi").onclick= mint();
    
 
</script>

you want to execute that mint function twice here?

yeah i do by mistake but the same error comes if i m doing this

document.getElementById(ā€œhiā€).onclick= mint();

you could change the name of the parameter here in the ABI if you want, in case that is a problem with that name

how ? i didā€™nt understand what u want to say sir ā€¦

and when i m clicking on mint button then why my metamask pop up is not coming ?

it looks like there is this error, that may be why it doesnā€™t open metamask

How can I declare minQty? I make const , I pass it in Params but not get result

And yes metamask working I logged out from metamask and then clock on mint it opens pop up to login again , but it donā€™t sign messageā€¦

sir , can u check the abi .is it correct ?

you can modify the abi, instead of _mintQty in the abi you can write x