Solidity - Smart Contract - Send eth from external account to smart contract

by Louisio95   Last Updated November 14, 2018 01:28 AM - source

I read this example 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.

The very few contract's row: enter image description here

THE ERROR: enter image description here

Thanks to everyone!

Answers 3

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.

Lauri Peltonen
Lauri Peltonen
May 28, 2018 19:33 PM

It looks ok, you just need to deploy the contract on the ropsten network (select JavaScript VM from the top right menu on remix). Then use metamask to send ether to the contract after it gets deployed. Obviously for complex operations you are gonna need a normal function, public and payable, like the "bid" function you already got.

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.

May 28, 2018 20:01 PM

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:

You can interact with it via MetaMask here:

November 14, 2018 00:28 AM

Related Questions

Start and Endblock doesnt allow transaction

Updated August 20, 2017 14:28 PM

Debugging loops and non-executing code

Updated February 09, 2018 01:28 AM