Skip to content

Commit e4e4be9

Browse files
committed
clean up response
1 parent 8d37d3b commit e4e4be9

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

src/server/utils/transaction.ts

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,12 @@ export const cancelTransactionAndUpdate = async ({
6464
} else {
6565
switch (txData.status) {
6666
case TransactionStatus.Errored: {
67-
if (!txData.chainId || !txData.fromAddress) {
68-
throw new Error("Invalid transaction state to cancel.");
69-
}
70-
if (txData.nonce) {
71-
const { transactionHash, error } = await sendNullTransaction({
67+
if (txData.chainId && txData.fromAddress && txData.nonce) {
68+
return await sendNullTransaction({
7269
chainId: parseInt(txData.chainId),
7370
walletAddress: txData.fromAddress,
7471
nonce: txData.nonce,
7572
});
76-
if (error) {
77-
return { message: error };
78-
}
79-
80-
return {
81-
message: "Transaction cancelled successfully.",
82-
transactionHash,
83-
};
8473
}
8574

8675
throw createCustomError(
@@ -113,30 +102,24 @@ export const cancelTransactionAndUpdate = async ({
113102
"TransactionAlreadyMined",
114103
);
115104
case TransactionStatus.Sent: {
116-
if (!txData.chainId || !txData.fromAddress || !txData.nonce) {
117-
throw new Error("Invalid transaction state to cancel.");
118-
}
105+
if (txData.chainId && txData.fromAddress && txData.nonce) {
106+
const { transactionHash, message } = await sendNullTransaction({
107+
chainId: parseInt(txData.chainId),
108+
walletAddress: txData.fromAddress,
109+
nonce: txData.nonce,
110+
});
111+
if (transactionHash) {
112+
await updateTx({
113+
queueId,
114+
pgtx,
115+
data: {
116+
status: TransactionStatus.Cancelled,
117+
},
118+
});
119+
}
119120

120-
const { transactionHash, error } = await sendNullTransaction({
121-
chainId: parseInt(txData.chainId),
122-
walletAddress: txData.fromAddress,
123-
nonce: txData.nonce,
124-
});
125-
if (error) {
126-
return { message: error };
121+
return { message, transactionHash };
127122
}
128-
129-
await updateTx({
130-
queueId,
131-
pgtx,
132-
data: {
133-
status: TransactionStatus.Cancelled,
134-
},
135-
});
136-
return {
137-
message: "Transaction cancelled successfully.",
138-
transactionHash,
139-
};
140123
}
141124
}
142125
}
@@ -150,8 +133,8 @@ const sendNullTransaction = async (args: {
150133
nonce: number;
151134
transactionHash?: string;
152135
}): Promise<{
136+
message: string;
153137
transactionHash?: string;
154-
error?: string;
155138
}> => {
156139
const { chainId, walletAddress, nonce, transactionHash } = args;
157140

@@ -162,7 +145,7 @@ const sendNullTransaction = async (args: {
162145
if (transactionHash) {
163146
const txReceipt = await provider.getTransactionReceipt(transactionHash);
164147
if (txReceipt) {
165-
return { error: "Transaction already mined." };
148+
return { message: "Transaction already mined." };
166149
}
167150
}
168151

@@ -176,8 +159,11 @@ const sendNullTransaction = async (args: {
176159
nonce,
177160
...multiplyGasOverrides(gasOverrides, 2),
178161
});
179-
return { transactionHash: hash };
162+
return {
163+
message: "Transaction cancelled successfully.",
164+
transactionHash: hash,
165+
};
180166
} catch (e: any) {
181-
return { error: e.toString() };
167+
return { message: e.toString() };
182168
}
183169
};

0 commit comments

Comments
 (0)