Useful SDKs in Stellar for Blockchain dev

Tram Ho

Stellar is one of the earliest blockchain platforms (released before Ethereum ) and has focused directly on payments from the outset, not expanding towards healthcare, estate … like other blockchains. Besides, Stellar is also cooperating with Samsung , maybe we will soon see lumens integrated on Samsung phone models.

Preamble

First, this article is geared towards the new Stellar SDKs, so it will be suitable for blockchain Devs who have known through Stellar a little, but those who are just starting to learn blockchain or starting, can read through this article to Take a quick look at Stellar: What is Stellar? How does Stellar work?

However, in this post, if you follow step-by-step, you will not be able to transfer lumens due to an error that you have not registered an account on the stellar network (Although in the article we use TESTNET). This is how the Stellar revolution restricts junk addresses on their network. In order to become a usable address we have to faucet for that address a little lumens and also be treated as a way to register with the network.

Below is the function faucet, you will be given 1000 lumens to test:

In this article I will use the “stellar-sdk”: “5.0.3”

Practice

In this article, I will guide you to use the 2 Protocol included in Protocol 13 that I find quite useful:

  • Fee Bumps : This is a very useful protocol that allows users to pay transaction fees to other accounts without tampering with the paid addresses.
  • Claimable Balances : This protocol will basically divide an ordinary payment into two parts including Initialize balance and Claim balance . Balance creator can directly specify who can claim that balance.

View the balance of the account

First, to easily check the protocols, we will need a function to check the balance, in the stellar universe we will see payment will transfer 2 types of values ​​( Assets ) called native (lumens) and the rest ( User-defined Assets – ETH devs will find them akin to ERC20). In this article we’ll be working with Stellar ‘s native Asset:

Fee Bumps

This article I will guide you to use Stellar’s SDK to use this Protocol, but if you want to learn more about the principle, you can refer to the article: https://medium.com/stellar-community/ fee-bump-transactions-explained-9a6a365c0fb6

I will write the code to divide a traditional Trx into 2 parts: Create Trx and Pay Trx:

First, with the createFeeBumpTx function will take 3 input parameters including:

  • _privateKey : Privatekey used to sign Trx is initialized
  • _to : Publickey of account will receive _amount lumen
  • _amount : The amount of lumens transferred during the transaction

The result will be the object of a Trx generated from TransactionBuilder SDK, the send and receive address information has been filled in. Once completed, we will use the submitFeeBumpTrx function to submit the Trx just initialized to the network.

In this function it will take 2 input arguments:

  • _privatekey: Used to sign Trx
  • _trxObj: This is the object created through the TransactionBuilder SDK above that is sent to pay fees.

Finally, run the combination of 2 functions to complete a Fee Bump stream:

You open the terminal and run to see the results

Claimable Balances

As defined, this Protocol will also divide an ordinary Trx into two parts including Balance Initialization and Claim

The createClaimable function will first accept the input arguments:

  • _privatekey: Used to sign Trx
  • _publickey: publickey’s address will be able to claim balance created
  • __amount: The amount of lumens created can make claim

With this function will use the SDK Operation to initialize the balance with the claimants argument which is the publickey of the address that can claim this balance.

In the claim function, only the recipient’s private key can be created to create Trx Claim. In this function, the main sections are:

  • const {records} = await server.claimableBalances (). claimant ( account.id ) .call (): This function is used to query the balance assets that this account is assigned (can claim to be)
  • claimClaimableBalance: this part is used to claim balance according to the balanceId that was queryed from above, in this example I will query a whole series of claims that can be claimed.

Finally, you will have to submitTransaction to the Stellar network

And you can run the test:

Let’s open the terminal together and feel it!

The code I have prepared here you can refer to it: https://github.com/tranchien2002/lumens

Refer

Share the news now

Source : Viblo