1

Calling a specific function from smart contract through react jsx file , console is giving me error "transactionContract.addtoblockchain is not a function", even the function is present in ABI as well.

Jsx file contecnt

import React, { useEffect, useState } from "react";
import { ethers } from 'ethers';
import { contractABI, contractAddress } from '../utils/constants';
export const TransactionContext = React.createContext();
const { Ethereum } = window;

const getEthereumContract = async () => { 
    try {
        if (!ethereum) {
            return console.log("ethereum object is not available");
        }
        await ethereum.request({ method: 'eth_requestAccounts' }); 
        const provider =  new ethers.providers.Web3Provider(window.ethereum , 'any');
        const signer = provider.getSigner();
        const transactionContract = new ethers.Contract(contractAddress, contractABI, signer);
        return transactionContract;
    } catch (error) {
        console.log(error);
    }
};


const sendTransaction = async () => {
        try {
            if (!ethereum) {
                return alert("Please install Metamask");
            }

            const { addressTo, amount, message } = formData;

            const transactionContract = getEthereumContract();
            
            const parsedAmount = ethers.utils.parseEther(amount);
            

            
          
            ethereum.request({
                method: 'eth_sendTransaction',
                params: [{
                    from: currentAccount,
                    to: addressTo,  
                    gas: '0x5208',//21000 GWEI  
                    value: parsedAmount.toHexString(),// 0.00001
                }]
            });
            console.log(transactionContract);
            const transactionHash = await transactionContract.addtoblockchain(addressTo, parsedAmount, message);
           
            setIsLoading(true);
            console.log(`Loading - ${transactionHash.hash}`);
            await transactionHash.wait();//wait until the transactionHash will get completed.
            setIsLoading(false);
            console.log(`Success - ${transactionHash.hash}`);
            
            const transactionCount = await transactionContract.transactionCount();
            
            setTransactionCount(transactionCount.toNumber());
        } catch (error) {
            console.log(error);
            throw new Error("No ethereum object. send transaction");
        }
    };

Transaction.sol file

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0; 

contract Transaction {
    
    uint256 transactionCount; 
    event transfer(
        address from,
        address receiver,
        uint amount,
        string mesage,
        uint256 timestamp
    ); 
    struct TransferStruct {
        address sender;
        address receiver;
        uint amount;
        string message;
        uint256 timestamp;
    } 
    TransferStruct[] transactions; 
  
    function addtoblockchain( 
        address payable receiver, 
        uint256 amount,
        string memory message 
    ) public {
        transactionCount += 1;
        transactions. Push( 
            TransferStruct(
                msg.sender,
                receiver, 
                amount, 
                message,
                block.Timestamp 
            )
        ); 
        emit transfer(msg.sender, receiver, amount, message, block.timestamp); 
    }

    function displayAllTransactions()
        public
        view
        returns (TransferStruct[] memory)
    {
        return transactions;
    }

    function displayTranssactioncount() public view returns (uint256) {
        return transactionCount;
    }
}

and Transaction.json (ABI)

{
  "_format": "hh-sol-artifact-1",
  "contractName": "Transaction",
  "sourceName": "contracts/Transaction.sol",
  "abi": [
    {
      "anonymous": false,
      "inputs": [
        {
          "indexed": false,
          "internalType": "address",
          "name": "from",
          "type": "address"
        },
        {
          "indexed": false,
          "internalType": "address",
          "name": "receiver",
          "type": "address"
        },
        {
          "indexed": false,
          "internalType": "uint256",
          "name": "amount",
          "type": "uint256"
        },
        {
          "indexed": false,
          "internalType": "string",
          "name": "mesage",
          "type": "string"
        },
        {
          "indexed": false,
          "internalType": "uint256",
          "name": "timestamp",
          "type": "uint256"
        }
      ],
      "name": "transfer",
      "type": "event"
    },
    {
      "inputs": [
        {
          "internalType": "address payable",
          "name": "receiver",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "amount",
          "type": "uint256"
        },
        {
          "internalType": "string",
          "name": "message",
          "type": "string"
        }
      ],
      "name": "addtoblockchain",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "displayAllTransactions",
      "outputs": [
        {
          "components": [
            {
              "internalType": "address",
              "name": "sender",
              "type": "address"
            },
            {
              "internalType": "address",
              "name": "receiver",
              "type": "address"
            },
            {
              "internalType": "uint256",
              "name": "amount",
              "type": "uint256"
            },
            {
              "internalType": "string",
              "name": "message",
              "type": "string"
            },
            {
              "internalType": "uint256",
              "name": "timestamp",
              "type": "uint256"
            }
          ],
          "internalType": "struct Transaction.TransferStruct[]",
          "name": "",
          "type": "tuple[]"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "name": "displayTranssactioncount",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    }
  ],
  "bytecode": "xxxx",
  "deployedBytecode": xxxxxxx"
  "linkReferences": {},
  "deployedLinkReferences": {}
}

1
  • Are you sure you import the correct ABI from constants? The error clearly says that it can't read the function so the issue is with reading from ABI. Oct 20 at 17:37

1 Answer 1

0

Had the same issue. I was able to resolve it by using a later version of ethers on the client side use "ethers":^5.7.2

Your Answer

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

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