Developers Forum for XinFin XDC Network

Discussion on: Issues with Contract Verification and Interaction on Metamask

Collapse
luv_malani_dae959d40aee9c profile image
Luv Malani Author

We're using metamask on our frontend code, and it is working on other chains.

this is the code, which works fine for all other chains like arbitrum and eth:
if (window.ethereum) {
try {
const web3 = new Web3(window.ethereum);
await window.ethereum.request({ method: "eth_requestAccounts" }); // Request account access
const accounts = await web3.eth.getAccounts();
const fromAddress = accounts[0];

    const jobDetails = {
      title: jobTitle,
      description: jobDescription,
      type: jobType,
      jobTaker: jobTaker,
      amount: amount,
      jobGiver: fromAddress,
    };

    // Pin job details to IPFS
    const response = await pinJobDetailsToIPFS(jobDetails);

    if (response && response.IpfsHash) {
      const jobDetailHash = response.IpfsHash;
      console.log("IPFS Hash:", jobDetailHash);

      const contract = new web3.eth.Contract(
        JobContractABI,
        contractAddress,
      );

      // Convert amount to Wei
      const amountInWei = web3.utils.toWei(amount, "ether");

      // Send transaction
      await contract.methods
        .enterDirectContract(jobDetailHash, jobTaker)
        .send({
          from: fromAddress,
          value: amountInWei,
        });

      console.log("Transaction successful");
    } else {
      console.error("Failed to pin job details to IPFS");
    }
  } catch (error) {
    console.error("Error sending transaction:", error);
  }
} else {
  console.error("MetaMask not detected");
}
Enter fullscreen mode Exit fullscreen mode

};

const pinJobDetailsToIPFS = async (jobDetails) => {
const url = api.pinata.cloud/pinning/pinJSONTo...;
const headers = {
"Content-Type": "application/json",
pinata_api_key: pinataApiKey,
pinata_secret_api_key: pinataSecretApiKey,
};

const body = JSON.stringify({
  pinataOptions: { cidVersion: 1 },
  pinataMetadata: { name: "JobDetails.json" },
  pinataContent: jobDetails,
});

try {
  const response = await fetch(url, {
    method: "POST",
    headers: headers,
    body: body,
  });

  const data = await response.json();
  return data;
} catch (error) {
  console.error("Error pinning to IPFS:", error);
  return null;
}
Enter fullscreen mode Exit fullscreen mode

};