用JavaScript开发Stellar区块链应用
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); } })()
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
修改PostgreSQL字段长度导致cached plan must not change result type错误
问题 有业务反馈在修改一个表字段长度后,Java应用不停的报下面的错误,但是越往后错误越少,过了15分钟错误就没有再发生。 ### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: cached plan must not change result type 原因 调查判断原因是修改字段长度导致执行计划缓存失效,继续使用之前的预编译语句执行会失败。 很多人遇到过类似错误,比如: https://blog.csdn.net/qq_27791709/article/details/81198571 但是,有两个疑问没有解释清楚。 以前业务也改过字段长度,但为什么没有触发这个错误? 这个错误能否自愈? 下面是进一步的分析 PostgreSQL中抛出此异常的代码如下: static List * RevalidateCachedQuery(CachedPlanSource *plansource, QueryEnvironment *queryEnv) { if (plansource->...
- 下一篇
MaxCompute创建仅有查询权限的自定义角色
需求场景 在很多的数据开发场景下,MaxCompute项目管理员需要能够提供给某些角色或团队(如开发人员、运维人员)对项目内所有表具备特定权限。例如,某些客户可能需要在生产项目中,给ETL开发团队赋予所有表(或者所有ods开头的表)只读权限,当项目内创建了新表后,对应角色人员可以自动获取到新表的权限,方便开发人员能够快速处理线上数据问题排查。MaxCompute原有的权限模型提供的ACL授权方式,需要明确指定授权对象才能授权,ACL方式不支持通配符方式对新增表做授权。本文通过一个自定义角色的场景,介绍使用MaxCompute新升级的权限模型,利用ACL实现对特定规则的表进行授权。 前置条件 您的MaxCompute项目需要支持MaxCompute新权限模型。截止目前(2020/1/6),MaxCompute新权限模型正在陆续灰度发布,如
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS关闭SELinux安全模块
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Hadoop3单机部署,实现最简伪集群
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS6,CentOS7官方镜像安装Oracle11G
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8编译安装MySQL8.0.19
- CentOS6,7,8上安装Nginx,支持https2.0的开启