I'm working on an app that allows the user to create NFTs and list them on a market place. When I try to create the token on the UI using metamask, a createToken
function is being called. The resolved promise of createToken
is an object for which I expect an events key with 2 events (I'm able to confirm this by running npx hardhat test
. However I don't actually see these events emitted to the UI... I need these events to get the tokenId
. If someone knows an alternative way to get the tokenId
I'm open to that as well.
createToken
:
contract NFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
address contractAddress;
constructor(address marketplaceAddress) ERC721("Metaverse", "METT") {
contractAddress = marketplaceAddress;
}
function createToken(string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
// Not emitting this event to the UI but it works in the test
setApprovalForAll(contractAddress, true);
return newItemId;
}
}
The function on the UI looks like below where createSale
creates a listing on the marketplace:
async function createSale(url) {
const web3Modal = new Web3Modal();
const connection = await web3Modal.connect();
const provider = new ethers.providers.Web3Provider(connection);
const signer = provider.getSigner();
/* next, create the item */
let contract = new ethers.Contract(nftAddress, NFT.abi, signer);
let transaction = await contract.createToken(url);
let tx = await transaction.wait();
// Seeing a successful transaction
console.log("tx ==> ", tx);
let event = tx.events[0];
// Breaks here sine event[0] is `undefined`
let value = event.args[2];
let tokenId = value.toNumber();
const price = ethers.utils.parseUnits(formInput.price, "ether");
/* then list the item for sale on the marketplace */
contract = new ethers.Contract(nftMarketAddress, Market.abi, signer);
let listingPrice = await contract.getListingPrice();
listingPrice = listingPrice.toString();
transaction = await contract.createMarketItem(nftAddress, tokenId, price, {
value: listingPrice,
});
await transaction.wait();
router.push("/");
}
Below is a screenshot of the resolved promise with the empty events array: