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);
}
}
transferEth
. It is probablynull
.transferEth
and let me know where you deploy your contract localy or public test net?