Mint Price From Contract ABI

Hello,

So in my mint function on my smart conrtact I have a function that returns the cost of the NFT given the _mintAmount. I want to use this function to place into the msg.Value section on the async mint function. But when I do so, nothing happens when I click the mint button, and I check the console and nothing has logged. Has anybody else ran into this problem?

Here is my js code:

async function mint() {
    await Moralis.enableWeb3({ provider: "metamask" });
   	console.log("HelloWorld");
    
    const price = await Moralis.Web3API.native.runContractFunction(checkCost);
    const sendOptions = {
      contractAddress: "0xa1A3012FbB75157A63F032a635F8039ec2376DF9",
      functionName: "checkCost",
      abi: ABI,
      params: {
        _mintAmount: price,
      }
    };
    
    console.log("Helloworld");
    const mintAmount = document.getElementById("mintAmount").value;
    const sendOptions = {
      contractAddress: "0xa1A3012FbB75157A63F032a635F8039ec2376DF9",
      functionName: "mint",
      abi: ABI,
      params: {
        _mintAmount: mintAmount,
      }, msgValue: Moralis.Units.Token(price);,
    };

    await Moralis.executeFunction(sendOptions);
    await transaction.wait();
    console.log(message);
  }

document.getElementById("login").onclick = login;
document.getElementById("mint").onclick = mint;
document.getElementById("mintAmount").value = mintAmount;

Hello can you read this on how to post code.

1 Like
async function mint() {
    await Moralis.enableWeb3({ provider: "metamask" });
   	console.log("HelloWorld");
    
    async function price() {
        return checkCost;
      }
    
    console.log("Helloworld");
    const mintAmount = document.getElementById("mintAmount").value;
    const sendOptions = {
      contractAddress: "0xa1A3012FbB75157A63F032a635F8039ec2376DF9",
      functionName: "mint",
      abi: ABI,
      params: {
        _mintAmount: mintAmount,
      }, msgValue: price(),
    };

    await Moralis.executeFunction(sendOptions);
    await transaction.wait();
    console.log(message);
  }

document.getElementById("login").onclick = login;
document.getElementById("mint").onclick = mint;
document.getElementById("mintAmount").value = mintAmount;

So, I changed the code up a little bit and I think I am getting closer. Now I just need to figure out how to fix the bigNumber error I keep getting

UPDATE 07/16/2022:

@qudusayo has been helping me for a little bit and we got a step closer in my opinion. Now the only issue is the network error. He offered the idea of wrapping price in a function with the constant attatched and place this in my msg.Value.

Here is the code:

async function cost(mintAmount) {
  const options = {
    chain: "rinkeby",
    address: "0xa1A3012FbB75157A63F032a635F8039ec2376DF9",
    function_name: "checkCost",
    abi: ABI,
    params: {
      _mintAmount: mintAmount,
    },
  };
  const price = await Moralis.Web3API.native.runContractFunction(options);
  return price;
}

async function mint() {
  await Moralis.enableWeb3({ provider: "metamask" });
  console.log("HelloWorld");

  const mintAmount = document.getElementById("mintAmount").value;

  const costPrice = Number(await cost(mintAmount));
  console.log(costPrice);

  const sendOptions = {
    contractAddress: "0xa1A3012FbB75157A63F032a635F8039ec2376DF9",
    functionName: "mint",
    abi: ABI,
    params: {
      _mintAmount: mintAmount,
    },
    msgValue: Moralis.Units.ETH(costPrice),
  };

  let transaction = await Moralis.executeFunction(sendOptions);
  await transaction.wait();
  console.log(transaction);
}

And here is the network error:

This might be helpful [SOLVED] Why am I getting this error message: ERR_NAME_NOT_RESOLVED?