0

I'm learning how to build a smart contract that is able to receive ETH and, if certain conditions are met, send ETH back to the address from which it received it.

My understanding currently is that a smart contract has a wallet address. That means that somebody could hypothetically just type in a smart contract address in Coinbase and send funds that way.

If that is the case, how can the smart contract be programmed to detect which wallet sent it the ETH and execute some action for each such situation? If that isn't the case, please explain to me where I'm wrong.

If that is the case but it is impossible to implement functionality like what I described in the previous paragraph, are there any other solutions you would suggest?

Links to wider tutorials, articles, etc. are much appreciated.

1

A smart-contract account and an externally-owned account (what you refer to as wallet) are indeed just plain Ethereum addresses.

However, while an externally-owned account can receive ether without any "special preparations", a smart-contract account can receive ether only if the smart contract implements the anonymous fallback function:

function() external payable {
}

Inside this function, the address of the ether sender is msg.sender.

You can of course extend it and add your desired logic after receiving ether.

| improve this answer | |
  • In solc v0.6 there's a function receive that can be use for that purpose. – Ismael Jul 1 at 16:53
  • 1
    @Ismael: Yeah, I wasn't sure whether or not to mention that. It's pretty new to most experienced users, let alone a newbie (though maybe that's a counter-argument, since everything is equally "new" in the perspective of a newbie). In any case, he/she seems to have a lot to learn before jumping to that, so I figured explaining the "traditional" fallback would be sufficient in this context. – goodvibration Jul 1 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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