github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/src/periphery/AssetReceiver.sol (about) 1 // SPDX-License-Identifier: MIT 2 pragma solidity ^0.8.0; 3 4 import { ERC20 } from "@rari-capital/solmate/src/tokens/ERC20.sol"; 5 import { ERC721 } from "@rari-capital/solmate/src/tokens/ERC721.sol"; 6 import { Transactor } from "./Transactor.sol"; 7 8 /// @title AssetReceiver 9 /// @notice AssetReceiver is a minimal contract for receiving funds assets in the form of either 10 /// ETH, ERC20 tokens, or ERC721 tokens. Only the contract owner may withdraw the assets. 11 contract AssetReceiver is Transactor { 12 /// @notice Emitted when ETH is received by this address. 13 /// @param from Address that sent ETH to this contract. 14 /// @param amount Amount of ETH received. 15 event ReceivedETH(address indexed from, uint256 amount); 16 17 /// @notice Emitted when ETH is withdrawn from this address. 18 /// @param withdrawer Address that triggered the withdrawal. 19 /// @param recipient Address that received the withdrawal. 20 /// @param amount ETH amount withdrawn. 21 event WithdrewETH(address indexed withdrawer, address indexed recipient, uint256 amount); 22 23 /// @notice Emitted when ERC20 tokens are withdrawn from this address. 24 /// @param withdrawer Address that triggered the withdrawal. 25 /// @param recipient Address that received the withdrawal. 26 /// @param asset Address of the token being withdrawn. 27 /// @param amount ERC20 amount withdrawn. 28 event WithdrewERC20(address indexed withdrawer, address indexed recipient, address indexed asset, uint256 amount); 29 30 /// @notice Emitted when ERC20 tokens are withdrawn from this address. 31 /// @param withdrawer Address that triggered the withdrawal. 32 /// @param recipient Address that received the withdrawal. 33 /// @param asset Address of the token being withdrawn. 34 /// @param id Token ID being withdrawn. 35 event WithdrewERC721(address indexed withdrawer, address indexed recipient, address indexed asset, uint256 id); 36 37 /// @param _owner Initial contract owner. 38 constructor(address _owner) Transactor(_owner) { } 39 40 /// @notice Make sure we can receive ETH. 41 receive() external payable { 42 emit ReceivedETH(msg.sender, msg.value); 43 } 44 45 /// @notice Withdraws full ETH balance to the recipient. 46 /// @param _to Address to receive the ETH balance. 47 function withdrawETH(address payable _to) external onlyOwner { 48 withdrawETH(_to, address(this).balance); 49 } 50 51 /// @notice Withdraws partial ETH balance to the recipient. 52 /// @param _to Address to receive the ETH balance. 53 /// @param _amount Amount of ETH to withdraw. 54 function withdrawETH(address payable _to, uint256 _amount) public onlyOwner { 55 // slither-disable-next-line reentrancy-unlimited-gas 56 (bool success,) = _to.call{ value: _amount }(""); 57 success; // Suppress warning; We ignore the low-level call result. 58 emit WithdrewETH(msg.sender, _to, _amount); 59 } 60 61 /// @notice Withdraws full ERC20 balance to the recipient. 62 /// @param _asset ERC20 token to withdraw. 63 /// @param _to Address to receive the ERC20 balance. 64 function withdrawERC20(ERC20 _asset, address _to) external onlyOwner { 65 withdrawERC20(_asset, _to, _asset.balanceOf(address(this))); 66 } 67 68 /// @notice Withdraws partial ERC20 balance to the recipient. 69 /// @param _asset ERC20 token to withdraw. 70 /// @param _to Address to receive the ERC20 balance. 71 /// @param _amount Amount of ERC20 to withdraw. 72 function withdrawERC20(ERC20 _asset, address _to, uint256 _amount) public onlyOwner { 73 // slither-disable-next-line unchecked-transfer 74 _asset.transfer(_to, _amount); 75 // slither-disable-next-line reentrancy-events 76 emit WithdrewERC20(msg.sender, _to, address(_asset), _amount); 77 } 78 79 /// @notice Withdraws ERC721 token to the recipient. 80 /// @param _asset ERC721 token to withdraw. 81 /// @param _to Address to receive the ERC721 token. 82 /// @param _id Token ID of the ERC721 token to withdraw. 83 function withdrawERC721(ERC721 _asset, address _to, uint256 _id) external onlyOwner { 84 _asset.transferFrom(address(this), _to, _id); 85 // slither-disable-next-line reentrancy-events 86 emit WithdrewERC721(msg.sender, _to, address(_asset), _id); 87 } 88 }