Skip to content

Commit

Permalink
Refactor MTVTests to improve multiple deposit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasia committed Oct 17, 2024
1 parent 8d5f55f commit e51a7e4
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 103 deletions.
38 changes: 16 additions & 22 deletions packages/contracts/test/src/token/ERC1155/MultiTokenVaultTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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/test/token/ERC1155/IMultiTokenVaultTestBase.t.sol";
import { IMTVTestParamArray } from "@test/test/token/ERC1155/IMTVTestParamArray.t.sol";
import { IMTVTestParams } from "@test/test/token/ERC1155/IMTVTestParams.t.sol";
import { MultiTokenVaultDailyPeriods } from "@test/test/token/ERC1155/MultiTokenVaultDailyPeriods.t.sol";
import { ERC1967Proxy } from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

Expand Down Expand Up @@ -70,7 +70,7 @@ contract MultiTokenVaultTest is IMultiTokenVaultTestBase {

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

testVaultAtPeriod(_charlie, vault, _testParams1);
testVaultAtOffsets(_charlie, vault, _testParams1);
}

function test__MultiTokenVaulTest__RedeemBeforeDepositPeriodReverts() public {
Expand Down Expand Up @@ -188,21 +188,15 @@ contract MultiTokenVaultTest is IMultiTokenVaultTestBase {
uint256 assetToSharesRatio = 2;
uint256 redeemPeriod = 2001;

IMTVTestParamArray testParamsArray = new IMTVTestParamArray();
testParamsArray.addTestParam(
TestParam({ principal: 1001 * _scale, depositPeriod: 1, redeemPeriod: redeemPeriod })
);
testParamsArray.addTestParam(
TestParam({ principal: 2002 * _scale, depositPeriod: 202, redeemPeriod: redeemPeriod })
);
testParamsArray.addTestParam(
TestParam({ principal: 3003 * _scale, depositPeriod: 303, redeemPeriod: redeemPeriod })
);
IMTVTestParams testParamss = new IMTVTestParams();
testParamss.add(TestParam({ principal: 1001 * _scale, depositPeriod: 1, redeemPeriod: redeemPeriod }));
testParamss.add(TestParam({ principal: 2002 * _scale, depositPeriod: 202, redeemPeriod: redeemPeriod }));
testParamss.add(TestParam({ principal: 3003 * _scale, depositPeriod: 303, redeemPeriod: redeemPeriod }));

IMultiTokenVault vault = _createMultiTokenVault(_asset, assetToSharesRatio, 10);

uint256[] memory shares = _testDepositOnly(_alice, vault, testParamsArray.all());
uint256[] memory depositPeriods = testParamsArray.depositPeriods();
uint256[] memory shares = _testDepositOnly(_alice, vault, testParamss);
uint256[] memory depositPeriods = testParamss.depositPeriods();

// ------------------------ batch convert to assets ------------------------
uint256[] memory assets = vault.convertToAssetsForDepositPeriodBatch(shares, depositPeriods, redeemPeriod);
Expand All @@ -225,7 +219,7 @@ contract MultiTokenVaultTest is IMultiTokenVaultTestBase {
);

// ------------------------ batch approvalForAll safeBatchTransferFrom balance ------------------------
uint256[] memory aliceBalances = _testBalanceOfBatch(_alice, vault, testParamsArray, assetToSharesRatio);
uint256[] memory aliceBalances = _testBalanceOfBatch(_alice, vault, testParamss, assetToSharesRatio);

// have alice approve bob for all
vm.prank(_alice);
Expand All @@ -235,22 +229,22 @@ contract MultiTokenVaultTest is IMultiTokenVaultTestBase {
vm.prank(_bob);
vault.safeBatchTransferFrom(_alice, _charlie, depositPeriods, aliceBalances, "");

_testBalanceOfBatch(_charlie, vault, testParamsArray, assetToSharesRatio); // verify bob
_testBalanceOfBatch(_charlie, vault, testParamss, assetToSharesRatio); // verify bob
}

function _testBalanceOfBatch(
address account,
IMultiTokenVault vault,
IMTVTestParamArray testParamsArray,
IMTVTestParams testParams,
uint256 assetToSharesRatio
) internal view returns (uint256[] memory balances_) {
address[] memory accounts = testParamsArray.accountArray(account);
uint256[] memory balances = vault.balanceOfBatch(accounts, testParamsArray.depositPeriods());
address[] memory accounts = testParams.accountArray(account);
uint256[] memory balances = vault.balanceOfBatch(accounts, testParams.depositPeriods());
assertEq(3, balances.length, "balances size incorrect");

assertEq(testParamsArray.get(0).principal / assetToSharesRatio, balances[0], "balance mismatch period 0");
assertEq(testParamsArray.get(1).principal / assetToSharesRatio, balances[1], "balance mismatch period 1");
assertEq(testParamsArray.get(2).principal / assetToSharesRatio, balances[2], "balance mismatch period 2");
assertEq(testParams.get(0).principal / assetToSharesRatio, balances[0], "balance mismatch period 0");
assertEq(testParams.get(1).principal / assetToSharesRatio, balances[1], "balance mismatch period 1");
assertEq(testParams.get(2).principal / assetToSharesRatio, balances[2], "balance mismatch period 2");

return balances;
}
Expand Down
32 changes: 16 additions & 16 deletions packages/contracts/test/src/token/ERC1155/RedeemOptimizerTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import { RedeemOptimizerFIFO } from "@credbull/token/ERC1155/RedeemOptimizerFIFO
import { IMultiTokenVault } from "@credbull/token/ERC1155/IMultiTokenVault.sol";

import { MultiTokenVaultTest } from "@test/src/token/ERC1155/MultiTokenVaultTest.t.sol";
import { IMTVTestParamArray } from "@test/test/token/ERC1155/IMTVTestParamArray.t.sol";
import { IMTVTestParams } from "@test/test/token/ERC1155/IMTVTestParams.t.sol";

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

IMTVTestParamArray private testParamsArr;
IMTVTestParams private testParams;

function setUp() public override {
super.setUp();

testParamsArr = new IMTVTestParamArray();
testParamsArr.addTestParam(_testParams1);
testParamsArr.addTestParam(_testParams2);
testParamsArr.addTestParam(_testParams3);
testParams = new IMTVTestParams();
testParams.add(_testParams1);
testParams.add(_testParams2);
testParams.add(_testParams3);
}

function test__RedeemOptimizerTest__RedeemAllShares() public {
Expand All @@ -31,7 +31,7 @@ contract RedeemOptimizerTest is MultiTokenVaultTest {
IRedeemOptimizer redeemOptimizer =
new RedeemOptimizerFIFO(IRedeemOptimizer.OptimizerBasis.Shares, multiTokenVault.currentPeriodsElapsed());

uint256[] memory depositShares = _testDepositOnly(_alice, multiTokenVault, testParamsArr.all());
uint256[] memory depositShares = _testDepositOnly(_alice, multiTokenVault, testParams);
uint256 totalDepositShares = depositShares[0] + depositShares[1] + depositShares[2];

// warp vault ahead to redeemPeriod
Expand All @@ -42,7 +42,7 @@ contract RedeemOptimizerTest is MultiTokenVaultTest {
(uint256[] memory redeemDepositPeriods, uint256[] memory sharesAtPeriods) =
redeemOptimizer.optimize(multiTokenVault, _alice, totalDepositShares, 0, redeemPeriod); // optimize using share basis. assets not used

assertEq(testParamsArr.depositPeriods(), redeemDepositPeriods, "optimizeRedeem - depositPeriods not correct");
assertEq(testParams.depositPeriods(), redeemDepositPeriods, "optimizeRedeem - depositPeriods not correct");
assertEq(depositShares, sharesAtPeriods, "optimizeRedeem - shares not correct");
}

Expand All @@ -56,9 +56,9 @@ contract RedeemOptimizerTest is MultiTokenVaultTest {
IRedeemOptimizer.OptimizerBasis.AssetsWithReturns, multiTokenVault.currentPeriodsElapsed()
);

uint256[] memory depositShares = _testDepositOnly(_alice, multiTokenVault, testParamsArr.all());
uint256[] memory depositShares = _testDepositOnly(_alice, multiTokenVault, testParams);
uint256[] memory depositAssets = multiTokenVault.convertToAssetsForDepositPeriodBatch(
depositShares, testParamsArr.depositPeriods(), redeemPeriod
depositShares, testParams.depositPeriods(), redeemPeriod
);
assertEq(depositShares.length, depositAssets.length, "mismatch in convertToAssets");
uint256 totalAssets = depositAssets[0] + depositAssets[1] + depositAssets[2];
Expand All @@ -70,7 +70,7 @@ contract RedeemOptimizerTest is MultiTokenVaultTest {
(uint256[] memory withdrawDepositPeriods, uint256[] memory sharesAtPeriods) =
redeemOptimizer.optimize(multiTokenVault, _alice, 0, totalAssets, redeemPeriod); // optimize using asset basis. shares not used

assertEq(testParamsArr.depositPeriods(), withdrawDepositPeriods, "optimizeRedeem - depositPeriods not correct");
assertEq(testParams.depositPeriods(), withdrawDepositPeriods, "optimizeRedeem - depositPeriods not correct");
assertEq(depositShares, sharesAtPeriods, "optimizeRedeem - shares not correct");
}

Expand All @@ -83,7 +83,7 @@ contract RedeemOptimizerTest is MultiTokenVaultTest {
IRedeemOptimizer redeemOptimizer = new RedeemOptimizerFIFO(
IRedeemOptimizer.OptimizerBasis.AssetsWithReturns, multiTokenVault.currentPeriodsElapsed()
);
uint256[] memory depositShares = _testDepositOnly(_alice, multiTokenVault, testParamsArr.all());
uint256[] memory depositShares = _testDepositOnly(_alice, multiTokenVault, testParams);

uint256 sharesToWithdraw = depositShares[0] + depositShares[1] + depositShares[2] - residualShareAmount;

Expand All @@ -101,7 +101,7 @@ contract RedeemOptimizerTest is MultiTokenVaultTest {
"optimizeRedeem partial - wrong shares period 2"
); // reduced by 1 share

assertEq(testParamsArr.depositPeriods(), redeemDepositPeriods, "optimizeRedeem - depositPeriods not correct");
assertEq(testParams.depositPeriods(), redeemDepositPeriods, "optimizeRedeem - depositPeriods not correct");
}

function test__RedeemOptimizerTest__PartialWithdraw() public {
Expand All @@ -114,9 +114,9 @@ contract RedeemOptimizerTest is MultiTokenVaultTest {
IRedeemOptimizer.OptimizerBasis.AssetsWithReturns, multiTokenVault.currentPeriodsElapsed()
);

uint256[] memory depositShares = _testDepositOnly(_alice, multiTokenVault, testParamsArr.all());
uint256[] memory depositShares = _testDepositOnly(_alice, multiTokenVault, testParams);
uint256[] memory depositAssets = multiTokenVault.convertToAssetsForDepositPeriodBatch(
depositShares, testParamsArr.depositPeriods(), redeemPeriod
depositShares, testParams.depositPeriods(), redeemPeriod
);

uint256 residualAssetAmount = multiTokenVault.convertToAssetsForDepositPeriod(
Expand Down Expand Up @@ -145,7 +145,7 @@ contract RedeemOptimizerTest is MultiTokenVaultTest {
);

assertEq(
testParamsArr.depositPeriods().length,
testParams.depositPeriods().length,
actualAssetsAtPeriods.length,
"convertToAssetsForDepositPeriods partial - length incorrect"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.20;
import { LiquidContinuousMultiTokenVault } from "@credbull/yield/LiquidContinuousMultiTokenVault.sol";
import { LiquidContinuousMultiTokenVaultTestBase } from "@test/test/yield/LiquidContinuousMultiTokenVaultTestBase.t.sol";
import { IAccessControl } from "@openzeppelin/contracts/access/IAccessControl.sol";
import { IMTVTestParamArray } from "@test/test/token/ERC1155/IMTVTestParamArray.t.sol";
import { IMTVTestParams } from "@test/test/token/ERC1155/IMTVTestParams.t.sol";

contract LiquidContinuousMultiTokenVaultTest is LiquidContinuousMultiTokenVaultTestBase {
function test__RequestRedeemTest__RedeemAtTenor() public {
Expand Down Expand Up @@ -115,26 +115,26 @@ contract LiquidContinuousMultiTokenVaultTest is LiquidContinuousMultiTokenVaultT
}

function test__LiquidContinuousVaultTest__RedeemMultiPeriodsAllShares() public {
IMTVTestParamArray depositTestParams = new IMTVTestParamArray();
IMTVTestParams depositTestParams = new IMTVTestParams();

// run in some deposits
uint256 baseDepositAmount = 100 * _scale;
for (uint256 i = 0; i <= 10; ++i) {
depositTestParams.addTestParam(
depositTestParams.add(
TestParam({ principal: (baseDepositAmount * i) + 1 * _scale, depositPeriod: i, redeemPeriod: 1000 })
);
}

_testDepositOnly(alice, _liquidVault, depositTestParams.all());
_testDepositOnly(alice, _liquidVault, depositTestParams);

// ------------ requestRedeem #1 -----------
uint256 redeemPeriod1 = 31;
IMTVTestParamArray redeemParams1 = _split(depositTestParams, 0, 2);
IMTVTestParams redeemParams1 = _split(depositTestParams, 0, 2);
_testRequestRedeemMultiDeposit(alice, _liquidVault, redeemParams1, 31);

// ------------ requestRedeem #2 ------------
uint256 redeemPeriod2 = 41;
IMTVTestParamArray redeemParams2 = _split(depositTestParams, 3, 4);
IMTVTestParams redeemParams2 = _split(depositTestParams, 3, 4);
_testRequestRedeemMultiDeposit(alice, _liquidVault, redeemParams2, 41);

// ------------ redeems ------------
Expand All @@ -146,32 +146,32 @@ contract LiquidContinuousMultiTokenVaultTest is LiquidContinuousMultiTokenVaultT
}

function test__LiquidContinuousVaultTest__RedeemMultiPeriodsPartialShares() public {
IMTVTestParamArray depositTestParams = new IMTVTestParamArray();
IMTVTestParams depositTestParams = new IMTVTestParams();

// run in some deposits
uint256 baseDepositAmount = 100 * _scale;
for (uint256 i = 0; i <= 10; ++i) {
depositTestParams.addTestParam(
depositTestParams.add(
TestParam({ principal: (baseDepositAmount * i) + 1 * _scale, depositPeriod: i, redeemPeriod: 1000 })
);
}

_testDepositOnly(alice, _liquidVault, depositTestParams.all());
_testDepositOnly(alice, _liquidVault, depositTestParams);

uint256 partialShares = 1 * _scale;

// ------------ requestRedeem #1 ------------
uint256 redeemPeriod1 = 30;

IMTVTestParamArray redeemParams1 = _split(depositTestParams, 0, 2);
IMTVTestParams redeemParams1 = _split(depositTestParams, 0, 2);
redeemParams1.set(2, partialShares);

_testRequestRedeemMultiDeposit(alice, _liquidVault, redeemParams1, redeemPeriod1);

// ------------ requestRedeem #2 ------------
uint256 redeemPeriod2 = 50;

IMTVTestParamArray redeemParams2 = _split(depositTestParams, 2, 4);
IMTVTestParams redeemParams2 = _split(depositTestParams, 2, 4);
redeemParams2.set(0, depositTestParams.get(2).principal - partialShares);
redeemParams2.set(2, partialShares);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pragma solidity ^0.8.20;

import { IMultiTokenVaultTestBase } from "@test/test/token/ERC1155/IMultiTokenVaultTestBase.t.sol";

contract IMTVTestParamArray {
contract IMTVTestParams {
IMultiTokenVaultTestBase.TestParam[] private _all;

// Function to add testParams to the array
function addTestParam(IMultiTokenVaultTestBase.TestParam memory testParam) public {
function add(IMultiTokenVaultTestBase.TestParam memory testParam) public {
_all.push(testParam);
}

Expand Down
Loading

0 comments on commit e51a7e4

Please sign in to comment.