1
votes

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 parameter, 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 suppose both pay the same amount of gas, am I write ? If no, how I can analyse how much gas each node will cost to interact with the smart contract..

Thanks

1
I noticed that the first invoke will cost more, while the next invokes cost less, is there any extra cost for in the first invoke I have to consider it ?? If yes which node will have it ?? - sheemar

1 Answers

2
votes

If you share the smart contract code, it will be easier to help.

As an educated guess, 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.