solidity智能合约[25]-转账的3种方式与比较

转账的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函数转账,容易发生重入攻击。