0

So im trying to build an ethereum application in which i cant transfer my balance from one account to another account but dont know why I have encountered a strange error


So here goes the code : (don't read the full code according to the browser the error lies somewhere in the function getEthereumContract )

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

const {ethereum} = window;

const getEthereumContract = async () => {
    const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
    const signer = provider.getSigner();
    const transactionsContract = new ethers.Contract(contractAddress, contractABI, signer);

    console.log(
        provider,
        signer,
        transactionsContract
    );

    return transactionsContract;
}


export const TransactionProvider = ({children}) => {
    const [currentAccount, setCurrentAccount] = useState("");
    const [formData, setFormData] = useState({addressTo : '', amount : ''});
    const [isLoading, setIsLoading] = useState(false);
    const name = 'het';
    const age = 17;
 
    const connectWallet = async () => {
        try {
            if(!ethereum) return alert("Please install metamask");
            const accounts = await ethereum.request({
                method : "eth_requestAccounts"
            });
            setCurrentAccount(accounts[0]);
            console.log(currentAccount);

        }catch(error){
            console.log("Ethereum object not found");
        }
    }

    const handleChange = (e, name) => {
        console.log(formData);
        setFormData((prevState)=>({...prevState, [name] : e.target.value}));
        
    };

    const sendTransaction = async () => {

            if(!ethereum) return alert("Please Install Metamask.")
                const {addressTo, amount} = formData;
                const transactionContract = await getEthereumContract();
                const parsedAmount = ethers.utils.parseEther(amount);
                await ethereum.request({
                    method : "eth_sendTransaction",
                    params : [{
                        from : currentAccount,
                        to : addressTo,
                        gas : '0x5208',
                        value : parsedAmount._hex,
                    }]
                });

                const transactionHash = await transactionContract.addToBlockChain(addressTo, parsedAmount);
                setIsLoading(true);
                console.log(`Loading : ${transactionHash.hash}`);
                await transactionHash.wait();
                setIsLoading(false);
                console.log(`Success : ${transactionHash.hash}`);

                const transactionCount = await transactionContract.getTransactionCount();
    };

    return (
        <TransactionContext.Provider value={{connectWallet, currentAccount, handleChange, formData, sendTransaction, isLoading}}>
            {children}
        </TransactionContext.Provider>
    );
}

SO the error goes something like this : enter image description here


So i need a serious help to get this issue solved

3
  • Check the format of ABI. I think it should be inside an array. Mar 19 at 14:14
  • Can't believe this actually solved the issue bro....
    – Het
    Mar 19 at 15:36
  • Glad it helped :) Mark it as the answer please. Mar 20 at 5:37

1 Answer 1

1

Check the format of ABI. It should be inside an array.

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.