Los desarrolladores de C# pueden utilizar Nethereum, una biblioteca integral de integración .NET para el desarrollo en Ethereum.
La solución completa de .NET para el desarrollo en Ethereum.
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;
}
}
Trabajando con contratos inteligentes en 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);
}
}
Ejemplo de integración con ASP.NET Web API:
[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);
}
}
}
Suscripción a eventos de Ethereum:
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}");
});
}
}
Ejemplo de integración con Unity3D:
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}");
}
}
}