You are viewing a single comment's thread from:

RE: Solidity开发指南

in #starnotelast month

transfer 和 send异同:
两者都可以转移以太币,但尽量使用transfer。都有2300gas的限制。
在转帐时send异常时不会发生错误,只会返回一个布尔值(false),所以要使用判断函数assert(addr.send(1 ether))

address payable _to = 0xddsddfxxx;
//有2300gas的限制
 _to.transfer(1 ether);

bool sent = _to.send(1 ether);
require(sent, "Failed to send Ether");

// 当然也可以使用来call发送
(bool sent, bytes memory data) = _to.call{value: 1 ether}("");
require(sent, "Failed to send Ether");

//sendValue没有gas的限制
owner.sendValue(address(this).balance);
function sendValue(address payable recipient, uint256 amount) internal {
  require(address(this).balance >= amount, "Address: insufficient balance");

  // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
  (bool success, ) = recipient.call{ value: amount }("");
  require(success, "Address: unable to send value, recipient may have reverted");
}