• Stars
    star
    262
  • Rank 156,136 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🕵️ allows you to see internal calls, events and storage operations in the console

hardhat-tracer 🕵️

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

Installation

Step 1: Install the package

npm i hardhat-tracer

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

require("hardhat-tracer");

Usage

Test

npx hardhat test --traceError # prints calls for failed txs
npx hardhat test --fulltraceError # prints calls and storage ops for failed txs
npx hardhat test --trace # prints calls for all txs
npx hardhat test --fulltrace # prints calls and storage ops for all txs

npx hardhat test --v    # same as --traceError
npx hardhat test --vv   # same as --fulltraceError
npx hardhat test --vvv  # same as --trace
npx hardhat test --vvvv # same as --fulltrace

# specify opcode
npx hardhat test --v --opcodes ADD,SUB   # shows any opcode specified for only failed txs
npx hardhat test --vvv --opcodes ADD,SUB # shows any opcode specified for all txs

Console testing

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

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.

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.

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:

$ npx hardhat decode --data 0x095ea7b300000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ERC20.approve(spender=0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45, amount=115792089237316195423570985008687907853269984665640564039457584007913129639935)


$ npx hardhat decode --data 0x3850c7bd --returndata 0x000000000000000000000000000000000000000000024d0fa9cd4ba6ff769172fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcdea1000000000000000000000000000000000000000000000000000000000000a244000000000000000000000000000000000000000000000000000000000000ff78000000000000000000000000000000000000000000000000000000000000ffff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001

IUniswapV3Pool.slot0() => (sqrtPriceX96: 2781762795090269932261746, tick: -205151, observationIndex: 41540, observationCardinality: 65400, observationCardinalityNext: 65535, feeProtocol: 0, unlocked: true)

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:

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

or can be set in hardhat config

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

State overrides

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

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.

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.

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!;

Register --trace option to custom tasks

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

This allows to run

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.

More Repositories

1

forge-flamegraph

🔥 flamegraphs for solidity
Rust
33
star
2

evm-run

🏃 Run EVM code from console or file, on local or mainnet fork.
JavaScript
25
star
3

zk-proof-of-evm-challenge

🛠️ Uses zkevm-circuits and anvil mainnetfork to prove that a tx solves an EVM challenge
Rust
20
star
4

zk-static-call

prove eth_call result
Rust
15
star
5

ethers-repl

🔪 Ethers.js in Node.js REPL
JavaScript
11
star
6

ether-hunter

Random ethereum wallet balance checker
JavaScript
10
star
7

hardhat-live-fork

🚀 Keeps mainnet fork state updated by replaying relevant live txs
TypeScript
9
star
8

create-solidity-project

Sets up a raw dependency Solidity project with one npx command.
JavaScript
8
star
9

casino-solidity

Casino game built with Solidity, React & Truffle
JavaScript
8
star
10

uniswap-sushiswap-arbitrager

⚡️Flash Arbitrage UniswapV2 and Sushiswap Pairs
Solidity
5
star
11

partial-mpt

calculate new mpt root without loading entire trie
Rust
3
star
12

hardhat-storage-layout-changes

💾 Check for storage layout changes
TypeScript
3
star
13

sunflower

🌻 use L1 multisig owners to sign on L2 multisigs using zk proofs
Solidity
2
star
14

bitcoin3js

Bitcoin Provider with Rate Limit Handling and Fallback Mechanism
JavaScript
2
star
15

ethers-erc4337-demo-scripts

TypeScript
2
star
16

decentralised-registration-ipfs

POC of User Profile dApp powered by IPFS and Ethereum Smart Contracts
JavaScript
1
star
17

kickchainer

A Web 3.0 version of a popular crowd funding Web 2.0 app called KickStarter.
JavaScript
1
star
18

esn-spec

1
star
19

betdeex

The Decentralized Prediction Platform of Era Swap Ecosystem
JavaScript
1
star
20

contract-ownership-governance

Proposed standard to replace onlyOwner with onlyGovernance in Ethereum Smart Contracts
TypeScript
1
star
21

uniswap-v3-trials

Rough ui to try and see some swap txs with zero fees.
TypeScript
1
star
22

simple-lottery

Basic Lottery managed by Smart Contracts.
JavaScript
1
star
23

deno-type-check

Runtime type check library for Deno
TypeScript
1
star
24

pure-PoS-solidity

PoS in Solidity
TypeScript
1
star
25

scroll-repro

Rust
1
star