Skip to main content

Smart Contracts & Code Generation

A smart contract is a program deployed on the blockchain. Once deployed, its code is immutable and its functions can be called by anyone. Nethereum.Contracts is the main package for smart contract interaction and includes typed services for all major standards.

The Simple Path

For standard tokens and common contracts, Nethereum provides built-in typed services — no ABI, no code generation:

TaskSimple Path
Get ERC-20 balanceweb3.Eth.ERC20.GetContractService(addr).BalanceOfQueryAsync(owner)
Transfer ERC-20web3.Eth.ERC20.GetContractService(addr).TransferRequestAndWaitForReceiptAsync(to, amount)
Get NFT ownerweb3.Eth.ERC721.GetContractService(addr).OwnerOfQueryAsync(tokenId)
Get ERC-1155 balanceweb3.Eth.ERC1155.GetContractService(addr).BalanceOfQueryAsync(owner, tokenId)
Deploy a contractweb3.Eth.GetContractDeploymentHandler<T>().SendRequestAndWaitForReceiptAsync(deployment)
Batch queriesweb3.Eth.GetMultiQueryHandler().MultiCallAsync(call1, call2)
Resolve ENS nameweb3.Eth.GetEnsService().ResolveAddressAsync("vitalik.eth")
Detect interfaceweb3.Eth.ERC165.GetContractService(addr).SupportsInterfaceQueryAsync(interfaceId)
CREATE2 predictweb3.Eth.Create2DeterministicDeploymentProxyService.CalculateCreate2Address(deployment, salt)

For custom contracts, generate typed C# classes from Solidity ABI with one command and get the same experience.

Two Ways to Interact

Generate C# classes from the ABI:

dotnet tool install -g Nethereum.Generator.Console
Nethereum.Generator.Console generate from-abi -abi MyContract.abi -o Generated
var service = new MyContractService(web3, contractAddress);
var balance = await service.BalanceOfQueryAsync(userAddress);
var receipt = await service.TransferRequestAndWaitForReceiptAsync(
new TransferFunction { To = recipient, Value = amount });

2. Dynamic Interaction (No Code Generation)

var contract = web3.Eth.GetContract(abi, contractAddress);
var function = contract.GetFunction("balanceOf");
var balance = await function.CallAsync<BigInteger>(userAddress);

Or use built-in typed services for standard contracts:

var erc20 = web3.Eth.ERC20.GetContractService(contractAddress);
var name = await erc20.NameQueryAsync();
var balance = await erc20.BalanceOfQueryAsync(userAddress);

Token Standards

ERC-20 (Fungible Tokens)

var erc20 = web3.Eth.ERC20.GetContractService(contractAddress);
var name = await erc20.NameQueryAsync();
var symbol = await erc20.SymbolQueryAsync();
var decimals = await erc20.DecimalsQueryAsync();
var balance = await erc20.BalanceOfQueryAsync(myAddress);

ERC-721 (Non-Fungible Tokens)

var erc721 = web3.Eth.ERC721.GetContractService(contractAddress);
var owner = await erc721.OwnerOfQueryAsync(tokenId);
var tokenUri = await erc721.TokenURIQueryAsync(tokenId);

ERC-1155 (Multi-Token)

var erc1155 = web3.Eth.ERC1155.GetContractService(contractAddress);
var balance = await erc1155.BalanceOfQueryAsync(myAddress, tokenId);

Query vs Transaction

OperationGas CostState ChangeReturns
Query (call)FreeNoFunction return value
Transaction (sendTransaction)YesYesTransaction receipt

Functions marked view or pure in Solidity are queries. Everything else is a transaction.

Guides

GuideWhat You'll Learn
Smart Contract InteractionDefine typed DTOs for functions, events, and errors — the foundation for all contract work
Deploy a ContractDeploy contracts with typed deployment handlers and constructor parameters
ERC-20 TokensQuery and transfer any ERC-20 token using the built-in typed service
Code GenerationGenerate typed C# classes from Solidity ABI — CLI, VS Code, MSBuild, and Foundry workflows
Events & LogsFilter, query, and decode contract events and transfer logs
Error HandlingCatch and decode Solidity custom error types in C#
Built-in StandardsPre-built typed services for ERC-721, ERC-1155, ENS, EIP-3009, and more

Advanced Patterns

GuideWhat You'll Learn
Multicall & Batch QueriesBatch multiple reads into a single call for efficiency
CREATE2 DeploymentDeploy contracts to deterministic addresses across chains