How would I get the transaction cost inside of my contract? Would it just be: tx.gasprice
? And will this always be a value in gwei or will it be wei ?
How to get transaction cost in smart contract - Solidity, Ethereum
The cost of a transaction isn't really known until execution completes. In an extreme example, perhaps your function which is computing this is being called by another function, and after you return, that function throw
s, consuming all remaining gas. There's no way for you to know in advance that this will happen.
To calculate the cost of the transaction, you'd need two pieces of information:
- The gas price.
- How much gas will be consumed.
If you knew both, you could multiple them together and get the total cost. tx.gasprice
tells you (1), but as explained above, you can't really know (2). The best you can do is probably to use msg.gas
at the top and bottom of a function to tell you roughly how much gas that function consumes.
© 2022 - 2024 — McMap. All rights reserved.