An Overview of Stellar Network and JS SDK

Posted By : Vishal

Jul 31, 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.
 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

March 11, 2024 at 10:54 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text