1
- import { ChainOrRpc } from "@thirdweb-dev/sdk" ;
2
1
import { BigNumber } from "ethers" ;
3
2
import { FastifyInstance } from "fastify" ;
3
+ import { Knex } from "knex" ;
4
4
import { connectWithDatabase , env , getSDK } from "../../core" ;
5
5
import {
6
6
getSubmittedTransactions ,
@@ -12,52 +12,69 @@ const MINED_TX_CRON_ENABLED = env.MINED_TX_CRON_ENABLED;
12
12
export const checkForMinedTransactionsOnBlockchain = async (
13
13
server : FastifyInstance ,
14
14
) => {
15
- const knex = await connectWithDatabase ( ) ;
16
- if ( ! MINED_TX_CRON_ENABLED ) {
17
- server . log . warn ( "Mined Tx Cron is disabled" ) ;
18
- return ;
19
- }
20
- server . log . info ( "Running Cron to check for mined transactions on blockchain" ) ;
21
- const trx = await knex . transaction ( ) ;
22
- const transactions = await getSubmittedTransactions ( knex , trx ) ;
23
- if ( transactions . length === 0 ) {
24
- server . log . warn ( "No transactions to check for mined status" ) ;
15
+ let knex : Knex | undefined ;
16
+ let trx : Knex . Transaction | undefined ;
17
+ try {
18
+ knex = await connectWithDatabase ( ) ;
19
+ if ( ! MINED_TX_CRON_ENABLED ) {
20
+ server . log . warn ( "Mined Tx Cron is disabled" ) ;
21
+ return ;
22
+ }
23
+ server . log . info (
24
+ "Running Cron to check for mined transactions on blockchain" ,
25
+ ) ;
26
+ trx = await knex . transaction ( ) ;
27
+ const transactions = await getSubmittedTransactions ( knex ) ;
28
+ if ( transactions . length === 0 ) {
29
+ server . log . warn ( "No transactions to check for mined status" ) ;
30
+ await trx . commit ( ) ;
31
+ await trx . destroy ( ) ;
32
+ await knex . destroy ( ) ;
33
+ return ;
34
+ }
35
+
36
+ const txReceipts = await Promise . all (
37
+ transactions . map ( async ( txData ) => {
38
+ server . log . debug (
39
+ `Getting receipt for tx: ${ txData . txHash } on chain: ${ txData . chainId } for queueId: ${ txData . identifier } ` ,
40
+ ) ;
41
+ const sdk = await getSDK ( txData . chainId ! ) ;
42
+ return sdk . getProvider ( ) . getTransactionReceipt ( txData . txHash ! ) ;
43
+ } ) ,
44
+ ) ;
45
+
46
+ for ( let txReceipt of txReceipts ) {
47
+ const txData = transactions . find (
48
+ ( tx ) => tx . txHash === txReceipt . transactionHash ,
49
+ ) ;
50
+ if ( txData ) {
51
+ server . log . debug (
52
+ `Got receipt for tx: ${ txData . txHash } ${ txData . identifier } , ${ txReceipt . effectiveGasPrice } ` ,
53
+ ) ;
54
+ await updateTransactionState (
55
+ knex ,
56
+ txData . identifier ! ,
57
+ "mined" ,
58
+ trx ,
59
+ undefined ,
60
+ undefined ,
61
+ { gasPrice : BigNumber . from ( txReceipt . effectiveGasPrice ) . toString ( ) } ,
62
+ ) ;
63
+ }
64
+ }
25
65
await trx . commit ( ) ;
26
66
await trx . destroy ( ) ;
27
67
await knex . destroy ( ) ;
28
68
return ;
29
- }
30
-
31
- const txReceipts = await Promise . all (
32
- transactions . map ( async ( txData ) => {
33
- server . log . debug (
34
- `Getting receipt for tx: ${ txData . txHash } on chain: ${ txData . chainId } for queueId: ${ txData . identifier } ` ,
35
- ) ;
36
- const sdk = await getSDK ( txData . chainId as ChainOrRpc ) ;
37
- return sdk . getProvider ( ) . getTransactionReceipt ( txData . txHash ! ) ;
38
- } ) ,
39
- ) ;
40
-
41
- for ( let txReceipt of txReceipts ) {
42
- const txData = transactions . find (
43
- ( tx ) => tx . txHash === txReceipt . transactionHash ,
44
- ) ;
45
- if ( txData ) {
46
- server . log . debug (
47
- `Got receipt for tx: ${ txData . txHash } ${ txData . identifier } , ${ txReceipt . effectiveGasPrice } ` ,
48
- ) ;
49
- await updateTransactionState (
50
- knex ,
51
- txData . identifier ! ,
52
- "mined" ,
53
- trx ,
54
- undefined ,
55
- undefined ,
56
- { gasPrice : BigNumber . from ( txReceipt . effectiveGasPrice ) . toString ( ) } ,
57
- ) ;
69
+ } catch ( error ) {
70
+ if ( trx ) {
71
+ await trx . rollback ( ) ;
72
+ await trx . destroy ( ) ;
58
73
}
74
+ if ( knex ) {
75
+ await knex . destroy ( ) ;
76
+ }
77
+ server . log . error ( error ) ;
78
+ return ;
59
79
}
60
- await trx . commit ( ) ;
61
- await trx . destroy ( ) ;
62
- await knex . destroy ( ) ;
63
80
} ;
0 commit comments