Skip to content

Commit 4f21329

Browse files
committed
typing...
1 parent 66259cc commit 4f21329

File tree

14 files changed

+94
-69
lines changed

14 files changed

+94
-69
lines changed

src/db/transactions/queueTx.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const queueTx = async ({
5959
const queueId = await insertTransaction({
6060
insertedTransaction: {
6161
...baseTransaction,
62+
isUserOp: true,
6263
deployedContractAddress,
6364
deployedContractType,
6465
signerAddress,
@@ -90,6 +91,7 @@ export const queueTx = async ({
9091
const queueId = await insertTransaction({
9192
insertedTransaction: {
9293
...baseTransaction,
94+
isUserOp: false,
9395
deployedContractAddress,
9496
deployedContractType,
9597
from: fromAddress.toLowerCase() as Address,

src/server/routes/backend-wallet/sendTransaction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export async function sendTransaction(fastify: FastifyInstance) {
8585
} else {
8686
queueId = await insertTransaction({
8787
insertedTransaction: {
88+
isUserOp: false,
8889
chainId,
8990
from: fromAddress as Address,
9091
to: toAddress as Address | undefined,

src/server/routes/backend-wallet/simulateTransaction.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,23 @@ export async function simulateTransaction(fastify: FastifyInstance) {
100100
functionArgs: args,
101101
data: data as Hex | undefined,
102102
value: value ? BigInt(value) : 0n,
103-
};
104103

105-
if (accountAddress) {
106-
queuedTransaction = {
107-
...queuedTransaction,
108-
accountAddress: accountAddress as Address,
109-
target: toAddress as Address,
110-
signerAddress: walletAddress as Address,
111-
};
112-
}
104+
...(accountAddress
105+
? {
106+
isUserOp: true,
107+
accountAddress: accountAddress as Address,
108+
target: toAddress as Address,
109+
signerAddress: walletAddress as Address,
110+
}
111+
: {
112+
isUserOp: false,
113+
}),
114+
};
113115

114-
const error = await simulateQueuedTransaction(queuedTransaction);
115-
if (error) {
116+
const simulateError = await simulateQueuedTransaction(queuedTransaction);
117+
if (simulateError) {
116118
throw createCustomError(
117-
error,
119+
simulateError,
118120
StatusCodes.BAD_REQUEST,
119121
"FAILED_SIMULATION",
120122
);

src/server/routes/backend-wallet/transfer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export async function transfer(fastify: FastifyInstance) {
9999
currencyAddress === NATIVE_TOKEN_ADDRESS
100100
) {
101101
insertedTransaction = {
102+
isUserOp: false,
102103
chainId,
103104
from: walletAddress as Address,
104105
to: to as Address,
@@ -129,6 +130,7 @@ export async function transfer(fastify: FastifyInstance) {
129130
const transaction = transferERC20({ contract, to, amount });
130131

131132
insertedTransaction = {
133+
isUserOp: false,
132134
chainId,
133135
from: walletAddress as Address,
134136
to: (await resolvePromisedValue(transaction.to)) as

src/server/routes/backend-wallet/withdraw.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export async function withdraw(fastify: FastifyInstance) {
5555

5656
const queueId = await insertTransaction({
5757
insertedTransaction: {
58+
isUserOp: false,
5859
chainId,
5960
from: walletAddress as Address,
6061
to: toAddress as Address,

src/server/routes/transaction/cancel.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { Hex } from "thirdweb";
54
import { TransactionDB } from "../../../db/transactions/db";
65
import { getBlockNumberish } from "../../../utils/block";
76
import { getConfig } from "../../../utils/cache/getConfig";
@@ -88,9 +87,9 @@ export async function cancelTransaction(fastify: FastifyInstance) {
8887
}
8988

9089
let message = "Transaction successfully cancelled.";
91-
let transactionHash: Hex = "0x";
92-
switch (transaction.status) {
93-
case "queued":
90+
let cancelledTransaction: CancelledTransaction | null = null;
91+
if (!transaction.isUserOp) {
92+
if (transaction.status === "queued") {
9493
// Remove all retries from the SEND_TRANSACTION queue.
9594
const config = await getConfig();
9695
for (
@@ -100,42 +99,44 @@ export async function cancelTransaction(fastify: FastifyInstance) {
10099
) {
101100
await SendTransactionQueue.remove({ queueId, retryCount });
102101
}
103-
break;
104-
case "sent":
102+
103+
cancelledTransaction = {
104+
...transaction,
105+
status: "cancelled",
106+
cancelledAt: new Date(),
107+
sentAt: new Date(),
108+
sentAtBlock: await getBlockNumberish(transaction.chainId),
109+
110+
isUserOp: false,
111+
nonce: -1,
112+
sentTransactionHashes: [],
113+
};
114+
} else if (transaction.status === "sent") {
105115
// Cancel a sent transaction with the same nonce.
106116
const { chainId, from, nonce } = transaction;
107-
transactionHash = await sendCancellationTransaction({
117+
const transactionHash = await sendCancellationTransaction({
108118
chainId,
109119
from,
110-
nonce: Number(nonce),
120+
nonce,
111121
});
112-
break;
122+
cancelledTransaction = {
123+
...transaction,
124+
status: "cancelled",
125+
cancelledAt: new Date(),
126+
sentTransactionHashes: [transactionHash],
127+
};
128+
}
129+
}
113130

114-
case "mined":
115-
case "cancelled":
116-
case "errored":
117-
// Cannot cancel a transaction that is already completed.
118-
throw createCustomError(
119-
"Transaction cannot be cancelled.",
120-
StatusCodes.BAD_REQUEST,
121-
"TRANSACTION_CANNOT_BE_CANCELLED",
122-
);
131+
if (!cancelledTransaction) {
132+
throw createCustomError(
133+
"Transaction cannot be cancelled.",
134+
StatusCodes.BAD_REQUEST,
135+
"TRANSACTION_CANNOT_BE_CANCELLED",
136+
);
123137
}
124138

125-
const cancelledTransaction: CancelledTransaction = {
126-
...transaction,
127-
status: "cancelled",
128-
sentAt: "sentAt" in transaction ? transaction.sentAt : new Date(),
129-
sentAtBlock:
130-
"sentAtBlock" in transaction
131-
? transaction.sentAtBlock
132-
: await getBlockNumberish(transaction.chainId),
133-
data: transaction.data ?? "0x",
134-
nonce: "nonce" in transaction ? transaction.nonce : -1,
135-
retryCount: "retryCount" in transaction ? transaction.retryCount : 0,
136-
sentTransactionHashes: [transactionHash],
137-
cancelledAt: new Date(),
138-
};
139+
// A queued or sent transaction was successfully cancelled.
139140
await TransactionDB.set(cancelledTransaction);
140141
await enqueueTransactionWebhook(cancelledTransaction);
141142
await _reportUsageSuccess(cancelledTransaction);
@@ -145,7 +146,7 @@ export async function cancelTransaction(fastify: FastifyInstance) {
145146
queueId,
146147
status: "success",
147148
message,
148-
transactionHash,
149+
transactionHash: cancelledTransaction.sentTransactionHashes.at(-1),
149150
},
150151
});
151152
},

src/server/routes/transaction/syncRetry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export async function syncRetryTransaction(fastify: FastifyInstance) {
6969
"TRANSACTION_NOT_FOUND",
7070
);
7171
}
72-
if (transaction.status !== "sent") {
72+
if (transaction.status !== "sent" || transaction.isUserOp) {
7373
throw createCustomError(
7474
"Transaction cannot be retried.",
7575
StatusCodes.BAD_REQUEST,
@@ -90,7 +90,7 @@ export async function syncRetryTransaction(fastify: FastifyInstance) {
9090
maxPriorityFeePerGas: maxPriorityFeePerGas
9191
? BigInt(maxPriorityFeePerGas)
9292
: undefined,
93-
nonce: Number(transaction.nonce),
93+
nonce: transaction.nonce,
9494
},
9595
});
9696

src/server/schemas/transaction/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ export const toTransactionSchema = (
241241
const resolveTransactionHash = (): string | null => {
242242
switch (transaction.status) {
243243
case "sent":
244-
return transaction.sentTransactionHashes.at(-1) ?? null;
244+
if (!transaction.isUserOp) {
245+
return transaction.sentTransactionHashes.at(-1) ?? null;
246+
}
247+
break;
245248
case "mined":
246249
return transaction.transactionHash;
247250
}

src/utils/env.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,6 @@ export const env = createEnv({
118118
throw new Error("Invalid environment variables");
119119
},
120120
});
121+
122+
console.log("[DEBUG] env", env);
123+
throw "done";

src/utils/transaction/cancelTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const sendCancellationTransaction = async (
2323
to: from,
2424
data: "0x",
2525
value: 0n,
26-
nonce: Number(nonce),
26+
nonce,
2727
},
2828
});
2929

0 commit comments

Comments
 (0)