Transfer replacement (#1962)
* Add Address.sendEther * Add documentation to sendEther * Add changelog entry * Rename sendEther to sendValue
This commit is contained in:
@ -40,4 +40,28 @@ library Address {
|
||||
function toPayable(address account) internal pure returns (address payable) {
|
||||
return address(uint160(account));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
|
||||
* `recipient`, forwarding all available gas and reverting on errors.
|
||||
*
|
||||
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
|
||||
* of certain opcodes, possibly making contracts go over the 2300 gas limit
|
||||
* imposed by `transfer`, making them unable to receive funds via
|
||||
* `transfer`. {sendValue} removes this limitation.
|
||||
*
|
||||
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
|
||||
*
|
||||
* IMPORTANT: because control is transferred to `recipient`, care must be
|
||||
* taken to not create reentrancy vulnerabilities. Consider using
|
||||
* {ReentrancyGuard} or the
|
||||
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
|
||||
*/
|
||||
function sendValue(address payable recipient, uint256 amount) internal {
|
||||
require(address(this).balance >= amount, "Address: insufficient balance");
|
||||
|
||||
// solhint-disable-next-line avoid-call-value
|
||||
(bool success, ) = recipient.call.value(amount)("");
|
||||
require(success, "Address: unable to send value, recipient may have reverted");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user