0

I have to safeTransfer an ERC20 to a smart contract. How can I do this?

I understand that SafeERC20 is not an extension of ERC20, this is why I don't understand how I am able to interact with safe functions if there is not an actual deploy of it.

Do I have to deploy my own "safe" smart contract and wrap the ERC20 I want to send in order to be able to use safeTransfer?

1
  • ethereum.stackexchange.com/questions/137882/… explains what safeTransfer is. It doesn't make the ERC20 standard 'safe', whatever that means, it just ensures that your transfer will work with all allowed implementations of the standard.
    – Foxxxey
    Jun 21, 2023 at 11:39

1 Answer 1

1

To safely transfer an ERC20 token to a smart contract, you can use the SafeERC20 library to wrap the ERC20 token in a "safe" contract. This allows you to use the safe transfer functions provided by the library. You can follow this example where I "safe transfer" the tokens to the contract itself.

import {IERC20, SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract SafeTransferToken {
    IERC20 private token;
    using SafeERC20 for IERC20;

    constructor(address tokenAddress) {
        token = IERC20(tokenAddress);
    }

    function transferTokensToContract(uint256 amount) external {
        // I'm transferring the tokens to this contract
        token.safeTransferFrom(msg.sender, address(this), amount);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.