Hiii,
I need some help to start hearing the events and putting them in the database with Moralis.Cloud.run
, I have tried several things but I keep stuck, this is the error:
useradd@PC-Sebas:~/hh-fcc/nextjs-nft-marketplace-fcc$ node addEvents.js
Working with contract address: 0xb007167714e2940013EC3bb551584130B7497E22...
First got: [object Object]
Second got: [object Object]
Third got [object Object]
TypeError: Cannot read properties of undefined (reading 'run')
at main (/home/useradd/hh-fcc/nextjs-nft-marketplace-fcc/addEvents.js:204:48)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
I found this on the code of moralis-v1
but I can not get it to work, I do not know if that is involved in the issue, beacuse if so, I think more people would have my issue in the course, and it does not seem so…
This is the natspec I found about the function:
/**
* Makes a call to a cloud function.
*
* @function run
* @name Parse.Cloud.run
* @param {string} name The function name.
* @param {object} data The parameters to send to the cloud function.
* @param {object} options
* @returns {Promise} A promise that will be resolved with the result
* of the function.
*/
I already tried Parse.Cloud.run
requiring Parse from moralis-v1/node
but I get that Cloud
is not defined, or run
when requiring Cloud as well.
This is my whole code:
/* import moralis */
const Moralis = require("moralis-v1/node");
require("dotenv").config();
const contractAddresses = require("./constants/networkMapping.json");
let chainId = process.env.chainId || 31337;
/* Moralis init code */
const contractAddress = contractAddresses[chainId]["NftMarketplace"][0];
const SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL;
const APP_ID = process.env.NEXT_PUBLIC_APP_ID;
const moralisChainId = chainId == "31337" ? "1337" : chainId;
const moralisSecret = process.env.NEXT_PUBLIC_MORALIS_SECRET;
const masterKey = process.env.NEXT_PUBLIC_MASTER_KEY;
async function main() {
await Moralis.start({ SERVER_URL, APP_ID, moralisSecret });
console.log(`Working with contract address: ${contractAddress}...`);
let itemListedOptions = {
//Moralis undestands that a local chain is 1337
chainId: moralisChainId,
sync_historical: true,
address: contractAddress,
topic: "ItemList(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: "ItemList",
type: "event",
},
tableName: "ItemListed",
};
console.log(`First got: ${itemListedOptions}`);
let itemBoughtOptions = {
chainId: moralisChainId,
sync_historical: true,
address: contractAddress,
topic: "ItemBought(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: "ItemBought",
type: "event",
},
tableName: "ItemBought",
};
console.log(`Second got: ${itemBoughtOptions}`);
let itemCanceledOptions = {
chainId: moralisChainId,
sync_historical: true,
address: contractAddress,
topic: "ItemCanceled(address, address, uint256)",
abi: {
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "owner",
type: "address",
},
{
indexed: true,
internalType: "address",
name: "tokenAddress",
type: "address",
},
{
indexed: true,
internalType: "uint256",
name: "tokenId",
type: "uint256",
},
],
name: "ItemCanceled",
type: "event",
},
tableName: "ItemCanceled",
};
console.log(`Third got ${itemCanceledOptions}`);
// let priceUpdatedOptions = {
// chainId: moralisChainId,
// sync_historical: true,
// address: contractAddress,
// topic: "PriceUpdated(address, address, uint256, uint256)",
// abi: {
// anonymous: false,
// inputs: [
// {
// indexed: false,
// internalType: "address",
// name: "changer",
// type: "address",
// },
// {
// indexed: false,
// internalType: "address",
// name: "tokenAddress",
// type: "address",
// },
// {
// indexed: false,
// internalType: "uint256",
// name: "tokenId",
// type: "uint256",
// },
// {
// indexed: false,
// internalType: "uint256",
// name: "newPrice",
// type: "uint256",
// },
// ],
// name: "PriceUpdated",
// type: "event",
// },
// tableName: "PriceUpdated",
// };
// let withdrawalOptions = {
// chainId: moralisChainId,
// sync_historical: true,
// address: contractAddress,
// topic: "Withdrawal(address, uint256)",
// abi: {
// anonymous: false,
// inputs: [
// {
// indexed: true,
// internalType: "address",
// name: "owner",
// type: "address",
// },
// {
// indexed: true,
// internalType: "uint256",
// name: "proceeds",
// type: "uint256",
// },
// ],
// name: "Withdrawal",
// type: "event",
// },
// tableName: "Withdrawal",
// };
const listedResponse = await Moralis.Cloud.run("watchContractEvent", itemListedOptions, {
useMasterKey: true,
});
console.log("Running first...");
const boughtResponse = await Moralis.Cloud.run("watchContractEvent", itemBoughtOptions, {
useMasterKey: true,
});
const canceledResponse = await Moralis.Cloud.run("watchContractEvent", itemCanceledOptions, {
useMasterKey: true,
});
if (listedResponse.success && boughtResponse.success && canceledResponse.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);
});
Please trust that the .env
files are correct, I have checked each at least 3 times. And at await Moralis.start({ SERVER_URL, APP_ID, moralisSecret });
I am using the moralisSecret
because it did not work with the masterKey
after checking it was well set on the .env
several times, I think somebody in the community saied that that way Moralis could not work ok, but if that is the case, I need some help to start Moralis with the masterKey
.
Thank you very much again…
Update:
Saw that the start
functions accepted more parameters and just added the masterKey
inside and it runs perfectly and the same error is displayed, so, I think that confirms the issue is not from the start
but from the Cloud
function itself…
Looking like this right now:
await Moralis.start({ SERVER_URL, APP_ID, masterKey, moralisSecret });