DEV Community

Satya Karki
Satya Karki

Posted on

Stratis Smart Contract Development using Visual Studio Code and C#

Introduction

Visual Studio Code (VS Code) is a source code editor developed by Microsoft. It is most popular with several developers and educational institutions worldwide. It can be used for various programming languages, for instance, C, C#, C++, Java, Go, JavaScript, Python, Node.js, rust, and many more. These days VS code is getting more popular. It is a handy IDE for developing applications of any platform and is open source and entirely free for any development.

Moreover, it is available for Windows, Linux, and macOS. We can get several extensions and packages to develop applications in several languages inside it. Additionally, it is lightweight in comparison to Visual Studio.

This article will teach us to create the Stratis Smart Contarct program with Visual Studio code in the .NET 6 framework. Stratis Smart Contract must be written in C# language, and Stratis Blockchain is fully developed in .NET and C# language. However, some SDKs can be utilized for other programming languages, such as python SDK for python developers, Unity SDK for unity developers, JavaScript SDK, and Unreal Engine SDK, etc.

Prerequisites

  • Visual Studio Code
  • .NET 6 SDK and C# extension to be installed
  • Knowledge of C#

Create a Solution using VS Code

Step 1

Open VS code and the folder where you want to create a project. From the main menu, select File-> Open Folder.

Image description

Step 2

Open the Terminal by selecting View, then go to Terminal from the menu or(Ctrl+Shift+P), or simply click on the Terminal. Then the Terminal opens with the command prompt with the folder name.

Image description

Step 3
Run the below command to create a solution in the Terminal.

dotnet new sln
Enter fullscreen mode Exit fullscreen mode

The solution will be created as shown below with the name of the folder.

Image description
Now, we will create a Class Library project.

Create a Class library project

Step 1

In the Terminal, you must run the command below to create a class library project.

dotnet new classlib -o YourClassLibraryprojectname
Enter fullscreen mode Exit fullscreen mode

Below is an example of creating a HelloWorld class library project.

dotnet new classlib -o HelloWorld
Enter fullscreen mode Exit fullscreen mode

It will create a class library project with the name HelloWorld.

Image description
Step 2

Add class lib project to the solution.

Run the below command to add the class lib project to the solution.

dotnet sln add HelloWorld/HelloWorld.csproj
Enter fullscreen mode Exit fullscreen mode

Image description
The default Project structure will be like the one below.

Image description

Install Stratis Smart Contract NuGet Package

To add the NuGet package, you can use a command in the Terminal like the one below. The below command installs the Stratis.SmartContracts.NET6 package from NuGet store.

dotnet add HelloWorld package Stratis.SmartContracts.NET6
Enter fullscreen mode Exit fullscreen mode

Image description
Now, we are ready to write the Stratis Smart Contract program.

Write Smart Contract Program in C

Now, we will write a simple Smart contract program that returns Greeting value. Rename the default Class1.cs to HelloWorld.cs and Write below the HelloWorld contract program.

using Stratis.SmartContracts;
[Deploy]
public class HelloWorld: SmartContract {
    private string Greeting {
        get {
            return this.State.GetString("Greeting");
        }
        set {
            this.State.SetString("Greeting", value);
        }
    }
    public HelloWorld(ISmartContractState smartContractState): base(smartContractState) {
        this.Greeting = "Namaste!";
    }
    public string SayHello() {
        return this.Greeting;
    }
}
Enter fullscreen mode Exit fullscreen mode

Once the contract code is ready, we must validate it for format and determinism. We must use the Stratis Smart Contract tool (Sct) for that.

Smart Contract Tool

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

After completing the Smart Contract code based on the requirement, we must validate it. Stratis has provided an Sct tool to validate whether the smart contract is correct. The validation process is mandatory to verify the valid constraints used in the contract. It validates the determinism and constraints used in the Smart Contract, i.e., validates the 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 the below command.

git clone https://github.com/stratisproject/Stratis.SmartContracts.Tools.Sct.git
Enter fullscreen mode Exit fullscreen mode

Validating the Contract
Open the command terminal, and then you can go to the Smart Contract tool directly by running the below command.
cd src/Stratis.SmartContracts.Tools.Sct
After that, run the below command to validate the contract.

dotnet run — validate [Contract_PATH]

e.g.

dotnet run -- validate "C:\Users\rijsat\Desktop\Desktop\SampleDotnetProject\HelloWorld\HelloWorld\HelloWorld.cs"
Enter fullscreen mode Exit fullscreen mode

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.

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

Compiling Contract
Once the Contract is validated, we must 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

example:

dotnet run -- validate "C:\Users\rijsat\Desktop\Desktop\SampleDotnetProject\HelloWorld\HelloWorld\HelloWorld.cs" -sb
Enter fullscreen mode Exit fullscreen mode

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 on the Blockchain. So, copy and keep the hash and Contract Byte code.

Image description
For Stratis Smart Contract deployment and interaction we can use Cirrus core wallet, for that you can refer to the previous article here.

Summary

Hence, this article explained us to develop Stratis Smart Contract using Visual Studio code and .NET 6 framework. Additionally, we learned what is Stratis Smart Contract tool (SCT) is and how to validate, compile and generate the bytecode of a Stratis Smart Contract.

References

Top comments (1)

Collapse
 
animeshworkplace profile image
Animesh_Singh

Commenting here for the badge