浅析EOS区块链柚子钱包前端插件scatter开发

 4240

本篇文章给大家了解EOS区块链柚子钱包前端插件scatter开发,伙伴们来看看一下。


浅析EOS区块链柚子钱包前端插件scatter开发


基于区块链柚子(EOS)钱包前端插件scatter安装和使用

安装和使用

  1. npm i scatterjs-core scatterjs-plugin-eosjs eosjs -D
  2.  
  3. //main.js
  4. import ScatterJS from "scatterjs-core";
  5. import ScatterEOS from "scatterjs-plugin-eosjs";
  6. import Eos from "eosjs";
  7. ScatterJS.plugins(new ScatterEOS());


网络链

  1. // EOS公链(正式环境)
  2. let main = {
  3.   protocol: "https",
  4.   blockchain: "eos",
  5.   host: "nodes.get-scatter.com",
  6.   port: 443,
  7.   chainId: "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906",
  8. };
  9.  
  10. // 测试链 (就是用来测试的)
  11. let jungle2 = {
  12.   protocol: "http",
  13.   blockchain: "eos",
  14.   host: "jungle2.cryptolions.io",
  15.   port: 80,
  16.   chainId: "e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473",
  17. };


调试和配置

翻墙下载scatter插件安装。

1、打开scatter---setting--network---新建把jungle2的信息填到对应的位置,(取名随便取,jungle2)--保存

2、生成秘钥对,一键生成私钥和公钥

生成秘钥对的地址 https://eostea.github.io/eos-generate-key/

3、新建测试账号,需要填入刚才的公钥,账号名是z-a,1-5长度 12 位组合

新建测试账号的地址 https://monitor.jungletestnet.io/#account 

4、充值,账号建立成功之后莫有钱, 可以先充值 100 块。测试的时候省着点用

充值的地址 http://monitor.jungletestnet.io/#faucet 

5、测试链上的账号建好之后,打开scatter---身份---新建,选择刚才建立的network => jungle2,然后选择对应的账号。点导入---保存

这样scatter插件配置完毕。可以愉快的开发了。


创建scatter

  1. ScatterJS.scatter.connect("app").then((connected) => {
  2.   if (!connected) return false;
  3.   let scatter = ScatterJS.scatter; //这里就是
  4.   window.ScatterJS = null;
  5.   window.scatter = null;
  6.   //通过两种方式拿到eos 对象
  7.   // this.eos = Eos({ httpEndpoint: '', signatureProvider: ScatterJS.scatter.eosHook(jungle2) });
  8.   this.eos = scatter.eos(jungle2, Eos, { expireInSeconds: 60 });
  9.   //如果授权成功,则可以拿到用户相关信息
  10.   if (scatter.identity) {
  11.     this.account = scatter.identity.accounts.find(
  12.       (x) => x.blockchain === "eos"
  13.     );
  14.   }
  15. });


授权和取消授权

  1. //授权
  2. const requiredFields = { accounts: [jungle2] };
  3.  
  4. scatter
  5.   .getIdentity(requiredFields)
  6.   .then(() => {
  7.     //分别拿到用户信息 和 eos 对象
  8.     this.account = scatter.identity.accounts.find(
  9.       (x) => x.blockchain === "eos"
  10.     );
  11.     this.eos = scatter.eos(jungle2, Eos, { expireInSeconds: 60 }, "https");
  12.   })
  13.   .catch((res) => {});
  14.  
  15. //退出
  16. scatter.forgetIdentity().then((id) => {
  17.   this.account = null;
  18.   this.eos = null;
  19. });


转账交易部分

  1. //取币种相关信息
  2. let config = {
  3.   account: "xxx", //账号名称
  4.   code: "eosio.token", //合约名称
  5.   symbol: "ETH", //币种
  6. };
  7. eos.getCurrencyBalance(config).then((e) => {
  8.   console.log(e);
  9. });
  10.  
  11. //取用户相关信息
  12. eos.getAccount({ account_name: "xxx" }).then((res) => {
  13.   // console.log(res)
  14.   let totoal = res.core_liquid_balance; //余额
  15.   let cpu = res.cpu_limit; //CPU
  16.   let net = res.net_limit; //NET
  17. });
  18.  
  19. //发起转账
  20. // eos.transfer('发送方帐号', '接收方帐号', '0.3000 DEV','memo', options, callback)
  21.  
  22. eos
  23.   .transfer(account.name, user, `${coin} EOS`, memo, transactionOptions)
  24.   .then((trx) => {
  25.     // That's it!
  26.     console.log(`Transaction ID: ${trx.transaction_id}`);
  27.     //有transaction_id 就代表转账成功了
  28.   })
  29.   .catch((res) => {});
  30.  
  31. //还可以使用对象
  32. eos.transfer({
  33.   from: "发送方帐号",
  34.   to: "接收方帐号",
  35.   quantity: "0.1000 DEV",
  36.   memo: "备注",
  37.   callback,
  38. });


交互部分

  1. // 获取Table行数据
  2. eosjs.getTableRows({"scope":'合约名字', "code":'合约名字', "table":"game", "json": true},callback)
  3.  
  4. //执行合约上的函数
  5.  
  6. eos.contract("合约名字").then(actions => {
  7. //actions随便起的变量名
  8. actions.test('hello', {
  9.     //test是方法名, 'hello'是该actions合约test方法的参数
  10.     authorization: [{actor:'lilei'}]
  11.     //lilei是建立该合约的用户
  12. }).then(result => {
  13.     console.log(result);
  14. });

当然EOS的API 非常多地址:https://developers.eos.io/eosio-nodeos/v1.6.0/reference#get_block  但是对于前端而言,以上足够开发一个线上游戏了。

至于说发代币啊,部署合约,抵押,竞拍,出售,购买,新建帐号这些都用不着,可以转给后端。



本文网址:https://www.zztuku.com/detail-9208.html
站长图库 - 浅析EOS区块链柚子钱包前端插件scatter开发
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