Search code examples
javascriptreactjssolidityethers.jshardhat

how can i connect an address to lunch a function in the smart contract ? | TypeError: Cannot read properties of undefined (reading 'connect') ether.js


i want send 0.001 eth using submitTransaction (function inside multisig.sol)

  function submitTransaction(
        address _to,
        uint256 _value,
        bytes memory _data
    ) public onlyOwner {
        uint256 txIndex = transactions.length; //u forgot to add this

        transactions.push(
            Transaction({
                to: _to,
                value: _value,
                data: _data,
                executed: false,
                numConfirmations: 0
            })
        );

        emit SubmitTransaction(msg.sender, txIndex, _to, _value, _data);
    }

using hardhat works in this way :

  await multiSig.connect(accounts[0]).submitTransaction(
            testContract.address, //to
            ethers.utils.parseEther("1"),
            "0x"
        )

but when im in a react component that lunch "submitTransction" i receive this error : MultiSig.tsx:36 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'connect'), seems that the i can't use "connect"

react component:

multisig = await new ethers.Contract("0x0f0b261E85e545B559B1Fa3621A931Cf59287396", MultisigAbi.abi, provider)
const [multisigData, setmultisigData] = useState<any>({})

    const submitTransction = async () => {
        await multisig.connect(multisigData.owner[0]).submitTransaction(
            "0x0bec31C8b5101eF53b8A0E5e5423165a35E0E7A3", //to
            ethers.utils.parseEther("0.001"),
            "0x"
        )
        console.log(transactions);
    }

return (
  <button className="btn  btn-primary w-full" onClick={submitTransction}> submit transaction 0.001eth</button>
)

Solution

  • in this way :

    const submitTransction = async () => {
        const provider = new ethers.providers.Web3Provider(ethereum);
        const signer = provider.getSigner()
        let multisig = await new ethers.Contract(address, MultisigAbi.abi, provider);
        let transactions = await multisig.getTransactionCount()
        const ContractWithSigner = await multisig.connect(signer)
        await ContractWithSigner.submitTransaction(
            "0x0bec31C8b5101eF53b8A0E5e5423165a35E0E7A3", //to
            ethers.utils.parseEther("0.001"),
            "0x"
        )
    
    
    }