Created
February 4, 2022 04:31
-
-
Save z0r0z/2132ac653fe42a1c4bb1d9eeab620e4a to your computer and use it in GitHub Desktop.
simple ETH receiver that automates fees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: GPL-3.0-or-later | |
| pragma solidity >=0.8.4; | |
| contract ETHfeed { | |
| uint256 feeRate; | |
| address feeReceiver; | |
| constructor(uint256 feeRate_) { | |
| feeRate = feeRate_; | |
| } | |
| receive() external payable { | |
| deposit(); | |
| } | |
| function deposit() public payable { | |
| uint256 fee = msg.value / feeRate; | |
| _safeTransferETH(feeReceiver, fee); | |
| } | |
| /// @dev ETH tranfer helper - optimized in assembly: | |
| function _safeTransferETH(address to, uint256 amount) internal { | |
| bool callStatus; | |
| assembly { | |
| // transfer the ETH and store if it succeeded or not | |
| callStatus := call(gas(), to, amount, 0, 0, 0, 0) | |
| } | |
| require(callStatus, 'ETH_TRANSFER_FAILED'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment