I am using the ERC721 OpenZepplin contract, extending ERC721URIStorage so I can use the _setTokenURI function. In the actualy implementation, I created a public function so I can call the internal function:
function setTokenURI(uint256 tokenId, string memory _tokenURI)
public
onlyOwner
{
_setTokenURI(tokenId, _tokenURI);
}
But after deploying the contract still does not recognize the function:
TypeError: nftContract.tokenURI is not a function
Any ideas? Thanks!
Looks like a typo. Your function would be nftContract.setTokenURI
Whoops I didnt mean to change that. Here is my actual code:
//call this file when API pulls in updated uri
//1. find a way to update a json andthen upload it to ipfs.
//2. then await that and call setTokenURI to update the nft instance.
require("dotenv").config()
const API_URL = process.env.API_URL
const { createAlchemyWeb3 } = require("@alch/alchemy-web3")
const web3 = createAlchemyWeb3(API_URL)
const contract = require("../artifacts/contracts/CreditBatchNFT.sol/CreditBatchNFT.json")
const contractAddress = "0xB95C56993070e49aD80EdA231c113bCAC0C7FEa7" //setURI: 0xB95C56993070e49aD80EdA231c113bCAC0C7FEa7
const nftContract = new web3.eth.Contract(contract.abi, contractAddress)
console.log(nftContract)
nftContract.setTokenURI(1, "https://gateway.pinata.cloud/ipfs/QmYueiuRNmL4MiA2GwtVMm6ZagknXnSpQnB3z2gWbz36hP")
It still gives
TypeError: nftContract.setTokenURI is not a function
Here is the contract :
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
contract CreditBatchNFT is ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() public ERC721("CreditBatchNFT", "CreditBatchNFT") {}
function mintNFT(address recipient, string memory tokenURI)
public
onlyOwner
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
function setTokenURI(uint256 tokenId, string memory _tokenURI)
public
onlyOwner
{
_setTokenURI(tokenId, _tokenURI);
}
}
Try nftContract.methods.setTokenURI()