stellar區塊鏈JavaScript開發包是怎樣的,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
Stellar JS SDK封裝了Stellar交易的提交,以及與Stellar Horizon API服務器的交互過程,可以運行在Node.js環境或Web瀏覽器中。js-stellar-sdk主要有兩個作用:1、通過HorizonAPI服務器查詢Stellar區塊鏈數據 2、構建Stellar交易、簽名并提交到Stellar網絡中。
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服務器的響應。
許多請求可以使用.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 })
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')) ); };
在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); }); });
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開發包是怎樣的的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。