Protocol Name: Uniswap v3: Router
Category: DeFi
Smart Contract: TransferHelper.sol (located at SwapRouter/contracts/libraries if opened in IDE from Etherscan)
Function Name: safeTransferFrom
Block Explorer Link: https://etherscan.io/address/0xe592427a0aece92de3edee1f18e0157c05861564#code
Function Code:
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.6.0;
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
library TransferHelper {
/// @notice Transfers tokens from the targeted address to the given destination
/// @notice Errors with 'STF' if transfer fails
/// @param token The contract address of the token to be transferred
/// @param from The originating address from which the tokens will be transferred
/// @param to The destination address of the transfer
/// @param value The amount to be transferred
function safeTransferFrom(
address token,
address from,
address to,
uint256 value
) internal {
(bool success, bytes memory data) =
token.call(abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'STF');
}
// Other functions omitted for brevity
}
Used Call Method: abi.encodeWithSelector
, call
The purpose of the safeTransferFrom
function within the TransferHelper
library is to securely transfer a specified amount of an ERC-20 token from a source address to a destination address. This function abstracts the complexities involved in token transfers, ensuring they adhere to the ERC-20 standard and handling potential errors gracefully.
The function leverages abi.encodeWithSelector
to encode the transferFrom
function selector along with the parameters (from
, to
, and value
). This encoding ensures the correct function is called on the token contract.
-
Function Encoding:
- The function uses
abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value)
to encode thetransferFrom
function call. This selector ensures that thetransferFrom
function of the ERC-20 token contract is correctly identified and called. - The
token.call(...)
is used to invoke thetransferFrom
function on the token contract.
- The function uses
-
Execution and Verification:
- The call returns two values:
success
(a boolean indicating if the call was successful) anddata
(the returned data from the call). - The
require
statement checks if the call was successful and if the returned data is either empty or decodes totrue
.
- The call returns two values:
(bool success, bytes memory data) =
token.call(abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'STF');
The safeTransferFrom
function is crucial for the TransferHelper
library’s functionality within the Uniswap v3 protocol for several reasons:
-
Token Transfer Abstraction:
- It abstracts the ERC-20 token transfer process, making it easier and safer to transfer tokens without directly interacting with the ERC-20 contract’s
transferFrom
method. - This function ensures compatibility with any ERC-20 token that adheres to the standard, providing flexibility in handling different tokens.
- It abstracts the ERC-20 token transfer process, making it easier and safer to transfer tokens without directly interacting with the ERC-20 contract’s
-
Error Handling:
- The function includes error handling mechanisms to ensure that failed transfers are caught and handled appropriately, reverting with a specific error message (
'STF'
) if the transfer fails.
- The function includes error handling mechanisms to ensure that failed transfers are caught and handled appropriately, reverting with a specific error message (
-
Protocol Operations:
- In the context of Uniswap v3, which facilitates token swaps, the
safeTransferFrom
function enables the protocol to transfer tokens from a user’s wallet to the protocol’s contracts. This is essential for the swap functionality, where tokens need to be moved between different addresses to execute trades. - By using this helper function, Uniswap can ensure that the token transfers required for swaps and other operations are performed securely and reliably.
- In the context of Uniswap v3, which facilitates token swaps, the
Overall, the safeTransferFrom
function enhances the robustness and security of token transfers within the Uniswap v3 protocol, contributing significantly to the protocol’s seamless and efficient operation.