@@ -3,6 +3,7 @@ import { DeployOptions, Deployment } from "hardhat-deploy/types";
3
3
import { promises as fs } from "fs" ;
4
4
import path from "path" ;
5
5
import { format } from "prettier" ;
6
+ import { keccak256 , toUtf8Bytes , defaultAbiCoder } from "ethers/lib/utils" ;
6
7
import deployConfig from "../deployConfig" ;
7
8
8
9
export const getConfig = async ( hre : HardhatRuntimeEnvironment ) => {
@@ -99,15 +100,35 @@ export const handleUpgradeDeploy = async ({
99
100
hre : HardhatRuntimeEnvironment ;
100
101
contractName : string ;
101
102
deployOptions : DeployOptions ;
102
- } ) : Promise < Deployment | undefined > => {
103
- const { deploy, network } = await getConfig ( hre ) ;
103
+ } ) : Promise < Deployment > => {
104
+ const { deploy, network, deployments } = await getConfig ( hre ) ;
105
+
106
+ const defaultProxyAdminDeployment = await getDeploymentFileByName (
107
+ "DefaultProxyAdmin" ,
108
+ network
109
+ ) ;
110
+ const defaultProxyAdminContract = (
111
+ await hre . ethers . getContractFactory (
112
+ defaultProxyAdminDeployment . abi ,
113
+ defaultProxyAdminDeployment . bytecode
114
+ )
115
+ ) . attach ( defaultProxyAdminDeployment . address ) ;
116
+
117
+ // set the current owner of the proxy, in the deployOptions
118
+ deployOptions . proxy = {
119
+ ...( typeof deployOptions . proxy === "object" ? deployOptions . proxy : { } ) ,
120
+ owner : await defaultProxyAdminContract . owner ( ) ,
121
+ } ;
104
122
105
123
let deployment : Deployment | undefined = undefined ;
106
124
try {
107
125
deployment = await deploy ( contractName , deployOptions ) ;
108
126
} catch ( e ) {
109
127
// update the deployment file for correct implementation address
110
128
await setImplementation ( contractName , network ) ;
129
+ deployment = await deployments . get ( contractName ) ;
130
+
131
+ console . log ( e ) ;
111
132
112
133
console . warn (
113
134
`[⚠️ NOTE!] call "upgrade" on DefaultProxyAdmin to upgrade the implementation for ${ contractName } `
@@ -117,6 +138,23 @@ export const handleUpgradeDeploy = async ({
117
138
return deployment ;
118
139
} ;
119
140
141
+ const getEIP1967ProxyOwner = async (
142
+ hre : HardhatRuntimeEnvironment ,
143
+ contractName : string
144
+ ) => {
145
+ const khash = keccak256 ( toUtf8Bytes ( `eip1967.proxy.admin` ) ) ;
146
+ const num = BigInt ( khash ) ;
147
+ const storageSlot = num - BigInt ( 1 ) ;
148
+
149
+ const provider = hre . ethers . provider ;
150
+ const res = await provider . getStorageAt ( contractName , storageSlot ) ;
151
+
152
+ const owner : string = defaultAbiCoder . decode ( [ "address" ] , res ) [ 0 ] ;
153
+ console . log ( { owner } ) ;
154
+
155
+ return owner ;
156
+ } ;
157
+
120
158
export const setImplementation = async (
121
159
contractName : string ,
122
160
network : Network
@@ -130,7 +168,10 @@ export const setImplementation = async (
130
168
contract . implementation = contractImplementation . address ;
131
169
132
170
await fs . writeFile (
133
- path . join ( __dirname , `./deployments/${ network . name } /${ contractName } .json` ) ,
171
+ path . join (
172
+ __dirname ,
173
+ `../../deployments/${ network . name } /${ contractName } .json`
174
+ ) ,
134
175
format ( JSON . stringify ( contract ) , {
135
176
semi : false ,
136
177
parser : "json" ,
@@ -146,7 +187,10 @@ export const getDeploymentFileByName = async (
146
187
) => {
147
188
return JSON . parse (
148
189
await fs . readFile (
149
- path . join ( __dirname , `./deployments/${ network . name } /${ fileName } .json` ) ,
190
+ path . join (
191
+ __dirname ,
192
+ `../../deployments/${ network . name } /${ fileName } .json`
193
+ ) ,
150
194
"utf8"
151
195
)
152
196
) ;
0 commit comments