-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7616ef6
commit a4f0d6e
Showing
4 changed files
with
141 additions
and
103 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
pragma solidity 0.8.19; | ||
|
||
import {ERC20} from "solmate/tokens/ERC20.sol"; | ||
import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol"; | ||
import {Clone} from "solady/utils/Clone.sol"; | ||
|
||
abstract contract BaseSplit is Clone { | ||
error Invalid_Address(); | ||
error Invalid_FeeShare(); | ||
|
||
/// ----------------------------------------------------------------------- | ||
/// libraries | ||
/// ----------------------------------------------------------------------- | ||
using SafeTransferLib for ERC20; | ||
using SafeTransferLib for address; | ||
|
||
address internal constant ETH_ADDRESS = address(0); | ||
uint256 internal constant PERCENTAGE_SCALE = 1e5; | ||
|
||
/// @notice fee share | ||
uint256 public immutable feeShare; | ||
|
||
/// @notice fee address | ||
address public immutable feeRecipient; | ||
|
||
// withdrawal (adress, 20 bytes) | ||
// 0; first item | ||
uint256 internal constant WITHDRAWAL_ADDRESS_OFFSET = 0; | ||
// 20 = withdrawalAddress_offset (0) + withdrawalAddress_size (address, 20 bytes) | ||
uint256 internal constant TOKEN_ADDRESS_OFFSET = 20; | ||
|
||
constructor(address _feeRecipient, uint256 _feeShare) { | ||
if (_feeShare == 0 || _feeShare > PERCENTAGE_SCALE) revert Invalid_FeeShare(); | ||
|
||
feeShare = _feeShare; | ||
feeRecipient = _feeRecipient; | ||
} | ||
|
||
/// ----------------------------------------------------------------------- | ||
/// View | ||
/// ----------------------------------------------------------------------- | ||
|
||
/// Address to send funds to to | ||
/// @dev equivalent to address public immutable withdrawalAddress | ||
function withdrawalAddress() public pure returns (address) { | ||
return _getArgAddress(WITHDRAWAL_ADDRESS_OFFSET); | ||
} | ||
|
||
/// Token addresss | ||
/// @dev equivalent to address public immutable token | ||
function token() public pure virtual returns (address) { | ||
return _getArgAddress(TOKEN_ADDRESS_OFFSET); | ||
} | ||
|
||
/// ----------------------------------------------------------------------- | ||
/// Public | ||
/// ----------------------------------------------------------------------- | ||
|
||
/// @notice Rescue stuck ETH and tokens | ||
/// Uses token == address(0) to represent ETH | ||
/// @return balance Amount of ETH or tokens rescued | ||
function rescueFunds(address tokenAddress) external virtual returns (uint256 balance) { | ||
_beforeRescueFunds(tokenAddress); | ||
|
||
if (tokenAddress == ETH_ADDRESS) { | ||
balance = address(this).balance; | ||
if (balance > 0) withdrawalAddress().safeTransferETH(balance); | ||
} else { | ||
balance = ERC20(tokenAddress).balanceOf(address(this)); | ||
if (balance > 0) ERC20(tokenAddress).safeTransfer(withdrawalAddress(), balance); | ||
} | ||
} | ||
|
||
/// @notice distribute funds to withdrawal address | ||
function distribute() external virtual { | ||
uint256 amount = 0; | ||
address tokenAddress = token(); | ||
|
||
if (tokenAddress == ETH_ADDRESS) { | ||
amount = address(this).balance; | ||
} else { | ||
amount = ERC20(tokenAddress).balanceOf(address(this)); | ||
} | ||
|
||
if (feeShare > 0) { | ||
//TODO check if feeShares > 0 | ||
uint256 fee = (amount * feeShare) / PERCENTAGE_SCALE; | ||
_transfer(tokenAddress, feeRecipient, fee); | ||
_transfer(tokenAddress, withdrawalAddress(), amount -= fee); | ||
} else { | ||
_transfer(tokenAddress, withdrawalAddress(), amount); | ||
} | ||
|
||
} | ||
|
||
/// ----------------------------------------------------------------------- | ||
/// Internal | ||
/// ----------------------------------------------------------------------- | ||
|
||
function _beforeRescueFunds(address tokenAddress) internal virtual {} | ||
|
||
function _transfer( | ||
address tokenAddress, | ||
address receiver, | ||
uint256 amount | ||
) internal { | ||
if (tokenAddress == ETH_ADDRESS) { | ||
receiver.safeTransferETH(amount); | ||
} else { | ||
ERC20(tokenAddress).safeTransfer(receiver, amount); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
pragma solidity 0.8.19; | ||
|
||
|
||
abstract contract BaseSplitFactory { | ||
/// ----------------------------------------------------------------------- | ||
/// errors | ||
/// ----------------------------------------------------------------------- | ||
/// @dev Invalid address | ||
error Invalid_Address(); | ||
|
||
/// ----------------------------------------------------------------------- | ||
/// events | ||
/// ----------------------------------------------------------------------- | ||
/// Emitted on createCollector | ||
event CreateSplit(address token, address withdrawalAddress); | ||
|
||
function createCollector(address token, address withdrawalAddress) external virtual returns (address collector); | ||
} |
This file contains 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
This file contains 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