0

I am little confuse in the concept of Gas Estimation and price. When i add new user i get popup through metamask for the transaction amount and gas used which is totally fine.

I checked my code in the remix. When i see details the function which are purely reading data or returning the array length of user also use some Gas estimation like function name "totalInvestorInvestment" and "totalLoans". Where as "totalInvestedAmountInLoan" has gas estimate of Infinite its because I am using "For" loop.

enter image description here

1) So how this Gas estimation work as i read that reading data in blockchain is free, so if its free how that Gas usage is paid by the function "totalInvestorInvestment" and "totalLoans"?

2) And is there any limit for the usage of Gas in reading data which is free?

3) Is it good practice to use for loop? To avoid large data set i am also storing the indexes in array and using for loop on that array.

2

So how this Gas estimation work as i read that reading data in blockchain is free, so if its free how that Gas usage is paid by the function "totalInvestorInvestment" and "totalLoans"?

It's net free because read-only ops can't update the chain state. Gas is still subject to accounting and limits. Further, if a read-only function is called in the context of a state-changing transaction, then the gas cost is accounting for and charged.

And is there any limit for the usage of Gas in reading data which is free?

Yes. The block gasLimit applies.

Is it good practice to use for loop? To avoid large data set i am also storing the indexes in array and using for loop on that array.

It's not a good practice to have an unbounded loop or recursion. Things need to be O(1). For more elucidations on patterns, see this: https://blog.b9lab.com/getting-loopy-with-solidity-1d51794622ad

Hope it helps.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.