Skip to content

Commit 5921658

Browse files
authored
fix: Handle error message for mined transactions (#637)
This commit adds support for handling error messages in mined transactions. The `MinedTransaction` type in `types.ts` now includes an optional `errorMessage` field. In the `mineTransactionWorker.ts` file, the code has been updated to retrieve and store the error message if the `userOpReceipt` is not successful. This allows for better error handling and reporting for mined transactions.
1 parent e0de832 commit 5921658

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/utils/transaction/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ export type MinedTransaction = (
8181
gasUsed: bigint;
8282
effectiveGasPrice?: bigint;
8383
cumulativeGasUsed?: bigint;
84+
85+
// mined transactions can have an error message if they revert
86+
errorMessage?: string;
8487
};
8588

8689
// ErroredTransaction received an error before or while sending to RPC.

src/worker/tasks/mineTransactionWorker.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
getRpcClient,
99
} from "thirdweb";
1010
import { stringify } from "thirdweb/utils";
11-
import { getUserOpReceiptRaw } from "thirdweb/wallets/smart";
11+
import { getUserOpReceipt, getUserOpReceiptRaw } from "thirdweb/wallets/smart";
1212
import { TransactionDB } from "../../db/transactions/db";
1313
import { recycleNonce, removeSentNonce } from "../../db/wallets/walletNonce";
1414
import { getBlockNumberish } from "../../utils/block";
@@ -190,6 +190,7 @@ const _mineUserOp = async (
190190
chain,
191191
userOpHash,
192192
});
193+
193194
if (!userOpReceiptRaw) {
194195
return null;
195196
}
@@ -205,6 +206,28 @@ const _mineUserOp = async (
205206
hash: transaction.hash,
206207
});
207208

209+
let errorMessage: string | undefined;
210+
211+
// if the userOpReceipt is not successful, try to get the parsed userOpReceipt
212+
// we expect this to fail, but we want the error message if it does
213+
if (!userOpReceiptRaw.success) {
214+
try {
215+
const userOpReceipt = await getUserOpReceipt({
216+
client: thirdwebClient,
217+
chain,
218+
userOpHash,
219+
});
220+
await job.log(`Found userOpReceipt: ${userOpReceipt}`);
221+
} catch (e) {
222+
if (e instanceof Error) {
223+
errorMessage = e.message;
224+
await job.log("Failed to get userOpReceipt: " + e.message);
225+
} else {
226+
throw e;
227+
}
228+
}
229+
}
230+
208231
return {
209232
...sentTransaction,
210233
status: "mined",
@@ -219,6 +242,7 @@ const _mineUserOp = async (
219242
cumulativeGasUsed: receipt.cumulativeGasUsed,
220243
sender: userOpReceiptRaw.sender as Address,
221244
nonce: userOpReceiptRaw.nonce.toString(),
245+
errorMessage,
222246
};
223247
};
224248

0 commit comments

Comments
 (0)