How to call `meetadataURI` to mint with `Moralis.executeFunction();`

I don’t see parameters used here, you can look in documentation to see how to add them

I am still making lots of tries and errors. When something goes right, other things go wrong…

I cannot figure out why this error occurs:

This is the current code:

const txt = await mintToken(metadataURI).then(notify)
})();

async function mintToken(_uri){

const encodedFunction = web3.eth.abi.encodeFunctionCall({
name: "mint",
type: "function",
inputs: [{type: 'string', name: 'tokenURI'},]
}, [_uri]);

const options = {
contractAddress: nft_contract_address,
functionName: "mint",
abi: ABI,
params: {
type: 'string', 
name: 'tokenURI',
msgValue: Moralis.Units.ETH("0.01")
},
};

const transaction = await Moralis.executeFunction(options);
const result = await transaction.wait();

const transactionParameters = {
to: nft_contract_address,
from: ethereum.selectedAddress,
data: encodedFunction,
  };
  
const txt = await ethereum.request({
method: 'eth_sendTransaction',
params: [transactionParameters]
  });
  return txt
}

async function notify(_txt){
  document.getElementById("resultSpace").innerHTML =  
  `<input disabled = "true" id="result" type="text" class="form-control" placeholder="Description" aria-label="URL" aria-describedby="basic-addon1" value="Your NFT was minted in transaction ${_txt}">`;
}

This is the contract function:

    function mint(string memory tokenURI) external payable {
        require(isMintEnabled, 'minting not enabled');
        require(mintedWallets[msg.sender] < 5, 'exceeds max per wallet');
        require(msg.value == mintPrice, 'wrong value');
        require(maxSupply > totalSupply, 'sold out');

        mintedWallets[msg.sender]++;
        totalSupply++;
        uint256 tokenId = totalSupply;
        _safeMint(msg.sender, tokenId);
        _setTokenURI(tokenId, tokenURI);

    }

Could I have any advises, please.

it looks like you are using two different methods there, one with moralis.ExecuteFunction an one with

try to use only one of them

Thanks for telling me that.
I have removed all the redundant codes:
I still get the same error.

const txt = await mintToken(metadataURI)
})();

async function mintToken(_uri){

const encodedFunction = web3.eth.abi.encodeFunctionCall({
name: "mint",
type: "function",
inputs: [{type: 'string', name: 'tokenURI'},]
}, [_uri]);

const options = {
contractAddress: nft_contract_address,
functionName: "mint",
abi: ABI,
params: {
type: 'string', 
name: 'tokenURI',
msgValue: Moralis.Units.ETH("0.01")
},
};

const transaction = await Moralis.executeFunction(options);
const result = await transaction.wait();

const transactionParameters = {
to: nft_contract_address,
from: ethereum.selectedAddress,
data: encodedFunction,
};

}
}

look at the example from documentation

Now, I started to understand how to catch the variable that resides in the smart contract.
But, I get new error which is related to msg.value.

Here is the error:

Here is the code:

const metadataURI = metadataFile.ipfs();
console.log("metadataURI:", metadataURI);

const options = {
contractAddress: nft_contract_address,
functionName: "mint",
abi: ABI,
params: {
tokenURI: metadataURI,
msgValue: Moralis.Units.ETH("0.01")
}
};

await mintToken(metadataURI);


async function mintToken(_uri){

const encodedFunction = web3.eth.abi.encodeFunctionCall({
name: "mint",
type: "function",
inputs: [{"name": "tokenURI", "type": "string"},]
}, [_uri]);


const transaction = await Moralis.executeFunction(options);
const result = await transaction.wait();

const transactionParameters = {
to: nft_contract_address,
from: ethereum.selectedAddress,
data: encodedFunction,
};

}


})();

Why is MetaMask returning me the error? I gave it the correct value tho.

maybe that wrong value message is directly from the contract? maybe there is a check in that contract function

msgValue needs to be outside of the params object.

const options = {
  contractAddress: nft_contract_address,
  functionName: "mint",
  abi: ABI,
  params: {
    tokenURI: metadataURI,
  },
  msgValue: Moralis.Units.ETH("0.01")
};

Thanks, @alex.
You are right. msgValue worked at outside of the param object.

But, I still cannot figure out why the total amount is way higher than the msgValue: Moralis.Units.ETH("0.01").

Total amount is 18.4894ETH. Gas fee is 0.04275ETH.
I was supposed to have the total of 1.04275. Why 18.4894?

Btw, msgValue: Moralis.Units.ETH("0.01") is called right, because whenever I change the value to something other than the 0.01, I get wrong value in return. This message is exactly what I meant as in require(msg.value == mintPrice, 'wrong value');

chain: "rinkeby", solved the issue related to the total amount.

Could you help me with the issue related to tokenURI: metadataURI?
It does mint, but unable to make tokenURI: metadataURI work. I cannot see the metadata via OpenSea.

What am I missing here?
Code I used is here:

const metadataURI = metadataFile.ipfs();
console.log("metadataURI:", metadataURI);

const options = {
chain: "rinkeby",
contractAddress: nft_contract_address,
functionName: "mint",
abi: ABI,
params: {
tokenURI: metadataURI,
},
msgValue: Moralis.Units.ETH("0.01")
};

