Roadmap › Blockchain & Web3 Fundamentals › Module: Building dApps
Lessons in this course (15)
13. .NET: Nethereum
Intermediate★ Must knowLesson 13 of 15
csharp
// dotnet add package Nethereum.Web3
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Contracts.Standards.ERC20.ContractDefinition;
var web3 = new Web3("https://sepolia.infura.io/v3/<project-id>"); // read-only
var balanceWei = await web3.Eth.GetBalance.SendRequestAsync("0xAddress");
var eth = Web3.Convert.FromWei(balanceWei.Value);
var erc20 = web3.Eth.ERC20.GetContractService("0xTokenAddress");
var decimals = await erc20.DecimalsQueryAsync();
var tokens = await erc20.BalanceOfQueryAsync("0xAddress");
// Writing requires an account (private key from Key Vault / HSM — NEVER hard-coded)
var account = new Account(privateKeyFromVault, chainId: 11155111); // Sepolia
var signerWeb3 = new Web3(account, "https://sepolia.infura.io/v3/<project-id>");
var receipt = await signerWeb3.Eth.ERC20.GetContractService("0xTokenAddress")
.TransferRequestAndWaitForReceiptAsync(new TransferFunction { To = "0xRecipient", Value = Web3.Convert.ToWei(10, decimals) });
// Typed contract services can be generated from ABIs with the Nethereum code generator (VS Code extension / CLI)Typical .NET roles in Web3 projects: backend APIs that index events into SQL Server, custodial/treasury services signing with keys in Azure Key Vault Managed HSM, payment confirmation services (watch for transfers to deposit addresses and wait for N confirmations), reporting, and admin tools.