How To Perform Transaction Using Swagger API On Stratis Blockchain

Introduction

This article describes what is Swagger and how we can perform blockchain transactions using Stratis Blockchain APIs and Swagger with an example. In the previous part of this article series, we got to know about different blockchain networks and performed a transaction using Stratis Cirrus Core Private Net Wallet. You can refer to the previous part of the article series here. In this part, we will do a similar transaction, get to know how to interact with Stratis Full Node APIs with Swagger as well as will get familiar with some of the Stratis Blockchain API endpoints.

What is Swagger?

Swagger is a tool that simplifies API development for millions of developers, users, teams, and enterprises. It is open-source and used by several enterprise application developers.

  • It is an interface description language for RESTful APIs.
  • It provides a direct connection to the live APIs through an interactive user interface.
  • Swagger UI visualizes Open-API definitions through an interaction Html based user interface.

Swagger can be used for:

  • Developing APIs
  • Interacting or Testing APIs
  • Documenting APIs

Let’s move for hands-on. In this hands-on, we will be performing transactions using the Swagger API. Stratis Full Node is the core of Stratis blockchain. This is the first and very crucial step to set up and run the Stratis Full Node on your development machine to get started with blockchain development. This is needed to run the project locally and interact with the Stratis Blockchain. You need to be familiar with it to know about Stratis blockchain core APIs. In this part, we will cover some basic API interactions and perform the transaction on Stratis Blockchain through Swagger.

Create a Private Blockchain Network

Firstly, we need to create a private or local blockchain network. There are two ways to create a private instance of the Stratis Blockchain network to interact with Stratis Full Node API locally:

  1. Running Cirrus Core Private Net wallet or
  2. Running Stratis Full Node project.

You can choose any of these. For beginners using a wallet is an easier approach.

Method 1- Running Cirrus Core Wallet and Creating Network

Cirrus Core Private Net wallet is designed to launch the node for a local development environment to deploy, call the contract, and ease of use for developers to build the solutions without external interference. It runs with an instance of the Stratis Full Node; ‘Stratis.CirrusMinerD’ project in devmode=miner. It provides a graphical user interface for developers to get familiar with cirrus wallet. This wallet is a private instance of the Cirrus Wallet.

If you don’t have Cirrus core private net wallet, then you can download it from here and install it.

To create a Stratis Private Blockchain network, firstly, open your Cirrus core private net wallet. After that, you can go to the browser and see the Stratis fullnode Blockchain APIs. Open Swagger in the browser by clicking http://localhost:38223/swagger/index.html. The Swagger interface will look like the one below.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Method 2- Downloading and Running Full Node

Download the full Node from here. Alternatively, you can clone the Stratis Full Node.

git clone https://github.com/stratisproject/StratisFullNode.git

Once the Stratis Full Node is 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 successful execution 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.

Run Full Node Cirrus MinerD project.

How To Perform Transaction Using Swagger API On Stratis Blockchain

When you observe the command window, initially there are two wallets in this demo example. However, if you are running the full node for the very first time you will have only one wallet: cirrusdev, having a balance of 100,000 TCRS.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Interact with Stratis Full Node API

Create a New Wallet

Once Stratis.CirrusMinerD project is running, open Swagger in the browser by clicking http://localhost:38223/swagger/index.html.

Go to Wallet and click on API endpoint: /api/Wallet/mnemonic. Then click on Try it out.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Then click Execute. You will get 12-word phrases in the response body that are called mnemonic. Copy it and keep those safe.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Now, we will create a new wallet using the above mnemonic. Go to API endpoint: /api/Wallet/create and click on Try it out.

Provide the mnemonic which was generated in the earlier step, password, name, and passphrase for the wallet that you are going to create, and click on Execute.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Once the wallet is created, you will get response code 200 as shown below.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Keep safely the wallet details such as:

{
  "mnemonic": "battle box essence muffin nominee twin engine curious size toddler blossom spread",
  "password": "password",
  "passphrase": "password",
  "name": "Test2"
}

Mnemonic will need to recover/restore the wallet in the future. So, keep it in a safe place.

Get a Newly Created Wallet Address

Go to API endpoint: /api/Wallet/unusedaddress click on Try it out. Provide the Wallet Name: Test2 and account Name: account 0 and click Execute.

