Intermediate

 

How to finding WriteSpeed of transactions for Stellar

STEP 1.

  • Create a folder and open it on vscode and creating a  package.json using npm init  file and also a file like writespeed.js

STEP 2.

  • Install three dependencies like  express, stellar-sdk, moment

Express :

  • Express is a node js web application framework that provides broad features for building web and mobile applications.
  • It is used to build a single page, multipage, and hybrid web application.
  • It’s a layer built on the top of the Node js that helps manage servers and routes.

stellar-sdk :

  • stellar-sdk js-stellar-sdk is a Javascript library for communicating with a Stellar Horizon server .
  • It is used for building Stellar apps either on Node.js or in the browser. It provides: a networking layer API for Horizon endpoints.

Moment :

A JavaScript date library for parsing, validating, manipulating, and formatting dates.

Step 3.

Now, import dependencies and create a express server in read.js like this

//writespeed.js
const express =require('express')
const StellarSdk = require("stellar-sdk");
const moment=require('moment')

//creating a server

app.listen('3000',(req,res)=>
{
console.log('server running on 3000');
})

Step 4

Creating a writeTransaction function and also write a logic for getting write speed value of transaction.

Note : Stellar’s pubnet and testnet each have their own unique passphrase. These are used when validating signatures on a given transaction use here passpharse

const STELLAR_PASSPHRASE = "Test SDF Network ; September 2015"
writeTransaction = async (req) => {
try {

// here we get integer value and convert into string using toString()

const calculateFee = Math.ceil(req + 100).toString();

//for networking we are using network like '"Test SDF Network ; September 2015" '

StellarSdk.Network.use(new StellarSdk.Network(STELLAR_PASSPHRASE));

//use here horizon testnet endpoint
const server = new StellarSdk.Server("https://horizon-testnet.stellar.org");

// load source-account public key
const acc = await server.loadAccount("GB3QZRSQ6OUVMPYGACBEB44TKO62G2KL23TKZ4GVOOHLUWFLHOBL3CM7");
let transaction;
try {

//load here destination public key
await server.loadAccount("GASKZQQLZAQQXYTRGXFRFHFMQRUWCSPKQ5252R7WPK5SIEBAJVGVZYJB");
//transction details and write speed charge
transaction = new StellarSdk.TransactionBuilder(acc, {
fee: calculateFee,
})
//details about transaction destination
.addOperation(
StellarSdk.Operation.payment({
amount: req.toString(),
//source code public key
source: "GB3QZRSQ6OUVMPYGACBEB44TKO62G2KL23TKZ4GVOOHLUWFLHOBL3CM7",
//destination public key
destination:"GASKZQQLZAQQXYTRGXFRFHFMQRUWCSPKQ5252R7WPK5SIEBAJVGVZYJB",
asset: StellarSdk.Asset.native(),
})
)
.build();

}

catch (error) {
transaction = new StellarSdk.TransactionBuilder(acc, {
fee: calculateFee,

})
.addOperation(
StellarSdk.Operation.createAccount({
//destination public key
destination: "GASKZQQLZAQQXYTRGXFRFHFMQRUWCSPKQ5252R7WPK5SIEBAJVGVZYJB",
startingBalance: req.toString(),
})
).setTimeout(1000)
.build();
}

//source-account secret key
const privatekey="SAZZQE5TYTRXA642IO3MJ7JXRAVII5TE6MIDORVAX73WCP3PNIJBL74K"

// key pair used to sign transactions
transaction.sign(
StellarSdk.Keypair.fromSecret(privatekey)
);

const startTime = Date.now();

//submit the transaction

await server
.submitTransaction(transaction)
.then(
async (msg) => {
console.log(msg.created_at)
transactionId = msg.hash;
return msg;
}
)
.catch((err) => {
// console.error("Error ", err);
return err;
});

// differnece of current time and before submittion time

let value= moment().diff(moment(startTime), "milliseconds");
console.log(value);

} catch (error) {
console.log(error);
}
};

//call the function

writeTransaction(100)

app.listen('3000',(req,res)=>
{
console.log('server running on 3000');
})

 

write-speed is getting in milliseconds unit