4949 */
5050
5151import { execSync } from "node:child_process" ;
52+ import { existsSync , readFileSync , unlinkSync } from "node:fs" ;
5253import path from "node:path" ;
54+ import { fileURLToPath } from "node:url" ;
5355
5456import yargs from "yargs" ;
5557import { hideBin } from "yargs/helpers" ;
@@ -126,6 +128,12 @@ const parser = yargs(hideBin(process.argv))
126128 return true ;
127129 } ) ;
128130
131+ type DeploymentOutput = {
132+ implementationAddress : string ;
133+ proxyAddress : string ;
134+ chainId : number ;
135+ } ;
136+
129137/**
130138 * Deploys the PythLazer contract using forge script
131139 * @param chain - The EVM chain to deploy to
@@ -140,12 +148,26 @@ async function deployPythLazerContract(
140148 verify : boolean ,
141149 etherscanApiKey ?: string ,
142150) : Promise < string > {
143- const lazerContractsDir = path . resolve ( "../../lazer/contracts/evm" ) ;
151+ // Resolve path relative to this script's location, not CWD
152+ const scriptDir = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
153+ const lazerContractsDir = path . resolve (
154+ scriptDir ,
155+ "../../lazer/contracts/evm" ,
156+ ) ;
157+ const deploymentOutputPath = path . join (
158+ lazerContractsDir ,
159+ "deployment-output.json" ,
160+ ) ;
144161 const rpcUrl = chain . rpcUrl ;
145162
146163 console . log ( `Deploying PythLazer contract to ${ chain . getId ( ) } ...` ) ;
147164 console . log ( `RPC URL: ${ rpcUrl } ` ) ;
148165
166+ // Clean up any previous deployment output
167+ if ( existsSync ( deploymentOutputPath ) ) {
168+ unlinkSync ( deploymentOutputPath ) ;
169+ }
170+
149171 // Build forge command
150172 let forgeCommand = `forge script script/PythLazerDeploy.s.sol --rpc-url ${ rpcUrl } --private-key ${ privateKey } --broadcast` ;
151173
@@ -162,19 +184,37 @@ async function deployPythLazerContract(
162184 stdio : "pipe" ,
163185 } ) ;
164186
165- console . log ( "Deployment output:" ) ;
166187 console . log ( output ) ;
167188
168- // Extract proxy address from output
169- const proxyMatch = / D e p l o y e d p r o x y t o : ( 0 x [ a - f A - F 0 - 9 ] { 40 } ) / . exec ( output ) ;
170- if ( ! proxyMatch ) {
171- throw new Error ( "Could not extract proxy address from deployment output" ) ;
189+ // Read deployment output from JSON file written by the forge script
190+ if ( ! existsSync ( deploymentOutputPath ) ) {
191+ throw new Error (
192+ "Deployment output file not found. Deployment may have failed." ,
193+ ) ;
172194 }
173195
174- const proxyAddress = proxyMatch [ 1 ] ;
175- console . log ( `\nPythLazer proxy deployed at: ${ proxyAddress } ` ) ;
196+ const deploymentOutput = JSON . parse (
197+ readFileSync ( deploymentOutputPath , "utf8" ) ,
198+ ) as DeploymentOutput ;
199+
200+ // Verify chain ID matches
201+ if ( deploymentOutput . chainId !== chain . networkId ) {
202+ throw new Error (
203+ `Chain ID mismatch: expected ${ chain . networkId } , got ${ deploymentOutput . chainId } ` ,
204+ ) ;
205+ }
206+
207+ console . log (
208+ `\nPythLazer implementation deployed at: ${ deploymentOutput . implementationAddress } ` ,
209+ ) ;
210+ console . log (
211+ `PythLazer proxy deployed at: ${ deploymentOutput . proxyAddress } ` ,
212+ ) ;
213+
214+ // Clean up the output file
215+ unlinkSync ( deploymentOutputPath ) ;
176216
177- return proxyAddress ?? "" ;
217+ return deploymentOutput . proxyAddress ;
178218 } catch ( error ) {
179219 console . error ( "Deployment failed:" , error ) ;
180220 throw error ;
0 commit comments