Thank you.
Some more questions related to self hosting:
Following the Patrick Collins FCC course on NFT market place, we have the addEvents.js file (see code below) in the NextJS front end to listen to events using Moralis:
const Moralis = require("moralis-v1/node")
require("dotenv").config()
const contractAddresses = require("./constants/networkMapping.json")
let chainId = process.env.chainId || 31337
let moralisChainId = chainId == "31337" ? "1337" : chainId
const contractAddress = contractAddresses[chainId]["NftMarketplace"][0]
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 contract address ${contractAddress}`)
let ItemListedOptions = {
// Mortalis understands a local chain is 1337
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: "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,
topic: "ItemBought(address, address, uint256, uint256)",
sync_historical: true,
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",
}
let ItemCanceledOptions = {
chainId: moralisChainId,
address: contractAddress,
topic: "ItemCanceled(address, address, uint256)",
sync_historical: true,
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",
},
],
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 event")
} else {
console.log("Something went wrong...")
}
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
Now that we are self hosting, are we still allowed to use these functions in the front end:
- Moralis.start({ serverUrl, appId, masterKey })
- Moralis.Cloud.run("watchContractEvent, ⌠, {