ethers.js
ethers.js v6 का उपयोग करके 256 Blocks से कनेक्ट करें
ethers.js Ethereum ब्लॉकचेन और इसके इकोसिस्टम के साथ इंटरैक्ट करने के लिए एक पूर्ण और कॉम्पैक्ट लाइब्रेरी है। यह गाइड ethers.js v6 को कवर करती है।
इंस्टालेशन
npm install ethersया yarn के साथ:
yarn add ethersत्वरित शुरुआत
FetchRequest के साथ X-API-Key हेडर का उपयोग करके 256 Blocks से कनेक्ट करें:
import { ethers, FetchRequest } from 'ethers';
// Create a FetchRequest with custom headers
const fetchRequest = new FetchRequest('https://rpc.256blocks.com/ethereum');
fetchRequest.setHeader('X-API-Key', 'your-api-key');
// Create the provider
const provider = new ethers.JsonRpcProvider(fetchRequest);
// Get the latest block number
const blockNumber = await provider.getBlockNumber();
console.log('Latest block:', blockNumber);अकाउंट बैलेंस पढ़ना
import { ethers, FetchRequest } from 'ethers';
const fetchRequest = new FetchRequest('https://rpc.256blocks.com/ethereum');
fetchRequest.setHeader('X-API-Key', 'your-api-key');
const provider = new ethers.JsonRpcProvider(fetchRequest);
// Get account balance
const address = '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb';
const balance = await provider.getBalance(address);
console.log('Balance:', ethers.formatEther(balance), 'ETH');कॉन्ट्रैक्ट डेटा पढ़ना
import { ethers, FetchRequest } from 'ethers';
const fetchRequest = new FetchRequest('https://rpc.256blocks.com/ethereum');
fetchRequest.setHeader('X-API-Key', 'your-api-key');
const provider = new ethers.JsonRpcProvider(fetchRequest);
// ERC-20 ABI (minimal)
const erc20Abi = [
'function balanceOf(address account) view returns (uint256)',
'function decimals() view returns (uint8)',
'function symbol() view returns (string)'
];
// USDC contract on Ethereum
const usdcAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
const contract = new ethers.Contract(usdcAddress, erc20Abi, provider);
const balance = await contract.balanceOf('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb');
const decimals = await contract.decimals();
const symbol = await contract.symbol();
console.log(`Balance: ${ethers.formatUnits(balance, decimals)} ${symbol}`);मल्टी-चेन सपोर्ट
एंडपॉइंट बदलकर विभिन्न चेन से कनेक्ट करें:
import { ethers, FetchRequest } from 'ethers';
function createProvider(chain: string, apiKey: string) {
const fetchRequest = new FetchRequest(`https://rpc.256blocks.com/${chain}`);
fetchRequest.setHeader('X-API-Key', apiKey);
return new ethers.JsonRpcProvider(fetchRequest);
}
// Create providers for different chains
const ethProvider = createProvider('ethereum', 'your-api-key');
const baseProvider = createProvider('base', 'your-api-key');
const arbProvider = createProvider('arbitrum', 'your-api-key');
const polygonProvider = createProvider('polygon', 'your-api-key');