# RPC Guide

### Getting Started

#### 1. Project Setup

First, create and initialize your project:

```bash
mkdir lumia-api-quickstart
cd lumia-api-quickstart
npm init --yes
```

#### 2. Install Dependencies

You can use any HTTP client or Web3 library. Here are a few options:

**Using Axios (HTTP Client)**

```bash
npm install axios
```

**Using Web3.js**

```bash
npm install web3
```

**Using Ethers.js**

```bash
npm install ethers
```

#### 3. Making RPC Requests

Here are examples using different libraries:

**Using Axios**

```javascript
const axios = require('axios');

const RPC_URL = 'https://RPC-URL.io';

async function getLatestBlock() {
    const response = await axios.post(RPC_URL, {
        jsonrpc: '2.0',
        id: 1,
        method: 'eth_blockNumber',
        params: []
    });
    
    console.log('Latest Block:', response.data.result);
}

getLatestBlock();
```

**Using Web3.js**

```javascript
const Web3 = require('web3');

const web3 = new Web3('https://RPC-URL.io');

async function getNetworkInfo() {
    const [blockNumber, gasPrice, chainId] = await Promise.all([
        web3.eth.getBlockNumber(),
        web3.eth.getGasPrice(),
        web3.eth.getChainId()
    ]);

    console.log({
        blockNumber,
        gasPrice: web3.utils.fromWei(gasPrice, 'gwei') + ' gwei',
        chainId
    });
}

getNetworkInfo();
```

**Using Ethers.js**

```javascript
const { ethers } = require('ethers');

const provider = new ethers.JsonRpcProvider('https://RPC-URL.io');

async function getAccountBalance(address) {
    const balance = await provider.getBalance(address);
    
    console.log(
        'Balance:', 
        ethers.formatEther(balance), 
        'LUMIA'
    );
}

getAccountBalance('0x742d35Cc6634C0532925a3b844Bc454e4438f44e');
```

### Common RPC Methods

Here are some frequently used RPC methods:

```javascript
// Get latest block number
eth_blockNumber

// Get network version
net_version

// Get gas price
eth_gasPrice

// Get balance
eth_getBalance

// Get transaction count
eth_getTransactionCount

// Send raw transaction
eth_sendRawTransaction

// Get transaction by hash
eth_getTransactionByHash

// Get block by number
eth_getBlockByNumber

// Get logs
eth_getLogs
```

### Example: Complete Transaction Flow

Here's a complete example of sending a transaction using ethers.js:

```javascript
const { ethers } = require('ethers');

async function sendTransaction() {
    // Initialize provider
    const provider = new ethers.JsonRpcProvider('https://RPC-URL.io');
    
    // Create wallet from private key
    const privateKey = 'your-private-key';
    const wallet = new ethers.Wallet(privateKey, provider);
    
    // Transaction parameters
    const tx = {
        to: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
        value: ethers.parseEther("0.1")
    };
    
    try {
        // Send transaction
        const transaction = await wallet.sendTransaction(tx);
        console.log('Transaction Hash:', transaction.hash);
        
        // Wait for confirmation
        const receipt = await transaction.wait();
        console.log('Transaction confirmed in block:', receipt.blockNumber);
    } catch (error) {
        console.error('Error:', error);
    }
}
```

### Best Practices

1. **Error Handling**
   * Always implement proper error handling for RPC requests
   * Consider implementing retry logic for failed requests
   * Handle rate limiting appropriately
2. **Performance Optimization**
   * Batch related calls when possible
   * Cache responses when appropriate
   * Use WebSocket connections for real-time updates
3. **Security**
   * Never expose private keys in your code
   * Use environment variables for sensitive data
   * Validate all input parameters
