When one contract's function read data from second contract's function (that is, there is no state change on the second contract). Does it consumes gas?
1) Queries
If you just want to get information without changing state then yes you can query a contract for free. Querying means you can call any function that is marked as view or pure and there is no gas cost. In these cases whatever node you ask can answer the query immediately without having to ask any other node.
2) Transactions
If you want to modify state then there is a gas cost, and you have to send a transaction and pay the gas.
3) Queries inside Transactions
I thought your original question was about whether there was a cost to querying inside a transaction. This does consume additional gas. I tried this experiment in Remix with Solidity 0.6.1 (most code omitted for clarity):
// Gas used = 24,656
function SetSomethingInAnotherContract_WithoutCall() public
{
anotherContract.SetSomething(4);
}
// Gas used = 28,124
function SetSomethingInAnotherContract_WithCall() public
{
uint temp = anotherContract.GetSomething(); // in a query this would be free
anotherContract.SetSomething(4);
}
I think it makes sense that it should incur a cost because a query can be answered from a single node, but the transaction calls have to be validated by all nodes.
© 2022 - 2024 — McMap. All rights reserved.