In this article, I will elucidate how to integrate Stratis Blockchain API in custom or third-party applications. We will create an angular application and integrate Stratis Private APIs. We will interact with Stratis Blockchain APIs and show the response in the custom application.
Additionally, this article will describe creating a user interface to deploy the Smart Contract. This write-up explains and provides the code for all endpoints necessary for deploying the Smart Contract; however, you can get ideas and interact with any other endpoints according to your custom software requirement. For more details on how to write Smart Contract and generate byte code, you can visit here.
The source code is available here.
Prerequisites
- IDE for Angular (Visual Studio or Visual Studio Code) as per you
- Stratis FullNode
- Smart Contract Byte Code
For this article, we will be using angular 12, however, it will be the same for another version as well.
Create Angular Application
We will create an angular application with angular CLI as shown below steps,
Install Angular CLI ( if you don’t have it).
We will create a new app with the following command,
We will navigate to the project folder and run the application. To build and run the angular application we use the serve command as shown.
Alternatively, to open the default Angular Application: Run below command which builds, run and open at once.
The –open command opens a project to the browser at http://localhost:4200/. To design lucrative UI, I have implemented the AdminLTE theme. If you want, you can also implement it following steps from here.
Create and Add Model in Angular App
Create _model folder where we will keep all the models for each component.
We can add the model using the command and also manually as described below: In _model folder right-click–> add a new item –> Add typescript File and rename it according to your structure.
Let’s Add the config.ts file under the model. Where we can keep all the configuration-related information and it will be easy to maintain the configuration-related information from one place. I am creating this to put the root/base URL of API which we call in any services or components. Sample config.ts code,
We will integrate the Full Node APIs running at localhost: http://localhost:38223.
I have assumed that you have FullNode already. If you don’t have you can download it from here. Open the project and run the Stratis.CirrusMinerD project in devmode. You can use the below command to run FullNode.
Open http://localhost:38223/swagger/index.html in your browser and you can see a list of APIs.
Wallet Component
To integrate the wallet, we will create a wallet service and wallet component. Run the below command to generate service and components for the wallet.
In this article, we will implement 3 endpoints from wallet API, however, the implementations of other API(s) will be similar. From the Wallet API Controller, we will call 3 endpoints: Wallet Load, Wallet balance, and WalletSplitcoins.
Endpoint: /api/Wallet/load: Wallet Load is to make sure the private chain is running smoothly and your app is connected with the chain.
Use the below credential for Wallet Load after the complete design.
Parameters | Value |
Name | cirrusdev |
Password | password |
Endpoint:/api/Wallet/balance: It gives the lists of Wallet addresses, amounts, etc. We need a wallet address having a balance on it to deploy the Smart Contract.
Endpoint: /api/Wallet/splitcoins: It creates the requested amount of equal value and gives the list of addresses with balance. If you get one address having balance and need more addresses, then you can use this to split balance and get more addresses with balance.
To handle API posts and responses easily, it is better to create a model for each of them. Likewise, we will create models for WalletLoad, WalletBalance, and WalletSplitcoins. In order to wallet Load, we have to pass parameters: name and password. Therefore, we will create a model walletload with name and password properties.
Code of walletload.ts model is given below,
Similarly, we will create a model for Walletbalance. Code of walletbalance.ts model is,
As above, we will create a model For WalletCoinSplit. Code for waletsplitcoin.ts is given below,
Wallet Service
Now, we will create a service class for wallet API(s) as wallet.services.ts. We will import the Root URL from config and use the base URL to call API endpoints. From the Stratis FullNode swagger page, we can see WalletLoad is post method that needs parameters name and password. Similarly, WalletSplitCoin is also a post, and WalletBalance is a get method with parameters. So, we can write code and pass parameters accordingly.
Here, we will create creating three methods in this services as shown,
Below is the code of wallet.services.ts class for those API endpoints.
We need HttpClient for the http requests so import HttpClient in-app-module.ts as depicted below, if it is not available there, and face an error with it.
Wallet Component
Now we will import Wallet Service, all Wallet-related models, and call methods of service in wallet.component.ts and pass parameters using model respectively.
Code of wallet.component.ts
For routing, please refer to the app-routing.module.ts from source code. After addition of each component we can add it to this app-routing.module.ts.
Wallet Component Html
In wallet.component.html, we will design forms to pass parameters as user input. It needs one form and submits button for each endpoint. When it gets the response, we are showing the response results just below the submit button. Here is the complete code for wallet.component.html page.
The output of above code of Wallet, Wallet Load.
The output of Wallet Balance,
The output of Wallet Split Coin,
Smart Contract Wallet
Similarly, we will implement two API endpoints from SmartContractWallet: WalletCreate and WalletCall. Endpoint: /api/SmartContractWallet/create is used to deploy the Smart Contract using the Bytecode generated from the Sct tool. If we want to change the state of the contract, API endpoint /api/SmartContractWallet/call can be utilized. You can see business case use of it from the previous article Voting Contract. These endpoints need parameters to pass, So we will create a model for both endpoints.
Create model as SmartContractWalletCreate. Code of smartcontractwalletcreate.ts model is given below,
Code for smartcontractwalletcall.ts,
After that, we will create Service and Component for Smart Contract Wallet as shown in angular commands.
Code for SmartcontractwalletService: smartcontractwallet.service.ts.
Similarly, we will import Service and model in component and pass parameters to get responses from API endpoints.
Code for smartcontractwallet.component.ts
HTML page of SmartContractWallet.Component.html page.
The output of above Smart Contract Wallet Creates page.
Output UI to interact with Amart Contract Wallet Call (endpoint: /api/SmartContractWallet/call).
Smart Contract Component
From the SmartContract API Controller, we will call endpoint API/SmartContracts/receipt which gives the receipt of the transaction. We have to pass transaction hash to get transaction receipt. It gives transaction receipts including Gas Used, From and To address, Logs, etc. Additionally, we will integrate endpoint /api/SmartContracts/local-call which creates a local call with a contract without a transaction. As we saw in the previous article when to use local call and contract call.
We will create a Model for GetReceipt and Local-Call.
Model code for smartcontractreceipt.ts
Model code for smartcontractlocalcall.ts class.
Generate Component and Service for Smart Contract API Controller.
Code of SmartContractService class,
Code of smartcontract.component.ts,
HTML for smartcontract.component.html,
Output of Smart Contract Get Receipt UI.
Smart Contract Local call UI.
No comments:
Post a Comment