1

I am in China and cannot access the solana devnet chain directly. How do I configure 'Connection' to use a local proxy

const connection = new Connection(clusterApiUrl('devnet'), { commitment: 'confirmed'});

const feePayer = Keypair.fromSecretKey(
    bs58.decode("ekD6Sg9Df******A45ef")
  );

(async () => {
    console.log(clusterApiUrl('devnet'));
    console.log('Public Key:', feePayer.publicKey.toString());

    const airdropSignature = await connection.requestAirdrop(
        feePayer.publicKey,
        LAMPORTS_PER_SOL, // 1 SOL = 1,000,000,000 Lamports
    );
    
    const balance = await connection.getBalance(feePayer.publicKey);
    console.log("balance (Lamports): ", balance);
})();

enter image description here

1 Answer 1

1

Resolved

  1. npm install node-fetch @types/node-fetch https-proxy-agent
  2. code
import fetch, { RequestInit, Response } from 'node-fetch';
import { HttpsProxyAgent } from 'https-proxy-agent';

// proxy
const proxyUrl = 'http://127.0.0.1:8080';
const agent = new HttpsProxyAgent(proxyUrl);

async function fetchWithProxy(url: string, options?: RequestInit): Promise<Response> {
    const optionsWithAgent: RequestInit = {
        ...options,
        agent
    };
    return fetch(url, optionsWithAgent);
}

const connection = new Connection(clusterApiUrl('devnet'), { 
    fetch: fetchWithProxy as any
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.