TheRPCを始める
APIリファレンス
イーサリアムAPI
Core API
ガイド

C# Tools

C# developers can use Nethereum, a comprehensive .NET integration library for Ethereum development.

ご注意ください!
こんにちは!友好的な注意点:このページは、 C# リクエストの操作に関する確かな概要を提供することを目的としています。実践的なコード例については、 API Methods documentation をチェックしてください。そこにはすべてのサポートされている言語ですぐに使用できる例があります!

# Nethereum

The complete .NET solution for Ethereum development.

using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Util;
using Nethereum.Hex.HexTypes;

public class EthereumService
{
    private readonly Web3 _web3;

    public EthereumService(string url)
    {
        _web3 = new Web3(url);
    }

    public async Task<decimal> GetBalanceAsync(string address)
    {
        var balance = await _web3.Eth.GetBalance.SendRequestAsync(address);
        return Web3.Convert.FromWei(balance.Value);
    }

    public async Task<string> SendTransactionAsync(
        string privateKey,
        string toAddress,
        decimal etherAmount)
    {
        var account = new Account(privateKey);
        var web3 = new Web3(account, _web3.Client.Url);

        var transaction = await web3.Eth.GetEtherTransferService()
            .TransferEtherAsync(toAddress, etherAmount);

        return transaction;
    }
}
  • GitHub: Nethereum
  • Documentation: docs.nethereum.com
  • Features:
    • Full Ethereum integration
    • Smart contract deployment and interaction
    • HD Wallet support
    • Unity3D support
    • Xamarin compatibility
    • IPC, RPC, WebSocket support

# Smart Contract Integration

Working with smart contracts in C#:

public class SmartContractService
{
    private readonly Web3 _web3;
    private readonly Contract _contract;

    public SmartContractService(string url, string contractAddress, string abi)
    {
        _web3 = new Web3(url);
        _contract = _web3.Eth.GetContract(abi, contractAddress);
    }

    public async Task<T> CallFunctionAsync<T>(string functionName, params object[] parameters)
    {
        var function = _contract.GetFunction(functionName);
        return await function.CallAsync<T>(parameters);
    }

    public async Task<string> ExecuteFunctionAsync(
        string functionName,
        string fromAddress,
        params object[] parameters)
    {
        var function = _contract.GetFunction(functionName);
        return await function.SendTransactionAsync(fromAddress, parameters);
    }
}

# ASP.NET Integration

Example of ASP.NET Web API integration:

[ApiController]
[Route("api/[controller]")]
public class EthereumController : ControllerBase
{
    private readonly EthereumService _ethereumService;

    public EthereumController(EthereumService ethereumService)
    {
        _ethereumService = ethereumService;
    }

    [HttpGet("balance/{address}")]
    public async Task<ActionResult<decimal>> GetBalance(string address)
    {
        try
        {
            var balance = await _ethereumService.GetBalanceAsync(address);
            return Ok(balance);
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }
}

# Event Handling

Subscribe to Ethereum events:

public class EventMonitorService
{
    private readonly Web3 _web3;

    public async Task MonitorEventsAsync(string contractAddress, string eventName)
    {
        var filterAll = _web3.Eth.GetEvent<YourEventDTO>(contractAddress)
            .CreateFilterInput();

        var subscription = _web3.Eth.GetEvent<YourEventDTO>(contractAddress)
            .GetFilterChanges(filterAll);

        subscription.Subscribe(evt =>
        {
            Console.WriteLine($"New event: {evt.Event}");
        });
    }
}

# Unity3D Integration

Example of Unity3D integration:

public class EthereumUnityManager : MonoBehaviour
{
    private Web3 _web3;

    private async void Start()
    {
        _web3 = new Web3("YOUR_ETHEREUM_NODE_URL");
        await InitializeWalletAsync();
    }

    private async Task InitializeWalletAsync()
    {
        try
        {
            var balance = await _web3.Eth.GetBalance.SendRequestAsync("YOUR_ADDRESS");
            Debug.Log($"Wallet Balance: {Web3.Convert.FromWei(balance.Value)} ETH");
        }
        catch (Exception ex)
        {
            Debug.LogError($"Failed to initialize wallet: {ex.Message}");
        }
    }
}

See also

より良くするためにご協力ください!
このページを共有して、より良い製品を作るのに協力してください。