[Ethereum] How to send ether to contract’s address

contract-designsolidity

I have a problem with sending ether to my contract address via metamask. I found that the function has to be with 'payable' modifier, but it doesn't help. I send transaction, spend ethers from my account, but the contract's address still has 0 value. For example the contract looks like:

contract Mycontract
{
  function Mycontract() payable{}
}

Best Answer

The problem is that the constructor Mycontract() gets called only once.

After that you need to be calling a particular function. Without specifying a particular function, the invocation defaults to the default function which is not present in your example. Every smart contract has a default function that does nothing and is not payable. Because of this, your contract simply refuses to accept the ether.

If you want your contract to do absolutely nothing but accept ether (which would be a silly because you could never get the ether back), you can do this:

contract C {
    function C() {}
    function () payable {}
}

This will accept ether, but it will be stuck there forever. The second function is the syntax for a default function.

Related Topic