Skip to content

Commit 22e0702

Browse files
committed
Handle tx hash and txUrl for failed transactions ERC721Items
1 parent 728eb86 commit 22e0702

4 files changed

Lines changed: 115 additions & 22 deletions

File tree

src/routes/contract/extensions/erc721/erc721Items/write/batchBurn.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ export async function erc721ItemsBatchBurn(fastify: FastifyInstance) {
8989
logRequest(request)
9090

9191
let tenderlyUrl: string | null = null
92+
let txHash: string | null = null
93+
const { chainId, contractAddress } = request.params
9294

9395
try {
94-
const { chainId, contractAddress } = request.params
9596
const { tokenIds } = request.body
9697

9798
const signer = await getSigner(chainId)
@@ -138,6 +139,7 @@ export async function erc721ItemsBatchBurn(fastify: FastifyInstance) {
138139
to: contractAddress,
139140
data: batchBurnData
140141
})
142+
txHash = txResponse.hash
141143
logStep(request, 'BatchBurn transaction sent', { txResponse })
142144

143145
const receipt = await txResponse.wait()
@@ -173,18 +175,40 @@ export async function erc721ItemsBatchBurn(fastify: FastifyInstance) {
173175
}
174176
})
175177
} catch (error) {
178+
// Extract transaction hash from error receipt if available
179+
let errorTxHash: string | null = null
180+
181+
if ((error as any)?.receipt?.txnReceipt) {
182+
const txnReceiptString = (error as any).receipt.txnReceipt
183+
try {
184+
const txnReceipt = JSON.parse(txnReceiptString)
185+
errorTxHash = txnReceipt.transactionHash
186+
} catch (parseError) {
187+
console.log('Failed to parse txnReceipt:', parseError)
188+
}
189+
}
190+
191+
// If we have logs, we can also get the hash from the first log
192+
if ((error as any)?.receipt?.logs?.[0]?.transactionHash) {
193+
errorTxHash = (error as any).receipt.logs[0].transactionHash
194+
}
195+
196+
const finalTxHash = txHash ?? errorTxHash
197+
176198
logError(request, error, {
177199
params: request.params,
178-
body: request.body
200+
body: request.body,
201+
txHash: finalTxHash
179202
})
203+
180204
const errorMessage =
181205
error instanceof Error
182206
? error.message
183207
: 'Unknown error during batchBurn'
184208
return reply.code(500).send({
185209
result: {
186-
txHash: null,
187-
txUrl: null,
210+
txHash: finalTxHash,
211+
txUrl: finalTxHash ? getBlockExplorerUrl(Number(chainId), finalTxHash) : null,
188212
txSimulationUrl: tenderlyUrl ?? null,
189213
error: errorMessage
190214
}

src/routes/contract/extensions/erc721/erc721Items/write/burn.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ export async function erc721ItemsBurn(fastify: FastifyInstance) {
8585
logRequest(request)
8686

8787
let tenderlyUrl: string | null = null
88+
let txHash: string | null = null
89+
const { chainId, contractAddress } = request.params
8890

8991
try {
90-
const { chainId, contractAddress } = request.params
9192
const { tokenId } = request.body
9293

9394
const signer = await getSigner(chainId)
@@ -134,6 +135,7 @@ export async function erc721ItemsBurn(fastify: FastifyInstance) {
134135
to: contractAddress,
135136
data: burnData
136137
})
138+
txHash = txResponse.hash
137139
logStep(request, 'Burn transaction sent', { txResponse })
138140

139141
const receipt = await txResponse.wait()
@@ -165,18 +167,40 @@ export async function erc721ItemsBurn(fastify: FastifyInstance) {
165167
}
166168
})
167169
} catch (error) {
170+
// Extract transaction hash from error receipt if available
171+
let errorTxHash: string | null = null
172+
173+
if ((error as any)?.receipt?.txnReceipt) {
174+
const txnReceiptString = (error as any).receipt.txnReceipt
175+
try {
176+
const txnReceipt = JSON.parse(txnReceiptString)
177+
errorTxHash = txnReceipt.transactionHash
178+
} catch (parseError) {
179+
console.log('Failed to parse txnReceipt:', parseError)
180+
}
181+
}
182+
183+
// If we have logs, we can also get the hash from the first log
184+
if ((error as any)?.receipt?.logs?.[0]?.transactionHash) {
185+
errorTxHash = (error as any).receipt.logs[0].transactionHash
186+
}
187+
188+
const finalTxHash = txHash ?? errorTxHash
189+
168190
logError(request, error, {
169191
params: request.params,
170-
body: request.body
192+
body: request.body,
193+
txHash: finalTxHash
171194
})
195+
172196
const errorMessage =
173197
error instanceof Error
174198
? error.message
175199
: 'Unknown error during burn, please check that you own the NFT you are trying to burn'
176200
return reply.code(500).send({
177201
result: {
178-
txHash: null,
179-
txUrl: null,
202+
txHash: finalTxHash,
203+
txUrl: finalTxHash ? getBlockExplorerUrl(Number(chainId), finalTxHash) : null,
180204
txSimulationUrl: tenderlyUrl ?? null,
181205
error: errorMessage
182206
}

src/routes/contract/extensions/erc721/erc721Items/write/initialize.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ export async function erc721ItemsInitialize(fastify: FastifyInstance) {
143143
logRequest(request)
144144

145145
let tenderlyUrl: string | null = null
146+
let txHash: string | null = null
147+
const { chainId, contractAddress } = request.params
146148

147149
try {
148-
const { chainId, contractAddress } = request.params
149150
const {
150151
owner,
151152
tokenName,
@@ -216,6 +217,7 @@ export async function erc721ItemsInitialize(fastify: FastifyInstance) {
216217
to: contractAddress,
217218
data: initializeData
218219
})
220+
txHash = txResponse.hash
219221
logStep(request, 'Initialize transaction sent', { txResponse })
220222

221223
const receipt = await txResponse.wait()
@@ -261,18 +263,40 @@ export async function erc721ItemsInitialize(fastify: FastifyInstance) {
261263
}
262264
})
263265
} catch (error) {
266+
// Extract transaction hash from error receipt if available
267+
let errorTxHash: string | null = null
268+
269+
if ((error as any)?.receipt?.txnReceipt) {
270+
const txnReceiptString = (error as any).receipt.txnReceipt
271+
try {
272+
const txnReceipt = JSON.parse(txnReceiptString)
273+
errorTxHash = txnReceipt.transactionHash
274+
} catch (parseError) {
275+
console.log('Failed to parse txnReceipt:', parseError)
276+
}
277+
}
278+
279+
// If we have logs, we can also get the hash from the first log
280+
if ((error as any)?.receipt?.logs?.[0]?.transactionHash) {
281+
errorTxHash = (error as any).receipt.logs[0].transactionHash
282+
}
283+
284+
const finalTxHash = txHash ?? errorTxHash
285+
264286
logError(request, error, {
265287
params: request.params,
266-
body: request.body
288+
body: request.body,
289+
txHash: finalTxHash
267290
})
291+
268292
const errorMessage =
269293
error instanceof Error
270294
? error.message
271295
: 'Unknown error during initialization'
272296
return reply.code(500).send({
273297
result: {
274-
txHash: null,
275-
txUrl: null,
298+
txHash: finalTxHash,
299+
txUrl: finalTxHash ? getBlockExplorerUrl(Number(chainId), finalTxHash) : null,
276300
txSimulationUrl: tenderlyUrl ?? null,
277301
error: errorMessage
278302
}

src/routes/contract/extensions/erc721/erc721Items/write/mint.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ export async function erc721ItemsMint(fastify: FastifyInstance) {
8585
logRequest(request)
8686

8787
let tenderlyUrl: string | null = null
88+
let txHash: string | null = null
8889

89-
try {
90-
const { to, tokenId } = request.body
91-
const { chainId, contractAddress } = request.params
90+
const { to, tokenId } = request.body
91+
const { chainId, contractAddress } = request.params
9292

93+
try {
9394
const signer = await getSigner(chainId)
9495
if (!signer || !signer.account?.address) {
9596
logError(request, new Error('Signer not configured correctly.'), {
@@ -134,19 +135,17 @@ export async function erc721ItemsMint(fastify: FastifyInstance) {
134135
rawFunctionInput: simulationData
135136
})
136137

137-
console.log('tenderlyUrl', tenderlyUrl)
138-
139138
const txService = new TransactionService(fastify)
140139

141140
logStep(request, 'Sending mint transaction...')
142141
const txResponse: TransactionResponse = await signer.sendTransaction(tx)
142+
txHash = txResponse.hash
143143
logStep(request, 'Mint transaction sent', { txResponse })
144144

145145
const receipt = await txResponse.wait()
146146
if (receipt?.status === 0) {
147-
throw new Error('Transaction reverted')
147+
throw new Error('Transaction reverted', {cause: receipt})
148148
}
149-
150149
await txService.createTransaction({
151150
chainId,
152151
contractAddress,
@@ -169,14 +168,36 @@ export async function erc721ItemsMint(fastify: FastifyInstance) {
169168
}
170169
})
171170
} catch (error) {
171+
// Extract transaction hash from error receipt if available
172+
let errorTxHash: string | null = null
173+
174+
if ((error as any)?.receipt?.txnReceipt) {
175+
const txnReceiptString = (error as any).receipt.txnReceipt
176+
try {
177+
const txnReceipt = JSON.parse(txnReceiptString)
178+
errorTxHash = txnReceipt.transactionHash
179+
} catch (parseError) {
180+
console.log('Failed to parse txnReceipt:', parseError)
181+
}
182+
}
183+
184+
// If we have logs, we can also get the hash from the first log
185+
if ((error as any)?.receipt?.logs?.[0]?.transactionHash) {
186+
errorTxHash = (error as any).receipt.logs[0].transactionHash
187+
}
188+
189+
const finalTxHash = txHash ?? errorTxHash
190+
172191
logError(request, error, {
173192
params: request.params,
174-
body: request.body
193+
body: request.body,
194+
txHash: finalTxHash
175195
})
196+
176197
return reply.code(500).send({
177198
result: {
178-
txHash: null,
179-
txUrl: null,
199+
txHash: finalTxHash,
200+
txUrl: finalTxHash ? getBlockExplorerUrl(Number(chainId), finalTxHash) : null,
180201
txSimulationUrl: tenderlyUrl ?? null,
181202
error:
182203
error instanceof Error

0 commit comments

Comments
 (0)