web3.js

web3.js का उपयोग करके 256 Blocks से कनेक्ट करें

web3.js मूल Ethereum JavaScript API है, जो Ethereum नोड्स के साथ इंटरैक्ट करने के लिए लाइब्रेरी का संग्रह प्रदान करता है। यह गाइड web3.js v4 को कवर करती है।

इंस्टालेशन

npm install web3

या yarn के साथ:

yarn add web3

त्वरित शुरुआत

HttpProvider के साथ X-API-Key हेडर का उपयोग करके 256 Blocks से कनेक्ट करें:

import { Web3, HttpProvider } from 'web3';
 
// Configure provider with custom headers
const httpOptions = {
  providerOptions: {
    headers: {
      'X-API-Key': 'your-api-key'
    }
  }
};
 
// Create the provider and Web3 instance
const provider = new HttpProvider('https://rpc.256blocks.com/ethereum', httpOptions);
const web3 = new Web3(provider);
 
// Get the latest block number
const blockNumber = await web3.eth.getBlockNumber();
console.log('Latest block:', blockNumber);

अकाउंट बैलेंस पढ़ना

import { Web3, HttpProvider } from 'web3';
 
const httpOptions = {
  providerOptions: {
    headers: {
      'X-API-Key': 'your-api-key'
    }
  }
};
 
const provider = new HttpProvider('https://rpc.256blocks.com/ethereum', httpOptions);
const web3 = new Web3(provider);
 
// Get account balance
const address = '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb';
const balance = await web3.eth.getBalance(address);
 
console.log('Balance:', web3.utils.fromWei(balance, 'ether'), 'ETH');

कॉन्ट्रैक्ट डेटा पढ़ना

import { Web3, HttpProvider } from 'web3';
 
const httpOptions = {
  providerOptions: {
    headers: {
      'X-API-Key': 'your-api-key'
    }
  }
};
 
const provider = new HttpProvider('https://rpc.256blocks.com/ethereum', httpOptions);
const web3 = new Web3(provider);
 
// ERC-20 ABI (minimal)
const erc20Abi = [
  {
    name: 'balanceOf',
    type: 'function',
    stateMutability: 'view',
    inputs: [{ name: 'account', type: 'address' }],
    outputs: [{ name: '', type: 'uint256' }]
  },
  {
    name: 'decimals',
    type: 'function',
    stateMutability: 'view',
    inputs: [],
    outputs: [{ name: '', type: 'uint8' }]
  }
];
 
// USDC contract on Ethereum
const usdcAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
const contract = new web3.eth.Contract(erc20Abi, usdcAddress);
 
const balance = await contract.methods.balanceOf('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb').call();
const decimals = await contract.methods.decimals().call();
 
// Convert balance to human-readable format
const formattedBalance = Number(balance) / Math.pow(10, Number(decimals));
console.log('USDC Balance:', formattedBalance);

मल्टी-चेन सपोर्ट

एंडपॉइंट बदलकर विभिन्न चेन से कनेक्ट करें:

import { Web3, HttpProvider } from 'web3';
 
function createWeb3(chain, apiKey) {
  const httpOptions = {
    providerOptions: {
      headers: {
        'X-API-Key': apiKey
      }
    }
  };
 
  const provider = new HttpProvider(`https://rpc.256blocks.com/${chain}`, httpOptions);
  return new Web3(provider);
}
 
// Create Web3 instances for different chains
const ethWeb3 = createWeb3('ethereum', 'your-api-key');
const baseWeb3 = createWeb3('base', 'your-api-key');
const arbWeb3 = createWeb3('arbitrum', 'your-api-key');
const polygonWeb3 = createWeb3('polygon', 'your-api-key');

संसाधन

web3.js | 256 Blocks