溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

stellar區塊鏈JavaScript開發包是怎樣的

發布時間:2021-09-30 10:22:30 來源:億速云 閱讀:160 作者:柒染 欄目:互聯網科技

stellar區塊鏈JavaScript開發包是怎樣的,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

Stellar JS SDK封裝了Stellar交易的提交,以及與Stellar Horizon API服務器的交互過程,可以運行在Node.js環境或Web瀏覽器中。js-stellar-sdk主要有兩個作用:1、通過HorizonAPI服務器查詢Stellar區塊鏈數據 2、構建Stellar交易、簽名并提交到Stellar網絡中。

1、構造Horizon訪問請求

js-stellar-sdk使用Builder模式來創建要發送給Horizon API服務器的請求。從一個server對象開始,你可以通過鏈式調用來生成最終的查詢請求。例如:

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

// get a list of transactions submitted by a particular account
server.transactions()
    .forAccount('GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW')
    .call().then(function(r){ console.log(r); });

一旦請求構造好了,就可以調用.call().stream()來提交請求。 .call()將返回一個promise對象,其解析結果為Horizon服務器的響應。

2、發送流式請求

許多請求可以使用.stream()來調用。與.call()返回promise對象不同,.stream()將返回一個EventSource對象。Horizon API服務器會自動推送相關的數據給請求的客戶端。

例如,下面的代碼顯示輸出指定的Stellar賬戶的交易:

var StellarSdk = require('stellar-sdk')
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
var lastCursor=0; // or load where you left off

var txHandler = function (txResponse) {
    console.log(txResponse);
};

var es = server.transactions()
    .forAccount(accountAddress)
    .cursor(lastCursor)
    .stream({
        onmessage: txHandler
    })

3、處理Stellar Horizon API服務器的響應

3.1 Stellar XDR格式解碼

Horizon API服務器的交易訪問端結點會以原始XDR格式返回某些字段。你可以使用.fromXDR()將XDR轉換為JSON格式。

例如,下面的代碼重寫了上面示例中的txHandler來將XDR字段顯示為JSON格式:

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 Horizon響應結果中的鏈接跟隨

在Horizon響應中包含的鏈接已經被轉換為對應的方法調用,這讓你可以簡單地通過.next()方法來逐頁查看結果,同時也讓獲取額外信息更加輕松。例如:

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交易構建與廣播

4.1 Stellar交易構建

Stellar的交易構建過程稍微復雜一點,我們將在另一篇文章中介紹,你可以先查看英文原文

4.2 Stellar交易提交

一旦創建好了交易,就可以使用Server.submitTransaction()方法將其提交到 Stellar網絡中。

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);
    }
})()

看完上述內容,你們掌握stellar區塊鏈JavaScript開發包是怎樣的的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女