Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the stellar blockchain JavaScript development kit like

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Stellar blockchain JavaScript development package is like, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Stellar JS SDK encapsulates the submission of Stellar transactions and the interaction with the Stellar Horizon API server, which can be run in a Node.js environment or in a Web browser. Js-stellar-sdk has two main functions: 1, query Stellar block chain data through HorizonAPI server 2, build Stellar transaction, sign and submit it to Stellar network.

1. Construct Horizon access request

Js-stellar-sdk uses Builder mode to create requests to be sent to the Horizon API server. Starting with a server object, you can use chained calls to generate the final query request. For example:

Var StellarSdk = require ('stellar-sdk'); var server = new StellarSdk.Server (' https://horizon-testnet.stellar.org');// get a list of transactions that occurred in ledger 1400server.transactions () .forLedger (1400) .call () .then (function (r) {console.log (r);}); / / get a list of transactions submitted by a particular accountserver.transactions () .forAccount ('GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW') .call () .then (function (r) {console.log (r)) })

Once the request is constructed, you can call .call () or .stream () to submit the request. .call () returns a promise object whose parsing result is the response of the Horizon server.

2. Send streaming request

Many requests can be called using .stream (). Unlike .call (), which returns a promise object, .call () returns an EventSource object. The Horizon API server automatically pushes the relevant data to the requesting client.

For example, the following code shows the output of transactions for the specified Stellar account:

Var StellarSdk = require ('stellar-sdk') var server = new StellarSdk.Server (' https://horizon-testnet.stellar.org');var lastCursor=0; / / or load where you left offvar txHandler = function (txResponse) {console.log (txResponse);}; var es = server.transactions () .forAccount (accountAddress) .cursor (lastCursor) .stream ({onmessage: txHandler}) 3, processing Stellar Horizon API server response 3.1 Stellar XDR format decoding

The transaction access node of the Horizon API server returns some fields in the original XDR format. You can use .fromXDR () to convert XDR to JSON format.

For example, the following code overrides the txHandler in the above example to display the XDR field in JSON format:

Var txHandler = function (txResponse) {console.log (JSON.stringify (StellarSdk.xdr.TransactionEnvelope.fromXDR (txResponse.envelope_xdr, 'base64')); console.log (JSON.stringify (StellarSdk.xdr.TransactionResult.fromXDR (txResponse.result_xdr,' base64')); console.log (JSON.stringify (StellarSdk.xdr.TransactionMeta.fromXDR (txResponse.result_meta_xdr, 'base64'));} 3.2 Link follow in Horizon response result

The links included in the Horizon response have been converted to the corresponding method calls, which allows you to simply view the results page by page through the .next () method, while also making it easier to get additional information. For example:

Server.payments () .limit (1) .call () .then (function (response) {/ / will follow the transactions link returned by Horizon response.records [0] .transaction (). Then (function (txs) {console.log (txs);}); 4. Stellar transaction Construction and broadcast 4.1 Stellar transaction Construction

The deal building process of Stellar is a little more complicated, which we will introduce in another article. You can check the original English text first.

4.2 Stellar deal submission

Once the transaction has been created, it can be submitted to the Stellar network using the Server.submitTransaction () method.

Const StellarSdk = require ('stellar-sdk') StellarSdk.Network.useTestNetwork (); const server = new StellarSdk.Server (' https://horizon-testnet.stellar.org');(async function main () {const account = await server.loadAccount (publicKey); / * Right now, we have one function that fetches the base fee. In the future, we'll have functions that are smarter about suggesting fees, e.g.: `fetchCheapFee`, `fetchAverageFee`, `fetchPriorityFee`, etc. * / const fee = await server.fetchBaseFee () Const transaction = new StellarSdk.TransactionBuilder (account, {fee}) .addOperation (/ / this operation funds the new account with XLM StellarSdk.Operation.payment ({destination: "GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW", asset: StellarSdk.Asset.native (), amount: "20000000"})) .setTimeout (30) .build () / / sign the transaction transaction.sign (StellarSdk.Keypair.fromSecret (secretString)); try {const transactionResult = await server.submitTransaction (transaction); console.log (transactionResult);} catch (err) {console.error (err);}}) () () after reading the above, have you mastered the method of stellar blockchain JavaScript development kit? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report