I am trying to build a Ethereum NFT minting website where the website users can mint NFTs on blockchain without an external wallet (e.g. Metamask). I asked a question here before and got reply as to how to achieve this. How to use one Ethereum account for all NFT minting transactions for website users
The advice given was that the user can use the hdwallet provider and the Ethers.js library. This way, the minting fees can be paid from a central Ethereum account.
Now, I have coded the application as follows. The minting works and the transaction is recorded on the Rinkerby test network.
import LocationNFT from '../abis/Location.json'
let nftAddress = "0x....";
let privateKey= "0x....";
let provider = ethers.getDefaultProvider('rinkeby');
let wallet = new ethers.Wallet(privateKey, provider);
var mainContract = new ethers.Contract(nftAddress, LocationNFT.abi, wallet);
const num = mainContract.mintNFT(location).send({ from: fromAddress })
.once('receipt', (receipt) =>
{
console.log ('num: ', num);
console.log ('JSON.stringify(num) : ', JSON.stringify(num));
})
Location Contract code: Contains only the following function:
function mintNFT(string memory _location) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
require(!_locationExists[_location]);
uint _id = locations.push(_location);
_mint(msg.sender, _id);
_locationExists[_location] = true;
return newItemId;
}
However, regardless of the successful minting on the Rinkerby test network, I do get the following error which is shown in the console. I would like to resolve this so that I get can get the transaction number ('num' in the above code) which I can show to the user.
Could anyone help me resolve this error?
TypeError: mainContract.mintNFT(...).send is not a function
at App._this.mint (App.js:359)
at onSubmit (App.js:475)
at HTMLUnknownElement.callCallback (react-dom.development.js:147)
at Object.invokeGuardedCallbackDev (react-dom.development.js:196)
at invokeGuardedCallback (react-dom.development.js:250)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:265)
at executeDispatch (react-dom.development.js:571)
at executeDispatchesInOrder (react-dom.development.js:596)
at executeDispatchesAndRelease (react-dom.development.js:695)
at executeDispatchesAndReleaseTopLevel (react-dom.development.js:704)
at forEachAccumulated (react-dom.development.js:676)
at runEventsInBatch (react-dom.development.js:844)
at runExtractedEventsInBatch (react-dom.development.js:852)
at handleTopLevel (react-dom.development.js:5030)
at batchedUpdates$1 (react-dom.development.js:21425)
at batchedUpdates (react-dom.development.js:2247)
at dispatchEvent (react-dom.development.js:5110)
at react-dom.development.js:21482
at Object.unstable_runWithPriority (scheduler.development.js:255)
at interactiveUpdates$1 (react-dom.development.js:21481)
at interactiveUpdates (react-dom.development.js:2268)
at dispatchInteractiveEvent (react-dom.development.js:5086)
await mainContract.mintNFT(location)
. The other problem in your example is that transactions do not return a value, see ethereum.stackexchange.com/questions/765/… for the details.