Skip to content

Commit

Permalink
chore: Clean up TransactionStatus enum (#454)
Browse files Browse the repository at this point in the history
* chore: Clean up TransactionStatus enum

* fix: dont rely on processedAt

* remove debug line

* cr feedback

* fix build
  • Loading branch information
arcoraven authored Mar 29, 2024
1 parent caf6a46 commit 86305f3
Show file tree
Hide file tree
Showing 24 changed files with 94 additions and 146 deletions.
3 changes: 0 additions & 3 deletions src/db/transactions/cleanTxs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const cleanTxs = (
id: undefined,
queuedAt: tx.queuedAt.toISOString(),
sentAt: tx.sentAt?.toISOString() || null,
processedAt: tx.processedAt?.toISOString() || null,
minedAt: tx.minedAt?.toISOString() || null,
cancelledAt: tx.cancelledAt?.toISOString() || null,
status: !!tx.errorMessage
Expand All @@ -26,8 +25,6 @@ export const cleanTxs = (
? "sent"
: !!tx.sentAt && tx.retryCount > 0
? "retried"
: !!tx.processedAt
? "processed"
: "queued",
};
});
Expand Down
14 changes: 7 additions & 7 deletions src/db/transactions/getAllTxs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Static } from "@sinclair/typebox";
import { ContractExtension } from "../../schema/extension";
import { PrismaTransaction } from "../../schema/prisma";
import {
TransactionStatusEnum,
TransactionStatus,
transactionResponseSchema,
} from "../../server/schemas/transaction";
import { getPrismaWithPostgresTx } from "../client";
Expand All @@ -13,7 +13,7 @@ interface GetAllTxsParams {
pgtx?: PrismaTransaction;
page: number;
limit: number;
filter?: TransactionStatusEnum;
filter?: TransactionStatus;
extensions?: ContractExtension[];
}

