Stratis Smart Contract Development using Visual Studio Code and C#
Tags: csharp, smartcontract, web3, blockchain
Published: 2023-09-03 06:28:44

## 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 ](https://www.stratisplatform.com/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3i7ir6qehqfyq1fuvgi.jpg)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jo033dceu6op1kebgc2m.jpg)

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

```
dotnet new sln
```
The solution will be created as shown below with the name of the folder.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brbph5lsz8foxussjh2h.jpg)
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
```

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

```
dotnet new classlib -o HelloWorld
```
It will create a class library project with the name HelloWorld.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmhalxzkwycs5vw1vgnh.jpg)
**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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/407a92g34bzc297gx7i6.jpg)
The default Project structure will be like the one below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rtmnr4hsclau8ofaa3mz.jpg)

## 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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsufootmwpwlfsojpdud.jpg)
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;
    }
}
```
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](https://academy.stratisplatform.com/Architecture%20Reference/SmartContracts/contracts-in-depth.html). You can download the Sct tool from [here](https://github.com/stratisproject/Stratis.SmartContracts.Tools.Sct).

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

```
git clone https://github.com/stratisproject/Stratis.SmartContracts.Tools.Sct.git
```

**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"
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sj914yjtckctb9mn1wu8.jpg)
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](https://academy.stratisplatform.com/Architecture%20Reference/SmartContracts/contracts-in-depth.html#contract-validation) 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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qd46sy18t6jrsftjui5d.jpg)
For Stratis Smart Contract deployment and interaction we can use Cirrus core wallet, for that you can refer to the previous article [here](https://www.c-sharpcorner.com/article/how-to-deploy-and-interact-with-smart-contract-using-cirrus-core-wallet-on-strat/).

## 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**

- [Rijsat.com](https://rijsat.com/2023/03/21/smart-contract-development-with-visual-studio-code-and-c-language/)
- [Stratis Academy](https://academy.stratisplatform.com/)