【三胖聊区块链-EOS】(十二)EOS入门系列-开发DAPP之Scatter.JS的使用

1,166 阅读2分钟

github源码:我在这呢

上一篇:【三胖聊区块链-EOS】(十一)EOS入门系列-开发DAPP之EOS.JS的使用

建议使用EOS20+以上版本的,先看github教程,因为官网教程给出来的是旧版本的,20+版本不适用,会使用的运行调用智能合约报错。

1.开启scatter钱包并导入account,自行参考使用scatter钱包的教程

2. 导包,并选择搭配使用的EOSJS,参考官网

选择20+版本
npm i -S @scatterjs/core @scatterjs/eosjs2 eosjs@20.0.0

3.import eosjs,scatterjs/core,scatterjs/eosjs2

import { Api, JsonRpc } from 'eosjs'
import ScatterJS from '@scatterjs/core'
import ScatterEOS from '@scatterjs/eosjs2'

4.加载plugins

ScatterJS.plugins(new ScatterEOS())

5.构建一个NetWork的对象,配置好连接的区块链节点

const network = ScatterJS.Network.fromJson({
  blockchain: 'eos',
  chainId: '5fff1dae8dc8e2fc4d5b23b2c7665c97f9e9d8edf2b6485a86ba311c25639191',
  host: 'api.kylin.alohaeos.com',
  port: 443,
  protocol: 'https'
})

6.连接钱包

 ScatterJS.connect('MyAppName', {network}).then(connected => {
    if(!connected) return false;
    ....
    这里可以写其他方法
});

7.配置EOSJS与Scatter连接(写在connect方法里)

const rpc = new JsonRpc(network.fullhost(), { fetch })
const eos = ScatterJS.eos(network, Api, {rpc})

8.登录account(写在connect方法里)

ScatterJS.login().then(...);

9.如何获取account,有几种方法(参考官网)

Using the helper
const account = ScatterJS.account('eos')
// Result: {name:'...', authority:'active', publicKey:'...', blockchain:'eos', chainId:'...'}

const account = ScatterJS.account('eth')
// Result: {address:'...', blockchain:'eth', chainId:'1'}

const account = ScatterJS.account('trx')
// Result: {address:'...', blockchain:'trx', chainId:'1'}
From the Identity
const account = ScatterJS.identity.accounts.find(x => {
    return x.blockchain === 'eos';
});

10.调用合约,发起一个transaction(与EOSjs发起transaction一样)

参考上一篇

11.下面是完整源代码

import { Api, JsonRpc } from 'eosjs'
import ScatterJS from '@scatterjs/core'
import ScatterEOS from '@scatterjs/eosjs2'

ScatterJS.plugins(new ScatterEOS())

const network = ScatterJS.Network.fromJson({
  blockchain: 'eos',
  chainId: '5fff1dae8dc8e2fc4d5b23b2c7665c97f9e9d8edf2b6485a86ba311c25639191',
  host: 'api.kylin.alohaeos.com',
  port: 443,
  protocol: 'https'
})

ScatterJS.scatter.connect('MyWallet', {network}).then(connected => {
  if (!connected) {
    console.error('未连接scatter')
    return false
  }

  const rpc = new JsonRpc(network.fullhost(), { fetch })
  const eos = ScatterJS.eos(network, Api, {rpc})

  ScatterJS.login().then(() => {
    const account = ScatterJS.identity.accounts.find(x => {
      return x.blockchain === 'eos'
    })

    eos.transact(
      {
        actions: [{
          account: 'xwjselftoken',
          name: 'upsert',
          authorization: [{
            actor: account.name,
            permission: account.authority
          }],
          data: {
            user: account.name,
            first_name: 'xwj',
            last_name: 'xwj',
            age: 10,
            street: 'xwj',
            city: 'xwj',
            state: 'xwj'
          }
        }]
      },
      {
        sign: true,
        blocksBehind: 3,
        expireSeconds: 30
      }).then(result => { console.log(result) }).catch(err => console.error(err))
  })
})

更多

想了解更多,请关注公众号哦!