Skip to content

Commit

Permalink
convert to upgradeable contracts and setting ynETH
Browse files Browse the repository at this point in the history
  • Loading branch information
danoctavian committed Apr 12, 2024
1 parent 6bcf987 commit 61d6a01
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
22 changes: 14 additions & 8 deletions src/PooledDeposits.sol
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
import "./interfaces/IynETH.sol";

contract PooledDeposits {
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract PooledDeposits is Initializable, OwnableUpgradeable {

error DepositsPeriodNotEnded();
error DepositMustBeGreaterThanZero();
error YnETHIsSet();
error YnETHNotSet();

mapping(address => uint256) public balances;
uint256 public depositEndTime;

event DepositReceived(address indexed depositor, uint256 amount);
event DepositsFinalized(address indexed depositor, uint256 totalAmount, uint256 ynETHAmount);

IynETH public ynETH;

constructor(IynETH _ynETH, uint256 _depositEndTime) {
function initialize(address initialOwner) public initializer {
__Ownable_init(initialOwner);
}

function setYnETH(IynETH _ynETH) public onlyOwner {
ynETH = _ynETH;
depositEndTime = _depositEndTime;
}

function deposit() public payable {
if (block.timestamp > depositEndTime) revert DepositsPeriodNotEnded();
if (address(ynETH) != address(0)) revert YnETHIsSet();
if (msg.value == 0) revert DepositMustBeGreaterThanZero();
balances[msg.sender] += msg.value;
emit DepositReceived(msg.sender, msg.value);
}

function finalizeDeposits(address[] calldata depositors) external {
if (block.timestamp <= depositEndTime) revert DepositsPeriodNotEnded();
function finalizeDeposits(address[] calldata depositors) external onlyOwner {
if (address(ynETH) == address(0)) revert YnETHNotSet();

for (uint i = 0; i < depositors.length; i++) {
address depositor = depositors[i];
Expand Down
62 changes: 44 additions & 18 deletions test/integration/PooledDeposits.t.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
pragma solidity ^0.8.24;

import "forge-std/Test.sol";
import "../../src/PooledDeposits.sol";
import "../../src/interfaces/IynETH.sol";
import "test/integration/IntegrationBaseTest.sol";
import "forge-std/console.sol";


contract PooledDepositsTest is IntegrationBaseTest {

function createPooledDeposits() internal returns (PooledDeposits pooledDeposits, address owner) {
PooledDeposits implementation = new PooledDeposits();
bytes memory initData = abi.encodeWithSelector(PooledDeposits.initialize.selector, address(this));
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(address(implementation), address(this), initData);
pooledDeposits = PooledDeposits(payable(address(proxy)));
owner = address(this);
return (pooledDeposits, owner);
}

function testDeposit() public {
// Arrange
PooledDeposits pooledDeposits = new PooledDeposits(IynETH(address(yneth)), block.timestamp + 2 days);
(PooledDeposits pooledDeposits, ) = createPooledDeposits();
uint256 depositAmount = 1 ether;
address depositor = address(this);

Expand All @@ -24,13 +34,17 @@ contract PooledDepositsTest is IntegrationBaseTest {

function testFinalizeDeposits() public {
// Arrange
PooledDeposits pooledDeposits = new PooledDeposits(IynETH(address(yneth)), block.timestamp + 2 days);
(PooledDeposits pooledDeposits, address owner) = createPooledDeposits();
address[] memory depositors = new address[](1);
depositors[0] = address(this);
uint256 depositAmount = 1 ether;
vm.deal(address(this), depositAmount);
pooledDeposits.deposit{value: depositAmount}();

// Set ynETH before finalizing deposits
vm.prank(owner);
pooledDeposits.setYnETH(IynETH(address(yneth)));

// Act
vm.warp(block.timestamp + 3 days); // Move time forward to allow finalizing deposits
pooledDeposits.finalizeDeposits(depositors);
Expand All @@ -47,7 +61,7 @@ contract PooledDepositsTest is IntegrationBaseTest {
vm.assume(baseDepositAmount > 0.01 ether && baseDepositAmount <= 100 ether); // Assuming a reasonable range for deposit amounts

// Arrange
PooledDeposits pooledDeposits = new PooledDeposits(IynETH(address(yneth)), block.timestamp + 2 days);
(PooledDeposits pooledDeposits, address owner) = createPooledDeposits();
address[] memory depositors = new address[](depositorsCount);
uint256 totalDepositAmount = 0;
uint256 varyingDepositAmount = baseDepositAmount;
Expand All @@ -62,6 +76,10 @@ contract PooledDepositsTest is IntegrationBaseTest {
varyingDepositAmount += 1 ether; // Increase the deposit amount by 1 ether for each depositor
}

// Set ynETH before finalizing deposits
vm.prank(owner);
pooledDeposits.setYnETH(IynETH(address(yneth)));

// Act
vm.warp(block.timestamp + 3 days); // Move time forward to allow finalizing deposits
pooledDeposits.finalizeDeposits(depositors);
Expand All @@ -76,33 +94,35 @@ contract PooledDepositsTest is IntegrationBaseTest {
}
}

function testDepositAfterEndTime() public {
function testDepositAfterSettingYnETH() public {
// Arrange
PooledDeposits pooledDeposits = new PooledDeposits(IynETH(address(yneth)), block.timestamp);
(PooledDeposits pooledDeposits, address owner) = createPooledDeposits();
uint256 depositAmount = 1 ether;
vm.deal(address(this), depositAmount);

vm.warp(block.timestamp + 1); // Move time forward to block deposits
// Set ynETH before finalizing deposits
vm.prank(owner);
pooledDeposits.setYnETH(IynETH(address(yneth)));

// Act & Assert
vm.expectRevert(PooledDeposits.DepositsPeriodNotEnded.selector);
vm.expectRevert(PooledDeposits.YnETHIsSet.selector);
pooledDeposits.deposit{value: depositAmount}();
}

function testFinalizeDepositsBeforeEndTime() public {
function testFinalizeDepositsBeforeSettingYnETH() public {
// Arrange
PooledDeposits pooledDeposits = new PooledDeposits(IynETH(address(yneth)), block.timestamp + 1 days);
(PooledDeposits pooledDeposits,) = createPooledDeposits();
address[] memory depositors = new address[](1);
depositors[0] = address(this);

// Act & Assert
vm.expectRevert(PooledDeposits.DepositsPeriodNotEnded.selector);
vm.expectRevert(PooledDeposits.YnETHNotSet.selector);
pooledDeposits.finalizeDeposits(depositors);
}

function testDepositZeroAmount() public {
// Arrange
PooledDeposits pooledDeposits = new PooledDeposits(IynETH(address(yneth)), block.timestamp + 1 days);
(PooledDeposits pooledDeposits,) = createPooledDeposits();

// Act & Assert
vm.expectRevert(PooledDeposits.DepositMustBeGreaterThanZero.selector);
Expand All @@ -111,11 +131,13 @@ contract PooledDepositsTest is IntegrationBaseTest {

function testFinalizeDepositsWithNoDepositors() public {
// Arrange
PooledDeposits pooledDeposits = new PooledDeposits(IynETH(address(yneth)), block.timestamp);
(PooledDeposits pooledDeposits, address owner) = createPooledDeposits();
address[] memory depositors = new address[](0);

// Act
vm.warp(block.timestamp + 1 days); // Move time forward to allow finalizing deposits
// Set ynETH before finalizing deposits
vm.prank(owner);
pooledDeposits.setYnETH(IynETH(address(yneth)));

pooledDeposits.finalizeDeposits(depositors);

// Assert
Expand All @@ -127,7 +149,7 @@ contract PooledDepositsTest is IntegrationBaseTest {
vm.assume(depositAmount > 0 && depositAmount <= 100 ether);

// Arrange
PooledDeposits pooledDeposits = new PooledDeposits(IynETH(address(yneth)), block.timestamp + 1 days);
(PooledDeposits pooledDeposits,) = createPooledDeposits();
vm.deal(address(this), depositAmount);

// Act
Expand All @@ -140,13 +162,17 @@ contract PooledDepositsTest is IntegrationBaseTest {

function testFinalizeDepositsMultipleTimesForSameUser() public {
// Arrange
PooledDeposits pooledDeposits = new PooledDeposits(IynETH(address(yneth)), block.timestamp + 1 days);
(PooledDeposits pooledDeposits, address owner) = createPooledDeposits();
address[] memory depositors = new address[](1);
depositors[0] = address(this);
uint256 depositAmount = 1 ether;
vm.deal(address(this), depositAmount);
pooledDeposits.deposit{value: depositAmount}();

// Set ynETH before finalizing deposits
vm.prank(owner);
pooledDeposits.setYnETH(IynETH(address(yneth)));

// Act
vm.warp(block.timestamp + 1 days + 1); // Move time forward to allow finalizing deposits
pooledDeposits.finalizeDeposits(depositors);
Expand Down

0 comments on commit 61d6a01

Please sign in to comment.