[SOLVED]Minting token in 25_NFT_MARKET_PLACE logic.js

Check the last post again, I edited it.

I think that you have to use a list for abi. Like abi =[{}], and is no need to do that json conversion

Got it, worked.

When I click the button, I get:

moralis.js:7005 Uncaught (in promise) Error: Web3Api not initialized, run Moralis.start() first
    at Function.<anonymous> (moralis.js:7005)
    at tryCatch (moralis.js:28071)
    at Generator.invoke [as _invoke] (moralis.js:28301)
    at Generator.next (moralis.js:28126)
    at asyncGeneratorStep (moralis.js:27580)
    at _next (moralis.js:27602)
    at moralis.js:27609
    at new Promise (<anonymous>)
    at new Wrapper (moralis.js:32483)
    at Function.<anonymous> (moralis.js:27598)

I tried adding

Moralis.start();

In a bunch of places, at the top and in the function.

Also I have properly filled in at the top.

Moralis.serverURL
Moralis.initialize

You have to use parameters for Moralis.start, you need to call it only once, and the other syntax with initialise is the old syntax before Moralis.start, meaning that you donā€™t have to use that syntax

Okay, created a Morlis.start(params)

async function mintTokens() { 
    const contractAddress = "0x570D251af3cc07bE28d930FaB0F31cD338EaC8f3";

    var abi = ([{
        "inputs": [
          {
            "internalType": "string",
            "name": "tokenURI",
            "type": "string"
          }
        ],
        "name": "mintToken",
        "outputs": [
          {
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
          }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
    }]);
    const options = {
        chain: "mumbai",
        address: contractAddress,
        function_name: "mintToken",
        abi: abi,
        params: {"image": "static/Images/4.Hobbit.jpg",
        "name": "Hobbit",
        "description": "A Hobbit"}
      };
    await Moralis.executeFunction(options);
}
document.getElementById(Mintbtn).onclick = mintTokens();

error:

Uncaught (in promise) Error: Function does not exist in abi

You have a set of extra parenthesis ([{ instead of only [{

Oh, your right.

Took them out.

Error remains.

It looks like it expects a tokenUri as parameter, but I donā€™t know why you get that error with function name now.

As I understand it.

ā€œtokenURIā€

is part of the ABI not a variable that contains the token uri.

From that abi it looks like mint function has only one parameter named tokenURI

How should I format it?

const options = {
        chain: "mumbai",
        address: contractAddress,
        function_name: "mintToken",
        abi: abi,
        params: {"tokenURI": {"image": "static/Images/4.Hobbit.jpg",
        "name": "Hobbit",
        "description": "A Hobbit"
    }}};

(Just a guess)

Iā€™m also getting

Uncaught ReferenceError: mintTokens is not defined
    at HTMLButtonElement.onclick

Iā€™m also getting this

Uncaught (in promise) Error: Function does not exist in abi
    at Function.<anonymous> (moralis.js:6445)
    at tryCatch (moralis.js:28070)
    at Generator.invoke [as _invoke] (moralis.js:28300)
    at Generator.next (moralis.js:28125)
    at asyncGeneratorStep (moralis.js:27579)
    at _next (moralis.js:27601)
    at moralis.js:27608
    at new Promise (<anonymous>)
    at new Wrapper (moralis.js:32482)
    at Function.<anonymous> (moralis.js:27597)

Which Iā€™m not sure is relevant to this function which is all the scope I care about right now.

here you usually save that data in a json file and upload it on your Moralis Server as a static file or on IPFS and use something like

params: {"tokenURI": "https://...../1.json"}

mintTokens is the function that you defined

Thanks,

here you usually save that data in a json file and upload it on your Moralis Server as a static file or on IPFS and use something like

params: {"tokenURI": "https://...../1.json"}

I was purely using local hosting to mint simple nfts for testing not for launching.

I see now I was going about it the wrong way.


Am I getting

Uncaught ReferenceError: mintTokens is not defined
    at HTMLButtonElement.onclick

Because the HTML cannot find my logic script?

I have in logic.js

document.getElementById(Mintbtn).onclick = mintTokens();

To make sure, will that do the trick?

you have to import that logic.js in your html, something like adding in your html a line like:

<script type="text/javascript" src="./logic.js"></script>

Yes.

Although the scripts are under where the button is in the index.html.

Perhaps the logic.js is executed later?

what you mean by this?

Iā€™m just assuming html is executed sequentially each line down the page.

And the onclick=ā€œmintTokens()ā€

Wonā€™t run, because the logic.js script were mintTokens() is, in this case hasnā€™t been imported yet.

Itā€™s okay, itā€™ll take 10 seconds to check for myself.

Okay, update.

There were a couple things wrong.

Like ā€œfunction_nameā€ is supposed to be ā€œfunctionNameā€ which I noticed changed in the docs.

I also set my web3 from eth based to Moralis based.

Now my function looks like this:

MintButton.onclick = async function() {
    
    const abi = [{
        "inputs": [
          {
            "internalType": "string",
            "name": "tokenURI",
            "type": "string"
          }
        ],
        "name": "mintToken",
        "outputs": [
          {
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
          }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
    }];
    const options = {
        chain: "mumbai",
        address: "0x570D251af3cc07bE28d930FaB0F31cD338EaC8f3",
        functionName: "mintToken",
        abi: abi,
        params: { "tokenURI": "build/contracts/URI/1.json" }
    };
    await Moralis.executeFunction(options);
};

When clicked, it returns this error:

Uncaught (in promise) Error: This contract object doesn't have address set yet, please set an address first.
    at Object.ContractNoAddressDefinedError (errors.js:122)
    at Object.b._processExecuteArguments (index.js:703)
    at Object.b._executeMethod (index.js:719)
    at Function.<anonymous> (moralis.js:6513)
    at tryCatch (moralis.js:28070)
    at Generator.invoke [as _invoke] (moralis.js:28300)
    at Generator.next (moralis.js:28125)
    at asyncGeneratorStep (moralis.js:27579)
    at _next (moralis.js:27601)
ContractNoAddressDefinedError @ errors.js:122
b._processExecuteArguments @ index.js:703
b._executeMethod @ index.js:719
(anonymous) @ moralis.js:6513
tryCatch @ moralis.js:28070
invoke @ moralis.js:28300
(anonymous) @ moralis.js:28125
asyncGeneratorStep @ moralis.js:27579
_next @ moralis.js:27601
async function (async)
MintButton.onclick @ logic.js:358

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

can you try with contractAddress instead of address ?

1 Like

Worked like a charm.

Marking as resolved.

Will open another question in a moment.

Anyone reading this in a historical context.

Feel free to use this contract to mint NFTs ERC-721s on Polygon Testnet Mumbai

0x570D251af3cc07bE28d930FaB0F31cD338EaC8f3

If you wish to audit the code:
https://github.com/DanielMoralisSamples/25_NFT_MARKET_PLACE
https://www.youtube.com/watch?v=EMOYpgl5S1w

1 Like