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);
    }
}
5
  • 1
    Please add your contract code to make it clear. Thanks Commented Mar 19, 2019 at 6:12
  • 1
    Check value of transferEth. It is probably null. Commented Mar 19, 2019 at 10:00
  • check value of transferEth and let me know where you deploy your contract localy or public test net? Commented Mar 19, 2019 at 10:46
  • I have deployed the contract using Remix IDE on Ropsten Test Net. Commented Mar 19, 2019 at 17:01
  • I have checked transferEth, it is not null. Commented Mar 19, 2019 at 17:08

1 Answer 1

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

9
  • 1
    I have tried all these things, but it didn't worked for me. I have written callback also, that also didn't worked. Commented Mar 19, 2019 at 9:35
  • Can you please, send me your ABI? can you see the sendEth() in ABI? Commented Mar 19, 2019 at 11:30
  • github.com/imnishant/Transfer-Ethereum/blob/master/… This is link to abi of this contract. Commented Mar 19, 2019 at 16:53
  • And where is yout contract deployed? Local? Or any other test net? Commented Mar 19, 2019 at 17:05
  • I have deployed the contract using Remix IDE on Ropsten Test Net. Commented Mar 19, 2019 at 17:09

Your Answer

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

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