Search code examples
smartcontractsnfthardhatopenzeppelinopensea

Error during minting with code from Opensea guide


I'm trying to deploy my first SmartContract following the Opensea guide. Everything was working fine until I set a price for my tokens and added the payable keyword. Now when I try to mint, I get the error Transaction value did not equal the mint price. Looking at the code I'm thinking I need to send ETH in the mint request in msg.value somehow but I'm not sure what the syntax would be for that?

Here's how I'm minting in shell:

npx hardhat mint --address {wallet_address}

Here's the mint function in JS:

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,
});
console.log(`Transaction Hash: ${transactionResponse.hash}`);
});

And the mintTo function in the .sol contract:

 // Main minting function
 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;
}

Solution

  • I figured out a solution for this issue. You need to set the price inside the mint task in mint.js like so:

    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.01")
        });
        console.log(`Transaction Hash: ${transactionResponse.hash}`);
    });
    

    I have not figured out a way to have this import the MINT_PRICE variable from the contract. Note: You may need to add const { ethers } = require("ethers"); at the top of the file.