nft-tutorial icon indicating copy to clipboard operation
nft-tutorial copied to clipboard

part 4 / mint operation cost is missing in the code

Open fixitornever opened this issue 3 years ago • 3 comments

During part 4 of the tutorial we add an operation cost to the mintTo() operation of the smart contract. However passing the cost to the method from the hardhat task is missing. As said in a PR comments you need to add something like value: ethers.utils.parseEther("0.08") to pass along the value with the transaction

Full code is:

task("mint", "*** Mints from the NFT contract")
.addParam("address", "The address to receive a token")
.setAction(async function (taskArguments, hre)
{
    const contract = await getContract("NFT", hre);
    const transactionResponse = await contract.mintTo(taskArguments.address,
    {
        gasLimit: 500_000,
        value: ethers.utils.parseEther("0.08") // Cost of the operation is 0.08 ETH
    });
    console.log(`Transaction Hash: ${transactionResponse.hash}`);
});

fixitornever avatar Feb 11 '22 10:02 fixitornever

Thanks! This is exactly the problem I was having. I'm getting the "Transaction value did not equal the mint price" error when I run npx hardhat mint --address {wallet_address}. What's the syntax for adding the MINT_PRICE variable to msg into that request or does it need to be always hard coded as shown above?

cronoklee avatar Apr 09 '22 15:04 cronoklee

I needed this too! Thank you!

rangerscience avatar Apr 20 '22 00:04 rangerscience

I followed this tutorial too, but I also have a frontend app that was calling the mint function. After adding this line in the contract require(msg.value == MINT_PRICE, "Transaction value did not equal the mint price"); the ui call to the mint function is no longer working. This is what I have:

function mintTo(address recipient) public payable returns (uint256) {
    uint256 tokenId = currentTokenId.current();
    require(tokenId < TOTAL_SUPPLY, "Max supply reached");
    require(msg.value == MINT_PRICE, "Transaction value did not equal the mint price");

    currentTokenId.increment();
    uint256 newItemId = currentTokenId.current();
    _safeMint(recipient, newItemId);
    return newItemId;
  }

UI code:

const provider = new ethers.providers.Web3Provider(ethereum)
  const signer = provider.getSigner()
  const nftContract = new ethers.Contract(
    nftContractAddress,
    NFT.abi,
    signer
)

let nftTx = await nftContract.mintTo(currentAccount)

I tried adding a second argument to mintTo but it fails `Error minting character Error: non-payable method cannot override value

  let nftTx = await nftContract.mintTo(currentAccount, {
      value: ethers.utils.parseEther("0.004")
    })

jaca420 avatar Jun 27 '22 06:06 jaca420