Address

Collection of functions related to the address type.

isContract

function isContract(address account) internal view returns (bool)

Returns true if account is a contract.

IMPORTANT

It is unsafe to assume that an address for which this function returns false is an externally owned account (EOA) and not a contract.

Among others, isContract will return false for the following types of addresses:

  • an externally-owned account

  • a contract in construction

  • an address where a contract will be created

  • an address where a contract lived, but was destroyed

Furthermore, isContract will also return true if the target contract within the same transaction is already scheduled for destruction by SELFDESTRUCT, which only has an effect at the end of a transaction.

IMPORTANT

You shouldn't rely on isContract to protect against flash loan attacks!

Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract constructor.

sendValue

function sendValue(address payable recipient, uint256 amount) internal

Replacement for Solidity's transfer: sends amount wei to recipient, forwarding all available gas, and reverting on errors.

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.

IMPORTANT

because control is transferred to recipient, care must be taken to not create reentrancy vulnerabilities. Consider using {ReentrancyGuard} or the checks-effects-interactions pattern.

functionCall

function functionCall(address target, bytes data) internal returns (bytes)

Performs a Solidity function call using a low-level call. A plain call is an unsafe replacement for a function call: use this function instead.

If target reverts with a revert reason, it is bubbled up by this function (like regular Solidity function calls).

Returns the raw returned data. To convert to the expected return value, use abi.decode.

Requirements:

  • target must be a contract.

  • calling target with data must not revert.

Available since v3.1.

functionCall

function functionCall(address target, bytes data, string errorMessage) internal returns (bytes)

Same as {xref-Address-functionCall-address-bytes-}[functionCall], but with errorMessage as a fallback revert reason when target reverts.

Available since v3.1.

functionCallWithValue

function functionCallWithValue(address target, bytes data, uint256 value) internal returns (bytes)

Same as {xref-Address-functionCall-address-bytes-}[functionCall], but also transferring value wei to target.

Requirements:

  • the calling contract must have an ETH balance of at least value.

  • the called Solidity function must be payable.

Available since v3.1.

functionCallWithValue

function functionCallWithValue(address target, bytes data, uint256 value, string errorMessage) internal returns (bytes)

Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[functionCallWithValue], but with errorMessage as a fallback revert reason when target reverts.

Available since v3.1.

functionStaticCall

function functionStaticCall(address target, bytes data) internal view returns (bytes)

Same as {xref-Address-functionCall-address-bytes-}[functionCall], but performing a static call.

Available since v3.3.

functionStaticCall

function functionStaticCall(address target, bytes data, string errorMessage) internal view returns (bytes)

Same as {xref-Address-functionCall-address-bytes-string-}[functionCall], but performing a static call.

Available since v3.3.

functionDelegateCall

function functionDelegateCall(address target, bytes data) internal returns (bytes)

Same as {xref-Address-functionCall-address-bytes-}[functionCall], but performing a delegate call.

Available since v3.4.

functionDelegateCall

function functionDelegateCall(address target, bytes data, string errorMessage) internal returns (bytes)

Same as {xref-Address-functionCall-address-bytes-string-}[functionCall], but performing a delegate call.

Available since v3.4.

verifyCallResultFromTarget

function verifyCallResultFromTarget(address target, bool success, bytes returndata, string errorMessage) internal view returns (bytes)

Tool to verify that a low-level call to smart-contract was successful, and revert (either by bubbling the revert reason or using the provided one) in case of an unsuccessful call or if the target was not a contract.

Available since v4.8.

verifyCallResult

function verifyCallResult(bool success, bytes returndata, string errorMessage) internal pure returns (bytes)

Tool to verify that a low-level call was successful, and revert if it wasn't, either by bubbling the revert reason or using the provided one.

Available since v4.3.


Address Summarize

The Address smart contract in question is a library of functions related to the address type.

This library provides a set of tools for interacting with the other addresses and contracts safely and flexibly.

  • Address Check: The function isContract(address account) checks if the provided address is a contract. It returns true if the address is a contract, and false otherwise. This function is used to determine if an address is a contract or an externally-owned account (EOA).

  • Value Transfer: The function sendValue(address payable recipient, uint256 amount) sends a specified amount of wei to a recipient address. It checks if the contract has enough balance to send the value, and if the recipient can receive the value.

  • Low-Level Calls: The library provides several functions for making low-level calls to other contracts. These include functionCall, functionCallWithValue, functionStaticCall, and functionDelegateCall. These functions allow the contract to interact with other contracts in a more flexible and customizable way than the standard Solidity function call syntax.

  • Call Result Verification: The function verifyCallResultFromTarget(address target, bool success, bytes memory returndata, string memory errorMessage) checks the result of a low-level call to a target contract. If the call was successful, it checks if the target is a contract and returns the return data. If the call was not successful, it reverts with the provided error message.

  • Error Handling: The function _revert(bytes memory returndata, string memory errorMessage) is used to handle errors. If the provided returndata is not empty, it bubbles up the revert reason from the target contract. If the returndata is empty, it reverts with the provided error message.

Last updated