Hi guys i am facing an issue with moralis provider I need to track my events with contract.on()
function how can I set my contract using moralis . HELP ME ASAP
Hi, can you provide more context?
You want an web3 or ethers object?
I want an ethers contract object in this code . I have tried this const contract = new ethers.Contract(promissoryAddress,abi,account)
account I imported from moralis but was giving error
this is My code I have wrapped my app in moralis provider and using web3uikit for connect button in HEader
import Head from "next/head";
import Image from "next/image";
import { Inter } from "next/font/google";
import styles from "@/styles/Home.module.css";
import AddProperty from "@/components/AddProperty";
import ShowProperties from "@/components/ShowProperties";
import { useMoralis, useWeb3Contract } from "react-moralis";
import { useEffect, useState } from "react";
import { ethers } from "ethers";
// Importing contract addresses and ABI
const contractAddresses = require("../constants/contractaddress.json");
const abi = require("../constants/Permissory-abi.json");
const inter = Inter({ subsets: ["latin"] });
export default function Home() {
// Initializing state to hold list of properties
const [properties, setProperties] = useState([]);
// Getting chainId, Web3 status, and account address from Moralis
const { chainId: hexChainId, isWeb3Enabled, account } = useMoralis();
// Getting contract function from Moralis
const { runContractFunction } = useWeb3Contract();
const chainId = parseInt(hexChainId); // Converting hex chainId to decimal
// Getting Permissory contract address based on chainId
const permissoryAddresses =
chainId in contractAddresses ? contractAddresses[chainId][0] : null;
console.log(account);
// Function to get list of properties from contract
async function GetProperty() {
//calling getProperties function on contract with necessary parameters
const property = await runContractFunction({
params: {
abi: abi,
contractAddress: permissoryAddresses,
functionName: "getProperties",
params: {},
},
onSuccess: (tx) => {
// Logging successful transaction
console.log(tx);
},
onError: (error) => console.log(error), // Logging error
});
// Converting returned property string into array of objects
const propertyString = String(property);
const inputPropertyArray = propertyString.split(",");
const resultPropertyArray = [];
for (let i = 0; i < inputPropertyArray.length; i += 8) {
const obj = {
propertyId: inputPropertyArray[i],
owner: inputPropertyArray[i + 1],
tokenName: inputPropertyArray[i + 2],
tokenSymbol: inputPropertyArray[i + 3],
tokenSupply: inputPropertyArray[i + 4],
interestRate: inputPropertyArray[i + 5],
lockingPeriod: inputPropertyArray[i + 6],
status: inputPropertyArray[i + 7],
};
resultPropertyArray.push(obj);
}
console.log(resultPropertyArray);
// setting state with it resultPropertyArray
setProperties(resultPropertyArray);
}
// Calling GetProperty function using useEffect
useEffect(() => {
isWeb3Enabled && GetProperty();
}, [isWeb3Enabled]);
// Returning JSX for index page
return (
<>
<Head>
<title>Create Next App</title>
<meta name="Permissory" content="Promissory" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
{/* Displaying welcome message or Metamask connection prompt based on Web3 status */}
<div className={styles.form_label_owner}>Welcome to Permissory</div>
{isWeb3Enabled ? (
<div>
{/* Rendering ShowProperties component by passing properties as props */}
<ShowProperties properties={properties} />
{/* Rendering AddProperty Component */}
<AddProperty />
</div>
) : (
<div className={styles.form_label_owner}>
Kindly Connect Your Metamask
</div>
)}
</>
);
}
I want to use contract.on() function in this code whenever AddProperty event fires
there should be an web3 object that you could use (in vanilla js the syntax is web3 = await Moralis.enableWeb()
), Iām not familiar with react/next syntax