Nethereum

Kết nối với 256 Blocks bằng Nethereum cho ứng dụng .NET

Nethereum là thư viện tích hợp .NET cho Ethereum, cung cấp triển khai hoàn chỉnh để tương tác với các node Ethereum, hợp đồng thông minh và hệ sinh thái rộng lớn hơn.

Cài đặt

Cài đặt Nethereum qua NuGet:

dotnet add package Nethereum.Web3

Hoặc qua Package Manager Console:

Install-Package Nethereum.Web3

Bắt đầu nhanh

Kết nối với 256 Blocks bằng header 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}");

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

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

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

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

Hỗ trợ Đa chuỗi

Kết nối với các chuỗi khác nhau bằng cách thay đổi 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);

Tài nguyên

Nethereum | 256 Blocks