zemse / hardhat-tracer

🕵️ allows you to see internal calls, events and storage operations in the console
MIT License
341 stars 35 forks source link

How to visit traces happened inside transaction? #29

Open k06a opened 1 year ago

k06a commented 1 year ago

Imagine I wish to check gas_used by 2 traces inside transaction using v2?

For example, I am writing gas tests for Limit Order Protocol, and wish to check gas used by protocol, so I am going to calculate

tx.gas_used - tokenA.transferFrom.gas_used - tokenB.transferFrom.gas_used
zemse commented 1 year ago

There could be a better API, but currently, the particular value can be taken from the tx trace object.

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