0 votes
1 view
in Blockchain by (12.8k 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.


1 Answer

0 votes
by (29.8k points)

 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !