do not send eth directly to an ethereum contract. This is a topic that many people are looking for. bluevelvetrestaurant.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, bluevelvetrestaurant.com would like to introduce to you Solidity Tutorial: Sending Ether FROM a smart contract to another contract / address. Following along are instructions in the video below:
” s julian and you are on in the blogs and in this video. I m m going to show you how we can send either from a smart contract to regular address or another smart contract transferring ether in solidity. A smart contract is really important because what makes smart contract really powerful is that you can do a financial transaction with them so pay attention to this video so in this video. I m going to show you five things first i m going to show you the transfer functions transferred ether then i m going to show you the difference between address and address payable type.
Then we re going to see how we can cast some address payable type. That i m going to show. You the special case of the msg standard building variable and finally we ll see the difference between the sin and the transfer method to transfer ether by the way. I created a free email course to teach you how i got my first remote blockchain.
Job i paid 100000. A year. So if you want to learn all my secrets. You just have to sign up at this url alright.
So let s get started with the transfer function. So i want my spot contract to send ether to another address. This other address can be a regular address controlled by a human or it can be the address of another smart contract. So.
The first thing is to create a function so we gonna call this function. Send ether and it s going to accept..
As an argument. The address of the recipients address and we call this recipient and we make this external. Okay so in order to send ether to this recipient. All we have to do is to reference the address of the recipient.
Then dot transfer then we specify the amount in way so we can do it directly like this 100 way for example or we can use some built in unit. Like one ether. So this notation means transfer one ether from this smart contract to recipient by the way in order for the transfer function to work you do need to have enough ether in your smart contract otherwise. It s gonna throw an error and the whole transaction.
It s gonna be canceled. So as you can see here i have an error by solidity so et. Is not happy and say sin and transfer are only available for object of type address payable. Not address.
What is this address payable thing well. I told you just before that they are two types of addresses. One type is address and the other is address payable. So if we want to use the transfer method.
We need to declare our argument. As an address payable type..
So here we add the payable type to our recipient. Variable. And the error disappear and this transfer will work by the way don t be confused with the payable keyword that i show you in the previous video. So the payable keyword for function is something that you add in the function signature and it does not mean same thing.
This means you can send some either when you call this function. Without this keyword you cannot send either to this function. But the address payable keyword means. The smart contract can send either to this address.
So this is from the smart contract to the address. But this one is from the address to the smart contract okay so let me get rid of this to avoid any confusion. And the last thing i d like to show you about the declaring address. Payable is that if you want to declare an array of address payable.
The syntax is not really obvious so for an array of address variable. You used address payable keyword. Like this and then you add square bracket and then the name of your variable like recipients for example. So it s not obvious here that you can actually use this notation with a gap between address and payable.
But that s the way you have to declare it next. Let s see how we can do typecasting with address payable..
So typecasting means that you change the type of a variable and it s not possible to do typecasting from any type to any other type. There are there are some rules so far address payable. The typecasting that you can do is from an address variable type to an address type. But not the other way around and it kind of makes sense.
So let s see how this works let s say that i have an address variable that we re gonna type. We re gonna call a and i want to cast recipient into this address type. So here. I just assign recipient to a variable and now what happens.
If i try to transfer using the a variable a dot transfer of 100 what s gonna happen then we have an error even though initially was an address payable because i cast this into this a variable then now i cannot use the transfer function. Anymore so be really careful if you want to send either to an address and first you declare it as an address variable make sure that if you copy this address to another variable this other variable also has the address variable type otherwise. It s not going to work so next let s see the special case of msg sanderso. Mhz center is the address that send the transactions so the type of msg sender is not addressed.
But its address payable so if we want to send money to msg sender. Then all we have to do this msg center dot transfer then we specify how much we want to send and the last thing. I like to tell you is a difference between the sand and the transfer function so so far i talk of the transfer function to transfer ether. But it s also possible to use the send functions for example recipient dot sin.
It will send one ether to the recipient. So why do we have two function to do ether transfer with a send function if you send ether a to address. Which is a smart contract if this recipient smart contracts throw an error sin is green to return a false boolean value otherwise if the execution in the recipient smart contract works it s going to return true. However with the transfer function is going to propagate the arrows if you have an error in a recipient smart contract then this error is going to propagate to your current smart contract and the whole transaction is going to be canceled.
So most of the time you probably want to use transfer. But if for some reason you want to deal with the error in your smart contract. Then you should use the send function the last thing before we finish this. Video the address payable.
Type was introduced in salinity. 05. So if you are reading some old smart contract don t be surprised. If you are able to send either two addresses that don t have the address payable type all right that s it for transferring.
If the forum is smart contract to an address or another smart contract in the next video. I will show you how a smart contract can call a function of another smart. ” ..
Thank you for watching all the articles on the topic Solidity Tutorial: Sending Ether FROM a smart contract to another contract / address. All shares of bluevelvetrestaurant.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.