-
-
Save IamTalha-Sajid/da15aeefed14fa4fbbf9eca9945e0074 to your computer and use it in GitHub Desktop.
Transfer ERC20 from contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.7; | |
// SPDX-License-Identifier: MIT | |
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | |
contract FeeCollector { | |
address public owner; | |
uint256 public balance; | |
event TransferReceived(address _from, uint _amount); | |
event TransferSent(address _from, address _destAddr, uint _amount); | |
constructor() { | |
owner = msg.sender; | |
} | |
receive() payable external { | |
balance += msg.value; | |
emit TransferReceived(msg.sender, msg.value); | |
} | |
function withdraw(uint amount, address payable destAddr) public { | |
require(msg.sender == owner, "Only owner can withdraw funds"); | |
require(amount <= balance, "Insufficient funds"); | |
destAddr.transfer(amount); | |
balance -= amount; | |
emit TransferSent(msg.sender, destAddr, amount); | |
} | |
function transferERC20(IERC20 token, address to, uint256 amount) public { | |
require(msg.sender == owner, "Only owner can withdraw funds"); | |
uint256 erc20balance = token.balanceOf(address(this)); | |
require(amount <= erc20balance, "balance is low"); | |
token.transfer(to, amount); | |
emit TransferSent(msg.sender, to, amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment