Skip to content

Commit

Permalink
Refactored test to reduce inherited owner and user addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasia committed Oct 5, 2024
1 parent df5456f commit e5be74c
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract RedeemOptimizerFIFO is IRedeemOptimizer {
AssetsWithReturns
}

uint256 public immutable START_DEPOSIT_PERIOD = 0;
uint256 public immutable START_DEPOSIT_PERIOD;

constructor(uint256 startDepositPeriod) {
START_DEPOSIT_PERIOD = startDepositPeriod;
Expand Down
27 changes: 11 additions & 16 deletions packages/contracts/src/yield/LiquidContinuousMultiTokenVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/I
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IERC6372 } from "@openzeppelin/contracts/interfaces/IERC6372.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { AccessControlUpgradeable } from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import { UUPSUpgradeable } from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import { Initializable } from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import { AccessControlEnumerableUpgradeable } from
"@openzeppelin/contracts-upgradeable/access/extensions/AccessControlEnumerableUpgradeable.sol";

/**
* @title LiquidContinuousMultiTokenVault
Expand All @@ -38,7 +39,7 @@ contract LiquidContinuousMultiTokenVault is
IComponentToken,
TimelockAsyncUnlock,
TripleRateContext,
AccessControlUpgradeable,
AccessControlEnumerableUpgradeable,
IERC6372
{
using SafeERC20 for IERC20;
Expand Down Expand Up @@ -81,7 +82,9 @@ contract LiquidContinuousMultiTokenVault is
__TimelockAsyncUnlock_init(vaultParams.redeemNoticePeriod);
__TripleRateContext_init(vaultParams.contextParams);

_initVaultAuth(vaultParams.vaultAuth);
_initRole("owner", DEFAULT_ADMIN_ROLE, vaultParams.vaultAuth.owner);
_initRole("operator", OPERATOR_ROLE, vaultParams.vaultAuth.operator);
_initRole("upgrader", UPGRADER_ROLE, vaultParams.vaultAuth.upgrader);

_yieldStrategy = vaultParams.yieldStrategy;
_redeemOptimizer = vaultParams.redeemOptimizer;
Expand All @@ -92,20 +95,12 @@ contract LiquidContinuousMultiTokenVault is
}
}

function _initVaultAuth(VaultAuth memory vaultAuth) private {
if (vaultAuth.owner == address(0)) {
revert LiquidContinuousMultiTokenVault__InvalidAuthAddress("owner", vaultAuth.owner);
}
if (vaultAuth.operator == address(0)) {
revert LiquidContinuousMultiTokenVault__InvalidAuthAddress("operator", vaultAuth.operator);
}
if (vaultAuth.upgrader == address(0)) {
revert LiquidContinuousMultiTokenVault__InvalidAuthAddress("upgrader", vaultAuth.upgrader);
function _initRole(string memory roleName, bytes32 role, address account) private {
if (account == address(0)) {
revert LiquidContinuousMultiTokenVault__InvalidAuthAddress(roleName, account);
}

_grantRole(DEFAULT_ADMIN_ROLE, vaultAuth.owner);
_grantRole(OPERATOR_ROLE, vaultAuth.operator);
_grantRole(UPGRADER_ROLE, vaultAuth.upgrader);
_grantRole(role, account);
}

// solhint-disable-next-line no-empty-blocks
Expand Down Expand Up @@ -321,7 +316,7 @@ contract LiquidContinuousMultiTokenVault is
function supportsInterface(bytes4 interfaceId)
public
view
override(MultiTokenVault, AccessControlUpgradeable)
override(MultiTokenVault, AccessControlEnumerableUpgradeable)
returns (bool)
{
return MultiTokenVault.supportsInterface(interfaceId);
Expand Down
25 changes: 15 additions & 10 deletions packages/contracts/test/src/token/ERC1155/MultiTokenVaultTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.20;

import { IMultiTokenVault } from "@credbull/token/ERC1155/IMultiTokenVault.sol";
import { MultiTokenVault } from "@credbull/token/ERC1155/MultiTokenVault.sol";
import { IMultiTokenVaultTestBase } from "@test/src/token/ERC1155/IMultiTokenVaultTestBase.t.sol";
import { IMultiTokenVaultTestBase } from "@test/test/token/ERC1155/IMultiTokenVaultTestBase.t.sol";
import { MultiTokenVaultDailyPeriods } from "@test/test/token/ERC1155/MultiTokenVaultDailyPeriods.t.sol";
import { ERC1967Proxy } from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

Expand All @@ -14,6 +14,9 @@ import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/I
contract MultiTokenVaultTest is IMultiTokenVaultTestBase {
IERC20Metadata internal asset;

address private owner = makeAddr("owner");
address private alice = makeAddr("alice");

IMultiTokenVaultTestParams internal deposit1TestParams;
IMultiTokenVaultTestParams internal deposit2TestParams;
IMultiTokenVaultTestParams internal deposit3TestParams;
Expand All @@ -26,21 +29,12 @@ contract MultiTokenVaultTest is IMultiTokenVaultTestBase {

SCALE = 10 ** asset.decimals();
_transferAndAssert(asset, owner, alice, 100_000 * SCALE);
_transferAndAssert(asset, owner, bob, 100_000 * SCALE);

deposit1TestParams = IMultiTokenVaultTestParams({ principal: 500 * SCALE, depositPeriod: 10, redeemPeriod: 21 });
deposit2TestParams = IMultiTokenVaultTestParams({ principal: 300 * SCALE, depositPeriod: 15, redeemPeriod: 17 });
deposit3TestParams = IMultiTokenVaultTestParams({ principal: 700 * SCALE, depositPeriod: 30, redeemPeriod: 55 });
}

function test__MultiTokenVaulTest__Period10() public {
uint256 assetToSharesRatio = 1;

MultiTokenVault vault = _createMultiTokenVault(asset, assetToSharesRatio, 10);

_testVaultAtPeriod(bob, vault, deposit1TestParams);
}

function test__MultiTokenVaulTest__SimpleDeposit() public {
uint256 assetToSharesRatio = 1;

Expand All @@ -67,6 +61,17 @@ contract MultiTokenVaultTest is IMultiTokenVaultTestBase {
testVaultAtPeriods(alice, vault, deposit1TestParams);
}

function test__MultiTokenVaulTest__Period10() public {
uint256 assetToSharesRatio = 1;
address charlie = makeAddr("charlie");

_transferAndAssert(asset, owner, charlie, 100_000 * SCALE);

MultiTokenVault vault = _createMultiTokenVault(asset, assetToSharesRatio, 10);

_testVaultAtPeriod(charlie, vault, deposit1TestParams);
}

// Scenario: Calculating returns for a standard investment
function test__MultiTokenVaulTest__MultipleDeposits() public {
uint256 assetToSharesRatio = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { IMultiTokenVault } from "@credbull/token/ERC1155/IMultiTokenVault.sol";
import { MultiTokenVaultTest } from "@test/src/token/ERC1155/MultiTokenVaultTest.t.sol";

contract RedeemOptimizerTest is MultiTokenVaultTest {
address private owner = makeAddr("owner");
address private alice = makeAddr("alice");

function setUp() public override {
super.setUp();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ contract LiquidContinuousMultiTokenVaultTest is LiquidContinuousMultiTokenVaultT
// ---------------- sell (redeem) ----------------
uint256 expectedYield = _expectedReturns(sharesAmount, liquidVault, testParams);
assertEq(33_333333, expectedYield, "expected returns incorrect");
vm.prank(owner);
_transferAndAssert(_asset, owner, address(liquidVault), expectedYield); // fund the vault to cover redeem
_transferFromTokenOwner(_asset, address(liquidVault), expectedYield); // fund the vault to cover redeem

_warpToPeriod(liquidVault, testParams.redeemPeriod);

Expand Down
Loading

0 comments on commit e5be74c

Please sign in to comment.