Skip to content

Commit

Permalink
Use cancelledAt field for transaction cancellation instead of separat…
Browse files Browse the repository at this point in the history
…e table (#210)

* Remove extra table for transaction cancellation

* Add drop table migration
  • Loading branch information
adam-maj authored Oct 10, 2023
1 parent f5c6ae0 commit 1eef23b
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 101 deletions.
11 changes: 2 additions & 9 deletions server/utilities/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { TransactionResponse } from "@ethersproject/abstract-provider";
import { getDefaultGasOverrides } from "@thirdweb-dev/sdk";
import { BigNumber } from "ethers";
import { StatusCodes } from "http-status-codes";
import { createCancelledTxData } from "../../src/db/cancelledTransactions/createCancelledTxData";
import { getTxById } from "../../src/db/transactions/getTxById";
import { updateTx } from "../../src/db/transactions/updateTx";
import { createCustomError } from "../middleware/error";
Expand Down Expand Up @@ -43,17 +42,9 @@ export const cancelTransactionAndUpdate = async ({
);
break;
case TransactionStatusEnum.Queued:
case TransactionStatusEnum.Processed:
await createCancelledTxData({
queueId,
});

await updateTx({
queueId,
status: TransactionStatusEnum.Cancelled,
txData: {
cancelledAt: new Date(),
},
});
message = "Transaction cancelled on-database successfully.";
break;
Expand All @@ -64,6 +55,7 @@ export const cancelTransactionAndUpdate = async ({
"TransactionAlreadyMined",
);
break;
case TransactionStatusEnum.Processed:
case TransactionStatusEnum.Submitted: {
const sdk = await getSdk({
chainId: txData.chainId!,
Expand All @@ -80,6 +72,7 @@ export const cancelTransactionAndUpdate = async ({
"Transaction already mined. Cannot cancel transaction on-chain.";
break;
}

const gasOverrides = await getDefaultGasOverrides(sdk.getProvider());
transferTransactionResult = await sdk.wallet.sendRawTransaction({
to: txData.fromAddress!,
Expand Down
22 changes: 0 additions & 22 deletions src/db/cancelledTransactions/checkIfTxCancelled.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/db/cancelledTransactions/createCancelledTxData.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/db/cancelledTransactions/updateCancelStatus.ts

This file was deleted.

8 changes: 1 addition & 7 deletions src/db/transactions/updateTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export const updateTx = async ({
id: queueId,
},
data: {
// TODO: minedAt will always get overwritten in blockchainReader.ts
minedAt: new Date(),
...txData,
},
Expand All @@ -92,12 +91,7 @@ export const updateTx = async ({
id: queueId,
},
data: {
transactionHash: res?.hash,
transactionType: res?.type,
gasPrice: res?.gasPrice?.toString(),
gasLimit: res?.gasLimit?.toString(),
maxFeePerGas: res?.maxFeePerGas?.toString(),
maxPriorityFeePerGas: res?.maxPriorityFeePerGas?.toString(),
cancelledAt: new Date(),
...txData,
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- You are about to drop the `cancelled_transactions` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropTable
DROP TABLE "cancelled_transactions";
7 changes: 0 additions & 7 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,3 @@ model Transactions {
@@map("transactions")
}

model CancelledTransactions {
queueId String @id @map("queueId")
cancelledByWorkerAt DateTime? @map("cancelledByWorkerAt")
@@map("cancelled_transactions")
}
15 changes: 1 addition & 14 deletions src/worker/tasks/processTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
transactionResponseSchema,
} from "../../../server/schemas/transaction";
import { getSdk } from "../../../server/utils/cache/getSdk";
import { checkIfIDCancelled } from "../../db/cancelledTransactions/checkIfTxCancelled";
import { updateCancelStatus } from "../../db/cancelledTransactions/updateCancelStatus";
import { prisma } from "../../db/client";
import { getQueuedTxs } from "../../db/transactions/getQueuedTxs";
import { updateTx } from "../../db/transactions/updateTx";
Expand Down Expand Up @@ -47,21 +45,10 @@ export const processTx = async () => {
const txsToSend = [];
const userOpsToSend = [];
for (const tx of txs) {
const cancelled = await checkIfIDCancelled({
queueId: tx.queueId!,
});

if (cancelled) {
if (tx.cancelledAt) {
logger.worker.info(
`[Transaction] [${tx.queueId}] Cancelled by user`,
);

if (!cancelled.cancelledByWorkerAt) {
await updateCancelStatus({
queueId: tx.queueId!,
});
}

continue;
}

Expand Down

0 comments on commit 1eef23b

Please sign in to comment.