Create a Smart Contract Function that Transfers eth from One Address to Another

Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

It's time to add functionality to our smart contract. In this lesson we will define a function sendTip that accepts a _message and _name. We will check the senders balance and if it's higher than the tip amount transer the eth specified. We will also increment our totalTips contract variable and add the tip to our Tips array.

While adding this function you'll learn how to handle errors within Solidity functions

Instructor: [0:00] It's time to add actions to this contract by creating some functions to make it useful. The first method that you'll create will be the sendTip. To accomplish this, you'll use the function keyword and set two arguments. [0:18] One string to hold the message marked as stored in memory and another string stored in memory too called name. Then mark this function as public and payable so it can accept ETH.

[0:35] Let's introduce a new utility function called require(). Add this into the body of the function. This is a convenience function that checks invariance conditions and throw exceptions. The condition to validate is to check that the sender's balance is bigger than the amount the sender wants to send. If it's not enough, it will throw an error saying that there are not enough funds.

[1:08] It's time to write the logic to send the money to the owner by using the call function. That rates the amount of ETH that the sender wants to send from the msg.value argument. It uses an empty argument as a way to trigger the fallback function of the receiving address to forward the [inaudible] .

[1:34] Then, you use the require function again, this time to check the value of the success variable. If the value is false, the require function will throw an error saying that the transfer failed.

[1:48] Finally, you need to update the totalTips variable by adding 1 and put a new tipStruct into the tips array with the data accepted by the function. This is the msg.sender as address, the message string, the name string, the block timestamp, and the amount sent. Also, fix this typo here.

[2:13] Now, continue with the next function. The next function will be a way to get all the tips. It will be a public view function, meaning that will not affect the data stored in the blockchain and will return an array of tips. This is just returnTips.

[2:32] In summary, in this lesson you learned how to create a function, how to receive parameters, how to use a conditional way to throw errors, the require function, and finally, how to transfer money from one address to another.

egghead
egghead
~ 14 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today