@@ -44,6 +44,10 @@ const ContractABIPaths = {
4444 ChainBridgeBatchRebaseReport : 'contracts/_utilities' ,
4545} ;
4646
47+ const sleep = ( sec ) => {
48+ return new Promise ( ( resolve ) => setTimeout ( resolve , sec * 1000 ) ) ;
49+ } ;
50+
4751const getCompiledContractFactory = ( ethers , contract ) => {
4852 return ethers . getContractFactory (
4953 `${ ContractABIPaths [ contract ] } /${ contract } .sol:${ contract } ` ,
@@ -100,6 +104,63 @@ const deployProxyContract = async (
100104 return contract ;
101105} ;
102106
107+ const upgradeProxyContract = async (
108+ ethers ,
109+ network ,
110+ contractName ,
111+ deployedContractRef ,
112+ signer ,
113+ txParams ,
114+ force = false ,
115+ ) => {
116+ const proxyAdmin = await getDeployedContractInstance (
117+ network ,
118+ 'proxyAdmin' ,
119+ ethers . provider ,
120+ ) ;
121+
122+ const proxy = await getDeployedContractInstance (
123+ network ,
124+ deployedContractRef ,
125+ ethers . provider ,
126+ ) ;
127+
128+ const currentImplAddr = await proxyAdmin . getProxyImplementation ( proxy . address ) ;
129+ console . log ( `Current implementation for ${ contractName } is at` , currentImplAddr ) ;
130+
131+ // deploy new implementation
132+ let newImpl ;
133+ if ( ! force ) {
134+ const Factory = await getCompiledContractFactory ( ethers , contractName ) ;
135+ newImpl = await upgrades . prepareUpgrade ( proxy . address , Factory ) ;
136+ } else {
137+ console . log ( `CAUTION: Skpping storage layout verification!` )
138+ console . log ( `CANCEL NOW to stop, this action is not reversable` )
139+ await sleep ( 10 ) ;
140+ newImpl = await deployContract (
141+ ethers ,
142+ contractName ,
143+ signer ,
144+ [ ] ,
145+ txParams ,
146+ ) ;
147+ }
148+ console . log ( `New implementation for ${ contractName } is at` , newImpl . address ) ;
149+
150+ if ( ( await proxyAdmin . owner ( ) ) == ( await signer . getAddress ( ) ) ) {
151+ await proxyAdmin
152+ . connect ( signer )
153+ . upgrade ( proxy . address , newImpl . address , txParams ) ;
154+ } else {
155+ console . log ( 'Signer not proxy onwer, cant upgrade' ) ;
156+ console . log (
157+ `Execute proxyAdmin.upgrade(${ proxy . address } , ${ newImpl . address } )` ,
158+ ) ;
159+ }
160+
161+ return newImpl ;
162+ } ;
163+
103164const getDeployedContractInstance = async ( network , contractName , provider ) => {
104165 const contractData = await readContractDeploymentData ( network , contractName ) ;
105166 return new ethers . Contract ( contractData . address , contractData . abi , provider ) ;
@@ -188,10 +249,6 @@ const filterContractEvents = async (
188249 endBlock = endBlock || ( await provider . getBlockNumber ( ) ) ;
189250 const freq = timeFrameSec * BLOCKS_PER_SEC ;
190251
191- const sleep = ( sec ) => {
192- return new Promise ( ( resolve ) => setTimeout ( resolve , sec * 1000 ) ) ;
193- } ;
194-
195252 console . log ( address , event , startBlock , endBlock , timeFrameSec ) ;
196253
197254 let logs = [ ] ;
@@ -250,6 +307,7 @@ module.exports = {
250307 deployContract,
251308 deployProxyAdminContract,
252309 deployProxyContract,
310+ upgradeProxyContract,
253311
254312 filterContractEvents,
255313} ;
0 commit comments