How To Perform Transaction Using Swagger API On Stratis Blockchain

How To Perform Transaction Using Swagger API On Stratis Blockchain

Copy the Address for an example: “PK53viygnyxaqrqMZrY64oRBVRX5TrhTD9”

This is the address where we will send the transaction.

Performing the transaction

Getting Sender Address (Using “/api/Wallet/addresses”)

Go to API endpoint:”/api/Wallet/addresses”, click try it out. In this demo, we are going to send a transaction from the wallet: cirrusdev. So, we will use

Wallet name: cirrusdev

Account Name: account 0

How To Perform Transaction Using Swagger API On Stratis Blockchain

In the API response body, you will get lists of addresses. Select and copy the address, where you have the amount confirmed. We are selecting the address having a confirmed amount because we are going to use this address to send funds from it.

How To Perform Transaction Using Swagger API On Stratis Blockchain

The address having the amount confirmed is:” P8iT4dXxGSoAakrnhgmdNCXDigeJXk7SAF”. So, this will be the sender's address while transacting.

Build Transaction

Now, we will send the transaction from the default wallet: cirrusdev to the newly created wallet: Test2

Go to API endpoint: “/api/Wallet/build-transaction” and click on Try it Out.

Provide all the necessary parameters as given in below.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Below are sample parameters:

{
 "password": "password",
  "walletName": "cirrusdev",
  "accountName": "account 0",
  "recipients": [
    {
      "destinationAddress": "PK53viygnyxaqrqMZrY64oRBVRX5TrhTD9",
      "amount": "10000"
    }
  ],
  "feeType": "low",
  "allowUnconfirmed": true,
  "shuffleOutputs": true,
  "changeAddress": "P8iT4dXxGSoAakrnhgmdNCXDigeJXk7SAF"
}

Note: Some default parameters you can omit. Check the required parameters in the example above.

Once you click the Execute button you will be able to see the response as like below with fee, hex, and transaction Id. Copy the Hex code, this will need to send the transaction (Sign the transaction) to confirm it.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Example hex:

How To Perform Transaction Using Swagger API On Stratis Blockchain

If you are launching a node using the Full node project, at this point, you can see in the console that the confirmed amount in the wallet: cirrusdev is the same as before, and Wallet: Test2 has a 0 balance. It means that the transaction is not completed yet.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Sending Transaction (Broadcasting previously built Transaction)

Go to the API endpoint: “/api/Wallet/send-transaction”, it’s time to confirm the previously build transaction. Pass the Hex value that we got from the response body after execution of the API endpoint: “/api/Wallet/build-transaction”  and click on execute.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Once you execute you will get the response body with transactionId and outputs as shown below. As you can see, the amount is deducted from the sender's address and added to the destination (receiver) address.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Final Console Output

Now, you can check the console, where you can see the amount is transferred from cirrusdev wallet to test2.

How To Perform Transaction Using Swagger API On Stratis Blockchain

Check Balance

Go to API endpoint:” /api/Wallet/balance” and provide the wallet name and account name. This API endpoint returns the balance of the wallet.

We can check the balance of the wallet: cirrusdev by providing the below details in the parameters. You can use your own wallet name to check the balance of your wallet.

Wallet name: cirrusdev

Account Name: account 0

How To Perform Transaction Using Swagger API On Stratis Blockchain

Once you execute, you will be able to see the confirmed amount in the API response as depicted below. If you are integrating this API endpoint in your application, then you can get the response values in your app and display the balance there.

How To Perform Transaction Using Swagger API On Stratis Blockchain

To know more about API endpoints, you can refer here.

Summary

Hence, we learned to interact with the API endpoints through Swagger. Additionally, we got familiar with Stratis Blockchain API endpoints and performed the transaction using the Swagger interface. In the next part of the article series, we will learn about Smart Contract development and interaction.

You can refer to the next part of the article series by following the below links.

You can refer to some of the relevant articles below:

Additionally, to learn and get started with Stratis Blockchain in .NET, you can get enough resources, materials, and articles following the below links: Stratis Academy and C# Corner.

Reference

  1. Stratis Academy
  2. Swagger


Similar Articles