OpenZeppelin / openzeppelin-test-helpers

Assertion library for Ethereum smart contract testing
https://docs.openzeppelin.com/test-helpers
MIT License
416 stars 132 forks source link

txCost/Fee Helper? #16

Open simondlr opened 5 years ago

simondlr commented 5 years ago

Hey.

A pattern I've often had to do was to either estimate the tx cost (gas gasPrice) in wei beforehand OR after a transaction take gasUsed gasPrice to calculate how much wei was spent in the transaction. This is used in transactions, for example, where someone withdraws ETH, to see if the balance AFTER withdrawal is correct [withdrawn ETH - tx fee to withdraw ETH].

Is this something that would be meaningful? Are there others who want a similar helper? If so, I might take a stab at it, but first want to gauge interest.

Something like:

estimatedTxCost(<contract method>, <tx data>) or txCost(<txReceipt>, <gasPrice>)

Unsure if this would be meaningful (or something like it). So want to hear from others.

nventuro commented 5 years ago

This may be very useful, yes! A trick we sometimes do when calculating how a transaction affected balance, without taking into account gas, is to set gasPrice: 0 for that tx (which works fine on ganache). I can see however other scenarios where such a helper could be used.

frangio commented 5 years ago

I agree this would be very useful. I'm not sure what a nice API for it would look like. I wonder if we can do something similar to balance.tracker?

simondlr commented 5 years ago

A trick we sometimes do when calculating how a transaction affected balance, without taking into account gas, is to set gasPrice: 0 for that tx (which works fine on ganache).

Ah yes! Really simple trick to avoid this problem. :)