Expand All @@ -40,15 +40,15 @@ export const getAllTxs = async ({
// | "errorMessage"
// | undefined;

// if (filter === TransactionStatusEnum.Queued) {
// if (filter === TransactionStatus.Queued) {
// filterBy = "queuedAt";
// } else if (filter === TransactionStatusEnum.Submitted) {
// } else if (filter === TransactionStatus.Sent) {
// filterBy = "sentAt";
// } else if (filter === TransactionStatusEnum.Processed) {
// } else if (filter === TransactionStatus.Processed) {
// filterBy = "processedAt";
// } else if (filter === TransactionStatusEnum.Mined) {
// } else if (filter === TransactionStatus.Mined) {
// filterBy = "minedAt";
// } else if (filter === TransactionStatusEnum.Errored) {
// } else if (filter === TransactionStatus.Errored) {
// filterBy = "errorMessage";
// }

Expand Down
8 changes: 4 additions & 4 deletions src/db/transactions/getQueueStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ export const getQueueStatus = async ({
const queued = await prisma.transactions.count({
where: {
fromAddress: walletAddress?.toLowerCase(),
processedAt: null,
sentAt: null,
errorMessage: null,
cancelledAt: null,
},
});

const pending = await prisma.transactions.count({
where: {
fromAddress: walletAddress?.toLowerCase(),
sentAt: {
not: null,
},
sentAt: { not: null },
minedAt: null,
errorMessage: null,
cancelledAt: null,
},
});

Expand Down
3 changes: 1 addition & 2 deletions src/db/transactions/getQueuedTxs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export const getQueuedTxs = async ({ pgtx }: GetQueuedTxsParams = {}): Promise<
FROM
"transactions"
WHERE
"processedAt" IS NULL
AND "sentAt" IS NULL
"sentAt" IS NULL
AND "minedAt" IS NULL
AND "cancelledAt" IS NULL
ORDER BY
Expand Down
3 changes: 1 addition & 2 deletions src/db/transactions/getSentTxs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export const getSentTxs = async ({ pgtx }: GetSentTxsParams = {}): Promise<

return prisma.$queryRaw<Transactions[]>`
SELECT * FROM "transactions"
WHERE "processedAt" IS NOT NULL
AND "sentAt" IS NOT NULL
WHERE "sentAt" IS NOT NULL
AND "transactionHash" IS NOT NULL
AND "accountAddress" IS NULL
AND "minedAt" IS NULL
Expand Down
3 changes: 1 addition & 2 deletions src/db/transactions/getSentUserOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export const getSentUserOps = async ({

return prisma.$queryRaw<Transactions[]>`
SELECT * FROM "transactions"
WHERE "processedAt" IS NOT NULL
AND "sentAt" IS NOT NULL
WHERE "sentAt" IS NOT NULL
AND "accountAddress" IS NOT NULL
AND "userOpHash" IS NOT NULL
AND "minedAt" IS NULL
Expand Down
3 changes: 1 addition & 2 deletions src/db/transactions/getTxToRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ SELECT
FROM
"transactions"
WHERE
"processedAt" IS NOT NULL
AND "sentAt" IS NOT NULL
"sentAt" IS NOT NULL
AND "accountAddress" IS NULL
AND "minedAt" IS NULL
AND "errorMessage" IS NULL
Expand Down
4 changes: 2 additions & 2 deletions src/db/transactions/queueTxRaw.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Prisma, Transactions } from "@prisma/client";
import { v4 as uuid } from "uuid";
import { PrismaTransaction } from "../../schema/prisma";
import { TransactionStatusEnum } from "../../server/schemas/transaction";
import { TransactionStatus } from "../../server/schemas/transaction";
import { simulateTx } from "../../server/utils/simulateTx";
import { UsageEventTxActionEnum, reportUsage } from "../../utils/usage";
import { sendWebhooks } from "../../utils/webhook";
Expand Down Expand Up @@ -88,7 +88,7 @@ export const queueTxRaw = async ({
sendWebhooks([
{
queueId: txRow.id,
status: TransactionStatusEnum.Queued,
status: TransactionStatus.Queued,
},
]).catch((err) => {});

Expand Down
35 changes: 11 additions & 24 deletions src/db/transactions/updateTx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber, ethers } from "ethers";
import { PrismaTransaction } from "../../schema/prisma";
import { TransactionStatusEnum } from "../../server/schemas/transaction";
import { TransactionStatus } from "../../server/schemas/transaction";
import { getPrismaWithPostgresTx } from "../client";

interface UpdateTxParams {
Expand All @@ -11,30 +11,27 @@ interface UpdateTxParams {

type UpdateTxData =
| {
status: TransactionStatusEnum.Cancelled;
status: TransactionStatus.Cancelled;
}
| {
status: TransactionStatusEnum.Processed;
}
| {
status: TransactionStatusEnum.Errored;
status: TransactionStatus.Errored;
errorMessage: string;
}
| {
status: TransactionStatusEnum.Submitted;
status: TransactionStatus.Sent;
sentAt: Date;
transactionHash: string;
res: ethers.providers.TransactionRequest;
sentAtBlockNumber: number;
retryCount?: number;
}
| {
status: TransactionStatusEnum.UserOpSent;
status: TransactionStatus.UserOpSent;
sentAt: Date;
userOpHash: string;
}
| {
status: TransactionStatusEnum.Mined;
status: TransactionStatus.Mined;
gasPrice?: string;
blockNumber?: number;
minedAt: Date;
Expand All @@ -50,7 +47,7 @@ type UpdateTxData =
export const updateTx = async ({ pgtx, queueId, data }: UpdateTxParams) => {
const prisma = getPrismaWithPostgresTx(pgtx);
switch (data.status) {
case TransactionStatusEnum.Cancelled:
case TransactionStatus.Cancelled:
await prisma.transactions.update({
where: {
id: queueId,
Expand All @@ -60,17 +57,7 @@ export const updateTx = async ({ pgtx, queueId, data }: UpdateTxParams) => {
},
});
break;
case TransactionStatusEnum.Processed:
await prisma.transactions.update({
where: {
id: queueId,
},
data: {
processedAt: new Date(),
},
});
break;
case TransactionStatusEnum.Errored:
case TransactionStatus.Errored:
await prisma.transactions.update({
where: {
id: queueId,
Expand All @@ -80,7 +67,7 @@ export const updateTx = async ({ pgtx, queueId, data }: UpdateTxParams) => {
},
});
break;
case TransactionStatusEnum.Submitted:
case TransactionStatus.Sent:
await prisma.transactions.update({
where: {
id: queueId,
Expand All @@ -100,7 +87,7 @@ export const updateTx = async ({ pgtx, queueId, data }: UpdateTxParams) => {
},
});
break;
case TransactionStatusEnum.UserOpSent:
case TransactionStatus.UserOpSent:
await prisma.transactions.update({
where: {
id: queueId,
Expand All @@ -111,7 +98,7 @@ export const updateTx = async ({ pgtx, queueId, data }: UpdateTxParams) => {
},
});
break;
case TransactionStatusEnum.Mined:
case TransactionStatus.Mined:
await prisma.transactions.update({
where: {
id: queueId,
Expand Down
1 change: 1 addition & 0 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ model Transactions {
deployedContractType String? @map("deployedContractType")
// Timestamps
queuedAt DateTime @default(now()) @map("queuedAt")
// @deprecated
processedAt DateTime? @map("processedAt")
sentAt DateTime? @map("sentAt")
minedAt DateTime? @map("minedAt")
Expand Down
1 change: 0 additions & 1 deletion src/schema/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export enum WebhooksEventTypes {
SENT_TX = "sent_transaction",
MINED_TX = "mined_transaction",
ERRORED_TX = "errored_transaction",
RETRIED_TX = "retried_transaction",
CANCELLED_TX = "cancelled_transaction",
ALL_TX = "all_transactions",
BACKEND_WALLET_BALANCE = "backend_wallet_balance",
Expand Down
3 changes: 1 addition & 2 deletions src/server/routes/transaction/getAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const requestQuerySchema = Type.Object({
default: "10",
}),
// filter: Type.Optional(
// Type.Union([Type.Enum(TransactionStatusEnum), Type.Literal("all")], {
// Type.Union([Type.Enum(TransactionStatus), Type.Literal("all")], {
// description:
// "This parameter allows to define specific criteria to filter the data by. For example, filtering by processed, submitted or error",
// examples: ["all", "submitted", "processed", "errored", "mined", "queued"],
Expand Down Expand Up @@ -58,7 +58,6 @@ responseBodySchema.example = {
transactionHash:
"0xf11ca950299c9e72b8d6cac8a03623c6e5a43af1d1b0d45b3fd804c129b573f8",
queuedAt: "2023-09-29T18:17:36.929Z",
processedAt: "2023-09-29T18:17:37.011Z",
sentAt: "2023-09-29T18:17:40.832Z",
minedAt: "2023-09-29T18:17:44.000Z",
cancelledAt: null,
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/transaction/getAllDeployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const requestQuerySchema = Type.Object({
default: "10",
}),
// filter: Type.Optional(
// Type.Union([Type.Enum(TransactionStatusEnum), Type.Literal("all")], {
// Type.Union([Type.Enum(TransactionStatus), Type.Literal("all")], {
// description:
// "This parameter allows to define specific criteria to filter the data by. For example, filtering by processed, submitted or error",
// examples: ["all", "submitted", "processed", "errored", "mined", "queued"],
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/transaction/syncRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { parseTxError } from "../../../utils/errors";
import { UsageEventTxActionEnum, reportUsage } from "../../../utils/usage";
import { createCustomError } from "../../middleware/error";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
import { TransactionStatusEnum } from "../../schemas/transaction";
import { TransactionStatus } from "../../schemas/transaction";

// INPUT
const requestBodySchema = Type.Object({
Expand Down Expand Up @@ -141,7 +141,7 @@ export async function syncRetryTransaction(fastify: FastifyInstance) {
await updateTx({
queueId: tx.id,
data: {
status: TransactionStatusEnum.Submitted,
status: TransactionStatus.Sent,
transactionHash,
res: txRequest,
sentAt: new Date(),
Expand Down
5 changes: 0 additions & 5 deletions src/server/routes/webhooks/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ BodySchema.examples = [
name: "QueuedTx",
eventType: WebhooksEventTypes.QUEUED_TX,
},
{
url: "https://example.com/retiredTx",
name: "RetriedTx",
eventType: WebhooksEventTypes.RETRIED_TX,
},
{
url: "https://example.com/sentTx",
name: "Sent Transaction Event",
Expand Down
19 changes: 8 additions & 11 deletions src/server/schemas/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ export const transactionResponseSchema = Type.Object({
}),
Type.Null(),
]),
processedAt: Type.Union([
Type.String({
description: "When the transaction is handled by a worker",
}),
Type.Null(),
]),
sentAt: Type.Union([
Type.String({
description: "When the transaction is submitted to mempool",
Expand Down Expand Up @@ -190,16 +184,19 @@ export const transactionResponseSchema = Type.Object({
onChainTxStatus: Type.Union([Type.Number(), Type.Null()]),
});

export enum TransactionStatusEnum {
Processed = "processed",
export enum TransactionStatus {
// Tx was received and waiting to be processed.
Queued = "queued",
// TODO: Switch to sent
Submitted = "sent",
// Tx was submitted to mempool.
Sent = "sent",
// Tx (userOp for smart account) was submitted to mempool.
UserOpSent = "user-op-sent",
// Tx failed before submitting to mempool.
Errored = "errored",
// Tx was successfully mined onchain. Note: The tx may have "reverted" onchain.
Mined = "mined",
// Tx was cancelled and will not be re-attempted.
Cancelled = "cancelled",
Retried = "retried",
}

export interface TransactionSchema {
Expand Down
Loading

0 comments on commit 86305f3

Please sign in to comment.