Nethereum
.NETアプリケーション向けにNethereumを使用して256 Blocksに接続
Nethereumは、Ethereumノード、スマートコントラクト、およびより広範なエコシステムと対話するための完全な実装を提供する、Ethereum用の.NETインテグレーションライブラリです。
インストール
NuGet経由でNethereumをインストール:
dotnet add package Nethereum.Web3またはPackage Manager Consoleを使用:
Install-Package Nethereum.Web3クイックスタート
X-API-Keyヘッダーを使用して256 Blocksに接続:
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}");アカウント残高の読み取り
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");コントラクトデータの読み取り
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}");マルチチェーンサポート
エンドポイントを変更することで、異なるチェーンに接続できます:
// 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);