Skip to content

Commit 83ce381

Browse files
authored
Fix tx overrides (#130)
* Add util to remove invalid values * Add test * lint files * Fix linting
1 parent 3ed83eb commit 83ce381

File tree

4 files changed

+121
-6
lines changed

4 files changed

+121
-6
lines changed

.gitignore

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ crash.log
232232
crash.*.log
233233

234234
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
235-
# password, private keys, and other secrets. These should not be part of version
236-
# control as they are data points which are potentially sensitive and subject
235+
# password, private keys, and other secrets. These should not be part of version
236+
# control as they are data points which are potentially sensitive and subject
237237
# to change depending on the environment.
238238
*.tfvars
239239
*.tfvars.json
@@ -257,4 +257,7 @@ terraform.rc
257257

258258
# Ignore CLI configuration files
259259
.vscode
260-
*storybook.log
260+
*storybook.log
261+
262+
# IntelliJ
263+
.idea

apps/api/src/routes/transactions.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
normalizeEngineErrorMessage,
2828
parseEngineErrorMessage,
2929
} from "../utils/error";
30+
import { parseTxOverrides } from "../utils/transaction";
3031

3132
export const transactionsRoutes =
3233
({ db, engine, env }: TdkApiContext): FastifyPluginAsync =>
@@ -106,6 +107,8 @@ export const transactionsRoutes =
106107
),
107108
);
108109

110+
const parsedTxOverrides = parseTxOverrides(txOverrides);
111+
109112
const { result } = await engine.contract.write(
110113
chain.id.toString(),
111114
address,
@@ -114,7 +117,7 @@ export const transactionsRoutes =
114117
abi: transactionAbi,
115118
functionName,
116119
args,
117-
txOverrides,
120+
txOverrides: parsedTxOverrides,
118121
},
119122
simulateTransaction,
120123
undefined,
@@ -174,6 +177,8 @@ export const transactionsRoutes =
174177
});
175178
}
176179

180+
const parsedTxOverrides = parseTxOverrides(txOverrides);
181+
177182
try {
178183
Sentry.setExtra("transaction", { to, value, data });
179184
const { result } = await engine.backendWallet.sendTransaction(
@@ -183,7 +188,7 @@ export const transactionsRoutes =
183188
toAddress: to,
184189
value: value,
185190
data,
186-
txOverrides,
191+
txOverrides: parsedTxOverrides,
187192
},
188193
simulateTransaction,
189194
undefined,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { describe, expect, it } from "vitest";
2+
import { parseTxOverrides } from "./transaction";
3+
4+
describe("transaction utils", () => {
5+
it("should parse tx overrides", () => {
6+
expect(parseTxOverrides()).toBeUndefined();
7+
8+
expect(parseTxOverrides({})).toBeUndefined();
9+
10+
expect(parseTxOverrides({ gas: "10000000000" })).toStrictEqual({
11+
gas: "10000000000",
12+
maxFeePerGas: undefined,
13+
maxPriorityFeePerGas: undefined,
14+
value: undefined,
15+
});
16+
17+
expect(parseTxOverrides({ maxFeePerGas: "10000000000" })).toStrictEqual({
18+
gas: undefined,
19+
maxFeePerGas: "10000000000",
20+
maxPriorityFeePerGas: undefined,
21+
value: undefined,
22+
});
23+
24+
expect(
25+
parseTxOverrides({ maxPriorityFeePerGas: "10000000000" }),
26+
).toStrictEqual({
27+
gas: undefined,
28+
maxFeePerGas: undefined,
29+
maxPriorityFeePerGas: "10000000000",
30+
value: undefined,
31+
});
32+
33+
expect(parseTxOverrides({ value: "10000000000" })).toStrictEqual({
34+
gas: undefined,
35+
maxFeePerGas: undefined,
36+
maxPriorityFeePerGas: undefined,
37+
value: "10000000000",
38+
});
39+
40+
expect(
41+
parseTxOverrides({
42+
gas: "10000000000",
43+
maxFeePerGas: "10000000000",
44+
maxPriorityFeePerGas: "10000000000",
45+
value: "10000000000",
46+
}),
47+
).toStrictEqual({
48+
gas: "10000000000",
49+
maxFeePerGas: "10000000000",
50+
maxPriorityFeePerGas: "10000000000",
51+
value: "10000000000",
52+
});
53+
54+
expect(
55+
parseTxOverrides({
56+
gas: "",
57+
maxFeePerGas: "10000000000",
58+
maxPriorityFeePerGas: "10000000000",
59+
value: "10000000000",
60+
}),
61+
).toStrictEqual({
62+
gas: undefined,
63+
maxFeePerGas: "10000000000",
64+
maxPriorityFeePerGas: "10000000000",
65+
value: "10000000000",
66+
});
67+
68+
expect(
69+
parseTxOverrides({
70+
gas: null as unknown as string,
71+
maxFeePerGas: "10000000000",
72+
maxPriorityFeePerGas: null as unknown as string,
73+
value: "10000000000",
74+
}),
75+
).toStrictEqual({
76+
gas: undefined,
77+
maxFeePerGas: "10000000000",
78+
maxPriorityFeePerGas: undefined,
79+
value: "10000000000",
80+
});
81+
});
82+
});

apps/api/src/utils/transaction.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ import type {
99

1010
import type { TransactionArguments, TransactionOverrides } from "../schema";
1111

12+
export const parseTxOverrides = (
13+
txOverrides?: TransactionOverrides,
14+
): TransactionOverrides | undefined => {
15+
const gas = txOverrides?.gas ? txOverrides.gas : undefined;
16+
const maxFeePerGas = txOverrides?.maxFeePerGas
17+
? txOverrides.maxFeePerGas
18+
: undefined;
19+
const maxPriorityFeePerGas = txOverrides?.maxPriorityFeePerGas
20+
? txOverrides.maxPriorityFeePerGas
21+
: undefined;
22+
const value = txOverrides?.value ? txOverrides.value : undefined;
23+
if (gas || maxFeePerGas || maxPriorityFeePerGas || value) {
24+
return {
25+
gas,
26+
maxFeePerGas,
27+
maxPriorityFeePerGas,
28+
value,
29+
};
30+
}
31+
return undefined;
32+
};
33+
1234
export const writeTransaction = async <
1335
TAbi extends Abi,
1436
TFunctionName extends ExtractAbiFunctionNames<
@@ -64,6 +86,9 @@ export const writeTransaction = async <
6486
2,
6587
),
6688
);
89+
90+
const parsedTxOverrides = parseTxOverrides(txOverrides);
91+
6792
const { result } = await engine.contract.write(
6893
chainId.toString(),
6994
contractAddress,
@@ -74,7 +99,7 @@ export const writeTransaction = async <
7499
functionName,
75100
// @ts-ignore: stronger type-checking than Engine
76101
args,
77-
txOverrides,
102+
txOverrides: parsedTxOverrides,
78103
},
79104
simulateTransaction,
80105
idempotencyKey,

0 commit comments

Comments
 (0)