const transaction = await Moralis.executeFunction(options);
const receipt = await transaction.wait();
const events = receipt.events[0];

console.log('(executeFunction) receipt', receipt);
console.log('(executeFunction) events', events);

metadataURI is parsed fine, but unable to catch with params: {tokenURI: metadataURI,}
Do you have any suggestion?

Can you paste that IPFS link here as well as the contractAddress + tokenId, and the OpenSea page.

ipfsURL: https://ipfs.moralis.io:2053/ipfs/QmPesx2hv4A3Qqfoo4qP9zgXDKu9qofKCwWEN3LTHukVi1
VM406:533
metadataURI: https://ipfs.moralis.io:2053/ipfs/QmSyMiX3ykDbifX8UytQprM3to8L9BCWANoxDZKPYNbsH3

https://rinkeby.etherscan.io/address/0xaa8195070669bcc9dd61a0cf38c093998772e995

Sorry, I don’t know how I can specify the OpenSea URL. I just connect to MetaMask to see if it’s uploaded.

it looks like that contract has some nfts now:

{
  "total": 3,
  "page": 0,
  "page_size": 100,
  "cursor": null,
  "result": [
    {
      "token_hash": "cb1e8637c107aea84d2f94f69909c8d4",
      "token_address": "0xaa8195070669bcc9dd61a0cf38c093998772e995",
      "token_id": "1",
      "block_number_minted": "11427799",
      "amount": "1",
      "contract_type": "ERC721",
      "name": "Simple Mint",
      "symbol": "SIMPLEMINT",
      "token_uri": "https://ipfs.moralis.io:2053/ipfs/QmSVecSzUroFa7EvSAuxEyf3iKpdzSECPx76h7kPNFaXAg",
      "metadata": "{\"name\":\"kita\",\"description\":\"minami\",\"image\":\"https://ipfs.moralis.io:2053/ipfs/QmPesx2hv4A3Qqfoo4qP9zgXDKu9qofKCwWEN3LTHukVi1\"}",
      "last_token_uri_sync": "2022-09-23T01:23:21.993Z",
      "last_metadata_sync": "2022-09-23T01:23:30.642Z"
    },
    {
      "token_hash": "5f369fed6c78de1a9858ef85b5708d15",
      "token_address": "0xaa8195070669bcc9dd61a0cf38c093998772e995",
      "token_id": "2",
      "block_number_minted": "11428032",
      "amount": "1",
      "contract_type": "ERC721",
      "name": "Simple Mint",
      "symbol": "SIMPLEMINT",
      "token_uri": "https://ipfs.moralis.io:2053/ipfs/QmSXDki6oJPP4NfC6fgavdMkyFtRSRPKLnGLv7AqzY49ya",
      "metadata": "{\"name\":\"wait\",\"description\":\"for\",\"image\":\"https://ipfs.moralis.io:2053/ipfs/QmPesx2hv4A3Qqfoo4qP9zgXDKu9qofKCwWEN3LTHukVi1\"}",
      "last_token_uri_sync": "2022-09-23T02:22:04.972Z",
      "last_metadata_sync": "2022-09-23T02:22:13.893Z"
    },
    {
      "token_hash": "29a36edc6d487929f5456964ccd6b601",
      "token_address": "0xaa8195070669bcc9dd61a0cf38c093998772e995",
      "token_id": "3",
      "block_number_minted": "11428171",
      "amount": "1",
      "contract_type": "ERC721",
      "name": "Simple Mint",
      "symbol": "SIMPLEMINT",
      "token_uri": "https://ipfs.moralis.io:2053/ipfs/QmSyMiX3ykDbifX8UytQprM3to8L9BCWANoxDZKPYNbsH3",
      "metadata": "{\"name\":\"domo\",\"description\":\"kun\",\"image\":\"https://ipfs.moralis.io:2053/ipfs/QmPesx2hv4A3Qqfoo4qP9zgXDKu9qofKCwWEN3LTHukVi1\"}",
      "last_token_uri_sync": "2022-09-23T02:57:01.324Z",
      "last_metadata_sync": "2022-09-23T02:57:13.796Z"
    }
  ]
}

Yes. But not listed on OpenSea.
https://testnets-api.opensea.io/api/v1/asset/0xaa8195070669bcc9dd61a0cf38c093998772e995/1/?force_update=true

So, my problem is metadataURI is not caught with params: {tokenURI: metadataURI,}
What am I missing here?

Current code:

const metadataURI = metadataFile.ipfs();
console.log("metadataURI:", metadataURI);

const options = {
chain: "rinkeby",
contractAddress: nft_contract_address,
functionName: "mint",
abi: ABI,
params: {
tokenURI: metadataURI,
},
msgValue: Moralis.Units.ETH("0.01")
};

const transaction = await Moralis.executeFunction(options);
const result = await transaction.wait();

can you find that contract on opensea page?

No I cannot.
I have minted 4 nft, but none are listed on OpenSea.

I can see the mint is success on etherScan.

maybe it is an issue related to opensea, rinkeby in particular will be discontinued soon, it is already discontinued with the transition to prof of stake

ah…wtf.
Is there any way I can check the metatada is successfully recorded on chain?

you can check it directly on chain too, you can get the token uri for a specific token id

usually the metadata is not added on chain, only the token uri

1 Like