Skip to content

Nethereum Code Generation

Nethereum offers a code generator which allows you to generate .Net classes (C#, Vb.Net and F#) from the compilation output of smart contracts (ABI and Bin).

The core and first code generators output the .Net contract definitions and services to simplify and speed up the development to interact with Ethereum smart contracts using Nethereum. Our roadmap is to be able to generate User interfaces, view models and cloud services.

Nethereum provides different tooling based on the same code generation.

Web based code generation

The Web based code generation http://codegen.nethereum.com is a simple online tool, to code generate a smart contract definition without the need to install any tools. Just input your Abi, Bytecode, Smart contract name and Namespace.

Source code …

VsCode Solidity extension integrated code generation:

The vs code solidity extension can code generate your contract defintions automatically after compilation of a smart contract, and for any existing smart contract or all in the current solidity project workspace.

More info on usage..

Nethereum Autogen Nuget

The Nethereum.Autogen.ContractApi is a nuget package that automatically generates your code when building your project, just save your contract abi and bin files at the root of your project.

More info on usage ..

Nethereum Generator Console:

The nuget tool Nethereum.Generator.Console provides a cli tool for code generation. It can be installed simply using the following command:

dotnet tool install -g Nethereum.Generator.Console.

More info on usage ..

Nuget and Npm packages.

To integrate the generators in your own solution we provide both Nugets and Npm packages for .Net and Javascript.

Interacting with the generated code

The code below uses the generated code to deploy a standard contract to a test chain and invoke its Transfer function. (To run the code you need to ensure you have a test chain/node running and that you provide valid account addresses and passwords)

using System;
using System.Numerics;
using System.Threading.Tasks;
using MyStandardContractProject.StandardContract.CQS;
using MyStandardContractProject.StandardContract.Service;
using Nethereum.Hex.HexTypes;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Web3.Accounts.Managed;

namespace MyStandardContractProject
{
    public class Sample
    {
        public async Task DeployAndCall()
        {
            var account = new ManagedAccount("0x12890d2cce102216644c59dae5baed380d84830c", "password");
            var web3 = new Web3(account, "http://localhost:8545");

            var deployment = new StandardContractDeployment
            {
                InitialAmount = new HexBigInteger(100),
                TokenName = "Test",
                DecimalUnits = 0,
                TokenSymbol = "T"
            };

            var svc =
                await StandardContractService.DeployContractAndGetServiceAsync(web3, deployment);

            var receipt = await svc.TransferRequestAndWaitForReceiptAsync(new TransferFunction
            {
                To = "0x13f022d72158410433cbd66f5dd8bf6d2d129924",
                Value = new BigInteger(1)
            });

            if (receipt.Status.Value == 0)
                throw new Exception("Failure - status should equal 1");
        }
    }
}
Questions or feedback? You can discuss issues and obtain free support on Nethereum Discord channel.