[SOLVED] Interacting with smart contract

Hi.

I have a dapp that interacts with smart contract. Im using useWeb3ExecuteFunction() .
After i authenticate i can use all my functions and they work fine. But if a refresh the page none of them works. And i get 0 errors in console. I tried different browsers, wallets but i still have that error. What may have caused this ?

I thought user may be logged out when i refresh the page. I dont use anything to save the session but user doesnt logged out when i refresh the page cuz i can access other stuff.

import { useWeb3ExecuteFunction, useMoralis } from "react-moralis";

import React from 'react'
import { Button } from "@chakra-ui/react";

const Interaction = () => {
  const contractProcessor = useWeb3ExecuteFunction();
  const { Moralis, authenticate, isAuthenticated, user, logout } = useMoralis();
  async function interact() {
    let options = {
      chain: "rinkeby",
      contractAddress: contract_address,
      functionName: "addNewSubject",
      abi: [{ "inputs": [{ "name": "_attendance_giver", "type": "address" }, { "name": "_subject_id", "type": "string" }, { "name": "_batch_id", "type": "string" }], "name": "addNewSubject", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }],
      /*params:name,roll,email,int imei,address public_key, int status*/
      params: {
        _attendance_giver: user.get('ethAddress'),
        _subject_id: user.get('Name'),
        _batch_id: "1"
      },
    }
    await contractProcessor.fetch({
      params: options
    });
  }
  async function addStudent() {
    let optionsa = {
      chain: "rinkeby",
      contractAddress: contract_address,
      functionName: "addAttendee",
      abi: [{ "inputs":[{"name":"_name","type":"string"},{"name":"_roll","type":"string"},{"name":"_email","type":"string"},{"name":"_imei","type":"uint256"},{"name":"_public_key","type":"address"},{"name":"status","type":"uint256"}],"name":"addAttendee","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}],
      /*params:name,roll,email,int imei,address public_key, int status*/
      params: {
        _name: "test",
        _roll: "test",
        _email: "deneme",
        _imei: 1,
        _public_key: public_key,
        status: 1
      },
    }
    await contractProcessor.fetch({
      params: optionsa
    });
  }


  return (
    <div><Button onClick={() => interact()}>add subject </Button>
    <p></p>
          <Button onClick={() => addStudent()}>add student</Button>
    </div>
  )
}

export default Interaction

  • useWeb3ExecuteFunction expects web3Enabled and you should consider that.
  • The chain will be determined regarding the current network of the connected wallet.
  • You can also wrap your code in a try ... catch block and log errors in the catch block
1 Like

Thank you for your answer. After i add web3Enabled, it works fine. Thanks!

The chain will be determined regarding the current network of the connected wallet.

Should i declare chain or not. I couldnt understand what you mean.

No need to declare the chain, itโ€™ll pick that from the connected wallet. So you need to make sure youโ€™re on the proper network

1 Like