How to send ethers- transfer, send and call functions in solidity

How to send ethers- transfer, send and call functions in solidity

 

Are you curious to know how you can send ether in Solidity ? Do you want to know different ways to send ether in Solidity ?, if yes then this is right post for you as we will explain you how you can transfer/send the ether in Solidity. We also let you know what is recommended way to send ether and why.

 

Types of Accounts

Before we go in different methods, you need to understand some basic concepts like there are two types of accounts in Ethereum, one is account hold by person and second one hold by contract. In Ethereum, you can transfer the ether between the accounts hold by person and also between the contracts.

To transfer the ether between the accounts held by person, you use wallets but when you need to transfer/send the ether between contract then there are different option that you can opt and we will discuss that here.

 

How to transfer the ether

  1. Using transfer function (Recommended)
  2.  Using send function
  3. Using call function (Least recommended)

Let's go over these functions one by one.

 

Using transfer function

This is recommended method to transfer the funds(ether) in solidity. This function is function of address. This method has 2300 gas limit and that save the contract from different attacks. There is no option to add more gas to this function as of now but it may be expected in future release. Transfer function throw exception and revert the state if unsuccessful which is not the case with send and call. This is explained more in detail in video.

pragma solidity ^0.4.20;
contract first{
function() payable { }
function getBalance() public returns(uint){
return address(this).balance;
}
}
contract second{
function send(address _add)public payable{
_add.transfer(msg.value);
}
}

Using send function

This is another method to send the funds(ether) in solidity. This function is also function of address. This method has 2300 gas limit and that save the contract from different attacks. There is no option to add more gas to this function. This does not throw exception but return false if unsuccessful and true in case successful. It does not revert the state until we use require function. This is explained more in detail in video.

pragma solidity ^0.4.20;
contract first{
function() payable { }
function getBalance() public returns(uint){
return address(this).balance;
}
}
contract second{
function send(address _add)public payable{
require(_add.send(msg.value));
}
}

 

Using call function

This is least preferred method to send the funds(ether) in solidity and should not be used as this may lead to attack to contract. This function is also function of address. This method support setting gas and does not limit to 2300. This does not throw exception but return false if unsuccessful and true in case successful. It does not revert the state until we use require function. This is explained more in detail in video.

pragma solidity ^0.4.20;
contract first{
function() payable { }
function getBalance() public returns(uint){
return address(this).balance;
}
}
contract second{
function send(address _add)public payable{
require( _add.call.value(1 ether).gas(20317)());
}
}

No Comments

Post a Reply

Inquire Now
close slider