viem

Conecte-se ao 256 Blocks usando viem

viem é uma interface TypeScript-first para Ethereum que fornece primitivos sem estado de baixo nível para interagir com o blockchain. Foca na experiência do desenvolvedor, estabilidade, tamanho do bundle e desempenho.

Instalação

npm install viem

Ou com yarn:

yarn add viem

Início Rápido

Conecte-se ao 256 Blocks usando o cabeçalho X-API-Key com 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);

Lendo Saldo da Conta

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

Lendo Dados do Contrato

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

Suporte Multi-Chain

Conecte-se a diferentes chains usando a configuração de chain apropriada:

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

Recursos

viem | 256 Blocks