Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: OwnableRecipient implementation #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/owr/OwnableRecipient.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.19;

import {Ownable} from "solady/auth/Ownable.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";

import {ERC20} from "solmate/tokens/ERC20.sol";


/// @title OwnableRecipient
/// @author Obol
/// @notice OWR recipient
/// @dev This contract uses token = address(0) to refer to ETH.
contract OwnableRecipient is Ownable {
using SafeTransferLib for address;

/// -----------------------------------------------------------------------
/// errors
/// -----------------------------------------------------------------------

/// @dev thrown if ETH claim fails
error ClaimFailed(uint256 amount);

constructor() {
_initializeOwner(msg.sender);
}

receive() payable external {}

/// @notice Claims tokens from contract
/// @dev uses token = address(0) to refer to ETH.
/// @param withdrawalRecipient Account to receive tokens
function claim(address token, address withdrawalRecipient) onlyOwner external {
if (token == address(0)) {
uint256 amount = address(this).balance;
(bool sent,) = withdrawalRecipient.call{value: amount}("");
if (!sent) revert ClaimFailed(amount);
} else {
uint256 balance = ERC20(token).balanceOf(address(this));
token.safeTransfer(withdrawalRecipient, balance);
}
}
}
36 changes: 36 additions & 0 deletions src/test/owr/OptimisticWithdrawalRecipient.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.19;

import "forge-std/Test.sol";
import {OwnableRecipient} from "src/owr/OwnableRecipient.sol";
import {OptimisticWithdrawalRecipient} from "src/owr/OptimisticWithdrawalRecipient.sol";
import {OptimisticWithdrawalRecipientFactory} from "src/owr/OptimisticWithdrawalRecipientFactory.sol";
import {MockERC20} from "../utils/mocks/MockERC20.sol";
Expand Down Expand Up @@ -33,6 +34,8 @@ contract OptimisticWithdrawalRecipientTest is OWRTestHelper, Test {
address public rewardRecipient;
uint256 internal trancheThreshold;

receive() payable external {}

function setUp() public {
mERC20 = new MockERC20("demo", "DMT", 18);
mERC20.mint(type(uint256).max);
Expand Down Expand Up @@ -71,6 +74,39 @@ contract OptimisticWithdrawalRecipientTest is OWRTestHelper, Test {
owrFactory.createOWRecipient(address(mERC20), address(0), principalRecipient, rewardRecipient, trancheThreshold);
}

function testOwnableRecipient() public {
OwnableRecipient ownableRecipient = new OwnableRecipient();
OptimisticWithdrawalRecipient owrOwnableRecipient =
owrFactory.createOWRecipient(ETH_ADDRESS, recoveryAddress, address(ownableRecipient), rewardRecipient, trancheThreshold);


address(owrOwnableRecipient).safeTransferETH(36 ether);

uint256 principalPayout = 32 ether;
uint256 rewardPayout = 4 ether;

vm.expectEmit(true, true, true, true);
emit DistributeFunds(principalPayout, rewardPayout, 0);
owrOwnableRecipient.distributeFunds();
assertEq(address(owrOwnableRecipient).balance, 0 ether);
assertEq(address(ownableRecipient).balance, 32 ether);
assertEq(rewardRecipient.balance, 4 ether);

ownableRecipient.claim(address(0), address(this));

assertEq(ownableRecipient.owner(), address(this));
ownableRecipient.transferOwnership(principalRecipient);
assertEq(ownableRecipient.owner(), principalRecipient);

ownableRecipient.requestOwnershipHandover();
vm.prank(rewardRecipient);
ownableRecipient.requestOwnershipHandover();

vm.prank(principalRecipient);
ownableRecipient.completeOwnershipHandover(address(this));
assertEq(ownableRecipient.owner(), address(this));
}

function testGetTranches() public {
// eth
(address _principalRecipient, address _rewardRecipient, uint256 wtrancheThreshold) = owrETH.getTranches();
Expand Down
Loading