This article provides insights into Smart contract development in C#, contract validation, and byte code generation on Stratis Blockchain with an example of HelloWorld Contract. Beginners or newbies to the Smart contract development will get complete ideas and guidelines from Smart Contract development in C# to contract validation and byte code generation.

In this article, we will move for Smart Contract development in C#.

This article includes:

  • Setup smart contract template in visual studio
  • Create First Smart contract Project using a Project template in Visual Studio
  • Get familiar with Hello World Contract
  • Validate and Generate Byte Code


  • Visual Studio 2019 or Later version

Stratis Smart Contract Template Setup in Visual Studio

Stratis has provided a Stratis Smart Contract template for those who want to create Smart Contract using C# and .NET in Visual Studio. To set up Stratis Smart Contract Template run the below command in your Visual Studio command terminal.

dotnet new --install Stratis.SmartContracts.Templates.CLI

Once, the command is successful you can see the Stratis Smart Contract template in the list as depicted below.

You can run the below command to view the list of new templates installed in visual studio.

dotnet new --list

You can watch the below video for the Smart Contract project template setup in Visual Studio.

Getting Started with Stratis Smart Contract

Step 1 – Create Smart Contract Project

Create a new project in Visual Studio and search Stratis Smart Contract template by typing Stratis in the search textbox. You can see the template in the list. Select the Stratis Smart Contract Template (Stratis Group Ltd) and click on Next.

If you can’t see the Smart contract template while creating the project in the above step then you can quickly fix it by following the below steps:

  1. Go to Tool–>Options as shown below.

  2. Then under Environment go to Preview Features and select “Show all .NET Core templates in the New Project dialog (requires restart)” and click on OK.

3. Once you enable this, restart your Visual Studio.

Step 2 – Give Project Name

Once you select the Stratis Smart Contract Template while creating the project then, give the project name, folder location, and solution name and click on Create to create your first Stratis Smart Contract Project.

When Project is created you can see My Contract Class as shown below.

Smart Contract Example

In this section, we will take an example of the Hello world contract. Let’s rename the MyContract to HelloWorld. Right-click on MyContract.cs and rename it as HelloWorld.cs

Deployment of a contract involves calling the constructor of the Smart contract class.

That’s why any initialization of the smart contract should be in the constructor. If we need any parameters in the Smart contract, then we can pass in the constructor.

All Smart contract in C# inherits from SmartContract. You might have noticed that the first line in HelloWorld.cs contract contains:

using Stratis.SmartContracts;

Note: When more than one class/contract is declared then you need to use [Deploy] attribute on top of the class. However, specifying is fine even in a single class.

public class HelloWorld : SmartContract
  • Smart Contract constructor gets run when the contract is created for the first time.
  • Contracts must override the base class constructor and inject ISmartContractState.
  • In the constructor, the first parameters passed must be an object of the ISmartContractState interface as shown in the below code.
  • If you have any other parameters, then you can write after that.
public HelloWorld(ISmartContractState smartContractState)
    : base(smartContractState)

Smart Contract Property-Greeting

In this Hello World example, we will initiate the Greeting property as shown below.

private string Greeting
        return this.PersistentState.GetString("Greeting");
        this.PersistentState.SetString("Greeting", value);
  • Here, the PersistentState belongs to SmartContract class which facilitates to store and retrieve of the data (in this case, we are storing string data).
  • Smart Contract data is stored as a series of key-value pairs. In this example Greeting is Key
  • As we don’t need to use Greeting property outside of this class. So it is marked as private.
  • Unlike methods, C# properties of Smart contract cannot be called outside of the class even though they are public.

Now, we will initialize the Greeting property inside the HelloWorld constructor having the string value:” Hello World!”.

this.Greeting = "Hello World!";

Smart Contract Method – SayHello()

We will write a simple method SayHello(), that returns the Greeting: “Hello World!” string value.

public string SayHello()
    return this.Greeting;

The complete code can be found here.

Smart Contract Tool (Sct)

The smart contract tool is powered by Stratis platform which is used to validate and generate the byte code of the Smart contract.

After the completion of the Smart Contract code based on the requirement, we need to validate it. Stratis has provided an Sct tool to validate the smart contract whether it is correct. The validation process is mandatory to verify the valid constraints used in the contract. It validates for the determinism and constraints used in the Smart Contract i.e. validates format and deterministic element of the contract. Determinism and format validation rules can be found in the Startis Academy. You can download the Sct tool from here.

Or clone the Stratis.SmartContracts.Tools.Sct repository by running below command.

git clone

Note: If you are using a previously cloned or downloaded version of Sct Tool from the repository please, ensure that you are using the latest version of it before validating and compiling the contract.

Validating the Contract

Open Sct Tools solution in Visual Studio, you will see two projects, one is the main project, and another is the tests project as illustrated below.

Right-click on “Stratis.SmartContracts.Tools.Sct” and go to the terminal then run the following command.

dotnet run — validate [Contract_PATH]


dotnet run -- validate “F:\Blockchain\Demo Stratis Contract\Demo Stratis Contract\MyContract.cs”

Alternatively, from normal CLI you can run the below commands

cd src/Stratis.SmartContracts.Tools.Sct

dotnet run — validate [Contract_PATH]

Once you run the command you can see the success or error of validation. On the success of validation, you will get the below information in the terminal.

If you get an error in the validation step, then based on the error message you can correct your smart contract code or check validation rules and then again do validation as above.

Compiling Contract Using Sct Tool

Once, Contract is validated, after that, we have to compile the Smart Contract code and generate the byte code. This smart contract byte code is the code that we need to deploy in the Blockchain.

To compile the code run the below command.

dotnet run – – validate [CONTRACT_PATH] -sb


dotnet run -- validate “F:\Blockchain\Demo Stratis Contract\Demo Stratis Contract\MyContract.cs”-sb

The above command first validates the Smart contract and then compiles the code. On the success of your compilation, you will get hash and byte code in your terminal as illustrated below. We will need the hash and byte code while deploying the contract in the blockchain. So, copy and keep the hash and Contract Byte code which we will use in the next part of this article series.

You can see more information using the below command.

dotnet run — validate –help


Hence, in this part of the article series, we learned to set up a smart contract template in visual studio, created the first smart contract project using the template, and got familiar with the Hello World contract in C#. Moreover, we validated and generated Byte Code.

In the next part of the series, we will deploy and interact with this contract using Cirrus Core Private Net Wallet. For the next part to deploy and interact with the contract you can get the article series here.

You can refer to the previous article series following the below links.

Similarly, you can refer to the next part below.


  1. Stratis Academy

Leave a Reply

Your email address will not be published.