nft-tutorial
nft-tutorial copied to clipboard
part 4 / mint operation cost is missing in the code
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}`);
});
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?
I needed this too! Thank you!
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")
})