The Git Way for a Smart Contract to Receive Ether

Jordi Bassagañas
Nov 3 · 3 min read
Image for post
Image for post

Hey dapp developers, how are things going? Today I am excited to show you how to deploy another Solidity smart contract into Ropsten. This simple contract will be capable of only receiving ether so its balance can be queried at any moment.

Let’s do it step by step, the Git way!

Please visit programarivm/solidity-receive-ether and sit back, go take a cup of coffee while browsing the commit log of the repo.

Disclaimer: This tutorial is aimed to intermediate dapp learners who already know a few basics about Truffle and Solidity, and want to know more.

As you can see on GitHub, The Git Way for a Smart Contract to Receive Ether is the third part of a series on how to build decentralized apps with Solidity.

If words such as Ropsten, Truffle, Solidity or Infura sound completely new to you, let me recommend you take a look at the first repo of the series, The Git Way to Deploy a Solidity Contract on Ropsten With Truffle.

So, are you ready for the Git way to learning new things?

Here’s the commit log for you to do the exact same thing I did:

Let’s recap, your work starts with commit 23b4ab7:

npm init

And ends with commit 727495b:

truffle migrate --network ropsten

By following the git log steps on your side as described above, you’ll end up with a new smart contract address up and running on the Ropsten testnet, ready to receive ether and display its balance.

A pretty basic thing for the time being for learning purposes!

But remember, the important thing to note is that smart contracts can receive ether too as if they were Ethereum accounts. This may sound somewhat confusing to a newcomer to dapp development, but yes, it works like this.

In a nutshell, if you want your EtherReceiver.sol contract to behave as an ether receiver, just make sure to write a receive ether function.

Most probably this is the simplest one that can be written:

function () payable external {}

Basically this prepares the contract to receive ether, that’s it!

As the official Solidity documentation states, a contract can have at most one receive ether function, which in turn must have external visibility as well as payable state mutability.

It works.

Having said that, beware that withdrawing ether from a contract is a different story covered in another GitHub repo.

Now with the contract address at hand, let’s confirm everything works as expected by sending, let’s say 0.5 test ether to it, and see what happens.

Image for post
Image for post
Image for post
Image for post

A few seconds later, a Metamask confirmation popup will be displayed saying the transaction was successfully confirmed and therefore can be viewed on Etherscan.

Finally, don’t forget to visit Etherscan to type your contract’s address in the search box and get its balance.

Image for post
Image for post

Congratulations, this is absolutely awesome! EtherReceiver.sol is responding as expected when receiving ether.

As you can see in this example, the end balance is 0.55 ETH because prior to the 0.5 ETH transaction there was another one sending 0.05 ETH to the contract.

Did you find the Git way of doing things helpful? What are your thoughts on learning by doing? Stay tuned for the next episode on interacting with the smart contract’s balance through a Node.js application.

The Innovation

A place for variety of stories from different backgrounds

Sign up for The Innovation Digest

By The Innovation

Official newsletter of The Innovation Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Jordi Bassagañas

Written by

Hi there! How are you today? I blog about technology, the Internet, SEO, programming tips, PHP and more. programarivm.com

The Innovation

A place for a variety of stories from different backgrounds

Jordi Bassagañas

Written by

Hi there! How are you today? I blog about technology, the Internet, SEO, programming tips, PHP and more. programarivm.com

The Innovation

A place for a variety of stories from different backgrounds

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store