'Is there a way to take token out from the contract?

I created a contract and deposited some ERC20 tokens to there. Is there a way to take to token out form the contract to my wallet? I also want to know how to approve from the contract to other one wallet.



Solution 1:[1]

You need to implement a custom function in your contract that invokes the transfer() function of the token contract.

pragma solidity ^0.8;

interface IERC20 {
    function transfer(address to, uint256 amount) external returns (bool);
}

contract MyContract {
    address owner = address(0x123);

    function withdrawToken(address tokenContract, uint256 amount) external {
        // send `amount` of tokens
        // from the balance of this contract
        // to the `owner` address
        IERC20(tokenContract).transfer(owner, amount);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Petr Hejda