-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlexiblePortfolioFactory.sol
104 lines (94 loc) · 4.65 KB
/
FlexiblePortfolioFactory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {IFlexiblePortfolio} from "./interfaces/IFlexiblePortfolio.sol";
import {IDebtInstrument} from "./interfaces/IDebtInstrument.sol";
import {IValuationStrategy} from "./interfaces/IValuationStrategy.sol";
import {ITransferController} from "./interfaces/ITransferController.sol";
import {IDepositController} from "./interfaces/IDepositController.sol";
import {IWithdrawController} from "./interfaces/IWithdrawController.sol";
import {IFeeStrategy} from "./interfaces/IFeeStrategy.sol";
import {PortfolioFactory} from "./PortfolioFactory.sol";
contract FlexiblePortfolioFactory is PortfolioFactory {
using Address for address;
struct ControllersData {
/// @dev Implementation of the controller applied when calling deposit-related functions
address depositControllerImplementation;
/// @dev Encoded args with initialize method selector from deposit controller
bytes depositControllerInitData;
/// @dev Implementation of the controller applied when calling withdraw-related functions
address withdrawControllerImplementation;
/// @dev Encoded args with initialize method selector from withdraw controller
bytes withdrawControllerInitData;
/// @dev Implementation of the controller used when calling transfer-related functions
address transferControllerImplementation;
/// @dev Encoded args with initialize method selector from transfer controller
bytes transferControllerInitData;
/// @dev Address of valuation strategy directly set as valuationStrategy in the portfolio
address valuationStrategy;
/// @dev Implementation of the strategy used when calling fee-related functions
address feeStrategyImplementation;
/// @dev Encoded args with initialize method selector from fee strategy
bytes feeStrategyInitData;
}
function createPortfolio(
IERC20Metadata _asset,
uint256 _duration,
uint256 _maxSize,
ControllersData memory controllersData,
IDebtInstrument[] calldata _allowedInstruments,
IFlexiblePortfolio.ERC20Metadata calldata tokenMetadata
) public virtual onlyRole(MANAGER_ROLE) {
IFlexiblePortfolio.Controllers memory controllers = setupControllers(controllersData);
bytes memory initCalldata = setupPortfolioInitData(
_asset,
_duration,
_maxSize,
controllers,
_allowedInstruments,
tokenMetadata
);
_deployPortfolio(initCalldata);
}
function setupPortfolioInitData(
IERC20Metadata _asset,
uint256 _duration,
uint256 _maxSize,
IFlexiblePortfolio.Controllers memory controllers,
IDebtInstrument[] calldata _allowedInstruments,
IFlexiblePortfolio.ERC20Metadata calldata tokenMetadata
) internal view virtual returns (bytes memory) {
return
abi.encodeWithSelector(
IFlexiblePortfolio.initialize.selector,
protocolConfig,
_duration,
_asset,
msg.sender,
_maxSize,
controllers,
_allowedInstruments,
tokenMetadata
);
}
function setupControllers(ControllersData memory controllersData) internal returns (IFlexiblePortfolio.Controllers memory) {
address depositController = Clones.clone(controllersData.depositControllerImplementation);
depositController.functionCall(controllersData.depositControllerInitData);
address withdrawController = Clones.clone(controllersData.withdrawControllerImplementation);
withdrawController.functionCall(controllersData.withdrawControllerInitData);
address transferController = Clones.clone(controllersData.transferControllerImplementation);
transferController.functionCall(controllersData.transferControllerInitData);
address feeStrategy = Clones.clone(controllersData.feeStrategyImplementation);
feeStrategy.functionCall(controllersData.feeStrategyInitData);
return
IFlexiblePortfolio.Controllers(
IDepositController(depositController),
IWithdrawController(withdrawController),
ITransferController(transferController),
IValuationStrategy(controllersData.valuationStrategy),
IFeeStrategy(feeStrategy)
);
}
}