viem
viem का उपयोग करके 256 Blocks से कनेक्ट करें
viem Ethereum के लिए एक TypeScript-first इंटरफ़ेस है जो ब्लॉकचेन के साथ इंटरैक्ट करने के लिए लो-लेवल स्टेटलेस प्रिमिटिव्स प्रदान करता है। यह डेवलपर अनुभव, स्थिरता, बंडल साइज़ और प्रदर्शन पर केंद्रित है।
इंस्टालेशन
npm install viemया yarn के साथ:
yarn add viemत्वरित शुरुआत
fetchOptions के साथ X-API-Key हेडर का उपयोग करके 256 Blocks से कनेक्ट करें:
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
// Create the transport with custom headers
const transport = http('https://rpc.256blocks.com/ethereum', {
fetchOptions: {
headers: {
'X-API-Key': 'your-api-key'
}
}
});
// Create the client
const client = createPublicClient({
chain: mainnet,
transport
});
// Get the latest block number
const blockNumber = await client.getBlockNumber();
console.log('Latest block:', blockNumber);अकाउंट बैलेंस पढ़ना
import { createPublicClient, http, formatEther } from 'viem';
import { mainnet } from 'viem/chains';
const transport = http('https://rpc.256blocks.com/ethereum', {
fetchOptions: {
headers: {
'X-API-Key': 'your-api-key'
}
}
});
const client = createPublicClient({
chain: mainnet,
transport
});
// Get account balance
const balance = await client.getBalance({
address: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb'
});
console.log('Balance:', formatEther(balance), 'ETH');कॉन्ट्रैक्ट डेटा पढ़ना
import { createPublicClient, http, formatUnits } from 'viem';
import { mainnet } from 'viem/chains';
const transport = http('https://rpc.256blocks.com/ethereum', {
fetchOptions: {
headers: {
'X-API-Key': 'your-api-key'
}
}
});
const client = createPublicClient({
chain: mainnet,
transport
});
// ERC-20 ABI (minimal)
const erc20Abi = [
{
name: 'balanceOf',
type: 'function',
stateMutability: 'view',
inputs: [{ name: 'account', type: 'address' }],
outputs: [{ type: 'uint256' }]
},
{
name: 'decimals',
type: 'function',
stateMutability: 'view',
inputs: [],
outputs: [{ type: 'uint8' }]
}
] as const;
// USDC contract on Ethereum
const usdcAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
const balance = await client.readContract({
address: usdcAddress,
abi: erc20Abi,
functionName: 'balanceOf',
args: ['0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb']
});
const decimals = await client.readContract({
address: usdcAddress,
abi: erc20Abi,
functionName: 'decimals'
});
console.log('USDC Balance:', formatUnits(balance, decimals));मल्टी-चेन सपोर्ट
उपयुक्त चेन कॉन्फ़िग का उपयोग करके विभिन्न चेन से कनेक्ट करें:
import { createPublicClient, http } from 'viem';
import { mainnet, base, arbitrum, polygon, optimism } from 'viem/chains';
function createClient(chain: any, chainPath: string, apiKey: string) {
const transport = http(`https://rpc.256blocks.com/${chainPath}`, {
fetchOptions: {
headers: {
'X-API-Key': apiKey
}
}
});
return createPublicClient({ chain, transport });
}
// Create clients for different chains
const ethClient = createClient(mainnet, 'ethereum', 'your-api-key');
const baseClient = createClient(base, 'base', 'your-api-key');
const arbClient = createClient(arbitrum, 'arbitrum', 'your-api-key');
const polygonClient = createClient(polygon, 'polygon', 'your-api-key');