Introduction
A 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 transactions to be made without the 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 Smart Contract template for those who want to create a 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.
You can run the below command to view the list of new templates installed in visual studio.
dotnet new --list
If you are interested in watching a video for the same below is a video for you.
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.
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.
When Project is created you can see My Contract Class as shown below.
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 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 Sct tool from here.
How to Validate Smart Contract Using Sct Tool
Open the Sct Tools solution in Visual Studio, and you will see two projects there 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.
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 the success of validation, you will get the below information in the terminal.
If you get an error while validating the contract, 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 bytecode 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
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 compiles the code. On the success of your compilation, you will get hash and byte code in your terminal as illustrated below.
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 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.CirrusMinerD” Project 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 on your device where you can see a list of APIS. These API endpoints will be used to interact with the 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, how to run Stratis Full Node for development, and the testing environment locally using C#, dotNet, and visual studio.