NEO DevPack for .NET is a comprehensive suite of development tools for building smart contracts and decentralized applications (dApps) on the NEO blockchain platform using .NET. This toolkit enables developers to write, compile, test, and deploy smart contracts using C# and other .NET languages.
The NEO DevPack for .NET consists of several key components:
Neo.SmartContract.FrameworkThe framework provides the necessary libraries and APIs for writing NEO smart contracts in C#. It includes:
A specialized compiler that translates C# code into NEO Virtual Machine (NeoVM) bytecode. Features include:
A testing framework for NEO smart contracts that allows:
A tool for disassembling NeoVM bytecode back to readable C# code.
Neo.SmartContract.AnalyzerCode analyzers and linting tools to help write secure and efficient contracts.
Neo.SmartContract.TemplateProject templates for creating new NEO smart contracts with the proper structure and configurations.
Clone the repository with submodules:
git clone --recurse-submodules https://github.com/neo-project/neo-devpack-dotnet.git cd neo-devpack-dotnetCreating a New Smart Contract
SmartContract
Example:
using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Attributes; using Neo.SmartContract.Framework.Services; public class HelloWorldContract : SmartContract { [Safe] public static string SayHello(string name) { return $"Hello, {name}!"; } }Compiling a Smart Contract
The NEO C# compiler (nccs) translates your C# smart contract into NeoVM bytecode, which can then be deployed to the NEO blockchain. There are several ways to compile your contract:
dotnet run --project src/Neo.Compiler.CSharp/Neo.Compiler.CSharp.csproj -- path/to/your/contract.csproj
This command will compile your contract and generate the following files in the bin/sc
directory of your project:
YourContract.nef
: The compiled bytecode file that is deployed to the blockchainYourContract.manifest.json
: Contract manifest containing metadata and ABI informationYou can customize the compilation process with various options:
# For bash/zsh (macOS/Linux) dotnet run --project src/Neo.Compiler.CSharp/Neo.Compiler.CSharp.csproj -- \ path/to/your/contract.csproj \ -o output/directory \ --base-name MyContract \ --debug \ --assembly \ --optimize=All \ --generate-interface # For Windows Command Prompt dotnet run --project src/Neo.Compiler.CSharp/Neo.Compiler.CSharp.csproj -- ^ path/to/your/contract.csproj ^ -o output/directory ^ --base-name MyContract ^ --debug ^ --assembly ^ --optimize=All ^ --generate-interfaceWorking with Single Files or Directories
The compiler can also process individual .cs
files or entire directories:
# Compile a single file dotnet run --project src/Neo.Compiler.CSharp/Neo.Compiler.CSharp.csproj -- path/to/Contract.cs # Compile all contracts in a directory dotnet run --project src/Neo.Compiler.CSharp/Neo.Compiler.CSharp.csproj -- path/to/contract/directoryCompiler Command Reference
The NEO C# compiler supports the following options:
Option Description-o, --output
Specifies the output directory for compiled files --base-name
Specifies the base name of the output files (overrides contract name) --debug
Generates debug information (default is Extended
) --assembly
Generates a readable assembly (.asm) file --generate-artifacts
Generates additional artifacts for contract interaction (Source, Library, or All) --generate-interface
Generates a C# interface file for the contract (useful for type-safe interaction) --security-analysis
Performs security analysis on the compiled contract --optimize
Specifies the optimization level (None, Basic, All, Experimental) --no-inline
Disables method inlining during compilation --nullable
Sets the nullable context options (Disable, Enable, Annotations, Warnings) --checked
Enables overflow checking for arithmetic operations --address-version
Sets the address version for script hash calculations
The NEO DevPack includes a comprehensive testing framework specifically designed for smart contracts. Here's how to create unit tests for your contracts:
using Neo.SmartContract.Testing; using Neo.SmartContract.Testing.TestingStandards; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Example.SmartContract.MyContract.UnitTests { [TestClass] public class MyContractTests : TestBase<ArtifactMyContract> { [TestInitialize] public void TestSetup() { // Compile and generate testing artifacts var (nef, manifest) = TestCleanup.EnsureArtifactsUpToDateInternal(); // Setup the test base with the compiled contract TestBaseSetup(nef, manifest); } [TestMethod] public void TestMethod() { // Access contract properties and methods through the Contract property var result = Contract.MyMethod("parameter"); Assert.AreEqual("Expected result", result); // Test storage changes after operations var storedValue = Storage.Get(Contract.Hash, "myKey"); Assert.AreEqual("Expected storage value", storedValue); // Verify emitted events var notifications = Notifications; Assert.AreEqual(1, notifications.Count); Assert.AreEqual("ExpectedEvent", notifications[0].EventName); } } }
TestBase Class: Provides a base class for contract testing with access to the contract, storage, and notifications.
Automatic Artifact Generation: The testing framework automatically compiles your contracts and generates testing artifacts.
Direct Contract Interaction: Access contract properties and methods directly through the strongly-typed Contract
property.
Storage Simulation: Test persistent storage operations without deploying to a blockchain.
Event Verification: Validate that your contract emits the expected events.
Gas Consumption Analysis: Track and analyze GAS costs of operations:
[TestMethod] public void TestGasConsumption() { // Record initial gas var initialGas = Engine.GasConsumed; // Execute contract operation Contract.ExpensiveOperation(); // Check gas consumption var gasUsed = Engine.GasConsumed - initialGas; Console.WriteLine($"Gas used: {gasUsed}"); Assert.IsTrue(gasUsed < 100_000_000, "Operation used too much gas"); }Setting Up the Test Project
The repository includes various example contracts that demonstrate different features and capabilities in the examples
directory:
Each example comes with corresponding unit tests that demonstrate how to properly test the contract functionality.
For detailed documentation on NEO smart contract development with .NET:
Contributions are welcome! Please follow these steps:
git checkout -b feature/your-feature
)git commit -am 'Add your feature'
)git push origin feature/your-feature
)Please ensure that your code follows the existing coding style and includes appropriate tests and documentation.
This project is licensed under the MIT License - see the LICENSE file for details.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4