[SOLVED] Event is not logging in Moralis database on devchain

While following patrick course, I was trying to save events in Moralis database. but for some reason its not working. I am using hardhat and followed all the guide. Below is the code that create Tables.

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; // Moralis consider localchain at 1337.

/* Moralis init code */
const serverUrl = process.env.NEXT_PUBLIC_MORALIS_SERVER_URL;
const appId = process.env.NEXT_PUBLIC_MORALIS_APP_ID;
const masterKey = process.env.moralisMasterKey;

async function main() {
    await Moralis.start({ serverUrl, appId, masterKey });
    const contractAddress = contractAddresses[chainId]["NftMarketplace"][0];
    console.log(`Working with contract address: ${contractAddress}`);

    //events logging
    let itemListedOptions = {
        chainId: moralisChainId,
        address: contractAddress,
        sync_historical: true,
        topic: "ItemListed(address,address,uint256,uint256)",
        abi: {
            anonymous: false,
            inputs: [
                {
                    indexed: true,
                    internalType: "address",
                    name: "seller",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "address",
                    name: "marketplaceAddress",
                    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: "buyer",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "address",
                    name: "marketplaceAddress",
                    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 listingCancelledOptions = {
        chainId: moralisChainId,
        address: contractAddress,
        sync_historical: true,
        topic: "ListingCancelled(address,uint256,address)",
        abi: {
            anonymous: false,
            inputs: [
                {
                    indexed: true,
                    internalType: "address",
                    name: "marketplaceAddress",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "uint256",
                    name: "tokenId",
                    type: "uint256",
                },
                {
                    indexed: false,
                    internalType: "address",
                    name: "seller",
                    type: "address",
                },
            ],
            name: "ListingCancelled",
            type: "event",
        },
        tableName: "ListingCacelled",
    };

    let itemUpdatedOptions = {
        chainId: moralisChainId,
        address: contractAddress,
        sync_historical: true,
        topic: "ItemUpdated(address,address,uint256,uint256)",
        abi: {
            anonymous: false,
            inputs: [
                {
                    indexed: true,
                    internalType: "address",
                    name: "seller",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "address",
                    name: "marketplaceAddress",
                    type: "address",
                },
                {
                    indexed: true,
                    internalType: "uint256",
                    name: "tokenId",
                    type: "uint256",
                },
                {
                    indexed: false,
                    internalType: "uint256",
                    name: "newPrice",
                    type: "uint256",
                },
            ],
            name: "ItemUpdated",
            type: "event",
        },
        tableName: "ItemUpdated",
    };

    const listedRespo = await Moralis.Cloud.run("watchContractEvent", itemListedOptions, {
        useMasterKey: true,
    });
    const boughtRespo = await Moralis.Cloud.run("watchContractEvent", itemBoughtOptions, {
        useMasterKey: true,
    });
    const cancelledRespo = await Moralis.Cloud.run(
        "watchContractEvent",
        listingCancelledOptions,
        { useMasterKey: true }
    );
    const updateRespo = await Moralis.Cloud.run("watchContractEvent", itemUpdatedOptions, {
        useMasterKey: true,
    });

    if (
        listedRespo.success &&
        boughtRespo.success &&
        cancelledRespo.success &&
        updateRespo.success
    ) {
        console.log(
            "Updated! You should now be able to see these tables in your database. \n Note: You won't be able to see the events on the `sync` tab of the UI though."
        );
    } else {
        console.log(
            "Something went wrong uploading events... Try manually importing for a better error code. "
        );
    }
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

Upon executing this code, I can see the all the classes.
Screenshot

Also localchain is running fine

moralis-admin-cli connect-local-devchain --chain hardhat --moralisSubdomain 2lz5d2lgl7uf.usemoralis.com --frpcPath ./frp/frpc

And my local hardhat server is also running fine at http://127.0.0.1/8545/
On running a script that fire the events, I can not see the data populated in ItemListed class.

I have reset the server several times with no luck.

what is the server url that you use?

4l7ifivmqekk.usemoralis.com
I deleted server and installed new one.

did you connect the frpc proxy to this server?

Yes, its connected and I can see green connected button on my dashboard

in particular it didn’t look like it was connected at that time, anyway, it looks like we have to fix an issue specific to hardhat

So we need to wait? Or is there any temporary solution for time being?

a temporary solution could be to use ganache or to use a testnet

Thanks. I am in middle of the course, not sure how to change to ganache, will look around it.

it should work now with hardhat if you update the server

I tried with same setup, but it didn’t work. Do I need to reset anything? I mean I have restarted the proxy server, hardhat local server and run the script that fire event. But still I can not see events getting log in table.

you need to update the server, after that you can also try to click on reset local devchain

what is the server url that you use now?

http://4l7ifivmqekk.usemoralis.com/ Using this one

I am going to delete the current server and create a new one and will test then

2 Likes

make sure to update the server after you create it

Wow… great its working. Thanks for helping.
So for anyone who is struggling, you should start with a fresh server. It has issue which has been resolved by the @cryptokid for hardhat

2 Likes

I’m having a similar problem, I tried resetting my local dev chain from the UI and its not responding,

If you try with a new server then it works?

Did you also try to reset the local dev chain programmatically or from the legacy UI?

Hi. I’m facing the same issue. All is connected but the problem is the event is not logging in to the Moralis database. I tried deleting and creating new servers many times but it doesn’t work. My server URL is https://q7v0uk9l7iqg.usemoralis.com. What else can I try to solve?