1

I'm trying to convert my dapp from vanilla js + html (local hardhat node) to react-moralis + nextJS (local hardhat node).

Here is an example of a contract function call from the front-end. This code should create a new Event entity using provided params and ask for some gas in metamask. I hardcoded the params for now but when I call the function through an onClick on a button, metamask doesnt pop up:

import { useWeb3Contract } from "react-moralis"
import contractAddresses from "../constants/contractAddresses.json"
import abiEvent from "../constants/abiEvent.json"
import { useMoralis } from "react-moralis"

export default function CreateMatch() {
    const teamName1 = "t1"
    const teamName2 = "t2"
    const teamName3 = "t3"
    const eventDate = "090909"
    const matchPlace = "msk"
    const matchFinished = false
    const matchBets = []
    const { Moralis, isWeb3Enabled, chainId: chainIdHex } = useMoralis()
    const chainId = parseInt(chainIdHex)


    const universalContractAddress = chainId in contractAddresses ? contractAddresses[chainId][0] : null

    const eventOptions = {
        abi: abiEvent,
        contractAddress: universalContractAddress,
        functionName: "createMatch",
        params: {
            teamName1, teamName2, teamName3, eventDate, matchPlace, matchFinished, matchBets
        },
    };

    const { runContractFunction, isFetching, isLoading } =
        useWeb3Contract();

    const { runContractFunction: createMatch } = useWeb3Contract();

    return (
        <div>Hi from CreateMatch
            <div>
                <button
                    onClick={() => runContractFunction({ params: eventOptions })}
                    disabled={isFetching}
                >
                    CreateMatch
                </button>
            </div>
        </div>
    )
}

and here is the contract that the code is intercating with:


pragma solidity 0.8.7;

contract Event {
    uint256 public matchCount = 0;
    mapping(uint => Match) public matches;

    struct Match {
        uint256 matchId;
        string homeTeam;
        string awayTeam;
        string drawTeam;
        string matchDate;
        string place;
        bool finished;
        address[] userList;
    }

    function createMatch(
        string memory homeTeam,
        string memory awayTeam,
        string memory drawTeam,
        string memory matchDate,
        string memory place,
        bool finished,
        address[] memory userList
    ) public {
        matches[matchCount] = Match(
            matchCount,
            homeTeam,
            awayTeam,
            drawTeam,
            matchDate,
            place,
            finished,
            userList
        );
        matchCount++;
    }
}

What am I doing wrong? Thanks in advance for your time!

1 Answer 1

0

If anyone ever gets the same problem - I was sending parameters for my contract function incorrectly. Here is what I should have done:

  const eventOptions = {
    abi: abiEvent,
    contractAddress: universalContractAddress,
    functionName: "createMatch",
    //msgValue: 1,
    params: {
      homeTeam: "t1",
      awayTeam: "t2",
      drawTeam: "t3",
      matchDate: "0909",
      place: "msk",
      finished: false,
      userList: []
    }
  };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.