zemse/hardhat-tracer

How to visit traces happened inside transaction?

Opened this issue · 1 comments

k06a commented

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

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