Skip to content

Commit

Permalink
fix(trace): use colors/safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Oct 22, 2024
1 parent a5bcf71 commit 0bcc681
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 33 deletions.
3 changes: 1 addition & 2 deletions src/actions/traceCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
type ExactPartial,
type FormattedTransactionRequest,
type Hex,
type PrepareTransactionRequestParameters,
type RpcTransactionRequest,
type TransactionRequest,
type Transport,
Expand All @@ -21,7 +20,7 @@ import {
import { parseAccount } from "viem/accounts";
import { prepareTransactionRequest } from "viem/actions";
import { recoverAuthorizationAddress } from "viem/experimental";
import { extract, getAction, getTransactionError } from "viem/utils";
import { extract, getTransactionError } from "viem/utils";

export type TraceCallRpcSchema = {
Method: "debug_traceCall";
Expand Down
32 changes: 8 additions & 24 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import { existsSync, readFileSync } from "node:fs";
import { writeFile } from "node:fs/promises";
import { homedir } from "node:os";
import { join } from "node:path";
import "colors";
import { writeFile } from "node:fs/promises";
import { grey, red, yellow } from "colors";
import {
type Address,
type BlockTag,
type Chain,
type Client,
type ExactPartial,
type Hex,
type RpcTransactionRequest,
type Transport,
type UnionOmit,
decodeFunctionData,
isAddress,
parseAbi,
slice,
} from "viem";
import { getAction } from "viem/utils";
import { type RpcCallTrace, type TraceCallParameters, traceCall } from "./actions/traceCall.js";
import { bold, cyan, grey, red, white, yellow } from "colors/safe.js";
import { type Address, type Hex, decodeFunctionData, isAddress, parseAbi, slice } from "viem";
import type { RpcCallTrace } from "./actions/traceCall.js";

export const signaturesPath = join(homedir(), ".foundry", "cache", "signatures");

Expand Down Expand Up @@ -51,9 +35,9 @@ export const getCallTraceUnknownSelectors = (trace: RpcCallTrace): string => {
};

export const getIndentLevel = (level: number, index = false) =>
`${" ".repeat(level - 1)}${index ? `${level - 1} ↳ `.cyan : " "}`;
`${" ".repeat(level - 1)}${index ? cyan(`${level - 1} ↳ `) : " "}`;

export const formatAddress = (address: Address) => `${address.slice(0, 6)}...${address.slice(0, 4)}`;
export const formatAddress = (address: Address) => `${address.slice(0, 8)}${address.slice(0, 4)}`;

export const formatArg = (arg: unknown, level: number): string => {
if (Array.isArray(arg)) {
Expand Down Expand Up @@ -95,15 +79,15 @@ export const formatCallSignature = (trace: RpcCallTrace, level: number) => {

const formattedArgs = args?.map((arg) => formatArg(arg, level)).join(", ");

return `${(trace.error ? red : yellow)(functionName).bold}(${(formattedArgs ?? "").grey})`;
return `${bold((trace.error ? red : yellow)(functionName))}(${grey(formattedArgs ?? "")})`;
};

export const formatCallTrace = (trace: RpcCallTrace, level = 1): string => {
const rest = (trace.calls ?? []).map((subtrace) => formatCallTrace(subtrace, level + 1)).join("\n");

const returnValue = trace.revertReason ?? trace.output;

return `${level === 1 ? `${getIndentLevel(level, true)}FROM ${trace.from.grey}\n`.cyan : ""}${getIndentLevel(level, true)}${trace.type.yellow} ${trace.from === trace.to ? ("self").grey : `(${trace.to.white})`}.${formatCallSignature(trace, level)}${returnValue ? (trace.error ? red : grey)(` -> ${returnValue}`) : ""}
return `${level === 1 ? `${getIndentLevel(level, true)}${cyan("FROM")} ${grey(trace.from)}\n` : ""}${getIndentLevel(level, true)}${yellow(trace.type)} ${trace.from === trace.to ? grey("self") : `(${white(trace.to)})`}.${formatCallSignature(trace, level)}${returnValue ? (trace.error ? red : grey)(` -> ${returnValue}`) : ""}
${rest}`;
};

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./actions/traceCall.js";
export * from "./traceActions.js";
export * from "./middleware.js";
export * from "./format.js";
2 changes: 1 addition & 1 deletion test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BigInt.prototype.toJSON = function () {
return this.toString();
};

disable(); // To avoid snapshot mismatch due to OS colors.
disable();

declare global {
namespace NodeJS {
Expand Down
12 changes: 6 additions & 6 deletions test/traceCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ describe("traceCall", () => {
[ContractFunctionExecutionError: The contract function "transfer" reverted with the following reason:
0 ↳ FROM 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39F...0xf3, 100000000) -> ERC20: transfer amount exceeds balance
1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39F...0xf3, 100000000) -> ERC20: transfer amount exceeds balance
0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance
1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance
Contract Call:
Expand Down Expand Up @@ -109,8 +109,8 @@ describe("traceCall", () => {
[ContractFunctionExecutionError: The contract function "transfer" reverted with the following reason:
0 ↳ FROM 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39F...0xf3, 100000000) -> ERC20: transfer amount exceeds balance
1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39F...0xf3, 100000000) -> ERC20: transfer amount exceeds balance
0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance
1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance
Contract Call:
Expand Down Expand Up @@ -143,8 +143,8 @@ describe("traceCall", () => {
[
[
"0 ↳ FROM 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39F...0xf3, 100000000) -> ERC20: transfer amount exceeds balance
1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39F...0xf3, 100000000) -> ERC20: transfer amount exceeds balance
0 ↳ CALL (0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance
1 ↳ DELEGATECALL (0x43506849d7c04f9138d1a2050bbf3a0c054402dd).transfer(0xf39Fd6…0xf3, 100000000) -> ERC20: transfer amount exceeds balance
",
],
]
Expand Down

0 comments on commit 0bcc681

Please sign in to comment.