@@ -8,7 +8,11 @@ import {
88import { prepareTransactionsForTenderlySimulation } from '~/routes/contract/utils/tenderly/getSimulationUrl'
99import { TransactionService } from '~/services/transaction.service'
1010import { logError , logRequest , logStep } from '~/utils/loggingUtils'
11- import { extractTxHashFromErrorReceipt , getBlockExplorerUrl , getContractAddressFromEvent } from '~/utils/other'
11+ import {
12+ extractTxHashFromErrorReceipt ,
13+ getBlockExplorerUrl ,
14+ getContractAddressFromEvent
15+ } from '~/utils/other'
1216import { getSigner } from '~/utils/wallet'
1317
1418type DeployContractRequestBody = {
@@ -94,7 +98,7 @@ export async function deployContract(fastify: FastifyInstance) {
9498 async ( request , reply ) => {
9599 logRequest ( request )
96100
97- let tenderlyUrl : string | null = null
101+ const tenderlyUrl : string | null = null
98102 let txHash : string | null = null
99103 const { chainId } = request . params
100104
@@ -132,60 +136,65 @@ export async function deployContract(fastify: FastifyInstance) {
132136 to : zeroAddress
133137 }
134138
135- const { simulationData, signedTx } =
136- await prepareTransactionsForTenderlySimulation (
137- signer ,
138- [ deploymentTx ] ,
139- Number ( chainId )
139+ const { simulationData, signedTx } =
140+ await prepareTransactionsForTenderlySimulation (
141+ signer ,
142+ [ deploymentTx ] ,
143+ Number ( chainId )
144+ )
145+ const tenderlyUrl = getTenderlySimulationUrl ( {
146+ chainId : chainId ,
147+ gas : 3000000 ,
148+ block : await signer . provider . getBlockNumber ( ) ,
149+ contractAddress : signedTx . entrypoint ,
150+ blockIndex : 0 ,
151+ rawFunctionInput : simulationData
152+ } )
153+
154+ logStep ( request , 'Sending deploy transaction...' )
155+ const tx = await signer . sendTransaction (
156+ {
157+ data
158+ } ,
159+ { waitForReceipt : true }
140160 )
141- const tenderlyUrl = getTenderlySimulationUrl ( {
142- chainId : chainId ,
143- gas : 3000000 ,
144- block : await signer . provider . getBlockNumber ( ) ,
145- contractAddress : signedTx . entrypoint ,
146- blockIndex : 0 ,
147- rawFunctionInput : simulationData
148- } )
149-
150- logStep ( request , 'Sending deploy transaction...' )
151- const tx = await signer . sendTransaction ( {
152- data
153- } , { waitForReceipt : true } )
154- txHash = tx . hash
155- logStep ( request , 'Deploy transaction sent' , { txHash : tx . hash } )
156-
157- if ( tx . receipt ?. status === 0 ) {
158- logError ( request , new Error ( 'Transaction reverted' ) , { receipt : tx . receipt } )
159- throw new Error ( 'Transaction reverted' , { cause : tx . receipt } )
160- }
161+ txHash = tx . hash
162+ logStep ( request , 'Deploy transaction sent' , { txHash : tx . hash } )
161163
162- const deployedContractAddress = getContractAddressFromEvent (
163- tx . receipt ,
164- 'CreatedContract(address)'
165- )
166-
167- await txService . createTransaction ( {
168- chainId,
169- contractAddress : deployedContractAddress ,
170- abi,
171- data,
172- txHash : txHash ,
173- isDeployTx : true ,
174- args
175- } )
176- logStep ( request , 'Transaction added in db' , { txHash : txHash } )
177-
178- logStep ( request , 'Deploy transaction success' , {
179- txHash : txHash
180- } )
181- return reply . code ( 200 ) . send ( {
182- result : {
183- txHash : txHash ,
184- txUrl : getBlockExplorerUrl ( Number ( chainId ) , txHash ) ,
185- txSimulationUrl : tenderlyUrl ,
186- deployedContractAddress : deployedContractAddress
164+ if ( tx . receipt ?. status === 0 ) {
165+ logError ( request , new Error ( 'Transaction reverted' ) , {
166+ receipt : tx . receipt
167+ } )
168+ throw new Error ( 'Transaction reverted' , { cause : tx . receipt } )
187169 }
188- } )
170+
171+ const deployedContractAddress = getContractAddressFromEvent (
172+ tx . receipt ,
173+ 'CreatedContract(address)'
174+ )
175+
176+ await txService . createTransaction ( {
177+ chainId,
178+ contractAddress : deployedContractAddress ,
179+ abi,
180+ data,
181+ txHash : txHash ,
182+ isDeployTx : true ,
183+ args
184+ } )
185+ logStep ( request , 'Transaction added in db' , { txHash : txHash } )
186+
187+ logStep ( request , 'Deploy transaction success' , {
188+ txHash : txHash
189+ } )
190+ return reply . code ( 200 ) . send ( {
191+ result : {
192+ txHash : txHash ,
193+ txUrl : getBlockExplorerUrl ( Number ( chainId ) , txHash ) ,
194+ txSimulationUrl : tenderlyUrl ,
195+ deployedContractAddress : deployedContractAddress
196+ }
197+ } )
189198 } catch ( error ) {
190199 // Extract transaction hash from error receipt if available
191200 const errorTxHash = extractTxHashFromErrorReceipt ( error )
@@ -198,13 +207,13 @@ export async function deployContract(fastify: FastifyInstance) {
198207 } )
199208
200209 const errorMessage =
201- error instanceof Error
202- ? error . message
203- : 'Failed to deploy contract'
210+ error instanceof Error ? error . message : 'Failed to deploy contract'
204211 return reply . code ( 500 ) . send ( {
205212 result : {
206213 txHash : finalTxHash ,
207- txUrl : finalTxHash ? getBlockExplorerUrl ( Number ( chainId ) , finalTxHash ) : null ,
214+ txUrl : finalTxHash
215+ ? getBlockExplorerUrl ( Number ( chainId ) , finalTxHash )
216+ : null ,
208217 txSimulationUrl : tenderlyUrl ,
209218 deployedContractAddress : null ,
210219 error : errorMessage
0 commit comments