all files / contracts/ WithdrawableOwnable.sol

0% Statements 0/20
0% Branches 0/32
0% Functions 0/4
0% Lines 0/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
 
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
 
contract WithdrawableOwnable is Ownable, ReentrancyGuard {
    using Address for address;
    using SafeMath for uint256;
 
    /*
     * @dev Withdraw native token from this contract
     * @param amount the amount of tokens you want to withdraw
     */
    function withdraw(uint256 amount) virtual onlyOwner nonReentrant public {
        uint256 balance = address(this).balance;
 
        require(amount <= balance, "Withdrawable: you cannot remove this total amount" );
 
        Address.sendValue(payable(_msgSender()), amount);
 
        emit Withdraw(_msgSender(), amount);
    }
    event Withdraw(address sender, uint256 value);
 
    /**
     * @dev Withdraw any ERC20 token from this contract
     * @param tokenAddress ERC20 token to withdraw
     * @param amount the amount desired to remove
     */
    function withdrawERC20(
        address tokenAddress,
        uint256 amount
    ) external virtual nonReentrant onlyOwner {
        require(tokenAddress.isContract(), "Withdrawable: ERC20 token address must be a contract");
 
        IERC20 tokenContract = IERC20(tokenAddress);
 
        uint256 balance = tokenContract.balanceOf(address(this));
        require(amount <= balance, "Withdrawable: you cannot remove this total amount" );
 
        require(tokenContract.transfer(_msgSender(), amount), "Withdrawable: Fail on transfer");
 
        emit WithdrawERC20(_msgSender(), tokenAddress, amount);
    }
    event WithdrawERC20(address sender, address token, uint256 value);
 
    /**
     * @dev Withdraw any ERC721 token from this contract
     * @param tokenAddress ERC721 token to withdraw
     * @param tokenIds IDs of the NFTs to withdraw
     */
    function withdrawERC721(
        address tokenAddress,
        uint256[] memory tokenIds
    ) external virtual onlyOwner nonReentrant {
        require(tokenAddress.isContract(), "ERC721 token address must be a contract");
 
        IERC721 tokenContract = IERC721(tokenAddress);
        for (uint256 i = 0; i < tokenIds.length; i++) {
            require(
                tokenContract.ownerOf(tokenIds[i]) == address(this),
                "This contract doesn't own the NFT you are trying to withdraw"
            );
            tokenContract.safeTransferFrom(address(this), _msgSender(), tokenIds[i]);
        }
        emit WithdrawERC721(tokenAddress, tokenIds);
    }
    event WithdrawERC721(address tokenAddress, uint256[] tokenIds);
 
    /**
     * @dev Withdraw any ERC1155 token from this contract
     * @param tokenAddress ERC1155 token to withdraw
     * @param id ID of the token to withdraw
     * @param amount amount to withdraw
     */
    function withdrawERC1155(
        address tokenAddress,
        uint256 id,
        uint256 amount
    ) external virtual onlyOwner nonReentrant {
        require(tokenAddress.isContract(), "ERC1155 token address must be a contract");
 
        IERC1155 tokenContract = IERC1155(tokenAddress);
        require(
            tokenContract.balanceOf(address(this), id) >= amount,
            "this contract doesn't own the amount of tokens to withdraw"
        );
 
        tokenContract.safeTransferFrom(address(this), _msgSender(), id, amount, "");
    }
    event WithdrawERC1155(address tokenAddress, uint256 id, uint256 amount);
}