Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (12.7k points)

I would like to know how I can exactly calculate the amount of gas used in a transaction.

In Remix, I have used the first account and invoke a method in my smart contract with 2 parameters, string with value "p1" and uint with value 101.

The transaction cost is 198996 gas and the execution cost is 176892 gas

I have used the second account and invoke the same method with the same parameter {string with value "p1" and uint with value 101} but the transaction cost was 145032 gas and the execution cost was 122928

My question is: how the total amount of gas consumed can be different?? Although I have used the same load of inputs (the same string length and the same uint value)? How I can calculate how much gas will cost each node in the network if they pass the same parameter and the same method but the cost is different?

If 2 nodes in the network invoke the same method with the same parameter, it supposes both pay the same amount of gas, am I write? If no, how I can analyze how much gas each node will cost to interact with the smart contract.

Thanks

1 Answer

0 votes
by (29.5k points)
edited by

 I assume each transaction is setting a state variable? That involves an SSTORE opcode, and the gas cost of an SSTORE depends on the value being stored and the value previously in storage. Storing a non-zero where a zero used to be (e.g. a non-empty string where there used to be an empty string) costs 20,000 gas, while storing a non-zero where there was already a non-zero costs only 5,000 gas.

If you want to be an expert in your field, then enroll in Blockchain Certification by Intellipaat.

Browse Categories

...