What is JSON-RPC? - A Guide to Understanding Ethereum RPCs

What is JSON-RPC? - A Guide to Understanding Ethereum RPCs

An Article on Ethereum JSON-RPC, What is it and How it works?

Introduction

Decentralization is the next big thing in the tech world together and is fast gaining popularity amongst tech enthusiasts. Ethereum is a decentralized blockchain platform that enables developers to build and deploy decentralized applications. However, for our application to interact with the Ethereum Blockchain, it must connect to an Ethereum node.

A lot of decentralized platforms like Ethereum use JSON-RPC-based APIs, which provide a specification of the standard interface for Ethereum clients. So in this article, we will be digging into Ethereum JSON-RPCs and how it fits into the entire smart contract-EVM interaction.

What is JSON RPC?

JSON-RPC or JavaScript Object Notation Remote Procedure Call is a data exchange protocol that allows a client (for example, a blockchain explorer) to communicate to a server (such as your local blockchain node) by issuing commands and listening to responses.

It is a remote procedure call protocol that defines a number of data structures as well as the rules for processing them. JSON-RPC refers to two separate concepts, JSON and RPC, that need to be understood.

JSON

JSON (JavaScript Object Notation) is a stateless, lightweight data-interchange format that provides an efficient way of representing data in a human-readable format. For example, it can represent numbers, ordered sequences of values, and collections of value pairs. JSON was popularized in the early 2000s as an alternative to Extensible Markup Language (known as XML), the de facto web standard since the late 1990s. It has now largely replaced XML since its introduction.

JSON is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C#, C++, Java, Perl, Python, JavaScript, and many others. It makes the mapping of data structures much simpler and has the advantage of working with various computer programming languages. These properties make JSON an ideal data-interchange language.

{
"name": "eth_getBlockByNumber",
            "summary": "Returns information about a block by number.",
            "params": [
                {
                    "name": "Block number",
                    "required": true,
                    "schema": {
                        "title": "hex encoded unsigned integer",
                        "type": "string",
                        "pattern": "^0x([1-9a-f]+[0-9a-f]*|0)$"
                    }
                },
            ],
        }

RPC

RPC stands for Remote Procedure Call. It is a protocol in computer science that enables one computer to call a function not defined anywhere on another computer without understanding the network's details. RPC uses the client-server model

In RPCs, there is a method and some arguments, which is similar to calling a function in JavaScript while considering the method name and arguments.

Though RPCs help clients communicate with servers via the traditional use of procedure calls in high-level languages, they, however, aren't always suited for transferring large amounts of data. This is because the use of resources between the client and server is complex.

While some blockchain developers may use RESTful APIs (Representational State Transfer - used by popular frameworks), many focus on using RPC for a more direct connection to the blockchain.

What is Ethereum JSON-RPC?

Smart contacts allow for interaction between a network of users on the Ethereum network. Smart contracts also require JSON-RPC for interactions (an argument or a transaction) to occur on the blockchain.

The Ethereum JSON-RPC is a set of methods that all clients implement. This interface enables downstream tooling and infrastructure to treat various Ethereum clients as interchangeable modules.

This provides several options to construct a backend and fire up your node manually. Thirdweb and Moralis are tools that have made the deployment of dApps easier.

Most blockchain users communicate with clients via Web3 JSON-RPC. Simply put, you may define Ethereum's various data formats using Web3 JSON-RPC. It also specifies how such data structures should be processed in the network. You can use it to interact with an ETH node over various environments, sockets, or HTTP.

JSON-RPC and related applications, such as Ethereum JSON-RPC when working with Solidity, smart contracts, and the Ethereum Virtual Machine, are built on such synchronous and asynchronous processes (EVM).

Now, let's have a look at what all JSON-RPC supports.

Skarmavbild-2021-07-14-kl.-08.51.23-768x409.png

Presently, two specifications of JSON-RPC are available, JSON-RPC 1.0 a and JSON-RPC- 2.0. JSON RPC 1.0, which was more of a peer-to-peer communication method, could only support an area of parameters.

JSON-RPC 2.0

The JSON-RPC v2.0 specification was released in 2010 and aimed to provide a simple RPC mechanism by adding support for named parameters. Basically, the function we ask an Ethereum node to execute is a method. It will request data either from the node, execute an EVM function and return a response, or transmit data to the Ethereum network.

A successful Request - must contain the following property:

  • method - A String containing the name of the method we are asking the Ethererum node to invoke. This could either execute an EVM function, request data from the node, or transmit data to the Ethereum network. There are over 60 methods that supports popular Ethereum clients.

  • jsonrpc - A String specifying the version of the JSON-RPC protocol. It must be exactly "2.0".

And optionally contain the properties:

  • params - A Structured value that holds the parameter values to be used during the invocation of the method.

  • id - An identifier established by the client

An example JSON-RPC 2.0 request:

{"id":1234, 
       "method": "eth_foo", 
               "params":[...] }

A successful response must contain:

  • result — The value of this member is determined by the method invoked on the server.

  • id — An identifier that matches the value of id in the request object.

{"jsonrpc": "2.0", 
         "result": 143129, 
            "id": 1}

Conclusion

Having a good understanding of how Ethereum JSON-RPC works is essential in interacting with the Ethereum node.

Feel free to reach out to me on Twitter or Linkedln

I hope you enjoyed this. See you in the next post.