viem

Terhubung ke 256 Blocks menggunakan viem

viem adalah antarmuka TypeScript untuk Ethereum yang menyediakan primitif stateless tingkat rendah untuk berinteraksi dengan blockchain. Ini berfokus pada pengalaman pengembang, stabilitas, ukuran bundle, dan kinerja.

Instalasi

npm install viem

Atau dengan yarn:

yarn add viem

Mulai Cepat

Terhubung ke 256 Blocks menggunakan header X-API-Key dengan 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);

Membaca Saldo Akun

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

Membaca Data Kontrak

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

Dukungan Multi-Chain

Terhubung ke chain yang berbeda dengan menggunakan konfigurasi chain yang sesuai:

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

Sumber Daya

viem | 256 Blocks