Skip to content

Ozelea/SolanaProxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Solana RPC Proxy

A high-performance Solana RPC proxy server that intercepts deprecated methods and forwards requests to Helius RPC for better reliability and performance.

Features

  • Deprecated Method Support: Automatically handles getRecentBlockhash calls by converting them to getLatestBlockhash
  • Helius RPC Integration: Uses Helius RPC for improved performance and reliability
  • Smart Fee Calculation: Dynamically calculates transaction fees using recent prioritization fees
  • Request Validation: Validates JSON-RPC 2.0 format and required fields
  • Security Headers: Includes standard security headers
  • Health Check Endpoint: Monitor server and Solana network status
  • Graceful Shutdown: Handles shutdown signals properly
  • Environment Configuration: Configurable via environment variables
  • CORS Support: Configurable CORS settings

Installation

  1. Clone or download this project

  2. Install dependencies:

    npm install
  3. Copy the environment example file:

    copy .env.example .env
  4. Update your .env file with your Helius API key:

    HELIUS_RPC_URL=https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY_HERE
    PORT=3001
    NODE_ENV=development

Usage

Development

npm run dev

Production

npm start

The server will start on the port specified in your .env file (default: 3001).

Endpoints

Main RPC Endpoint

  • URL: POST /
  • Purpose: Handles all Solana RPC requests
  • Special Handling: Converts getRecentBlockhash to use getLatestBlockhash

Health Check

  • URL: GET /health
  • Purpose: Check server and Solana network status
  • Response: Current slot, timestamp, and connection status

Configuration

Environment Variables

Variable Description Default
PORT Server port 3001
NODE_ENV Environment mode development
HELIUS_RPC_URL Helius RPC endpoint with API key Required
ALLOWED_ORIGINS Comma-separated CORS origins All origins allowed

CORS Configuration

To restrict CORS origins, set the ALLOWED_ORIGINS environment variable:

ALLOWED_ORIGINS=http://localhost:3000,https://yourdomain.com

API Examples

Using the deprecated getRecentBlockhash method:

const response = await fetch("http://localhost:3001", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    jsonrpc: "2.0",
    id: 1,
    method: "getRecentBlockhash",
  }),
});
const data = await response.json();
console.log(data.result.value.blockhash);

Using any other Solana RPC method:

const response = await fetch("http://localhost:3001", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    jsonrpc: "2.0",
    id: 1,
    method: "getAccountInfo",
    params: ["YOUR_ACCOUNT_ADDRESS"],
  }),
});

Health Check:

const health = await fetch("http://localhost:3001/health");
const status = await health.json();
console.log(status);

Improvements Made

  1. Helius RPC Integration: Now uses your Helius RPC endpoint for better performance
  2. Enhanced Fee Calculation: Uses recent prioritization fees for more accurate fee estimation
  3. Request Validation: Validates JSON-RPC format and required fields
  4. Security Headers: Added standard security headers
  5. Error Handling: Improved error messages and logging
  6. Environment Configuration: Configurable via environment variables
  7. Health Check: Added health monitoring endpoint
  8. Graceful Shutdown: Proper signal handling
  9. CORS Configuration: Configurable CORS settings
  10. Documentation: Added comprehensive documentation

Performance Notes

  • The server uses Helius RPC which provides better reliability and performance than standard Solana RPC nodes
  • Fee calculations now use recent prioritization fees for more accurate estimates
  • Request validation happens early to reject invalid requests quickly
  • Connection reuse and proper timeout settings for optimal performance

Security Considerations

  • API keys are masked in logs and health check responses
  • CORS can be restricted to specific origins
  • Security headers are included in all responses
  • Request size is limited to prevent abuse
  • Input validation prevents malformed requests

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors