Skip to content

Commit 8835c65

Browse files
authored
Farhan/fix-blockreader (#98)
* trying to handle a weird error * updated query to fetch txHash
1 parent a9450d3 commit 8835c65

File tree

2 files changed

+65
-51
lines changed

2 files changed

+65
-51
lines changed

worker/controller/blockchainReader.ts

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ChainOrRpc } from "@thirdweb-dev/sdk";
21
import { BigNumber } from "ethers";
32
import { FastifyInstance } from "fastify";
3+
import { Knex } from "knex";
44
import { connectWithDatabase, env, getSDK } from "../../core";
55
import {
66
getSubmittedTransactions,
@@ -12,52 +12,69 @@ const MINED_TX_CRON_ENABLED = env.MINED_TX_CRON_ENABLED;
1212
export const checkForMinedTransactionsOnBlockchain = async (
1313
server: FastifyInstance,
1414
) => {
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+
}
2565
await trx.commit();
2666
await trx.destroy();
2767
await knex.destroy();
2868
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();
5873
}
74+
if (knex) {
75+
await knex.destroy();
76+
}
77+
server.log.error(error);
78+
return;
5979
}
60-
await trx.commit();
61-
await trx.destroy();
62-
await knex.destroy();
6380
};

worker/services/dbOperations.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,16 @@ export const getWalletDetailsWithoutTrx = async (
171171

172172
export const getSubmittedTransactions = async (
173173
database: Knex,
174-
transaction: Knex.Transaction,
175174
): Promise<TransactionSchema[]> => {
176-
const data = await database
177-
.raw(
178-
`select * from transactions
175+
const data = await database.raw(
176+
`select * from transactions
179177
where "txProcessed" = true
180178
and "txSubmitted" = true
181179
and "txMined" = false
182180
and "txErrored" = false
181+
and "txHash" is not null
183182
order by "txSubmittedTimestamp" ASC
184-
limit ${MIN_TX_TO_CHECK_FOR_MINED_STATUS}
185-
for update skip locked`,
186-
)
187-
.transacting(transaction);
183+
limit ${MIN_TX_TO_CHECK_FOR_MINED_STATUS}`,
184+
);
188185
return data.rows;
189186
};

0 commit comments

Comments
 (0)