转账的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函数转账,容易发生重入***。
  • 本文链接: https://dreamerjonson.com/2018/11/20/solidity-25-thansfer2/

  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

solidity智能合约[25]-转账的3种方式与比较_第1张图片