npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nethereum-codegen

v2.0.3

Published

Code generator of Nethereum and Ethereum integration classes and projects

Downloads

824

Readme

nethereum-codegen

The Nethereum code generator provides a simple way to generate the different messages and DTOs classes to integrate with Ethereum smart contracts using Nethereum.

Nethereum Code Generation

The nethereum code generator provides different ways to to generate code to interact with smart contracts using Nethereum, including standard class libraries, Unity requests, MUD tables, and services.

Table of Contents

Installation

To install the module, use npm or yarn:

npm install nethereum-codegen

or

yarn add nethereum-codegen

Usage

Basic Example

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 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
codegen.generateAllClasses(abi, contractByteCode, contractName, baseNamespace, basePath, 0);

// Generating Unity requests
codegen.generateUnityRequests(abi, contractByteCode, contractName, baseNamespace, basePath);

// Generating MUD tables
codegen.generateMudTables(mudTables, baseNamespace, "Tables", basePath, 0, "");

// Generating MUD service
codegen.generateMudService(abi, contractByteCode, contractName, baseNamespace, basePath, 0, "");

Generating Files from Configuration File

You can generate files using a JSON configuration file or string:

Using a JSON Configuration String

var jsonGeneratorSetsExample1 = 
`[
    {
        "paths": ["out/ERC20.sol/Standard_Token.json"],
        "generatorConfigs": [
            {
                "baseNamespace": "MyProject.Contracts",
                "basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts",
                "codeGenLang": 0,
                "generatorType": "ContractDefinition"
            },
            {
                "baseNamespace": "MyProject.Contracts",
                "basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts",
                "codeGenLang": 0,
                "generatorType": "UnityRequest"
            }
        ]
    },
    {
        "paths": ["out/IncrementSystem.sol/IncrementSystem.json"],
        "generatorConfigs": [
            {
                "baseNamespace": "MyProject.Contracts.MyWorld.Systems",
                "basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts.MyWorld.Systems",
                "codeGenLang": 0,
                "generatorType": "ContractDefinition"
            },
            {
                "baseNamespace": "MyProject.Contracts.MyWorld.Systems",
                "basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts.MyWorld.Systems",
                "codeGenLang": 0,
                "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");

Using a JSON Configuration File

codegen.generateFilesFromConfigJsonFile("path/to/config.json", "examples/testAbi");

Configuration Examples

JSON Configuration Example 1

[
    {
        "paths": ["out/ERC20.sol/Standard_Token.json"],
        "generatorConfigs": [
            {
                "baseNamespace": "MyProject.Contracts",
                "basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts",
                "codeGenLang": 0,
                "generatorType": "ContractDefinition"
            },
            {
                "baseNamespace": "MyProject.Contracts",
                "basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts",
                "codeGenLang": 0,
                "generatorType": "UnityRequest"
            }
        ]
    },
    {
        "paths": ["out/IncrementSystem.sol/IncrementSystem.json"],
        "generatorConfigs": [
            {
                "baseNamespace": "MyProject.Contracts.MyWorld.Systems",
                "basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts.MyWorld.Systems",
                "codeGenLang": 0,
                "generatorType": "ContractDefinition"
            },
            {
                "baseNamespace": "MyProject.Contracts.MyWorld.Systems",
                "basePath": "codeGenNodeTest/GeneratorSets/Example1/MyProject.Contracts.MyWorld.Systems",
                "codeGenLang": 0,
                "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"
            }
        ]
    }
]

JSON Configuration Example 2

[
    {
        "paths": ["out/ERC20.sol/Standard_Token.json"],
        "generatorConfigs": [
            {
                "baseNamespace": "MyProject.Contracts",
                "basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts",
                "codeGenLang": 0,
                "generatorType": "ContractDefinition"
            },
            {
                "baseNamespace": "MyProject.Contracts",
                "basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts",
                "codeGenLang": 0,
                "generatorType": "UnityRequest"
            }
        ]
    },
    {
        "paths": ["out/IncrementSystem.sol/IncrementSystem.json"],
        "generatorConfigs": [
            {
                "baseNamespace": "MyProject.Contracts.MyWorld1.Systems",
                "basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts.MyWorld1.Systems",
                "codeGenLang": 0,
                "generatorType": "ContractDefinition"
            },
            {
                "baseNamespace": "MyProject.Contracts.MyWorld1.Systems",
                "basePath": "codeGenNodeTest/GeneratorSets/Example2/MyProject.Contracts.MyWorld1.Systems",
                "codeGenLang": 0,
                "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"
            }
        ]
    }
]

API

generateNetStandardClassLibrary

Generates a .NET Standard class library.

Parameters

  • 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#).

Example

codegen.generateNetStandardClassLibrary("MyProject", "codeGenNodeTest", 0);

generateAllClasses

Generates all classes for a smart contract.

Parameters

  • 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.
  • codeGenLang (number): The programming language (0 for C#, 1 for VB, 3 for F#).

Example

codegen.generateAllClasses(abi, contractByteCode, "ERC20", "Nethereum.Unity.Contracts.Standards", "codeGenNodeTest", 0);

generateMudService

Generates a MUD service for a smart contract.

Parameters

  • 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.
  • codeGenLang (number): The programming language (0 for C#, 1 for VB, 3 for F#).
  • mudNamespace (string): The MUD namespace.

Example

codegen.generateMudService(abi, contractByteCode, "ERC20", "Nethereum.Unity.Contracts.Standards", "codeGenNodeTest", 0, "");

generateMudTables

Generates MUD tables from a JSON configuration.

Parameters

  • 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.

Example

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, "");

generateUnityRequests

Generates Unity requests for a smart contract.

Parameters

  • 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.

Example

codegen.generateUnityRequests(abi, contractByteCode, "ERC20", "Nethereum.Unity.Contracts.Standards", "codeGenNodeTest");

generateFilesFromConfigJsonString

Generates files from a JSON configuration string.

Parameters

  • configJson (string): The JSON configuration string.
  • rootPath (string): The root path for the configurations.

Example

var jsonConfig = '[...]';  // your JSON configuration
codegen.generateFilesFromConfigJsonString(jsonConfig, "examples/testAbi");

GeneratorType Enum

Enum for specifying the generator type.

export enum GeneratorType {
    ContractDefinition = "ContractDefinition",
    UnityRequest = "UnityRequest",
    MudExtendedService = "MudExtendedService",
    MudTables = "MudTables",
    NetStandardLibrary = "NetStandardLibrary"
}

GeneratorConfig Interface

Interface for a generator configuration.

export interface GeneratorConfig {
    baseNamespace: string;
    codeGenLang: number;
    basePath: string;
    generatorType: GeneratorType;
    mudNamespace: string;
}

GeneratorSetConfig Interface

Interface for a generator set configuration.

export interface GeneratorSetConfig {
    paths: string[];
    default: boolean;
    generatorConfigs: GeneratorConfig[];
}