I have a smart-contract that inherits from ERC721
, as follows:
contract myNFTsMinter is ERC721, Pausable, Ownable, ReentrancyGuard {
...
}
In my javascript file (I'm using Hardhat
+ ethers.js
) I instantiate my contract and then successfully call various functions from it - including functions it inherits from the ERC721
contract, such as balanceOf()
.
Here's how I do it:
await myNFTsMinterContractInstance.balanceOf("0x12345...").then((resultNFTsOwned) => {
...
}
This works great - HOWEVER, when I call the safeTransferFrom()
function, which is also from the ERC721
contract that my contract is inheriting from, I suddenly get the following error:
Uncaught (in promise) TypeError: myNFTsMinterContractInstance.safeTransferFrom is not a function
Why is that? What's happening here? How come it's recognizing one ERC721 function (balanceOf()
) but not another (safeTransferFrom()
)?
I've found that the only work-around is to create an additional function in my own contract (meaning in the myNFTsMinter
contract), which in turn calls the ERC721
contract's safeTransferFrom()
, and only then I can leverage the functionality I need from within my JS file.
But obviously adding this function to my contract makes it fatter and more costly to deploy - and obviously this doesn't provide me with the insight and understanding of what's happening here in terms of the inheritance anomaly.
Any thoughts?
=================================
UPDATED QUESTION:
Responding to the answer given by Mila A:
Regarding the very first point you made in your answer, about how I initialize my ERC721 contract in my constructor, well I have NOT been doing it the way you’ve suggested. The way I’ve been doing it is like this:
constructor() ERC721(“myERC721TokenContract”, “MYTK”) {
…
}
That very different from how you're saying to do it.
And really, the whole thing starts in my deployment script, which looks like this:
const myNFTsMinter = await ethers.getContractFactory("myNFTsMinter");
const myNFTsMinterInstance = await myNFTsMinter.deploy();
await myNFTsMinterInstance.deployed();
console.log("\n\n>>FINISHED 'myNFTsMinterInstance' deployment!\n\n-'myNFTsMinterInstance' deployed to:", myNFTsMinterInstance.address);
So this deployment script is deploying my "myNFTsMinter" contract, and then the constructor of my "myNFTsMinter" contract deploys the ERC721 Contract on the fly the way it's shown in the code I just included above.
Is the way I'm doing it wrong?
To be clear: I don't have an already deployed ERC721 Contract sitting out there that I'm trying to create a reference to from my new contract. I'm trying to deploy both at the same time.
Should I not be doing that? What's the right way to do this?