Skip to content

Commit a6fe358

Browse files
committed
refactor: use hex utils
1 parent 3098e04 commit a6fe358

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

v-next/hardhat-utils/src/hex.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function hexStringToNumber(hexString: string): number {
9595
* @returns PrefixedHexString The hexadecimal representation of the bytes.
9696
*/
9797
export function bytesToHexString(bytes: Uint8Array): PrefixedHexString {
98-
return `0x${Buffer.from(bytes).toString("hex")}`;
98+
return getPrefixedHexString(Buffer.from(bytes).toString("hex"));
9999
}
100100

101101
/**
@@ -141,9 +141,7 @@ export function normalizeHexString(hexString: string): PrefixedHexString {
141141
);
142142
}
143143

144-
return isPrefixedHexString(normalizedHexString)
145-
? normalizedHexString
146-
: `0x${normalizedHexString}`;
144+
return getPrefixedHexString(normalizedHexString);
147145
}
148146

149147
/**

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { numberToHexString } from "@ignored/hardhat-vnext-utils/hex";
2+
13
export function panicErrorCodeToMessage(errorCode: bigint): string {
24
const reason = panicErrorCodeToReason(errorCode);
35

46
if (reason !== undefined) {
5-
return `reverted with panic code 0x${errorCode.toString(16)} (${reason})`;
7+
return `reverted with panic code ${numberToHexString(errorCode)} (${reason})`;
68
}
79

8-
return `reverted with unknown panic code 0x${errorCode.toString(16)}`;
10+
return `reverted with unknown panic code ${numberToHexString(errorCode)}`;
911
}
1012

1113
function panicErrorCodeToReason(errorCode: bigint): string | undefined {

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,7 @@ function getMessageFromLastStackTraceEntry(
237237
}
238238

239239
if (!returnData.isEmpty()) {
240-
const buffer = Buffer.from(returnData.value).toString("hex");
241-
242-
return `VM Exception while processing transaction: reverted with an unrecognized custom error (return data: 0x${buffer})`;
240+
return `VM Exception while processing transaction: reverted with an unrecognized custom error (return data: ${bytesToHexString(returnData.value)})`;
243241
}
244242

245243
if (stackTraceEntry.isInvalidOpcodeError) {

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ import {
4040
L1_CHAIN_TYPE as EDR_L1_CHAIN_TYPE,
4141
GENERIC_CHAIN_TYPE as EDR_GENERIC_CHAIN_TYPE,
4242
} from "@ignored/edr-optimism";
43-
import { getUnprefixedHexString } from "@ignored/hardhat-vnext-utils/hex";
43+
import {
44+
bytesToHexString,
45+
getUnprefixedHexString,
46+
} from "@ignored/hardhat-vnext-utils/hex";
4447

4548
import { L1_CHAIN_TYPE, OPTIMISM_CHAIN_TYPE } from "../../../../constants.js";
4649
import { FixedValueConfigurationVariable } from "../../../../core/configuration-variables.js";
@@ -214,20 +217,23 @@ export function edrRpcDebugTraceToHardhat(
214217
});
215218

216219
// REVM trace adds initial STOP that Hardhat doesn't expect
217-
// ASK: is this still valid?
220+
// TODO: double check with EDR team that this is still the case
218221
if (structLogs.length > 0 && structLogs[0].op === "STOP") {
219222
structLogs.shift();
220223
}
221224

222-
let returnValue = debugTraceResult.output?.toString("hex") ?? "";
225+
let returnValue =
226+
debugTraceResult.output !== undefined
227+
? bytesToHexString(debugTraceResult.output)
228+
: "";
223229
if (returnValue === "0x") {
224230
returnValue = "";
225231
}
226232

227233
return {
228234
failed: !debugTraceResult.pass,
229235
gas: Number(debugTraceResult.gasUsed),
230-
returnValue, // ASK: do we expect an unprefixed hex string here?
236+
returnValue,
231237
structLogs,
232238
};
233239
}

v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/artifacts.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type {
99
SolidityBuildInfoOutput,
1010
} from "../../../../types/solidity/solidity-artifacts.js";
1111

12+
import { getPrefixedHexString } from "@ignored/hardhat-vnext-utils/hex";
13+
1214
export function getContractArtifact(
1315
buildInfoId: string,
1416
publicSourceName: string,
@@ -17,18 +19,16 @@ export function getContractArtifact(
1719
contract: CompilerOutputContract,
1820
): Artifact {
1921
const evmBytecode = contract.evm?.bytecode;
20-
let bytecode: string = evmBytecode?.object ?? "";
21-
22-
if (bytecode.slice(0, 2).toLowerCase() !== "0x") {
23-
bytecode = `0x${bytecode}`;
24-
}
22+
const bytecode: string =
23+
evmBytecode?.object !== undefined
24+
? getPrefixedHexString(evmBytecode.object)
25+
: "";
2526

2627
const evmDeployedBytecode = contract.evm?.deployedBytecode;
27-
let deployedBytecode: string = evmDeployedBytecode?.object ?? "";
28-
29-
if (deployedBytecode.slice(0, 2).toLowerCase() !== "0x") {
30-
deployedBytecode = `0x${deployedBytecode}`;
31-
}
28+
const deployedBytecode: string =
29+
evmDeployedBytecode?.object !== undefined
30+
? getPrefixedHexString(evmDeployedBytecode.object)
31+
: "";
3232

3333
const linkReferences = evmBytecode?.linkReferences ?? {};
3434
const deployedLinkReferences = evmDeployedBytecode?.linkReferences ?? {};

0 commit comments

Comments
 (0)