Save JSON-RPC
sjrpc or Save JSON-RPC is a Reverse Proxy focused on to reduce remote Ethereum/Web3 JSON-RPC calls to thrid-party nodes.
Let us save your time, if you use:
// Example of Javascript/Typescript static frontend getting Ethereum RPC connection via browser embedded connection, such as Metamask
if (window.ethereum) {
...
}
This project is not for you. Period.
How it works
Using an embedded and local BadgerDB database, sjrpc hashes the Request,
uses the request hash as Key, perform the remote JSON-RPC call and saves the remote response locally. At next call it gets the content from the local database.
Installation
Download
Installation from sources
Install Go Language
Visit Golang Download and Install page and follow the instructions.
Clone this repository
Compile it
In case you use Mac or Linux, install make
if you don't have it already, then run:
make build
In case you use Windows, run:
go build -o bin/sjrpc.exe cmd/main.go
Set your RPC Server URL using environment variables
Set SJRPC_URL
with your remote RPC Server URL
Example:
export SJRPC_URL=https://mainnet.infura.io/v3/<YOUR INFURA API KEY>
Run it
In case you use Mac or Linux run:
make run
In case you use Windows, run:
bin/sjrpc.exe
It will start a localhost Web Server on port 8434. This port number is fixed to avoid the risk you mess it with standard dev ETH node port: 8545
Usage
Configure your Foundry, Truffle, Go, Hardhat or any Web3 application to use this RPC server: http://localhost:8434
replacing your original
Alchemy, Infura, QuickNode, Llamanode or your own Ethereum-like node URL.
Debug
To debug your calls add ?debug=true
in the sjrpc URL: http://localhost:8434?debug=true
Different chainId
To call different chainId of what is defined in SJRPC_URL you need to add rpcUrl and chainId parameters in sjrpc URL.
Example: ?chainId=1337&rpcUrl=http://localhost:8545
Clean Up
When you need to call another Blockchain network your cache gets outdate and you need to clean it up. To do so you need to call the cleanup
endpoint:
http://localhost:8434/cleanup
of via Make
make clean
Perfomance hint
It runs better in 64-bit architect processors, such M1/M2 Apple chips, or Intel i7. The reason is it uses Blake2b 512 bits.
TODOs
- Add chainID as parameter to support different blockchains networks simultaneously.
- To optimize the response removing ID from hash generation, so same requests from different clients can use the same cache,
but update it back to the original ID of the call to allow SJRPC to be compatible with Ethers.