0

Hi I'm trying to interact with an Ethereum smart contract but it is always showing "TypeError : Cannot read property 'sendEth' of undefined" where 'sendEth' is a function in my smart contract

Here is code snippet:

function sendEth() {
            var transferEthAddress = "0x0152E36df4fB7587b035fbF5c02A059A0B3b9c38";
            var transferEth = new window.web3.eth.contract(transferEthABI, transferEthAddress);
            transferEth.methods.sendEth().send({from: web3.eth.accounts[0], value: web3js.utils.toWei("0.001","ether")});
        }
        window.addEventListener('load', async () => {
            if (window.ethereum) { // Modern dapp browsers... 
                window.web3 = new Web3(ethereum);
                try {
                    await ethereum.enable(); // Request account access if needed
                }
                catch (error) {
                    console.log('User denied permission to use their Ethereum account.');
                }
            }
            else if (window.web3) { // Legacy dapp browsers...
                window.web3 = new Web3(web3.currentProvider);
            }
            else { // Non-dapp browsers...
                console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
            }
        });

Web3 is getting successfully connected to my smart contract as I can see transaction in Metamask but I am unable to access the function, please help!

Here is my contract code:

pragma solidity ^0.4.25;
import "./Ownable.sol";
contract demo is Ownable
{
    function sendEth() external payable
    {
        //_owner will have the address of the deployer of contract.
        address _owner = owner();
        _owner.transfer(msg.value);
    }
}
  • 1
    Please add your contract code to make it clear. Thanks – Abdullah Aziz Mar 19 at 6:12
  • 1
    Check value of transferEth. It is probably null. – Mikhail Vladimirov Mar 19 at 10:00
  • check value of transferEth and let me know where you deploy your contract localy or public test net? – Abdullah Aziz Mar 19 at 10:46
  • I have deployed the contract using Remix IDE on Ropsten Test Net. – Nishant Sharma Mar 19 at 17:01
  • I have checked transferEth, it is not null. – Nishant Sharma Mar 19 at 17:08
0

Here are some typos to fix:

  • Capital C while calling contract function.

var transferEth = new window.web3.eth.Contract(transferEthABI, transferEthAddress);

  • It is web3.utils instead of web3js.utils:

transferEth.methods.sendEth().send({from: web3.eth.accounts[0], value: web3.utils.toWei("0.001","ether")});

Lastly, almost all functions from web3 are asynchronous and returning promise, so do not forget to write callback or use async/await syntax.

async function sendEth() {
 var transferEthAddress = "0x0152E36df4fB7587b035fbF5c02A059A0B3b9c38";
 var transferEth = await new window.web3.eth.Contract(transferEthABI, transferEthAddress);
 console.log(transferETH);
 await transferEth.methods.sendEth().send({from: web3.eth.accounts[0], value: web3.utils.toWei("0.001","ether")});
        }

Also show me the console.log.

I hope it will work, try and let me know if did not work. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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