[SOLVED] Running addEvents.js returns false

Hi!
I’m following/adapting Patrick Collin’s tutorial to create a NFT Marketplace using Moralis for the server.

I’ve successfully connected my dApp to the server, Server Status: Connected is displayed in the Devchain Proxy Server, and I can see the eth_blockNumber calls progressing.

Yet, when I run node addEventsMoralis.js, no event listeners are set up in the database, and no specific reason is given

Here is my addEventsMoralis.js :

const Moralis = require("moralis-v1/node")
require("dotenv").config()

const contractAddresses = require("./constants/networkMappings.json")
let chainId = process.env.chainId || "31337"
//moralis understands that localchainid is 1337, so there's a need to convert it
let moralisChainId = chainId == "31337?" ? "1337" : chainId
const contractAddressesArray = contractAddresses[chainId]["MarsKetplace"]
const contractAddress = contractAddressesArray[contractAddressesArray.length - 1]

const serverUrl = process.env.NEXT_PUBLIC_SERVERURL
const appId = process.env.NEXT_PUBLIC_APPID
const masterKey = process.env.MORALIS_MASTERKEY

async function main() {
  await Moralis.start({ serverUrl, appId, masterKey })
  console.log(`Working with ${contractAddress}`)

  let NFTListedOptions = {
    chainId: moralisChainId,
    address: contractAddress,
    sync_historical: true,
    topic: "NFTListed(address,address,uint256,uint256)",
    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: "NFTListed",
      type: "event",
    },
    tableName: "NFTListed",
  }

  let NFTBoughtOptions = {
    chainId: moralisChainId,
    address: contractAddress,
    sync_historical: true,
    topic: "NFTBought(address,address,uint256,uint256)",
    abi: {
      anonymous: false,
      inputs: [
        {
          indexed: true,
          internalType: "address",
          name: "buyer",
          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: "NFTBought",
      type: "event",
    },
    tableName: "NFTBought",
  }

  let NFTDeletedOptions = {
    chainId: moralisChainId,
    address: contractAddress,
    sync_historical: true,
    topic: "NFTDeleted(address,uint256)",
    abi: {
      anonymous: false,
      inputs: [
        {
          indexed: true,
          internalType: "address",
          name: "nftAddress",
          type: "address",
        },
        {
          indexed: true,
          internalType: "uint256",
          name: "tokenId",
          type: "uint256",
        },
      ],
      name: "NFTDeleted",
      type: "event",
    },
    tableName: "NFTDeleted",
  }
  const listedResponse = await Moralis.Cloud.run("watchContractEvent", NFTListedOptions, {
    useMasterKey: true,
  })
  console.log(listedResponse)
  const boughtResponse = await Moralis.Cloud.run("watchContractEvent", NFTBoughtOptions, {
    useMasterKey: true,
  })
  console.log(boughtResponse)
  const deletedResponse = await Moralis.Cloud.run("watchContractEvent", NFTDeletedOptions, {
    useMasterKey: true,
  })
  console.log(deletedResponse)

  console.log("Working on it...")
  if (listedResponse.success && boughtResponse.success && deletedResponse.success) {
    console.log("Database successfully updated with watching events!")
  } else {
    console.log("Something went wrong... and I won't show you what.")
  }
}

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

and here is the result of running node addEventsMoralis.js :

node addEventsMoralis.js
Working with 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
{ success: false }
{ success: false }
{ success: false }
Working on it...
Something went wrong... and I won't show you what.

The logs on the DataBase :

Ran cloud function watchContractEvent for user undefined with:
  Input: {"chainId":"31337","address":"0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512","sync_historical":true,"topic":"NFTDeleted(address,uint256)","abi":{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"nftAddress","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"NFTDeleted","type":"event"},"tableName":"NFTDeleted"}
  Result: {"success":false}

Things that I’ve tried :

  • verified that the abi for each event were correct, they were.
  • verified that the master key in my .env was correct, it was.
  • Reset Local Devchain hit in Networks on the admin dashboad, didn’t change.
  • Tried with another instance of the contract, didn’t change.

The entire repo is here if you need : https://github.com/AnneCh/FrontEndMarsKetplace

Thanks in advance!

is the server running, was any event added in that server based on the tables that are created on the server?

what is the latest block number that you see

what is the server url?

the server is running indeed, but no tables are created on the database, that’s indeed the problem.

The serverURL is : https://4byy6futg87w.usemoralis.com:2053/server

6480 was the latest block number

That is a huge block number.
You can check the hardhat settings if there is a fork for a public RPC url.

You should have less than 100 that block number.

You can also try to add the event sync from the admin interface.

I’ve let the server run for a while in case it was a problem of sync, hence the huge number (or so I thought)

I killed it now and restarted it, and it goes pretty fast indeed, already 240.

My last resort was to do it through the interface as you suggested, I’ll try it now, as well as checking the hardhar settings.

you will have to reset the local devchain, or create a new server, after you fix the settings for hardhat

1 Like

That’s my hardhat-config :

    hardhat: {
      // // If you want to do some forking, uncomment this
      // forking: {
      //   url: MAINNET_RPC_URL
      // }
      chainId: 31337,
    },
    localhost: {
      chainId: 31337,
    },
    rinkeby: {
      url: RINKEBY_RPC_URL,
      accounts: PRIVATE_KEY !== undefined ? [PRIVATE_KEY] : [],
      saveDeployments: true,
      chainId: 4,
      blockConfirmations: 8,
    },
    // mainnet: {
    //   url: MAINNET_RPC_URL,
    //   accounts: PRIVATE_KEY !== undefined ? [PRIVATE_KEY] : [],
    //   saveDeployments: true,
    //   chainId: 1,
    // },

how does it have so many block numbers then?

I have no idea, I’m pretty new to this…

could this have to do with the fact it it says subdomain not found ? :

/FrontEndMarsKetplace$ yarn moralis:sync
yarn run v1.22.15
$ moralis-admin-cli connect-local-devchain --chain hardhat --moralisSubdomain lkei3yi1kglb.usemoralis.com --frpcPath ./frp/frpc
Subdomain not found!
Following servers were found:
(0) MarsKetplace
What server do you want to connect to?: 0
Starting connection to Hardhat

I don’t know, I didn’t see that before

I’ve noticed that I made a mistake, from me re-writting the command to sync to the server, it does not match the frpc.ini.

I changed it, now I do not have the error message about the subdomain missing, but the command node addEventsMoralis.js still fails

are you still having a lot of block numbers synced?

yes, still running wild :confused: I’ve been googling the problem but so far, no luck

is there anything else in that hardhat config?
maybe it is not running with latest config version?

I have this in the hardhat.config.js :

require("@nomiclabs/hardhat-waffle")
require("@nomiclabs/hardhat-etherscan")
require("hardhat-deploy")
require("solidity-coverage")
require("hardhat-gas-reporter")
require("hardhat-contract-sizer")
require("dotenv").config()

/**
 * @type import('hardhat/config').HardhatUserConfig
 */

const RINKEBY_RPC_URL = process.env.RINKEBY_RPC_URL

const PRIVATE_KEY = process.env.PRIVATE_KEY

const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY
const REPORT_GAS = process.env.REPORT_GAS || false

module.exports = {
  defaultNetwork: "hardhat",
  networks: {
    hardhat: {
      chainId: 31337,
    },
    localhost: {
      chainId: 31337,
    },
    rinkeby: {
      url: RINKEBY_RPC_URL,
      accounts: PRIVATE_KEY !== undefined ? [PRIVATE_KEY] : [],
      saveDeployments: true,
      chainId: 4,
      blockConfirmations: 8,
    },
  },
  etherscan: {
    // npx hardhat verify --network <NETWORK> <CONTRACT_ADDRESS> <CONSTRUCTOR_PARAMETERS>
    apiKey: {
      rinkeby: ETHERSCAN_API_KEY,
    },
  },
  gasReporter: {
    enabled: REPORT_GAS,
    currency: "USD",
    outputFile: "gas-report.txt",
    noColors: true,
    // coinmarketcap: process.env.COINMARKETCAP_API_KEY,
  },
  contractSizer: {
    runOnCompile: false,
    only: ["Raffle"],
  },
  namedAccounts: {
    deployer: {
      default: 0, 
      1: 0,
    },
    player: {
      default: 1,
    },
  },
  solidity: {
    compilers: [
      {
        version: "0.8.7",
      },
      {
        version: "0.4.24",
      },
    ],
  },
  mocha: {
    timeout: 200000, // 200 seconds max for running tests
  },
}

and this in a file called helper-hardhat-config.js:

const networkConfig = {
  1337: {
    name: 'localhost',
    gasLane:
      '0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc', // 30 gwei
    mintFee: '1000000000000000000', // 1 ETH
    callbackGasLimit: '500000', // 500,000 gas
    subscriptionId: 'xxxx', // our own ID
  },

  4: {
    name: 'rinkeby',
    vrfCoordinatorV2: '0x6168499c0cFfCaCD319c818142124B7A15E857ab',
    gasLane:
      '0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc',
    callbackGasLimit: '500000', // 500,000 gas
    mintFee: '100000000000000000', // 0.1 ETH
    subscriptionId: 'xxxx', // our own ID
  },
}

const developmentChains = ['hardhat', 'localhost']

module.exports = {
  networkConfig,
  developmentChains,
}

then where from could it have so many block numbers?

I really don’t understand either. Would it help if I copy past my contract here? maybe there’s something wrong with it ? (tests are passing successfully with more than 90% coverage)

I don’t think that it help to paste the contract code, that is only a contract

how do you know how many block numbers are?