Skip to content

Instantly share code, notes, and snippets.

@tempe-techie
Last active November 5, 2022 11:41
Show Gist options
  • Save tempe-techie/8ec4d515bebad3d645dc83d33341b59f to your computer and use it in GitHub Desktop.
Save tempe-techie/8ec4d515bebad3d645dc83d33341b59f to your computer and use it in GitHub Desktop.
Redirect contract (quick prototype, untested)
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/interfaces/IERC721.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Redirect is Ownable {
address public receiver;
// CONSTRUCTOR
constructor(address _receiver) {
receiver = _receiver;
}
// REDIRECT NATIVE TOKENS (THAT COME TO THIS SMART CONTRACT) TO THE RECEIVER ADDRESS
receive() external payable {
receiver.call{value: address(this).balance}("");
}
fallback() external payable {
receiver.call{value: address(this).balance}("");
}
// manually redirect native token from contract (in case it wasn't redirected automatically)
function manualRedirect() external onlyOwner {
(bool success, ) = receiver.call{value: address(this).balance}("");
require(success, "Failed to redirect native token");
}
// change receiver address
function changeReceiverAddress(address _receiver) external onlyOwner {
receiver = _receiver;
}
// recover ERC20 tokens mistakenly sent to this contract
function recoverERC20(address tokenAddress_, uint256 tokenAmount_, address recipient_) external onlyOwner {
IERC20(tokenAddress_).transfer(recipient_, tokenAmount_);
}
// recover ERC721 tokens mistakenly sent to this contract, or transfer punk domain name back to you
function recoverERC721(address tokenAddress_, uint256 tokenId_, address recipient_) external onlyOwner {
IERC721(tokenAddress_).transferFrom(address(this), recipient_, tokenId_);
}
}
@tmoindustries
Copy link

This is really elegant.

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