I read this example http://solidity.readthedocs.io/en/latest/solidity-by-example.html#simple-open-auction to learn how to let my smart contract recieve eth from external account. It seems that I need a simple payable function. So I wrote a contract with a simple payable function on Remix and test it with MetaMask (Ropsten Network), sending 1 eth from my external account to my contract. Failure, MetaMask tells me the contract throw an exception. So, I added to the contract a fallback function and tried again to send eth. It worked! But the example linked above doesn't tell me I need a fallback function, and I can't performe data storage (like store msg.value and msg.sender into variables) in a fallback function, cause it requires more gas that it can have!
Please, help me. I know it's a noob question but I can't figure out.
Thanks to everyone!
The problem is that even if you have a
payable function in your contract you're not calling it. You have to call the function directly. If you do not call any specific function directly (which happens if you just send Ether with Metamask), the fallback function will be called (if such exists).
Unfortunately Metamask does not seem to support calling contract functions directly. You need some additional tools (or different tools). Check out Metamask: How to access/call deployed contract's functions using Metamask for example.
p.s the fallback function is needed when you want your contract to accept ether without executing a function, like when you want to send ether from metamask to the contract.
Here's a simple contract I made that is payable for the makeWish function. Essentially you can throw some ether at it and get your wish text string added to the blockchain in the form of an Event.
Here's the contract code: https://etherscan.io/address/0x592F9B9A7D7AaF70FD6b4dE9883a62cA888DE702#code
You can interact with it via MetaMask here: https://etherscan.io/address/0x592F9B9A7D7AaF70FD6b4dE9883a62cA888DE702#writeContract