'Not getting event from smart contract on ethers.js
I have contract this part is simply creating smart contract for NFT. At the end of the createToken() I am emitting event.
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.3;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract NFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
address contractAddress;
event ItemCreated(
uint256 indexed tokenId
);
constructor(address marketplaceAddress) ERC721("Metaverse Tokens", "METT") {
contractAddress = marketplaceAddress;
}
function createToken(string memory tokenURI) public returns (uint) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
setApprovalForAll(contractAddress, true);
emit ItemCreated(newItemId);
}
}
I am trying to read event ItemCreated on ethers.js
const web3Model = new Web3Modal()
const connection = await web3Model.connect()
const provider = new ethers.providers.Web3Provider(connection)
const signer = provider.getSigner()
let contract = new ethers.Contract(nftaddress, NFT.abi, signer)
let transaction = await contract.createToken(url)
let tx = await transaction.wait()
console.log('tx' ,tx)
let event = tx.events[0]
console.log('event', event)
Unfortunately, the response is not giving me any event Please see below response. evens property is empty
{
"to": "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
"from": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"contractAddress": null,
"transactionIndex": 0,
"gasUsed": {
"type": "BigNumber",
"hex": "0x5858"
},
"logsBloom": "0x0000000000000...",
"blockHash": "0xe66248c22832b34776c6720a6bec19bf20e9ba9af4a63ff25bd0f0689f19f037",
"transactionHash": "0xc6de2da6186ca972cc69efa265c7da5e6d780a0c25d4ad849aa4be4be9cac501",
"logs": [],
"blockNumber": 1,
"confirmations": 1,
"cumulativeGasUsed": {
"type": "BigNumber",
"hex": "0x5858"
},
"effectiveGasPrice": {
"type": "BigNumber",
"hex": "0x8d8f9fc0"
},
"status": 1,
"type": 2,
"byzantium": true,
"events": []
}
events are empty I am sure event is emitted as I checked in hardhat-tracker
npx hardhat test --logs
NFT market
[Receiver] Transfer(from=0x0000000000000000000000000000000000000000, to=[Sender], tokenId=1)
[Receiver] ApprovalForAll(owner=[Sender], operator=0x5FbDB2315678afecb367f032d93F642f64180aa3, approved=true)
[Receiver] ItemCreated(tokenId=1)
[Receiver] Transfer(from=0x0000000000000000000000000000000000000000, to=[Sender], tokenId=2)
[Receiver] ApprovalForAll(owner=[Sender], operator=0x5FbDB2315678afecb367f032d93F642f64180aa3, approved=true)
[Receiver] ItemCreated(tokenId=2)
Any idea what I am dong wrong? Below link to the repo https://github.com/Filip-Konkowski/Polygon-nft-market
Solution 1:[1]
Are you sure you have selected the correct network in your metamask? The same thing happened to me once :D Also, be sure to deploy using the network parameter such as npx hardhat run scripts/deploy.js --network localhost
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ege |