Introduction

Smart contract is a computer program where the parties have consent on predetermined conditions of the agreement. Smart contract stores and self executes in the blockchain network. It runs when the predefined conditions are met. Smart Contract allows transaction to be made without need of a middle man and those transactions are irreversible, transparent and traceable. This article describes how to get started with Smart Contract in C# using Stratis blockchain. In the article I will cover, setting up environment for development of Smart Contract in C# using Stratis blockchain which includes the setting up of Stratis Smart Contract template in Visual Studio. Additionally, I will explain how to get and setup all the necessary tools for Contract Validation and running up Stratis private blockchain network in the development environment. This article is the guide for newbie to setup and get ready for Smart Contract development in C# using Stratis Blockchain.

Article will cover following things:

  • Stratis Smart Contract Template setup in Visual Studio
  • Create Smart Contract Project in C# using Stratis Smart Contract Template
  • What is Sct Tool
  • How to Validate Smart Contract
  • How to generate Smart Contract Bytecode
  • What is Full Node
  • How to run Full Node in Development Environment

So, let’s begin. We need following things to develop Smart Contract in C# utilizing Stratis blockchain in the development machine.

  • Stratis Smart Contract Template in Visual Studio
  • Sct Tool
  • Full Node

Prerequisites:

  • Visual Studio 2019 or Later

Visual Studio

We need Visual Studio 2019 or later version either Community or any edition for Stratis Smart Contract development. If you don’t have you can download it and install in your machine from here.

Once you have installed Visual Studio, you can setup Stratis Smart Contract Template. Let’s move for this.

Stratis Smart Contract Template Setup in Visual Studio

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

dotnet new — install Stratis.SmartContracts.Templates.CLI

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

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

dotnet new –list

Smart Contract In C# Using Stratis Smart Contract Template

Step 1- Create Smart Contract Project

Create 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 find Smart Contract Template in the list then move to Step 2. Otherwise, you need to do quick fix. If you still cannot find Stratis Smart Contract template in Visual Studio then go to Tool–>Options as shown below.

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.

Once you enable this, restart your Visual Studio.

Step 2- Give Project Name

Once you select the Stratis Smart Contract Template while creating project then, give 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.

Stratis Smart Contract Template setup and project creation is completed. Now, you can write your contract code based on your requirement, however: for this article we will go for next steps how to validate and generate bytecode of the Smart Contract.

Sct Tool

We need Sct tool to validate the Stratis Smart Contract and generate byte code of the Contract. After the completion of the code of our Smart Contract based on the requirement, we need to validate it. Stratis has provided Sct tool to validate the smart contract whether it is correct. 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. You can download Sct tool from here.

How to Validate Smart Contract Using Sct Tool

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

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

dotnet run — validate [Contract_PATH]

e.g

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

or

from CLI you can run 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 success of validation, you will get below information in the terminal.

If you get error in validation, then based on error message you can correct your smart contract code 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 which we have to deploy in the Blockchain.

To compile the code run below command.

dotnet run — validate [CONTRACT_PATH] -sb

e.g.

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

This command line first validates the Smart contract and then compile the code. On success of your compilation, you will get hash and byte code in your terminal as illustrated below.

Stratis Full Node

Strais Full Node is core of Stratis private blockchain. This is the very crucial step to setup and run the Stratis Full Node on your development machine to run the project locally and interact with the Stratis Blockchain. for more details on Full Node visit this article.

You can download Stratis Full Node from here

How to Run the Stratis Full Node in Development Environment

Once downloaded, go to the folder and open the Stratis Full Node solution in Visual studio, there you will see many projects; however, you need to run the Stratis.CirusMinerD project with the -devmode parameter by executing below command from CLI.

cd StratisFullNode\src\Stratis.CirrusMinerD

dotnet run -devmode=miner

or

Right click on “Stratis.CirrusMinerD” Project and click on Open in Terminal then run below command.

dotnet run -devmode=miner

Above command runs a single block producing node using the PoA (Proof-of-Authority) consensus algorithm. After success of above command open the http://localhost:38223/swagger in your device where you can see list of APIS. These API endpoints will be used to interact with the blockchain.

Congratulations, your machine is setup for Stratis Smart Contract development and deployment for private blockchain which can be used for both development and testing purpose.

Conclusion

Hence, this article has described how to setup template and get started with Smart Contract in C# using Stratis Blockchain. Moreover, article explained what is Sct tool, how to validate and generate Smart contract bytecode and how to run Stratis Full Node for development and testing environment locally.

Leave a Reply

Your email address will not be published. Required fields are marked *