diff --git a/Topics/Tech_Stacks/Learning_JSON_RPC_with_ETH.md b/Topics/Tech_Stacks/Learning_JSON_RPC_with_ETH.md new file mode 100644 index 000000000..f1b6fca35 --- /dev/null +++ b/Topics/Tech_Stacks/Learning_JSON_RPC_with_ETH.md @@ -0,0 +1,146 @@ +## Learning JSON RPC with Ethereum API +JSON RPC (Remote Procedure Call) is a protocol for remotely calling functions or procedures on a server. In the context of Ethereum, JSON RPC is used to interact with Ethereum nodes via HTTP or IPC (Inter-process Communication) to perform various actions on the blockchain. This includes querying blockchain data, sending transactions, deploying smart contracts, and more. Understanding JSON RPC with the Ethereum API is crucial for developers and enthusiasts alike who wish to interact with the Ethereum blockchain programmatically. +### Understanding JSON RPC +JSON RPC operates over HTTP or IPC by sending JSON-encoded requests to a server and receiving JSON-encoded responses. These requests typically include a method name and parameters, while responses contain the result of the method execution or an error if applicable. Ethereum nodes expose a set of JSON RPC methods that allow interaction with the blockchain. +### Connecting to an Ethereum Node +Before using JSON RPC with the Ethereum API, you need to connect to an Ethereum node. This can be a local node running on your machine or a remote node hosted by a service provider. Once connected, you can send JSON RPC requests to the node's endpoint URL. +### Common JSON RPC Methods +1. **eth_blockNumber**: Returns the number of the most recent block. +2. **eth_getBlockByNumber**: Returns information about a block by block number. +3. **eth_getTransactionByHash**: Returns the information about a transaction requested by transaction hash. +4. **eth_getTransactionReceipt**: Returns the receipt of a transaction by transaction hash. +5. **eth_getBalance**: Returns the balance of the account of a given address. +6. **eth_sendTransaction**: Creates a new message call transaction or a contract creation for signed transactions. +7. **eth_call**: Executes a new message call immediately without creating a transaction on the block chain. +8. **eth_estimateGas**: Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. +### Examples of JSON RPC Requests +#### 1. Retrieving the Latest Block Number +``` +{ + "jsonrpc": "2.0", + "method": "eth_blockNumber", + "params": [], + "id": 1 +} +``` +#### 2. Getting Transaction Details by Hash +``` +{ + "jsonrpc": "2.0", + "method": "eth_getTransactionByHash", + "params": ["0xtransactionhash"], + "id": 1 +} +``` +#### 3. Sending a Transaction +``` +{ + "jsonrpc": "2.0", + "method": "eth_sendTransaction", + "params": [{ + "from": "0xsenderaddress", + "to": "0xrecipientaddress", + "value": "0xtransactionvalue", + "gas": "0xgasamount", + "gasPrice": "0xgasprice", + "data": "0xdata" + }], + "id": 1 +} +``` +### Using Libraries for JSON RPC +Several libraries and frameworks exist to simplify working with JSON RPC in Ethereum: + +1. **Web3.js**: A JavaScript library that allows interaction with the Ethereum blockchain. +2. **Ethers.js**: Another JavaScript library for Ethereum interaction with a focus on simplicity and ease of use. +### Simple tutorial: Learning JSON RPC with Ethereum API +In this tutorial, we'll cover the basics of interacting with the Ethereum blockchain using JSON RPC. We'll use a simple JavaScript application and the Web3.js library to demonstrate how to connect to an Ethereum node, send JSON RPC requests, and handle responses. +#### Prerequisites: + +1. Basic understanding of JavaScript. +2. Node.js installed on your machine. +3. Access to an Ethereum node (either local or remote). For example, we can use https://ethereum.publicnode.com/ for remote public nodes. +#### Step 1: Setting Up the Project +Create a new directory for your project and navigate into it via your terminal. +``` +mkdir json_rpc_ethereum +cd json_rpc_ethereum +``` +Initialize a new Node.js project. +``` +npm init -y +``` +Install Web3.js package. +``` +npm install web3 +``` +#### Step 2: Connecting to an Ethereum Node +Create a new JavaScript file (e.g., `app.js`) in your project directory. +``` +// app.js +const Web3 = require('web3'); + +// Connect to a local Ethereum node (replace with your node URL if remote) +const web3 = new Web3('http://localhost:8545'); + +// Test connection +web3.eth.getNodeInfo().then(console.log).catch(console.error); +``` +#### Step 3: Sending JSON RPC Requests +Let's send some JSON RPC requests to interact with the Ethereum blockchain. We'll demonstrate retrieving the latest block number and getting the balance of an Ethereum account. +``` +// app.js +const Web3 = require('web3'); + +// Connect to a local Ethereum node (replace with your node URL if remote) +const web3 = new Web3('http://localhost:8545'); + +// Retrieve the latest block number +web3.eth.getBlockNumber().then(blockNumber => { + console.log('Latest Block Number:', blockNumber); +}).catch(console.error); + +// Get the balance of an Ethereum account +const accountAddress = '0xYourAccountAddress'; +web3.eth.getBalance(accountAddress).then(balance => { + console.log('Account Balance:', web3.utils.fromWei(balance, 'ether'), 'ETH'); +}).catch(console.error); +``` +Replace `'0xYourAccountAddress'` with your Ethereum account address. +#### Step 4: Running the Application +Save the changes in `app.js`, then run the application. +``` +node app.js +``` +You should see output similar to the following: +``` +Latest Block Number: 1234567 +Account Balance: 10 ETH +``` +Congratulations! You've successfully connected to an Ethereum node and sent JSON RPC requests to interact with the blockchain. +### About Etherscan + +To find historical transactions you typically can't directly use JSON RPC but rather interact with Etherscan's API, which itself uses JSON RPC to interact with Ethereum nodes. Here's a sample code to get historical transaction data: +``` +const axios = require('axios'); + +// Your Etherscan API key +const apiKey = 'YourAPIKey'; + +// Ethereum address for which you want to retrieve historical transactions +const address = '0xYourEthereumAddress'; + +// Etherscan API endpoint for retrieving historical transactions +const apiUrl = `https://api.etherscan.io/api?module=account&action=txlist&address=${address}&startblock=0&endblock=99999999&sort=asc&apikey=${apiKey}`; + +// Make a GET request to the Etherscan API +axios.get(apiUrl) + .then(response => { + // Handle the response data + console.log(response.data); + }) + .catch(error => { + // Handle errors + console.error('Error fetching transactions:', error); + }); +``` \ No newline at end of file