viem

Kết nối với 256 Blocks bằng viem

viem là một giao diện ưu tiên TypeScript cho Ethereum cung cấp các nguyên hàm không trạng thái cấp thấp để tương tác với blockchain. Nó tập trung vào trải nghiệm nhà phát triển, tính ổn định, kích thước bundle và hiệu suất.

Cài đặt

npm install viem

Hoặc với yarn:

yarn add viem

Bắt đầu nhanh

Kết nối với 256 Blocks bằng header X-API-Key với fetchOptions:

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);

Đọc Số dư Tài khoản

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');

Đọc Dữ liệu Hợp đồng

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));

Hỗ trợ Đa chuỗi

Kết nối với các chuỗi khác nhau bằng cách sử dụng cấu hình chuỗi phù hợp:

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');

Tài nguyên

viem | 256 Blocks