Getting Started With Smart Contracts In C#

Smart Contracts In C#

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 an environment for the 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 set up all the necessary tools for Contract Validation and running up Stratis private blockchain network in the development environment. This article is the guide for newbies to set up and get ready for Smart Contract development in C# using Stratis Blockchain.

The article will cover the 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 the 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 a later version either Community or any edition for Stratis Smart Contract development. If you don’t have you can download it and install it on your machine from here.

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

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.

Getting Started with Smart Contract in C# Using Stratis Blockchain

You can run the 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 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.

Getting Started with Smart Contract in C# Using Stratis Blockchain

If you can find the Smart Contract Template in the list then move to Step 2. Otherwise, you need to do a quick fix. If you still cannot find the 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 the project then, give the project name, folder location, and solution name and click on Create to create your first Stratis Smart Contract Project.

Getting Started with Smart Contract in C# Using Stratis Blockchain

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

Getting Started with Smart Contract in C# Using Stratis Blockchain

If you are interested in watching the video for Stratis Smart Contract Template setup below is a video for you.

Stratis Smart Contract Template setup and project creation are completed. Now, you can write your contract code based on your requirement. You can get a Sample of Stratis Smart Contract from the repository here

However, for this article, we will go for the next steps on how to validate and generate the bytecode of the Smart Contract.

Sct Tool

We need the Sct tool to validate the Stratis Smart Contract and generate the 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 an Sct tool to validate the smart contract whether 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. You can download the Sct tool from here.

How to Validate Smart Contract Using Sct Tool

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

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

Getting Started with Smart Contract in C# Using Stratis Blockchain

dotnet run -- validate [Contract_PATH]

e.g

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

Alternatively, from 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.

Getting Started with Smart Contract in C# Using Stratis Blockchain

If you get an error invalidation, then based on the 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 that we have to deploy in the Blockchain.

To compile the code run the below command.

dotnet run -- validate [CONTRACT_PATH] -sb

example

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

This command line 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,

Getting Started with Smart Contract in C# Using Stratis Blockchain

Stratis Full Node

Stratis Full Node is the core of Stratis private blockchain. This is the very crucial step to set up and run the Stratis Full Node on your development machine. This is needed 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 the below command from CLI.

cd StratisFullNode\src\Stratis.CirrusMinerD

dotnet run -devmode=miner

or

Right-click on “Stratis.CirrusMinerDProject and click on Open in Terminal then run below command.

dotnet run -devmode=miner

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

Getting Started with Smart Contract in C# Using Stratis Blockchain

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

Conclusion

Hence, this article has described how to set up a template and get started with Smart Contract in C# using Stratis Blockchain. Moreover, the article explained what is Sct tool is, how to validate and generate Smart contract bytecode and how to run Stratis Full Node for development and the testing environment locally. Furthermore, to know more about deployment, exploring APIs, and the business case implementation of Smart Contract you can visit First Smart Contract and Voting Contract.


Similar Articles