1
+ import "module-alias/register" ;
2
+
3
+ import { HardhatRuntimeEnvironment as HRE } from "hardhat/types" ;
4
+ import { DeployFunction } from "hardhat-deploy/types" ;
5
+
6
+ import {
7
+ prepareDeployment ,
8
+ findDependency ,
9
+ getContractAddress ,
10
+ getCurrentStage ,
11
+ saveContractDeployment ,
12
+ stageAlreadyFinished ,
13
+ trackFinishedStage ,
14
+ writeTransactionToOutputs ,
15
+ getAccounts ,
16
+ } from "@utils/index" ;
17
+
18
+ import { Account } from "@utils/types" ;
19
+ import { InstanceGetter } from "@utils/instanceGetter" ;
20
+
21
+ import { DEPENDENCY } from "../deployments/utils/dependencies" ;
22
+ import { CONTRACT_NAMES } from "../deployments/constants/001_delegated_manager_system" ;
23
+
24
+ const {
25
+ MULTI_SIG_OWNER ,
26
+ CONTROLLER ,
27
+ SET_TOKEN_CREATOR ,
28
+ DEBT_ISSUANCE_MODULE_V2 ,
29
+ STREAMING_FEE_MODULE ,
30
+ TRADE_MODULE ,
31
+ } = DEPENDENCY ;
32
+
33
+ let owner : Account ;
34
+ let instanceGetter : InstanceGetter ;
35
+
36
+ const CURRENT_STAGE = getCurrentStage ( __filename ) ;
37
+
38
+ const func : DeployFunction = trackFinishedStage ( CURRENT_STAGE , async function ( bre : HRE ) {
39
+ const {
40
+ deploy,
41
+ deployer,
42
+ rawTx,
43
+ networkConstant
44
+ } = await prepareDeployment ( bre ) ;
45
+
46
+ [ owner ] = await getAccounts ( ) ;
47
+ instanceGetter = new InstanceGetter ( owner . wallet ) ;
48
+
49
+ await deployManagerCore ( ) ;
50
+ const managerCoreAddress = await getContractAddress ( CONTRACT_NAMES . MANAGER_CORE ) ;
51
+
52
+ const controllerAddress = await findDependency ( CONTROLLER ) ;
53
+ const setTokenCreatorAddress = await findDependency ( SET_TOKEN_CREATOR ) ;
54
+ await deployDelegatedManagerFactory ( ) ;
55
+ const delegatedManagerFactoryAddress = await getContractAddress ( CONTRACT_NAMES . DELEGATED_MANAGER_FACTORY ) ;
56
+
57
+ const issuanceModuleAddress = await findDependency ( DEBT_ISSUANCE_MODULE_V2 ) ;
58
+ await deployIssuanceExtension ( ) ;
59
+ const issuanceExtensionAddress = await getContractAddress ( CONTRACT_NAMES . ISSUANCE_EXTENSION ) ;
60
+
61
+ const streamingFeeModuleAddress = await findDependency ( STREAMING_FEE_MODULE ) ;
62
+ await deployStreamingFeeSplitExtension ( ) ;
63
+ const streamingFeeSplitExtensionAddress = await getContractAddress ( CONTRACT_NAMES . STREAMING_FEE_SPLIT_EXTENSION ) ;
64
+
65
+ const tradeModuleAddress = await findDependency ( TRADE_MODULE ) ;
66
+ await deployTradeExtension ( ) ;
67
+ const tradeExtensionAddress = await getContractAddress ( CONTRACT_NAMES . TRADE_EXTENSION ) ;
68
+
69
+ await initializeManagerCore ( ) ;
70
+
71
+ await transferManagerCoreOwnershipToMultisig ( ) ;
72
+
73
+ //
74
+ // Helper Functions
75
+ //
76
+
77
+ async function deployManagerCore ( ) : Promise < void > {
78
+ const checkManagerCoreAddress = await getContractAddress ( CONTRACT_NAMES . MANAGER_CORE ) ;
79
+ if ( checkManagerCoreAddress === "" ) {
80
+ const managerCoreDeploy = await deploy (
81
+ CONTRACT_NAMES . MANAGER_CORE ,
82
+ { from : deployer , log : true }
83
+ ) ;
84
+ managerCoreDeploy . receipt && await saveContractDeployment ( {
85
+ name : CONTRACT_NAMES . MANAGER_CORE ,
86
+ contractAddress : managerCoreDeploy . address ,
87
+ id : managerCoreDeploy . receipt . transactionHash ,
88
+ description : `Deployed ${ CONTRACT_NAMES . MANAGER_CORE } `
89
+ } ) ;
90
+ }
91
+ }
92
+
93
+ async function deployDelegatedManagerFactory ( ) : Promise < void > {
94
+ const checkDelegatedManagerFactoryAddress = await getContractAddress ( CONTRACT_NAMES . DELEGATED_MANAGER_FACTORY ) ;
95
+ if ( checkDelegatedManagerFactoryAddress === "" ) {
96
+ const constructorArgs = [ managerCoreAddress , controllerAddress , setTokenCreatorAddress ] ;
97
+ const delegatedManagerFactoryDeploy = await deploy (
98
+ CONTRACT_NAMES . DELEGATED_MANAGER_FACTORY ,
99
+ { from : deployer , args : constructorArgs , log : true }
100
+ ) ;
101
+ delegatedManagerFactoryDeploy . receipt && await saveContractDeployment ( {
102
+ name : CONTRACT_NAMES . DELEGATED_MANAGER_FACTORY ,
103
+ contractAddress : delegatedManagerFactoryDeploy . address ,
104
+ id : delegatedManagerFactoryDeploy . receipt . transactionHash ,
105
+ description : `Deployed ${ CONTRACT_NAMES . DELEGATED_MANAGER_FACTORY } ` ,
106
+ constructorArgs,
107
+ } ) ;
108
+ }
109
+ }
110
+
111
+ async function deployIssuanceExtension ( ) : Promise < void > {
112
+ const checkIssuanceExtensionAddress = await getContractAddress ( CONTRACT_NAMES . ISSUANCE_EXTENSION ) ;
113
+ if ( checkIssuanceExtensionAddress === "" ) {
114
+ const constructorArgs = [ managerCoreAddress , issuanceModuleAddress ] ;
115
+ const issuanceExtensionDeploy = await deploy (
116
+ CONTRACT_NAMES . ISSUANCE_EXTENSION ,
117
+ { from : deployer , args : constructorArgs , log : true }
118
+ ) ;
119
+ issuanceExtensionDeploy . receipt && await saveContractDeployment ( {
120
+ name : CONTRACT_NAMES . ISSUANCE_EXTENSION ,
121
+ contractAddress : issuanceExtensionDeploy . address ,
122
+ id : issuanceExtensionDeploy . receipt . transactionHash ,
123
+ description : `Deployed ${ CONTRACT_NAMES . ISSUANCE_EXTENSION } ` ,
124
+ constructorArgs,
125
+ } ) ;
126
+ }
127
+ }
128
+
129
+ async function deployStreamingFeeSplitExtension ( ) : Promise < void > {
130
+ const checkStreamingFeeSplitExtensionAddress = await getContractAddress ( CONTRACT_NAMES . STREAMING_FEE_SPLIT_EXTENSION ) ;
131
+ if ( checkStreamingFeeSplitExtensionAddress === "" ) {
132
+ const constructorArgs = [ managerCoreAddress , streamingFeeModuleAddress ] ;
133
+ const streamingFeeSplitExtensionDeploy = await deploy (
134
+ CONTRACT_NAMES . STREAMING_FEE_SPLIT_EXTENSION ,
135
+ { from : deployer , args : constructorArgs , log : true }
136
+ ) ;
137
+ streamingFeeSplitExtensionDeploy . receipt && await saveContractDeployment ( {
138
+ name : CONTRACT_NAMES . STREAMING_FEE_SPLIT_EXTENSION ,
139
+ contractAddress : streamingFeeSplitExtensionDeploy . address ,
140
+ id : streamingFeeSplitExtensionDeploy . receipt . transactionHash ,
141
+ description : `Deployed ${ CONTRACT_NAMES . STREAMING_FEE_SPLIT_EXTENSION } ` ,
142
+ constructorArgs,
143
+ } ) ;
144
+ }
145
+ }
146
+
147
+ async function deployTradeExtension ( ) : Promise < void > {
148
+ const checkTradeExtensionAddress = await getContractAddress ( CONTRACT_NAMES . TRADE_EXTENSION ) ;
149
+ if ( checkTradeExtensionAddress === "" ) {
150
+ const constructorArgs = [ managerCoreAddress , tradeModuleAddress ] ;
151
+ const tradeExtensionDeploy = await deploy (
152
+ CONTRACT_NAMES . TRADE_EXTENSION ,
153
+ { from : deployer , args : constructorArgs , log : true }
154
+ ) ;
155
+ tradeExtensionDeploy . receipt && await saveContractDeployment ( {
156
+ name : CONTRACT_NAMES . TRADE_EXTENSION ,
157
+ contractAddress : tradeExtensionDeploy . address ,
158
+ id : tradeExtensionDeploy . receipt . transactionHash ,
159
+ description : `Deployed ${ CONTRACT_NAMES . TRADE_EXTENSION } ` ,
160
+ constructorArgs,
161
+ } ) ;
162
+ }
163
+ }
164
+
165
+ async function initializeManagerCore ( ) : Promise < void > {
166
+ const managerCoreInstance = await instanceGetter . getManagerCore ( managerCoreAddress ) ;
167
+ if ( ! await managerCoreInstance . isInitialized ( ) ) {
168
+ const initializeData = managerCoreInstance . interface . encodeFunctionData (
169
+ "initialize" ,
170
+ [ [ issuanceExtensionAddress , streamingFeeSplitExtensionAddress , tradeExtensionAddress ] , [ delegatedManagerFactoryAddress ] ]
171
+ ) ;
172
+ const description = "Initialized ManagerCore with DelegatedManagerFactory, IssuanceExtension, StreamingFeeSplitExtension, and TradeExtension" ;
173
+
174
+ const initializeTransaction : any = await rawTx ( {
175
+ from : deployer ,
176
+ to : managerCoreAddress ,
177
+ data : initializeData ,
178
+ log : true ,
179
+ } ) ;
180
+ await writeTransactionToOutputs ( initializeTransaction . transactionHash , description ) ;
181
+ }
182
+ }
183
+
184
+ async function transferManagerCoreOwnershipToMultisig ( ) : Promise < void > {
185
+ if ( networkConstant === "production" ) {
186
+ const multisig = await findDependency ( MULTI_SIG_OWNER ) ;
187
+ const managerCoreInstance = await instanceGetter . getManagerCore ( managerCoreAddress ) ;
188
+
189
+ const managerCoreOwner = await managerCoreInstance . owner ( ) ;
190
+ if ( multisig !== "" && managerCoreOwner === deployer ) {
191
+ const transferOwnershipData = managerCoreInstance . interface . encodeFunctionData (
192
+ "transferOwnership" ,
193
+ [ multisig ]
194
+ ) ;
195
+
196
+ const transferOwnershipTransaction : any = await rawTx ( {
197
+ from : deployer ,
198
+ to : managerCoreAddress ,
199
+ data : transferOwnershipData ,
200
+ log : true ,
201
+ } ) ;
202
+
203
+ await writeTransactionToOutputs (
204
+ transferOwnershipTransaction . transactionHash ,
205
+ "Transfer ManagerCore ownership to Multisig"
206
+ ) ;
207
+ }
208
+ }
209
+ }
210
+ } ) ;
211
+
212
+ func . skip = stageAlreadyFinished ( CURRENT_STAGE ) ;
213
+
214
+ export default func ;
0 commit comments