Bắt đầu với TheRPC
Tham khảo API
Ethereum API
Core API
Hướng dẫn

C# Tools

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

Chú ý!
Xin chào! Lưu ý thân thiện: trang này nhằm cung cấp cho bạn tổng quan chi tiết về việc làm việc với C# các yêu cầu. Đối với ví dụ mã thực tế, hãy xem API Methods documentation nơi bạn sẽ tìm thấy các ví dụ sẵn sàng sử dụng trong tất cả các ngôn ngữ được hỗ trợ!

# 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

Giúp chúng tôi trở nên tốt hơn!
Chia sẻ trang này và giúp chúng tôi tạo ra sản phẩm tốt hơn cho bạn.