[Ethereum] the JSON-RPC method to call a contract’s function

json-rpc

I'm trying to figure out what exactly happens when a function of a smart contract gets called. Since all client communication is done through JSON-RPC I'm looking at their wiki to find which JSON-RPC function is called when you call a smart contract function.

Is this different from view functions to payable ones?
Or do all function calls use the JSON-RPC function eth_sendTransaction and include the function's name as hash in the data field?

Best Answer

To call contract function (read only) you can use

https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_call

To mutate Ethereum state (transferring value, data) you can use

https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sendtransaction

On both cases the transaction is specific to the smart contract codebase, defined by Solidity compiler. It is usually 32-bit function id followed by 256-bit function arguments.

To encode the payload for the smart contract call you usually use a library like web3.py or web3.js specific to your programming language. The library reads the contract ABI data (JSON) and constructs the function payload call accordingly.

Then the smart contract function entry point reads the data payload and jumps to the function it thinks the data payload was assigned to. If no function match the given function id then the smart contract executes Solidity's fallback function.