fbpx

How to transfer Ether between 2 smart contracts?

Julien Klepatch

Sending Ether between smart contracts is very important. After all, it’s because smart contracts can do financial transactions that they are so powerful.

But surprisingly there are a few caveats when you want to send ether from a smart contract:

  • Transfer units are in wei (10^-18 ether), not ether
  • There are several functions to do the same thing
  • If the recipient address has the `address` type, it does not work…

huh?

Alright, without going through all the details, you can just copy paste this snippet in your smart contract:

contract A {
  transferEther(address payable recipient) external {
      recipient.transfer(1000);
  }
}

This means “Send 1000 wei from the A smart contract to the recipient address (can be another smart contract or a regular address controlled by a human).

BONUS TIP: instead of 1000 you can also use some of the pre-build units of Solidity, such as 1 ether.

If you want to see a more thorough explanation of ether transfers from smart contracts, I have created a short video on youtube. Check it out it’s free!

And if you want to have even more practice with financial transfers in Solidity smart contracts, check out Dapp 30, a complete Solidity course where we build 30 smart contracts, test them, and build a frontend for them (full Dapps).

0 Comments

Leave a Reply

More great articles

(Part 7/18 updated) Introduction to Web3.js 1.2 (Latest version) – Beginners Tutorial

When you develop an Ethereum Dapp, once you have the frontend and the smart contract, you need to connect them…

Read Story

Learn To Build a DEX on Ethereum with my video course on Manning

I have just released "Ethereum Dapps In Motion", my premium video course for Ethereum Dapp Developers, in partnership with Manning:…

Read Story

How error propagate between contracts in Solidity?

Let's say we have 2 smart contracts. A and B. A calls B, and B throws an error. What happen in…

Read Story

Never miss a minute

Get great content to your inbox every week. No spam.
[contact-form-7 id="6" title="Footer CTA Subscribe Form"]
Arrow-up