Introduction
Visual Studio Code (VS Code) is a source code editor developed by Microsoft. It is most popular with several developers and educational institutions around the world and it can be used for varieties of 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 as well as it is open source and completely free for any kind of 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.
In this article, we will learn to create the Stratis Smart Contarct program with Visual Studio code in .NET 6 framework. Stratis Smart Contract must be written in C# language as well as Stratis Blockchain is fully developed in .NET and C# language. However, there are some SDKs that can be utilized for other programming languages such as python SDK for python developers, Unity SDK for unity developers, Java Script SDK and Unreal Engine SDK, and so on.
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 open the folder where you want to create a project. From the main menu select File-> Open Folder
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.
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.
Now, we will create a Class Library project.
Create a Class library project
Step 1– In the terminal, you need to run the below command 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.
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 YourClassLibraryprojectname/YourClassLibraryprojectname.csproj
dotnet sln add HelloWorld/HelloWorld.csproj
The default Project structure will be like below.
Install Stratis Smart Contract NuGet Package
To add the NuGet package simply, you can use a command in the terminal like below. The below command installs the Stratis.SmartContracts.NET6 package from NuGet store.
dotnet add HelloWorld package Stratis.SmartContracts.NET6
Now, we are ready to write the Stratis Smart Contract program.
Write Smart Contract Program
Now, we will write a simple smart contract program that just 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 have to validate it for format and determinism for that we need to use the Stratis Smart Contract tool (Sct).
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 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 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
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.
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
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
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.
For Stratis Smart Contract deployment and interaction, you can refer to the previous article here.
Summary
Hence, in this article, we have learned to develop Stratis Smart Contract using Visual Studio code. Additionally, we learned what is Stratis Smart Contract tool (SCT), and how to validate, compile and generate the bytecode of a Stratis Smart Contract.