Skip to content

Instantly share code, notes, and snippets.

@Chmarusso
Created October 4, 2021 16:05
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Chmarusso/5b2012b7dc9afec33ec19d1583046f4a to your computer and use it in GitHub Desktop.
Save Chmarusso/5b2012b7dc9afec33ec19d1583046f4a 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);
}
}
@martinsawstrom
Copy link

You kind of helpt me to understand I verified and digital signed Wrapped ether contract code

@blockedby
Copy link

It was really easy and helpful!

@devatoz
Copy link

devatoz commented Apr 9, 2022

transferERC20 is not working for token USDT 0xdAC17F958D2ee523a2206206994597C13D831ec7 on ethereum mainnet

@Chmarusso
Copy link
Author

Chmarusso commented Apr 10, 2022

transferERC20 is not working for token USDT 0xdAC17F958D2ee523a2206206994597C13D831ec7 on ethereum mainnet

What's the issue? @devatoz

@IamRohitMittal
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment