Overview
TransferHelper is a utility library that provides safe wrapper functions for ERC20 token transfers. It uses Solmate’s SafeTransferLib to ensure secure token operations.
Source: src/libraries/TransferHelper.sol
Dependencies
Functions
_transferFromERC20
token(address): The contract address of the ERC20 token to transferfrom(address): The originating address from which tokens will be transferredto(address): The destination address for the transferamount(uint256): The amount of tokens to transfer
- The caller must have approval to spend
amounttokens from thefromaddress - The
fromaddress must have sufficient balance
- Automatically handles tokens with non-standard return values
- Reverts on failed transfers
- Gas-optimized implementation via Solmate
_transfer
token(address): The contract address of the ERC20 token to transferto(address): The destination address for the transferamount(uint256): The amount of tokens to transfer
- The contract must have sufficient balance of the token
- Automatically handles tokens with non-standard return values
- Reverts on failed transfers
- Gas-optimized implementation via Solmate
Usage Example
Why Use TransferHelper?
Handles Non-Standard ERC20 Tokens
Some ERC20 tokens don’t follow the standard specification:- Tokens that don’t return a boolean on
transferortransferFrom - Tokens that revert on failure instead of returning false
- Tokens with missing return values
TransferHelper (via SafeTransferLib) handles all these cases safely.
Gas Optimization
Solmate’sSafeTransferLib is highly gas-optimized compared to OpenZeppelin’s SafeERC20, making it ideal for production use.
Consistent Interface
Provides a uniform interface for token transfers across the UMA CTF Adapter codebase.Security Considerations
These functions will revert if the transfer fails for any reason (insufficient balance, insufficient allowance, etc.). Always handle these cases appropriately in your calling code.