Skip to content

Commit d4103fb

Browse files
committed
fix: use isHex
1 parent c77baa1 commit d4103fb

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

features/defi-wrapper/timelock.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
confirmOperation,
55
callReadMethodSilent,
66
} from 'utils';
7-
import { Address, Hex, stringToHex, zeroHash } from 'viem';
7+
import { Address, Hex, stringToHex, zeroHash, isHash } from 'viem';
88
import { getTimeLockContract } from 'contracts/defi-wrapper/index.js';
99
import { getPublicClient } from 'providers';
1010

@@ -23,23 +23,21 @@ export const resolveRole = async (
2323
contractAddress: Address,
2424
getContract: (address: Address) => Promise<any>,
2525
): Promise<Hex> => {
26-
if (!roleInput.startsWith('0x')) {
27-
const contract = await getContract(contractAddress);
28-
try {
29-
const role = (await callReadMethodSilent({
30-
contract,
31-
methodName: roleInput as any,
32-
payload: [],
33-
})) as Hex;
34-
logInfo(`Resolved role "${roleInput}" to ${role}`);
35-
return role;
36-
} catch {
37-
throw new Error(
38-
`Failed to resolve role "${roleInput}". Please provide a valid role name (e.g., DEFAULT_ADMIN_ROLE) or bytes32 hex.`,
39-
);
40-
}
41-
} else {
42-
return roleInput as Hex;
26+
if (isHash(roleInput)) return roleInput;
27+
28+
const contract = await getContract(contractAddress);
29+
try {
30+
const role = (await callReadMethodSilent({
31+
contract,
32+
methodName: roleInput as any,
33+
payload: [],
34+
})) as Hex;
35+
logInfo(`Resolved role "${roleInput}" to ${role}`);
36+
return role;
37+
} catch {
38+
throw new Error(
39+
`Failed to resolve role "${roleInput}". Please provide a valid role name (e.g., DEFAULT_ADMIN_ROLE) or bytes32 hex.`,
40+
);
4341
}
4442
};
4543

utils/transactions/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Abi, decodeErrorResult, Hex, SimulateCallsReturnType } from 'viem';
1+
import { Abi, decodeErrorResult, isHex, SimulateCallsReturnType } from 'viem';
22
import { DashboardAbi } from 'abi';
33
import { printError } from 'utils';
44

@@ -18,11 +18,11 @@ export const simulateCallsErrorHandler = (
1818

1919
if (data) {
2020
// Check if data is already decoded (object) or needs decoding (hex string)
21-
if (typeof data === 'string' && data.startsWith('0x')) {
21+
if (typeof data === 'string' && isHex(data)) {
2222
// data is a hex string, decode it
2323
const { errorName, args } = decodeErrorResult({
2424
abi: abi ?? DashboardAbi,
25-
data: data as Hex,
25+
data: data,
2626
});
2727

2828
const errorArgs = args?.map((a) => a?.toString() ?? '') ?? [];

0 commit comments

Comments
 (0)