Hi @cryptokid
Hope you will be fine
I am facing the same issue, tried creating server many times, reseting existing chaing, configuring frpc, but all in vain.
Here is my addEvents.js file
const Moralis = require("moralis/node");
require("dotenv").config();
const contractAddresses = require("./constants/networkMapping.json");
let chainId = process.env.chainId || 31337;
let moralisChainId = chainId == "31337" ? "1337" : chainId;
const contractAddressArray = contractAddresses[chainId]["NftMarketplace"];
const contractAddress = contractAddressArray[contractAddressArray.length - 1];
const serverUrl = process.env.NEXT_PUBLIC_SERVER_URL;
const appId = process.env.NEXT_PUBLIC_APP_ID;
const masterKey = process.env.masterKey;
async function main() {
await Moralis.start({ serverUrl, appId, masterKey });
console.log(`Working with contrat address ${contractAddress}`);
let itemListedOptions = {
// Moralis understands a local chain is 1337
chainId: moralisChainId,
sync_historical: true,
topic: "ItemListed(address,address,uint256,uint256)",
address: contractAddress,
abi: {
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "seller",
type: "address",
},
{
indexed: true,
internalType: "address",
name: "nftAddress",
type: "address",
},
{
indexed: true,
internalType: "uint256",
name: "tokenId",
type: "uint256",
},
{
indexed: false,
internalType: "uint256",
name: "price",
type: "uint256",
},
],
name: "ItemListed",
type: "event",
},
tableName: "ItemListed",
};
let itemBoughtOptions = {
chainId: moralisChainId,
address: contractAddress,
sync_historical: true,
topic: "ItemBought(address,address,uint256,uint256)",
abi: {
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "sender",
type: "address",
},
{
indexed: true,
internalType: "address",
name: "nftAddress",
type: "address",
},
{
indexed: true,
internalType: "uint256",
name: "tokenId",
type: "uint256",
},
{
indexed: false,
internalType: "uint256",
name: "price",
type: "uint256",
},
],
name: "ItemBought",
type: "event",
},
tableName: "ItemBought",
};
let itemCanceledOptions = {
chainId: moralisChainId,
address: contractAddress,
topic: "ItemCanceled(address,address,uint256)",
sync_historical: true,
abi: {
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "sender",
type: "address",
},
{
indexed: true,
internalType: "address",
name: "nftAddress",
type: "address",
},
{
indexed: true,
internalType: "uint256",
name: "tokenId",
type: "uint256",
},
],
name: "ItemCanceled",
type: "event",
},
tableName: "ItemCanceled",
};
const listedResponse = await Moralis.Cloud.run(
"watchContractEvent",
itemListedOptions,
{
useMasterKey: true,
}
);
const boughtResponse = await Moralis.Cloud.run(
"watchContractEvent",
itemBoughtOptions,
{
useMasterKey: true,
}
);
const canceledResponse = await Moralis.Cloud.run(
"watchContractEvent",
itemCanceledOptions,
{
useMasterKey: true,
}
);
if (
listedResponse.success &&
canceledResponse.success &&
boughtResponse.success
) {
console.log("Success! Database Updated with watching events");
} else {
console.log("Something went wrong...");
}
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Here is my events at NftMarketplace.sol file
//events
event ItemListed(
address indexed seller,
address indexed nftAddress,
uint256 indexed tokenId,
uint256 price
);
event ItemBought(
address indexed sender,
address indexed nftAddress,
uint256 indexed tokenId,
uint256 price
);
event ItemCanceled(
address indexed sender,
address indexed nftAddress,
uint256 indexed tokenId
);
Hardhat node is running,
yarn moralis:sync is working and moralis is connected with Hardhat node
This is the error of moralis log
One this i noticed is that
When i run this script with this command
yarn hardhat deploy --network localhost --tags frontend
const {
frontEndContractsFile,
frontEndAbiLocation,
} = require("../helper-hardhat-config");
require("dotenv").config();
const fs = require("fs");
const { network, ethers } = require("hardhat");
module.exports = async () => {
if (process.env.UPDATE_FRONT_END) {
console.log("Writing to front end...");
await updateContractAddresses();
await updateAbi();
console.log("Front end written!");
}
};
async function updateAbi() {
const nftMarketplace = await ethers.getContract("NftMarketplace");
const basicNft = await ethers.getContract("BasicNft");
fs.writeFileSync(
`${frontEndAbiLocation}NftMarketplace.json`,
//do formate the nftMarkeplace contracts interface/abi code into ethers.utils.FormatTypes.json format
nftMarketplace.interface.format(ethers.utils.FormatTypes.json)
);
fs.writeFileSync(
`${frontEndAbiLocation}BasicNft.json`,
basicNft.interface.format(ethers.utils.FormatTypes.json)
);
}
async function updateContractAddresses() {
const chainId = network.config.chainId.toString();
const nftMarketplace = await ethers.getContract("NftMarketplace");
const contractAddresses = JSON.parse(
fs.readFileSync(frontEndContractsFile, "utf8")
);
if (chainId in contractAddresses) {
if (
!contractAddresses[chainId]["NftMarketplace"].includes(
nftMarketplace.address
)
) {
contractAddresses[chainId]["NftMarketplace"].push(nftMarketplace.address);
}
} else {
contractAddresses[chainId] = { NftMarketplace: [nftMarketplace.address] };
}
fs.writeFileSync(frontEndContractsFile, JSON.stringify(contractAddresses));
}
module.exports.tags = ["all", "frontend"];
its give me this error after runing;
Error: ERROR processing /Users/apple/Documents/Projects/blockchain/hardhat-nft-marketplace-backend/deploy/03-update-front-end.js:
TypeError: Cannot read properties of undefined (reading 'toString')
at updateContractAddresses (/Users/apple/Documents/Projects/blockchain/hardhat-nft-marketplace-backend/deploy/03-update-front-end.js:33:42)
at Object.module.exports [as func] (/Users/apple/Documents/Projects/blockchain/hardhat-nft-marketplace-backend/deploy/03-update-front-end.js:12:11)
at DeploymentsManager.executeDeployScripts (/Users/apple/Documents/Projects/blockchain/hardhat-nft-marketplace-backend/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1220:41)
at DeploymentsManager.runDeploy (/Users/apple/Documents/Projects/blockchain/hardhat-nft-marketplace-backend/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1053:16)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
because it not able to find the the chainId in this line network.config.chainId.
But when i ran this command
hh deploy
it works fine and deploy the stuff
then When i mint the nft using mint-and-list.js file , nft is minted and listed
But no event triger at Moralis side and DB is empty
Please help on this
Will be thankful