The Nethereum code generator provides a simple way to generate messages, DTOs, and services to integrate with Ethereum smart contracts using Nethereum.
The nethereum-codegen
module generates code for interacting with smart contracts, including .NET Standard class libraries, Unity requests, MUD tables and services, and Blazor page services. It supports shared types for reusable events and errors across contracts.
Install the module using npm or yarn:
npm install nethereum-codegen
or
yarn add nethereum-codegen
var codegen = require('nethereum-codegen');
var contractByteCode = "0x00";
var abierc20 = '[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ECDSAInvalidSignature","type":"error"},...]'; // truncated for brevity
var abi = abierc20;
var contractName = "ERC20";
var baseNamespace = "Nethereum.Unity.Contracts.Standards";
var sharedTypesNamespace = "SharedTypes";
var sharedTypes = ["events", "errors"];
var basePath = "codeGenNodeTest";
var mudTables = '{"tables":{"Counter":{"schema":{"value":"uint32"},"key":[]},"Item":{"schema":{"id":"uint32","price":"uint32","name":"string","description":"string","owner":"string"},"key":["id"]}}}';
var projectName = "MyProject";
// Generating a .NET Standard class library
codegen.generateNetStandardClassLibrary(projectName, basePath, 0);
// Generating all classes with shared types
codegen.generateAllClasses(abi, contractByteCode, contractName, baseNamespace, sharedTypesNamespace, sharedTypes, basePath, 0);
// Generating Unity requests
codegen.generateUnityRequests(abi, contractByteCode, contractName, baseNamespace, sharedTypesNamespace, sharedTypes, basePath);
// Generating MUD tables
codegen.generateMudTables(mudTables, baseNamespace, "Tables", basePath, 0, "");
// Generating MUD service
codegen.generateMudService(abi, contractByteCode, contractName, baseNamespace, basePath, sharedTypesNamespace, sharedTypes, 0, "");
// Generating Blazor page service
codegen.generateBlazorPageService(abi, contractName, baseNamespace, basePath, sharedTypesNamespace, 0);
Generate files using a JSON configuration file or string:
var jsonGeneratorSetsExample1 = `[
{
"paths": ["out/ERC20.sol/Standard_Token.json"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "ContractDefinition"
},
{
"baseNamespace": "MyProject.Contracts",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Blazor",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"generatorType": "BlazorPageService"
},
{
"baseNamespace": "MyProject.Contracts",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "UnityRequest"
}
]
},
{
"paths": ["out/IncrementSystem.sol/IncrementSystem.json"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts.MyWorld.Systems",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts/MyWorld/Systems",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "ContractDefinition",
"mudNamespace": "MyWorld"
},
{
"baseNamespace": "MyProject.Contracts.MyWorld.Systems",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts/MyWorld/Systems",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "MudExtendedService",
"mudNamespace": "MyWorld"
}
]
},
{
"paths": ["mudSingleNamespace/mud.config.ts"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts.MyWorld.Tables",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts/MyWorld/Tables",
"generatorType": "MudTables",
"mudNamespace": "MyWorld"
}
]
}
]`;
codegen.generateFilesFromConfigJsonString(jsonGeneratorSetsExample1, "examples/testAbi");
codegen.generateFilesFromConfigJsonFile("path/to/config.json", "examples/testAbi");
[
{
"paths": ["out/ERC20.sol/Standard_Token.json"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "ContractDefinition"
},
{
"baseNamespace": "MyProject.Contracts",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Blazor",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "BlazorPageService"
},
{
"baseNamespace": "MyProject.Contracts",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "UnityRequest"
}
]
},
{
"paths": ["out/IncrementSystem.sol/IncrementSystem.json"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts.MyWorld.Systems",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts/MyWorld/Systems",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "ContractDefinition",
"mudNamespace": "MyWorld"
},
{
"baseNamespace": "MyProject.Contracts.MyWorld.Systems",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts/MyWorld/Systems",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "MudExtendedService",
"mudNamespace": "MyWorld"
}
]
},
{
"paths": ["mudSingleNamespace/mud.config.ts"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts.MyWorld.Tables",
"basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts/MyWorld/Tables",
"generatorType": "MudTables",
"mudNamespace": "MyWorld"
}
]
}
]
[
{
"paths": ["out/ERC20.sol/Standard_Token.json"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts",
"basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "ContractDefinition"
},
{
"baseNamespace": "MyProject.Contracts",
"basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "UnityRequest"
}
]
},
{
"paths": ["out/IncrementSystem.sol/IncrementSystem.json"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts.MyWorld1.Systems",
"basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts/MyWorld1/Systems",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "ContractDefinition",
"mudNamespace": "myworld1"
},
{
"baseNamespace": "MyProject.Contracts.MyWorld1.Systems",
"basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts/MyWorld1/Systems",
"codeGenLang": 0,
"sharedTypesNamespace": "SharedTypes",
"sharedTypes": ["events", "errors"],
"generatorType": "MudExtendedService",
"mudNamespace": "myworld1"
}
]
},
{
"paths": ["mudMultipleNamespace/mud.config.ts"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts.MyWorld1.Tables",
"basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts/MyWorld1/Tables",
"generatorType": "MudTables",
"mudNamespace": "myworld1"
}
]
},
{
"paths": ["mudMultipleNamespace/mud.config.ts"],
"generatorConfigs": [
{
"baseNamespace": "MyProject.Contracts.MyWorld2.Tables",
"basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts/MyWorld2/Tables",
"generatorType": "MudTables",
"mudNamespace": "myworld2"
}
]
}
]
Generates a .NET Standard class library.
-
projectName
(string): The name of the project. -
basePath
(string): The base path where the project will be generated. -
codeLang
(number): The programming language (0 for C#, 1 for VB, 3 for F#).
codegen.generateNetStandardClassLibrary("MyProject", "codeGenNodeTest", 0);
Generates all classes for a smart contract, supporting shared types.
-
abi
(string): The ABI of the contract. -
byteCode
(string): The bytecode of the contract. -
contractName
(string): The name of the contract. -
baseNamespace
(string): The base namespace for the generated code. -
sharedTypesNamespace
(string): The namespace for shared types (e.g., events, errors). -
sharedTypes
(string[]): List of shared types to include (e.g.,["events", "errors"]
). -
basePath
(string): The base path where the code will be generated. -
codeGenLang
(number): The programming language (0 for C#, 1 for VB, 3 for F#). -
mudNamespace
(string, optional): The MUD namespace.
codegen.generateAllClasses(abi, contractByteCode, "ERC20", "Nethereum.Unity.Contracts.Standards", "SharedTypes", ["events", "errors"], "codeGenNodeTest", 0);
Generates a MUD service for a smart contract, supporting shared types.
-
abi
(string): The ABI of the contract. -
byteCode
(string): The bytecode of the contract. -
contractName
(string): The name of the contract. -
baseNamespace
(string): The base namespace for the generated code. -
basePath
(string): The base path where the code will be generated. -
sharedTypesNamespace
(string): The namespace for shared types. -
sharedTypes
(string[]): List of shared types to include. -
codeGenLang
(number): The programming language (0 for C#, 1 for VB, 3 for F#). -
mudNamespace
(string): The MUD namespace.
codegen.generateMudService(abi, contractByteCode, "ERC20", "Nethereum.Unity.Contracts.Standards", "codeGenNodeTest", "SharedTypes", ["events", "errors"], 0, "");
Generates MUD tables from a JSON configuration.
-
json
(string): The JSON configuration for the tables. -
baseNamespace
(string): The base namespace for the generated code. -
namespace
(string): The namespace for the tables. -
basePath
(string): The base path where the code will be generated. -
codeGenLang
(number): The programming language (0 for C#, 1 for VB, 3 for F#). -
mudNamespace
(string): The MUD namespace.
var mudTables = '{"tables":{"Counter":{"schema":{"value":"uint32"},"key":[]},"Item":{"schema":{"id":"uint32","price":"uint32","name":"string","description":"string","owner":"string"},"key":["id"]}}}';
codegen.generateMudTables(mudTables, "Nethereum.Unity.Contracts.Standards", "Tables", "codeGenNodeTest", 0, "");
Generates Unity requests for a smart contract, supporting shared types.
-
abi
(string): The ABI of the contract. -
byteCode
(string): The bytecode of the contract. -
contractName
(string): The name of the contract. -
baseNamespace
(string): The base namespace for the generated code. -
sharedTypesNamespace
(string): The namespace for shared types. -
sharedTypes
(string[]): List of shared types to include. -
basePath
(string): The base path where the code will be generated.
codegen.generateUnityRequests(abi, contractByteCode, "ERC20", "Nethereum.Unity.Contracts.Standards", "SharedTypes", ["events", "errors"], "codeGenNodeTest");
Generates a Blazor page service for a smart contract, supporting shared types.
-
abi
(string): The ABI of the contract. -
contractName
(string): The name of the contract. -
baseNamespace
(string): The base namespace for the generated code. -
basePath
(string): The base path where the code will be generated. -
sharedTypesNamespace
(string): The namespace for shared types. -
codeGenLang
(number): The programming language (0 for C#) It will default / to c# razor.
codegen.generateBlazorPageService(abi, "ERC20", "Nethereum.Unity.Contracts.Standards", "codeGenNodeTest", "SharedTypes", 0);
Generates files from a JSON configuration string.
-
configJson
(string): The JSON configuration string. -
rootPath
(string): The root path for the configurations.
var jsonConfig = '[...]'; // your JSON configuration
codegen.generateFilesFromConfigJsonString(jsonConfig, "examples/testAbi");
Enum for specifying the generator type.
export enum GeneratorType {
ContractDefinition = "ContractDefinition",
UnityRequest = "UnityRequest",
MudExtendedService = "MudExtendedService",
MudTables = "MudTables",
NetStandardLibrary = "NetStandardLibrary",
BlazorPageService = "BlazorPageService"
}
Interface for a generator configuration, including shared types support.
export interface GeneratorConfig {
baseNamespace: string;
codeGenLang: number;
basePath: string;
sharedTypesNamespace: string;
sharedTypes: string[];
generatorType: GeneratorType;
mudNamespace: string;
}
Interface for a generator set configuration.
export interface GeneratorSetConfig {
paths: string[];
default: boolean;
generatorConfigs: GeneratorConfig[];
}