An Overview of Stellar Network and JS SDK

An Overview of Stellar Network and JS SDK

Posted By : Vishal Yadav | 31-Jul-2020

Introduction: Stellar is a decentralized open-source blockchain network that provides a platform for the exchange of currencies over the globe. On Stellar Network you can build banking applications, mobile wallets, and any other application that involves transactions. Some of the important components of Stellar Network are as follows.

 

API Horizon: It is a RESTful API server through which applications can connect to the Stellar Network. Horizon provides API through which can be used by developers to connect to the Network directly. Currently, Stellar.org provide the JavaScript, Go, and Java SDKs to connect with Horizon, also some other SDKs are maintained by the community like  Ruby, Python, and C#.

 

Stellar Core: It is the backbone of Network. Every Horizon connects to the core behind the scenes. Complete Network is the collection of Core some are connected with Horizon while others are used to provide reliability to the Network. All the complex tasks are handled by these COREs.

 

Assets: Network can be used to hold any currency like BTC, Ether, Dollar, etc. Any asset can be traded with another asset. All the assets have asset type and issuer. Any account on Stellar can issue an asset and these accounts are called Anchors.

There are various SDKs provided by Stellar in this blog we will JS SDK to create an account and create transaction. This SDK can be used on browser or Node.JS


Step 1: Install SDK and require it in Application.

// Run in project directory inside the terminal 

sudo npm i stellar-sdk

// Require it in Application.
var StellarSdk = require("stellar-sdk");
const fetch = require('node-fetch');
var server = new StellarSdk.Server("https://horizon-testnet.stellar.org"); //To connect with test horizon

 

Step 2: Create account on Stellar. Unlike other blockchains you need to maintain some minimum balance on the account i.e. 2 Lumens.

const pair = StellarSdk.Keypair.random();

pair.secret();
pair.publicKey();


// After create key pair fund them with some Lumens 

(async function main() {
    try {
      const response = await fetch(
        `https://friendbot.stellar.org?addr=${encodeURIComponent(
          pair.publicKey(),
        )}`,
      );
      const responseJSON = await response.json();
      console.log("SUCCESS! You have a new account :)\n");
      server.loadAccount(pair.publicKey()).then(function(sucess){console.log("Success ",sucess)
    console.log("Public key: "+pair.publicKey()+" Secret: "+pair.secret());
    }).catch(error=>{console.log("error",error)});
    } catch (e) {
      console.error("ERROR!", e);
    }
  })();

 

Step 3: Send Transaction on Stellar.

server.accounts()
  .accountId(source.publicKey())
  .call()
  .then(({ sequence }) => {
    const account = new StellarSdk.Account(source.publicKey(), sequence)
    const transaction = new StellarSdk.TransactionBuilder(account, {
      fee: StellarSdk.BASE_FEE,
      networkPassphrase: Networks.TESTNET
    })
      .addOperation(StellarSdk.Operation.createAccount({
        destination: destination.publicKey(),
        startingBalance: '25'
      }))
      .setTimeout(30)
      .build()
    transaction.sign(StellarSdk.Keypair.fromSecret(source.secret()))
    return server.submitTransaction(transaction)
  })
  .then(results => {
    console.log('Transaction', results._links.transaction.href)
    console.log('New Keypair', destination.publicKey(), destination.secret())
  })

 

Conclusion: In this blog, we get the overview of the Stellar blockchain and get a hand on it Js SDK how it can be used in Application.
 

About Author

loading
Vishal Yadav

Vishal has good knowledge of MEAN stack, JavaScript,HTML,CSS. He has worked on both front-end and back-end. He likes to learn new technologies.

Leave a Comment

Name is required

Comment is required

Recaptcha is required.

No Comments Yet.

    Request For Proposal

    Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.

    We would love to hear from you!

    Oodles | Blockchain Development Company

    Please enter a valid Phone Number

    Please remove URL from text

    Recaptcha is required.