In this tutorial, we want to mint an NFT by calling function mint from contract. In order to mint, we need to know the price before running the mint function. to do this go to the contract address in etherscan and see what are the available write functions in a certain contract.
Since we know that this contract uses mintPrice as their basic pricing, we'll use mintPrice to calculate quantity x mintPrice.
To get mint functions detail, we need to read the code in the contract.
However, if the contract is a proxy, we have to go to the main contract to read the code
Find the function name by using ctrl+F
Copy and paste the mint function and put it in your daiAbi
Rinkeby balance
Please make sure that you have rinkeby eth to do transaction
AppBarMint
import { ethers } from 'ethers';
import React, { useEffect, useState } from 'react'
function AppBarMint() {
const handleMint = async () => {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
const address = await provider.getSigner().getAddress()
const daiAddress = "xxxx"; // contract address
const daiAbi = [
"function mintPrice(uint256) public view", // get the price first. This could be price/mintPrice or whatever the developer wants. Please read the contract first
"function mint(uint256) external payable" //cek in etherscan contract code
]
const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);
try {
const mintContract = new ethers.Contract(daiAddress, daiAbi, signer);
const cost = Number(await mintContract.mintPrice()) // get mintPrice
const freeMint = await daiContract.getFreeMint(address)
let reciept;
const costQty = String(cost * total)
const parse = ethers.utils.formatEther(costQty)
const options = { value: ethers.utils.parseEther(parse) }
reciept = await mintContract.mint(total, options);
let recieptor = await reciept.wait();
if (recieptor) {
console.log(rescieptor)
}
} catch (error) {
console.log(error)
}
}
return (
<Box>
<Button onClick={() => handleMint()}>Mint Now !</Button>
</Box>
)
}
export default AppBarMint