Questions regarding the act of calling or interacting with (a.k.a. invoking) a contract on the Ethereum blockchain by a user or another contract on the blockchain. Includes both local invocation by call and global invocation by transaction.
Local Invocation by Call
A call is a local invocation of a contract function that does not broadcast or publish anything on the blockchain. It is a read-only operation and will not consume any Ether. It simulates what would happen in a transaction, but discards all the state changes when it is done. It is synchronous and the return value of the contract function is returned immediately.
Its web3.js API is web3.eth.call and is what's used for Solidity constant
functions. Its underlying JSON-RPC is eth_call.
Global Invocation by Transaction
A transaction is broadcasted to the network, processed by miners, and if valid, is published on the blockchain. It is a write-operation that will affect other accounts, update the state of the blockchain, and consume Ether (unless a miner accepts it with a gas price of zero).
It is asynchronous, because it is possible that no miners will include the transaction in a block (for example, the gas price for the transaction may be too low). Since it is asynchronous, the immediate return value of a transaction is always the transaction's hash. To get the "return value" of a transaction to a function, Events need to be used (unless it's Case4 discussed below).
Its web3.js API is web3.eth.sendTransaction and is used if a Solidity function is not marked constant
. Its underlying JSON-RPC is eth_sendTransaction. sendTransaction
will be used when a verb is needed, since it is clearer than simply transaction.
Recommendation to Call first, then send Transaction
Since a sendTransaction
costs Ether, it is a good practice to "test the waters" by issuing a call first, before sending a transaction. This is a free way to debug and estimate if there will be any problems with the sendTransaction, for example if an Out of Gas exception will be encountered.
This "dry-run" usually works well, but in some cases be aware that call is an estimate, for example a contract function that returns the previous blockhash, will return different results based on when the call was performed, and when the transaction is actually mined.
Finally, note that even though a call does not consume any Ether, sometimes it may be necessary to specify the actual gas amount for the call: the default gas for call in clients such as Geth, may still be insufficient and can still lead to Out of Gas.
Read more: What is the difference between a transaction and a call?