Skip to content

Instantly share code, notes, and snippets.

@swkim109
Created February 28, 2022 05:35
Show Gist options
  • Save swkim109/d521a32cbcdacd8418e3c7207dc7c32a to your computer and use it in GitHub Desktop.
Save swkim109/d521a32cbcdacd8418e3c7207dc7c32a to your computer and use it in GitHub Desktop.
TokenRecipient
//SPDX-License-Identifier:MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract TokenRecipient {
address owner;
event ReceivedEther(address indexed sender, uint256 value);
event ReceivedErc20Token(address indexed sender, uint256 value, address indexed token);
constructor() {
owner = msg.sender;
}
modifier onlyOwner {
require(owner == msg.sender, "Caller is not the owner");
_;
}
receive() external payable {
emit ReceivedEther(msg.sender, msg.value);
}
function withdraw(uint256 _amount) external onlyOwner {
require(address(this).balance >= _amount, "Not enough balance");
payable(msg.sender).transfer(_amount);
}
// 임의의 토큰 컨트랙트에서 전송을 위임(approve) 받은 경우 허용된 범위 내에서 이 컨트랙트가 토큰을 인출할 수 있게 한다.
// 직접 이 컨트랙트에게 전송하기보다는 approve를 거쳐서 이 컨트랙트가 해당 토큰을 전송할 수 있도록 한다.
//
function receiveErc20Token(address _from, uint256 _value, address _token) public {
IERC20 erc20 = IERC20(_token);
require(erc20.transferFrom(_from, address(this), _value), "Can't receive the tokens");
emit ReceivedErc20Token(_from, _value, _token);
}
// 이더 인출처럼 이 컨트랙트의 배포자가 토큰을 인출할 수 있도록 한다.
function withdrawErc20Token(uint256 _value, address _token) external onlyOwner {
IERC20 erc20 = IERC20(_token);
require(erc20.transfer(msg.sender, _value), "Failed to withdraw");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment