Skip to content

Instantly share code, notes, and snippets.

@IamTalha-Sajid
Forked from Chmarusso/FeeCollector.sol
Created May 18, 2022 12:29
Show Gist options
  • Save IamTalha-Sajid/da15aeefed14fa4fbbf9eca9945e0074 to your computer and use it in GitHub Desktop.
Save IamTalha-Sajid/da15aeefed14fa4fbbf9eca9945e0074 to your computer and use it in GitHub Desktop.
Transfer ERC20 from contract
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