百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
H

hardhat-tracer

> 编程语言
开源

️ 允许您在控制台中查看内部呼叫、事件和存储操作

365 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

️ 允许您在控制台中查看内部呼叫、事件和存储操作

hardhat-tracer ️

Allows you to see events, calls and storage operations when running your tests.

  • Installation
  • Usage
    • Test
    • Trace Tx
    • Trace Call
    • Calldata decoder
    • Address name tags
    • State overrides
    • Chai util
    • Programatically access trace info
    • Register --trace option to custom tasks
    • Print debug logs
    • Config parameters

Installation

Step 1: Install the package

bash
npm i hardhat-tracer

Step 2: Add to your hardhat.config.js file

require("hardhat-tracer");

Usage

Test

…

You can just enable trace some code snippet in your tests:

typescript
hre.tracer.enabled = true;
await myContract.doStuff(val2);
hre.tracer.enabled = false;

Trace Tx

You can trace a mainnet transaction and ABIs/artifacts in your project and 4byte directory will be used to decode the internal message calls.

bash
npx hardhat trace --hash 0xTransactionHash # works if mainnet fork is on
npx hardhat trace --hash 0xTransactionHash --rpc https://url # must be archive node

Note: you can also use state overrides to override any state, e.g. things like adding console logs to the contracts involved in the trace or change balances.

Trace Call

You can trace a call on mainnet and ABIs/artifacts in your project and 4byte directory will be used to decode the internal message calls.

bash
npx hardhat tracecall --to 0xAddr --data 0xData --from 0xAddr --value 123 # works if mainnet fork is on
npx hardhat tracecall --to 0xAddr --data 0xData --opcodes SLOAD --rpc https://url --blocknumber 1200000 # must be archive node

Note: you can also use state overrides to override any state, e.g. things like adding console logs to the contracts involved in the trace or change balances.

Calldata decoder

If you are just looking for a quick decode of calldata, return data or Solidity"s Custom Error:

…

For decoding logs

$ npx hardhat decodelog 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef 0xdCB90A71Db48D673A9A483c9F355EBaE93F66A86 0x0D0707963952f2fBA59dD06f2b425ace40b492Fe --data 0x00000000000000000000000000000000000000000000054b40b1f852bd800000
Transfer(src: 0xdCB90A71Db48D673A9A483c9F355EBaE93F66A86, dst: 0x0D0707963952f2fBA59dD06f2b425ace40b492Fe, wad: 24999999999999997902848)

Address name tags

You can set display names / name tags for unknown addresses by adding new entry to hre.tracer.nameTags object in your test cases, see following example:

typescript
hre.tracer.nameTags[this.arbitrager.address] = "Arbitrager";

or can be set in hardhat config

typescript
tracer: {
  nameTags: {
    "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266": "Hunter",
    [someVariable]: "MyContract",
  },
},

State overrides

These state overrides are applied when the EthereumJS/VM is created inside hardhat.

typescript
tracer: {
  stateOverrides: {
    "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2": {
      storage: {
        "0": 100,
        "0x1abf42a573070203916aa7bf9118741d8da5f9522f66b5368aa0a2644f487b38": 0,
      },
      bytecode: "0x30FF",
      balance: parseEther("2"),
      nonce: 2
    },
    [someAddress]: {
      bytecode: "MyContract" // will compile and use the bytecode and link any libraries needed
    }
  },
},

Chai util

Allows to add a test case to check whether last tx did an internal message call.

typescript
expect(hre.tracer.lastTrace()).to.have.messageCall(
  await contract.populateTransaction.getData(),
  {
    returnData: defaultAbiCoder.encode(["uint"], ["1234"]),
    from: otherContract.address,
    isDelegateCall: true,
    isStaticCall: true,
    isSuccess: true,
  }
);

Programatically access trace info

You can programatically access detail information about previous traces in your test cases.

typescript
import {CallItem} from 'hardhat-tracer'

// can be a read call, estimate gas, write tx
await contract.method()

// traverse to the right location in the trace and extract the gasUsed
const trace1 = hre.tracer.lastTrace()!;
const callItem = trace1.top?.children?.[0].children?.[0] as CallItem;
const gasUsed = callItem.params.gasUsed!;

If you are running npx hardhat node --trace, then you can also access last trace using the rpc call tracer_lastTrace.

Register --trace option to custom tasks

typescript
// hardhat config
tracer: {
  tasks: ["deploy", "mycooltask"],
},

This allows to run

bash
npx hardhat deploy --trace
npx hardhat mycooltask --trace

Please note that hardhat-tracer will only be able to print trace for things that run on the EthereumJS VM inside Hardhat Network. So if your custom task is executing evm code somewhere else e.g. deploying on live network (instead of hardhat network), then this option would not be able to print trace.

Print debug logs

Set the DEBUG env variable to hardhat-tracer:*. This will print debug logs related to internal logic in hardhat-tracer for debugging purposes.

bash
DEBUG=hardhat-tracer:* npx hardhat test --trace

Config parameters

List of supported configs is available in TracerEnvUser.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptblockchainethereumeventsevm

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言