I created an ERC721 token using openzeppelin like this:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract Item is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
address contractAddress;
constructor(address marketAddress) ERC721("Item", "ITM") {
contractAddress = marketAddress;
}
function createToken(string memory _URI)
public
returns(uint256)
{
_tokenIds.increment();
uint256 itemId = _tokenIds.current();
_mint(msg.sender, itemId);
_setTokenURI(itemId, _URI);
setApprovalForAll(contractAddress, true);
return itemId;
}
}
In this contract, I have a function createToken
for minting tokens. I use hardhat for tests and I got this error: TypeError: nft.createToken(...) is not a function
/* deploy the NFT contract */
const Item = await ethers.getContractFactory("Item")
const nft = await Item.deploy(marketAddress)
await nft.deployed()
/* create two tokens */
await nft.createToken("https://www.mytokenlocation.com")
await nft.createToken("https://www.mytokenlocation2.com")
What do I miss?
Deploying a contract and interacting with the contract are two different things.
After you deployed the contract on a blockchain, then you need a provider which is kinda a bridge to a node in that blockchain.
import { ethers } from "ethers";
const provider = new ethers.providers.JsonRpcProvider();
You also need contract address and abi of that contract. abi
is kinda instruction.
const yourContract = new ethers.Contract(nftAddress, NFT.abi, provider);
Now you can call the methods of the contract. You should write the deploy script and set the state for the address if you want to do this on front end. (usually deploying script is wrtiten on the scripts directory of the hardhat)
const [nftAddress, setNftAddress] = useState("")
async function deployContract(){
const Item = await ethers.getContractFactory("Item")
const nft = await Item.deploy(marketAddress)
await nft.deployed()
setNftAddress(nft.address)
}