Nethereum

Conéctese a 256 Blocks usando Nethereum para aplicaciones .NET

Nethereum es la biblioteca de integración .NET para Ethereum, proporcionando una implementación completa para interactuar con nodos de Ethereum, contratos inteligentes y el ecosistema en general.

Instalación

Instale Nethereum a través de NuGet:

dotnet add package Nethereum.Web3

O a través de la Consola del Administrador de Paquetes:

Install-Package Nethereum.Web3

Inicio Rápido

Conéctese a 256 Blocks usando el encabezado X-API-Key:

using Nethereum.Web3;
using Nethereum.JsonRpc.Client;
using System.Net.Http;
 
// Create HttpClient with API key header
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-API-Key", "your-api-key");
 
// Create RPC client with custom HttpClient
var rpcClient = new RpcClient(
    new Uri("https://rpc.256blocks.com/ethereum"),
    httpClient
);
 
// Create Web3 instance
var web3 = new Web3(rpcClient);
 
// Get the latest block number
var blockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();
Console.WriteLine($"Latest Block: {blockNumber.Value}");

Lectura del Saldo de una Cuenta

using Nethereum.Web3;
using Nethereum.JsonRpc.Client;
using System.Net.Http;
 
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-API-Key", "your-api-key");
 
var rpcClient = new RpcClient(
    new Uri("https://rpc.256blocks.com/ethereum"),
    httpClient
);
 
var web3 = new Web3(rpcClient);
 
// Get account balance
var address = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb";
var balance = await web3.Eth.GetBalance.SendRequestAsync(address);
var etherAmount = Web3.Convert.FromWei(balance.Value);
 
Console.WriteLine($"Balance: {etherAmount} ETH");

Lectura de Datos de Contratos

using Nethereum.Web3;
using Nethereum.JsonRpc.Client;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts;
using System.Numerics;
using System.Net.Http;
 
// ERC-20 balanceOf function
[Function("balanceOf", "uint256")]
public class BalanceOfFunction : FunctionMessage
{
    [Parameter("address", "account", 1)]
    public string Account { get; set; }
}
 
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-API-Key", "your-api-key");
 
var rpcClient = new RpcClient(
    new Uri("https://rpc.256blocks.com/ethereum"),
    httpClient
);
 
var web3 = new Web3(rpcClient);
 
// USDC contract on Ethereum
var contractAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
var contract = web3.Eth.GetContractHandler(contractAddress);
 
var balanceOfMessage = new BalanceOfFunction
{
    Account = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb"
};
 
var balance = await contract.QueryAsync<BalanceOfFunction, BigInteger>(balanceOfMessage);
Console.WriteLine($"USDC Balance: {balance}");

Soporte Multi-Cadena

Conéctese a diferentes cadenas cambiando el endpoint:

// Ethereum Mainnet
var ethClient = new RpcClient(new Uri("https://rpc.256blocks.com/ethereum"), httpClient);
 
// Base
var baseClient = new RpcClient(new Uri("https://rpc.256blocks.com/base"), httpClient);
 
// Arbitrum
var arbClient = new RpcClient(new Uri("https://rpc.256blocks.com/arbitrum"), httpClient);
 
// Polygon
var polygonClient = new RpcClient(new Uri("https://rpc.256blocks.com/polygon"), httpClient);

Recursos

Nethereum | 256 Blocks