Skip to content

Commit 232c4f5

Browse files
committed
fix: assert the error.data response
1 parent ecd99c3 commit 232c4f5

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

v-next/hardhat/src/internal/builtin-plugins/network-manager/edr/edr-provider.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ import { getJsonRpcRequest, isFailedJsonRpcResponse } from "../json-rpc.js";
3939
import { getGlobalEdrContext } from "./edr-context.js";
4040
import { InvalidArgumentsError, ProviderError } from "./errors.js";
4141
import { createSolidityErrorWithStackTrace } from "./stack-traces/stack-trace-solidity-errors.js";
42+
import {
43+
isDebugTraceResult,
44+
isEdrProviderErrorData,
45+
} from "./type-validation.js";
4246
import { clientVersion } from "./utils/client-version.js";
4347
import { ConsoleLogger } from "./utils/console-logger.js";
4448
import {
@@ -50,7 +54,6 @@ import {
5054
hardhatChainsToEdrChains,
5155
hardhatForkingConfigToEdrForkConfig,
5256
hardhatChainTypeToEdrChainType,
53-
isDebugTraceResult,
5457
} from "./utils/convert-to-edr.js";
5558
import { printLine, replaceLastLine } from "./utils/logger.js";
5659

@@ -283,20 +286,18 @@ export class EdrProvider extends BaseProvider {
283286
}
284287

285288
if (stackTrace !== null) {
286-
// Pass data and transaction hash from the original error
287-
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
288-
-- If we have a stack trace, we know that the json rpc response data
289-
is an object with the data and transactionHash fields */
290-
const responseErrorData = responseError.data as {
291-
data?: string;
292-
transactionHash?: string;
293-
};
289+
// If we have a stack trace, we know that the json rpc response data
290+
// is an object with the data and transactionHash fields
291+
assertHardhatInvariant(
292+
isEdrProviderErrorData(responseError.data),
293+
"Invalid error data",
294+
);
294295

295296
error = createSolidityErrorWithStackTrace(
296297
responseError.message,
297298
stackTrace,
298-
responseErrorData?.data,
299-
responseErrorData?.transactionHash,
299+
responseError.data.data,
300+
responseError.data.transactionHash,
300301
);
301302
} else {
302303
error =

v-next/hardhat/src/internal/builtin-plugins/network-manager/edr/stack-traces/stack-trace-solidity-errors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
export function createSolidityErrorWithStackTrace(
2121
fallbackMessage: string,
2222
stackTrace: SolidityStackTrace,
23-
data?: string,
23+
data: string,
2424
transactionHash?: string,
2525
): SolidityError {
2626
const originalPrepareStackTrace = Error.prepareStackTrace;
@@ -298,7 +298,7 @@ export class SolidityError extends Error {
298298
constructor(
299299
message: string,
300300
public readonly stackTrace: SolidityStackTrace,
301-
public readonly data?: string,
301+
public readonly data: string,
302302
public readonly transactionHash?: string,
303303
) {
304304
super(message);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { DebugTraceResult } from "@ignored/edr-optimism";
2+
3+
import { isObject } from "@ignored/hardhat-vnext-utils/lang";
4+
5+
export function isDebugTraceResult(
6+
result: unknown,
7+
): result is DebugTraceResult {
8+
return (
9+
isObject(result) &&
10+
"pass" in result &&
11+
"gasUsed" in result &&
12+
"structLogs" in result
13+
);
14+
}
15+
16+
interface EdrProviderErrorData {
17+
data: string;
18+
transactionHash?: string;
19+
}
20+
21+
export function isEdrProviderErrorData(
22+
errorData: unknown,
23+
): errorData is EdrProviderErrorData {
24+
return isObject(errorData) && "data" in errorData;
25+
}

v-next/hardhat/src/internal/builtin-plugins/network-manager/edr/utils/convert-to-edr.ts

-12
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import {
4141
GENERIC_CHAIN_TYPE as EDR_GENERIC_CHAIN_TYPE,
4242
} from "@ignored/edr-optimism";
4343
import { getUnprefixedHexString } from "@ignored/hardhat-vnext-utils/hex";
44-
import { isObject } from "@ignored/hardhat-vnext-utils/lang";
4544

4645
import { L1_CHAIN_TYPE, OPTIMISM_CHAIN_TYPE } from "../../../../constants.js";
4746
import { FixedValueConfigurationVariable } from "../../../../core/configuration-variables.js";
@@ -233,17 +232,6 @@ export function edrRpcDebugTraceToHardhat(
233232
};
234233
}
235234

236-
export function isDebugTraceResult(
237-
result: unknown,
238-
): result is DebugTraceResult {
239-
return (
240-
isObject(result) &&
241-
"pass" in result &&
242-
"gasUsed" in result &&
243-
"structLogs" in result
244-
);
245-
}
246-
247235
export async function hardhatAccountsToEdrOwnedAccounts(
248236
accounts: EdrNetworkAccountsConfig,
249237
): Promise<OwnedAccount[]> {

0 commit comments

Comments
 (0)