Solidity的三种转账方式与比较

转账的3种方式

1
2
3
address.transfer()
address.send()
address.call.value().gas()()

转账transfer

1
2
3
4
5
6
7
8
9
10
function transfer(address _address) public payable{

    _address.transfer(msg.value);

}

 function transfer2(address _address) public payable{
    _address.transfer(10 * 10**18); }

转账send

1
2
3
function transfer4(address _address) public payable {
      _address.send(10 ether);
}

转账call

1
2
3
function transfer5(address _address) public payable returns(bool){
   return  _address.call.value(10 ether)();
}

对比总结

1
2
3
4
5
6
transfer与send相似,都为转账操作
transfer出错抛出异常
send、call出错不抛出异常,返回true或false
tansfer相对send更安全
send、call即便转账失败也会执行其后的代码
慎用call函数转账,容易发生重入攻击。

address.transfer()

  • throws on failure
  • forwards 2,300 gas stipend (not adjustable), safe against reentrancy
  • should be used in most cases as it's the safest way to send ether

address.send()

  • returns false on failure
  • forwards 2,300 gas stipend (not adjustable), safe against reentrancy
  • should be used in rare cases when you want to handle failure in the contract

address.call.value().gas()()

  • returns false on failure
  • forwards all available gas (adjustable), not safe against reentrancy
  • should be used when you need to control how much gas to forward when sending ether or to call a function of another contract
上一篇:微信小程序 - Util工具类


下一篇:CentOS7 使用ifconfig命令 ENS33没有IP地址的解决办法