[SOLVED]Minting token in 25_NFT_MARKET_PLACE logic.js

Using the source from: https://github.com/DanielMoralisSamples/25_NFT_MARKET_PLACE

I’m trying to call the mintToken Function like so:

import { fs } from "fs";

async function mintTokens() {
    const contractAddress = "0x570D251af3cc07bE28d930FaB0F31cD338EaC8f3";
    var jsonFile = "build/contracts/MyNFT.json";
    var parsed = JSON.parse(fs.readFileSync(jsonFile));
    var abi = parsed.abi;
    const MyNFTContract = new web3.eth.Contract(abi, contractAddress);

    MyNFTContract.methods.mintToken({
        "image" : "static/Images/4.Hobbit.jpg",
        "name" : "Hobbit",
        "description" : "A Hobbit"
    }).send();
}

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

I’m having trouble importing fs to format the file location for JSON.parse(**fs.readFileSync**(jsonFile));

I know there is a moralis function that can call functions in smart contract, I was trying to use it earlier today and was having problems too, If there is a complete work around I’m happy. I just want to send a smart contract function (MyNFT.mintToken) from within logic.js

If all you need is the abi, you can hardcode it in your script, you need only the abi specific to the function that you call.

I assume that this is front end, and you don’t have access to fs there maybe.

Do you need the full abi? MyNFT.json (the abi) has 17008 lines in it. That’s what
stopped me from wanting to do that.

I’ve tried amending the mintTokens function like so

    var abi = ({
        "inputs": [
          {
            "internalType": "string",
            "name": "tokenURI",
            "type": "string"
          }
        ],
        "name": "mintToken",
        "outputs": [
          {
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
          }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
      });
    const MyNFTContract = new web3.eth.Contract(JSON.parse(abi).abi, contractAddress);
    MyNFTContract.methods.mintToken({
        "image" : "static/Images/4.Hobbit.jpg",
        "name" : "Hobbit",
        "description" : "A Hobbit"
    }).send();
}

I’m getting this error

Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1

you don’t need all those lines to call that specific contract function, only the line that contains that function name. But you can also load all those lines in a separate js file, by writing something like windows.abi = […] in a file named abi.js that you load similar to 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?